From 955679ca8d0f18b838bf9551abecfef576c34445 Mon Sep 17 00:00:00 2001 From: Trevor Bramwell Date: Thu, 26 Oct 2017 11:57:05 -0700 Subject: [PATCH] Create script for uploading ssh pubkey to Gerrit 'gerrit-auth.sh' takes a single argument of an ssh public-key file to be added to the sandbox user in Gerrit. This way users don't need to go through an error-prone setup of copy & pasting the curl command. Some explainations of curl errors are also provided. Change-Id: I5b3af284348953b2cea37dedfff5b6bed852e4b7 Signed-off-by: Trevor Bramwell --- README.rst | 6 +----- gerrit-auth.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) create mode 100755 gerrit-auth.sh diff --git a/README.rst b/README.rst index a7a0a17..bee18c1 100644 --- a/README.rst +++ b/README.rst @@ -88,11 +88,7 @@ Once the environment is up and running, copy your ssh public-key and add it to the sandbox user in Gerrit. This can be either be done through the web interface or from the commandline:: - curl -L -X POST -u "sandbox:sandbox" -H "Content-type:text/plain" \ - -d @$HOME/.ssh/id_rsa.pub http://gerrit.localhost/a/accounts/self/sshkeys/ - -.. note: It's important here the Content-type header is set, as Gerrit - always expects JSON, and URLs must end in '/' + ./gerrit-auth.sh ~/.ssh/id_rsa.pub Then you can clone the ci-management repo and modify it to your hearts content:: diff --git a/gerrit-auth.sh b/gerrit-auth.sh new file mode 100755 index 0000000..67fc93e --- /dev/null +++ b/gerrit-auth.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +KEYFILE=$1 + +function usage() { + # Print usage and exit + cat < + +$0 uploads an ssh public key to Gerrit, so repos can be +accessed from ssh://gerrit.localhost:29418. + +Example: + $0 ~/.ssh/id_rsa.pub +EOF +exit 1 +} + +function print_repos() { + # Print out a list of array elements + local repos=($@) + for repo in "${repos[@]}"; do + echo -e " - $repo" + done +} + +if [ -z "$*" ]; then + usage +fi + +if [[ -s $KEYFILE ]]; then + # Upload SSH Public Key + curl --fail -s -L -X POST -u "sandbox:sandbox" -H "Content-type:text/plain" \ + -d @$KEYFILE http://gerrit.localhost/a/accounts/self/sshkeys/ > /dev/null + + # Provide guidance on curl errors + if [ $? -eq 7 ]; then + echo -e "\nPlease start Gerrit first:\n docker-compose up -d" + elif [ $? -eq 22 ]; then + echo -e "\nPlease wait for Gerrit to finish running and try again" + fi + + # Output future guidance + if [ $? -eq 0 ]; then + KEYID=$(ssh-keygen -l -f $KEYFILE) + GERRIT_REPOS="$(curl -s -L http://gerrit.localhost/projects/ \ + | grep \"id\" | cut -c12- | tr -d '",')" + echo -e "Successfully uploaded public keyfile:" + echo " $KEYID" + echo -e "\nYou can now clone the available repos:" + print_repos $GERRIT_REPOS + echo -e "\nWith the command:" + echo -e " git clone ssh://sandbox@gerrit.localhost:29418/" + fi +fi -- 2.16.6