Created
May 24, 2017 19:56
-
-
Save gyscos/b61dcd1a4c1f50886267583ff6e7f3a7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Boostrap script to create and upload a ssh key to gitlab. | |
| # Created with: gist -s -R bootstrap.sh | |
| function main() { | |
| set -e | |
| KEY="$1" | |
| TARGET="$2" | |
| EMAIL="$3" | |
| API="$TARGET/api/v4" | |
| AUTH="private_token=$KEY" | |
| jq -h > /dev/null 2>&1 || { echo "The jq command was not found. Please install it and try again."; exit 1; } | |
| ID=$(curl -s "$API/users?$AUTH" | jq '.[] | select(.email == "'$EMAIL'") | .id') | |
| if [ -z $ID ] | |
| then | |
| echo "User $EMAIL not found." | |
| exit 1 | |
| fi | |
| SSH_FILE="$HOME/.ssh/gitlab" | |
| if [ ! -f "$SSH_FILE" ] | |
| then | |
| ssh-keygen -q -f "$SSH_FILE" -t ed25519 -N '' | |
| fi | |
| if ! grep gitlab ~/.ssh/config | |
| then | |
| echo "Host git.diffbot.com | |
| Hostname git.diffbot.com | |
| Port 10022 | |
| User git | |
| IdentityFile $SSH_FILE" >> ~/.ssh/config | |
| fi | |
| TITLE="gitlab $USER@$(hostname)" | |
| CMD="curl -s -X POST --data-urlencode 'title=$TITLE' --data-urlencode 'key=$(cat $SSH_FILE.pub)' '$API/users/$ID/keys?$AUTH'" | |
| # echo $CMD | |
| RESPONSE=$(bash -c "$CMD") | |
| KEY_ID=$(echo $RESPONSE | jq .id) | |
| if [ -z $KEY_ID ] || [ $KEY_ID = null ] | |
| then | |
| echo "Error creating key": | |
| echo $RESPONSE | jq . | |
| exit 1 | |
| fi | |
| echo "Key created: $KEY_ID" | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment