Create script for uploading ssh pubkey to Gerrit 41/7141/3
authorTrevor Bramwell <tbramwell@linuxfoundation.org>
Thu, 26 Oct 2017 18:57:05 +0000 (11:57 -0700)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Fri, 3 Nov 2017 15:06:16 +0000 (11:06 -0400)
'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 <tbramwell@linuxfoundation.org>
README.rst
gerrit-auth.sh [new file with mode: 0755]

index a7a0a17..bee18c1 100644 (file)
@@ -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 (executable)
index 0000000..67fc93e
--- /dev/null
@@ -0,0 +1,55 @@
+#!/bin/bash
+
+KEYFILE=$1
+
+function usage() {
+    # Print usage and exit
+    cat <<EOF
+Usage: $0 <public-key-file>
+
+$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/<repo>"
+    fi
+fi