Last active
December 3, 2017 12:02
-
-
Save iv-m/4a7fe9343f9584b7bcc2237c64e57ccf to your computer and use it in GitHub Desktop.
Adding your key to google cloud project metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: add-gcloud-key.sh [PROJECT_ID] | |
set -eu | |
# Change these two lines | |
# USER=<your user name> | |
KEY="$HOME/.ssh/your_key.pub" | |
PROJECT="${1:+--project=$1}" | |
KEY_DATA="$(awk '{print $1, $2;}' $KEY)" | |
KEYS_FILE=$(mktemp ${TMP:-/tmp}/gcloud-keys-XXXXXX) | |
trap "rm -f '$KEYS_FILE'" HUP PIPE INT QUIT TERM EXIT | |
echo "Getting the current data..." | |
gcloud $PROJECT compute project-info describe | grep -ve '- key: sshKeys' | grep ssh | sed -r 's/^\s+//g' > "$KEYS_FILE" | |
if grep "^$USER:" "$KEYS_FILE"; then | |
echo 'Key already exists' | |
exit 0 | |
fi | |
echo "Adding the key..." | |
echo "$USER:$KEY_DATA google-ssh {\"userName\":\"$USER\",\"expireOn\":\"2120-01-01T20:00:00+0000\"}" >> "$KEYS_FILE" | |
gcloud $PROJECT compute project-info add-metadata --metadata-from-file sshKeys="$KEYS_FILE" | |
echo DONE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment