Update script with functions
authorSofian Brabez <sofian.brabez@gonitro.com>
Thu, 5 Sep 2019 09:12:24 +0000 (10:12 +0100)
committerSofian Brabez <sofian.brabez@gonitro.com>
Thu, 5 Sep 2019 13:48:12 +0000 (14:48 +0100)
release.sh

index fa7472615b5b5308077aa2be8303390f712ec740..0913f5017b1eabb163f849f27a84737d4b76048a 100755 (executable)
@@ -1,6 +1,5 @@
 #!/bin/bash
 
-set -o errexit
 set -o nounset
 set -o pipefail
 
@@ -11,81 +10,106 @@ die() {
 
 usage() {
     cat <<EOF
-    ${0} [options] [arguments]
+${0} [options] [arguments]
 
-    Arguments:
+Arguments:
 
-    -b Set the S3 bucket name
+-b Set the S3 bucket name
 
-    Options:
+Options:
 
-    -h Help
-    -s Dry run (simulate)
-    -n Does not upload artifact (just build)
+-h Help
+-s Dry run (simulate)
+-n Does not upload artifact (just build)
 EOF
     exit 1
 }
 
-declare -a deps=(gpg deb-s3)
-
-for dep in ${deps[*]}
-do
-    [[ ! -f $(command -v "$dep") ]] && {
-        die "You need to install: $dep"
-    }
-done
-
-while getopts ":b:hsu" opt; do
-    case ${opt} in
-        b ) BUCKET=${OPTARG}
-            ;;
-        h ) usage
-            ;;
-        s ) DRY_RUN="echo "
-            ;;
-        u ) NO_UPLOAD=1
-            ;;
-        \?) echo "Invalid Option: -${OPTARG}" 1>&2; exit 1
-            ;;
-    esac
-done
-
-COMMIT=$( (cd docker-gc && git rev-parse --short HEAD) )
-VERSION="2:$(cat "${PWD}"/docker-gc/version.txt)~${COMMIT}"
-TAG="gonitro/docker-gc-build:${COMMIT}"
-AWS_REGION=us-west-2
-BUCKET=${BUCKET:-nitro-apt-repo}
-DRY_RUN=${DRY_RUN:-}
-NO_UPLOAD=${NO_UPLOAD:-}
-KEYSERVER=${KEYSERVER:-pgpkeys.eu}
-NITRO_GPG_KEY=$(gpg \
-    --keyserver "${KEYSERVER}" \
-    --batch \
-    --search-keys \
-    --with-colons infra-guild@gonitro.com 2>&1| sed -E -n 's/^pub:.*(........):.*:.*:.*::/\1/p')
-
-printf  "[+] Using GPG %s for package signature\n" "${NITRO_GPG_KEY}"
-
-$DRY_RUN docker build \
-    -t "${TAG}" \
-    --build-arg VERSION="${VERSION}" \
-    --build-arg AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
-    --build-arg AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
-    -f ./Dockerfile .
-
-$DRY_RUN docker run -v /tmp/:/tmp "${TAG}" /bin/bash -c 'cp /docker-gc*.deb /tmp'
-
-package=$(ls /tmp/*.deb || :)
-printf "[+] Debian Package generated into '%s'\n" "${package}"
-
-if [[ -z ${NO_UPLOAD} ]]; then
-$DRY_RUN deb-s3 upload \
-    --access-key-id="${AWS_ACCESS_KEY_ID}" \
-    --secret-access-key="${AWS_SECRET_ACCESS_KEY}" \
-    --s3-region="${AWS_REGION}" \
-    --bucket="${BUCKET}" \
-    --sign="${NITRO_GPG_KEY}" "${package}" || exit 1
-printf "[+] Successfully uploaded package into '%s'\n" "${BUCKET}"
-fi
+do_prereq() {
+    declare -a deps=(gpg deb-s3)
+
+    for dep in ${deps[*]}
+    do
+        [[ ! -f $(command -v "$dep") ]] && {
+            die "You need to install: $dep"
+        }
+    done
+}
+
+parse_args() {
+    while getopts ":b:hsn" opt; do
+        case ${opt} in
+            b) BUCKET=${OPTARG}
+                ;;
+            h) usage
+                ;;
+            s) DRY_RUN="echo "
+                ;;
+            n) NO_UPLOAD=1
+                ;;
+            \?) echo "Invalid Option: -${OPTARG}" 1>&2; exit 1
+                ;;
+        esac
+    done
+}
+
 
-exit 0
+do_build() {
+    $DRY_RUN docker build \
+        -t "${TAG}" \
+        --build-arg VERSION="${VERSION}" \
+        --build-arg AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}" \
+        --build-arg AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}" \
+        -f ./Dockerfile .
+
+    $DRY_RUN docker run -v /tmp/:/tmp "${TAG}" /bin/bash -c 'cp /docker-gc*.deb /tmp'
+
+    package=$(ls /tmp/*.deb || :)
+    printf "[+] Debian Package generated into '%s'\n" "${package}"
+}
+
+
+do_upload() {
+    printf  "[+] Using GPG %s for package signature\n" "${NITRO_GPG_KEY}"
+    $DRY_RUN deb-s3 upload \
+        --access-key-id="${AWS_ACCESS_KEY_ID}" \
+        --secret-access-key="${AWS_SECRET_ACCESS_KEY}" \
+        --s3-region="${AWS_REGION}" \
+        --bucket="${BUCKET}" \
+        --sign="${NITRO_GPG_KEY}" "${package}" || exit 1
+    printf "[+] Successfully uploaded package into '%s'\n" "${BUCKET}"
+}
+
+
+
+main() {
+    COMMIT=$( (cd docker-gc && git rev-parse --short HEAD) )
+    VERSION="2:$(cat "${PWD}"/docker-gc/version.txt)~${COMMIT}"
+    TAG="gonitro/docker-gc-build:${COMMIT}"
+    AWS_REGION=us-west-2
+    BUCKET=${BUCKET:-nitro-apt-repo}
+    DRY_RUN=${DRY_RUN:-}
+    NO_UPLOAD=${NO_UPLOAD:-}
+    KEYSERVER=${KEYSERVER:-pgpkeys.eu}
+    NITRO_GPG_KEY=$(gpg \
+        --keyserver "${KEYSERVER}" \
+        --batch \
+        --search-keys \
+        --with-colons infra-guild@gonitro.com 2>&1| sed -E -n 's/^pub:.*(........):.*:.*:.*::/\1/p')
+
+    do_prereq
+
+    parse_args "$@"
+
+    do_build
+
+    if [[ -z ${NO_UPLOAD} ]]; then
+        do_upload
+    fi
+
+    exit 0
+}
+
+if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
+    main "$@"
+fi