Skip to content

Commit

Permalink
start-project: Use getopts in place of positional args
Browse files Browse the repository at this point in the history
  • Loading branch information
wrboyce authored and Will Boyce committed Sep 27, 2018
1 parent 8be8bca commit 1a30581
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions scripts/make-env
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -40,7 +40,7 @@ b64encode_str() {
cat <<STR
export OPENBALENA_DEBUG=true
export OPENBALENA_PRODUCTION_MODE=false
export OPENBALENA_HOST_NAME=$HOST_NAME
export OPENBALENA_HOST_NAME=$DOMAIN
export OPENBALENA_JWT_SECRET=$(randstr 32)
export OPENBALENA_RESINOS_REGISTRY_CODE=$(randstr 32)
export OPENBALENA_ROOT_CA=$(b64encode "$ROOT_CA")
Expand Down
10 changes: 5 additions & 5 deletions scripts/patch-hosts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ set -e
CMD=$0
DIR=$(dirname "$CMD")

HOST_NAME=$1
DOMAIN=$1

SERVICES="api registry vpn db"
SERVICES="${SERVICES} img devices" # FIXME: remove

usage() {
echo "usage: $0 HOST_NAME"
echo "usage: $0 DOMAIN"
echo
echo " HOST_NAME the domain name to add host entries for, eg. example.com"
echo " DOMAIN the domain name to add host entries for, eg. example.com"
echo
}

if [ -z "$HOST_NAME" ]; then
if [ -z "$DOMAIN" ]; then
usage
exit 1
fi
Expand All @@ -26,7 +26,7 @@ fi
# append all entries to hosts file.
tmp=$(mktemp --tmpdir openbalena.XXXX)
for service in $SERVICES; do
name="${service}.${HOST_NAME}"
name="${service}.${DOMAIN}"
if ! grep "\s$name" /etc/hosts >/dev/null 2>&1 ; then
echo "adding $name"
echo "127.0.0.1 $name" >>$tmp
Expand Down
4 changes: 2 additions & 2 deletions scripts/run-fig-command
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -28,4 +28,4 @@ PROJECT_NAME=$(basename "$PROJECT")
--project-name $PROJECT_NAME \
-f "${BASE_DIR}/compose/services.yml" \
-f "${PROJECT}/docker-compose.yml" \
"$@"
"$@"
41 changes: 30 additions & 11 deletions scripts/start-project
Original file line number Diff line number Diff line change
Expand Up @@ -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" "${@}"
}
Expand All @@ -31,26 +49,27 @@ 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")

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}"

0 comments on commit 1a30581

Please sign in to comment.