From 1a3058124342c9a9d562fee14c3fd81cee1f270f Mon Sep 17 00:00:00 2001 From: Will Boyce Date: Thu, 27 Sep 2018 18:49:16 +0100 Subject: [PATCH] start-project: Use getopts in place of positional args --- Vagrantfile | 3 ++- scripts/make-env | 6 +++--- scripts/patch-hosts | 10 +++++----- scripts/run-fig-command | 4 ++-- scripts/start-project | 41 ++++++++++++++++++++++++++++++----------- 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Vagrantfile b/Vagrantfile index 8708501980..396d36fb15 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -27,7 +27,8 @@ Vagrant.configure('2') do |config| config.vm.provision :shell, inline: "docker login --username resindev --password #{ENV.fetch('DOCKERHUB_PASSWORD')}" config.vm.provision :shell, privileged: false, - inline: "cd /home/vagrant/open-balena && ./scripts/start-project #{ENV.fetch('OPENBALENA_PROJECT_NAME', '')} #{ENV.fetch('OPENBALENA_HOST_NAME', '')}" + # FIXME: -n/-d should only be passed if the relevant ENV var is set + inline: "cd /home/vagrant/open-balena && ./scripts/start-project -n #{ENV.fetch('OPENBALENA_PROJECT_NAME', '')} -d #{ENV.fetch('OPENBALENA_HOST_NAME', '')}" config.vm.provision :shell, privileged: false, inline: 'cd /home/vagrant/open-balena && ./scripts/run-fig-command up -d || true', run: 'always' diff --git a/scripts/make-env b/scripts/make-env index 5f316a36d7..ff4f757a73 100755 --- a/scripts/make-env +++ b/scripts/make-env @@ -4,7 +4,7 @@ usage() { echo "usage: $0" echo echo "Required Variables:" - echo "HOST_NAME" + echo "DOMAIN" echo "ROOT_CA: Path to root CA certificate" echo "ROOT_CRT: Path to root/wildcard certificate" echo "ROOT_KEY: Path to root/wildcard private key" @@ -18,7 +18,7 @@ usage() { echo } -for var in HOST_NAME ROOT_CA ROOT_CRT ROOT_KEY JWT_CRT JWT_KEY JWT_KID VPN_CA VPN_CRT VPN_KEY VPN_DH; do +for var in DOMAIN ROOT_CA ROOT_CRT ROOT_KEY JWT_CRT JWT_KEY JWT_KID VPN_CA VPN_CRT VPN_KEY VPN_DH; do if [ -z "${!var-}" ]; then usage exit 1 @@ -40,7 +40,7 @@ b64encode_str() { cat </dev/null 2>&1 ; then echo "adding $name" echo "127.0.0.1 $name" >>$tmp diff --git a/scripts/run-fig-command b/scripts/run-fig-command index 3332ca8675..fa8ec1528d 100755 --- a/scripts/run-fig-command +++ b/scripts/run-fig-command @@ -7,7 +7,7 @@ DIR=$(dirname "$CMD") BASE_DIR=$(dirname "$DIR") echo_bold() { - printf "\033[1m${@}\033[0m\n" + printf "\033[1m%s\033[0m\n" "$@" } PROJECT_FILE="${BASE_DIR}/.project" @@ -28,4 +28,4 @@ PROJECT_NAME=$(basename "$PROJECT") --project-name $PROJECT_NAME \ -f "${BASE_DIR}/compose/services.yml" \ -f "${PROJECT}/docker-compose.yml" \ - "$@" + "$@" \ No newline at end of file diff --git a/scripts/start-project b/scripts/start-project index 1a4991ba6f..16057b2bb1 100755 --- a/scripts/start-project +++ b/scripts/start-project @@ -4,20 +4,38 @@ CMD=$0 DIR=$(dirname "$CMD") BASE_DIR=$(dirname "$DIR") -PROJECT_NAME=${1:-demo} -HOST_NAME=${2:-openbalena.local} +PROJECT_NAME=demo +DOMAIN=openbalena.local + +show_help=false +patch_hosts=false +while getopts ":hpP:H:" opt; do + case "${opt}" in + h) show_help=true;; + p) patch_hosts=true;; + P) PROJECT_NAME="${OPTARG}";; + H) DOMAIN="${OPTARG}";; + esac +done +shift $((OPTIND-1)) PROJECT_DIR="$(pwd)/${PROJECT_NAME}" CERTS_DIR="${PROJECT_DIR}/certs" usage() { - echo "usage: $0 [PROJECT_NAME [HOST_NAME]]" + echo "usage: $0 [-h] [-p] [-n PROJECT_NAME] [-d DOMAIN]" echo + echo " -p patch hosts - patch the host /etc/hosts file" echo " PROJECT_NAME a name for the deployment, eg. staging. Default is 'demo'" - echo " HOST_NAME the domain name this deployment will run as, eg. example.com. Default is 'openbalena.local'" + echo " DOMAIN the domain name this deployment will run as, eg. example.com. Default is 'openbalena.local'" echo } +if [ "$show_help" = "true" ]; then + usage + exit 1 +fi + echo_bold() { printf "\033[1m%s\033[0m\n" "${@}" } @@ -31,16 +49,16 @@ echo_bold "==> Creating new project at: $PROJECT_DIR" mkdir -p "$PROJECT_DIR" "$CERTS_DIR" echo_bold "==> Generating root CA cert..." -source "${DIR}/gen-root-ca" "${HOST_NAME}" "${CERTS_DIR}" +source "${DIR}/gen-root-ca" "${DOMAIN}" "${CERTS_DIR}" echo_bold "==> Generating root cert chain for haproxy..." -source "${DIR}/gen-root-cert" "${HOST_NAME}" "${CERTS_DIR}" +source "${DIR}/gen-root-cert" "${DOMAIN}" "${CERTS_DIR}" echo_bold "==> Generating token auth cert..." -source "${DIR}/gen-token-auth-cert" "${HOST_NAME}" "${CERTS_DIR}" +source "${DIR}/gen-token-auth-cert" "${DOMAIN}" "${CERTS_DIR}" echo_bold "==> Generating VPN CA, cert and dhparam (this may take a while)..." -source "${DIR}/gen-vpn-certs" "${HOST_NAME}" "${CERTS_DIR}" +source "${DIR}/gen-vpn-certs" "${DOMAIN}" "${CERTS_DIR}" echo_bold "==> Setting up environment..." cat >"${PROJECT_DIR}/activate" <(source "${DIR}/make-env") @@ -48,9 +66,10 @@ cat >"${PROJECT_DIR}/activate" <(source "${DIR}/make-env") echo_bold "==> Adding default compose file..." cp "${BASE_DIR}/compose/template.yml" "${PROJECT_DIR}/docker-compose.yml" -# FIXME: should be explicitly requested via a flag -echo_bold "==> Patching /etc/hosts..." -"${DIR}/patch-hosts" $HOST_NAME +if [ "${patch_hosts}" = "true" ]; then + echo_bold "==> Patching /etc/hosts..." + source "${DIR}/patch-hosts" "${DOMAIN}" +fi echo_bold "==> Activating project..." "${DIR}/select-project" "${PROJECT_DIR}" \ No newline at end of file