From fa841e0cee95afaf8e8a38d8e6e758f808dd4488 Mon Sep 17 00:00:00 2001 From: Raoul Millais Date: Tue, 1 Nov 2016 12:13:47 +0000 Subject: [PATCH] Make running the stack work locally (#55) * Do not check triton config for local setup * Log how to run stack locally - If docker is unconfigured (default) * Remove exports from setup-tools - Don't pollute the environment with these functions. This file is sourced so they will still be available * Fix linting errors and make code style consistent * Fix docker check when DOCKER_HOST is set * Make setup work whe triton env vars are not set * Document setup step --- README.md | 5 +++++ bin/setup | 4 ++-- bin/setup-tools | 54 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 6f041ecd..97c065b9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # Joyent Dashboard +## Setup + +```sh +./bin/setup +``` ## Installation To run the stack locally: diff --git a/bin/setup b/bin/setup index fe0fc014..1cae372c 100755 --- a/bin/setup +++ b/bin/setup @@ -18,6 +18,6 @@ readonly INCLUDE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # Main # ensure_prerequisites -ensure_docker_config_matches_triton_config_and_capture_triton_details -ensure_triton_cns_is_enabled +get_triton_details +check_docker_config write_env_file diff --git a/bin/setup-tools b/bin/setup-tools index 05c5ba3a..a90f9957 100644 --- a/bin/setup-tools +++ b/bin/setup-tools @@ -16,7 +16,7 @@ IFS=$'\n\t' # Utilities # die() { - local msg="$@" + local msg="$*" [[ -z "${msg}" ]] || { echo tput setaf 1 # red @@ -37,51 +37,69 @@ ensure_command() { die "Couldn't find required command: ${cmd}" } } -export -f ensure_command get_triton_details() { TRITON_USER=$(triton profile get | awk -F": " '/account:/{print $2}') TRITON_DC=$(triton profile get | awk -F"/" '/url:/{print $3}' | awk -F'.' '{print $1}') + TRITON_URL=$(triton profile get | awk -F' ' '/url:/{print $2}') TRITON_ACCOUNT=$(triton account get | awk -F": " '/id:/{print $2}') + TRITON_KEY=$(triton profile get | awk -F' ' '/keyId:/{print $2}') +} + +check_docker_config() { + [[ "${DOCKER_HOST:=unset}" == "unset" ]] && { + echo "Run \"docker-compose -f local-compose.yml\" up to run locally" + return 0 + } + + return 0 } -export -f get_triton_details ensure_docker_config_matches_triton_config_and_capture_triton_details() { - local docker_user=$(docker info 2>&1 | awk -F": " '/SDCAccount:/{print $2}') - local docker_dc=$(echo $DOCKER_HOST | awk -F"/" '{print $3}' | awk -F'.' '{print $1}') + local docker_user + docker_user=$(docker info 2>&1 | awk -F": " '/SDCAccount:/{print $2}') + local docker_dc + docker_dc="$(echo "${DOCKER_HOST}" | awk -F"/" '{print $3}' | awk -F'.' '{print $1}')" get_triton_details - if [[ ! "$docker_user" = "$TRITON_USER" ]] || [[ ! "$docker_dc" = "$TRITON_DC" ]]; then + [[ ! "$docker_user" = "$TRITON_USER" ]] || [[ ! "$docker_dc" = "$TRITON_DC" ]] && { echo "Docker user: ${docker_user}" echo "Triton user: ${TRITON_USER}" echo "Docker data center: ${docker_dc}" echo "Triton data center: ${TRITON_DC}" - die "Your Triton configuration does not match your Docker configuration." - fi + die "Your Triton config does not match your Docker configuration." + } + + return 0; } -export -f ensure_docker_config_matches_triton_config_and_capture_triton_details ensure_triton_cns_is_enabled() { - local triton_cns_enabled=$(triton account get | awk -F": " '/cns/{print $2}') + local triton_cns_enabled + triton_cns_enabled=$(triton account get | awk -F": " '/cns/{print $2}') [[ "$triton_cns_enabled" == "true" ]] || { die "Triton CNS is required and not enabled." } } -export -f ensure_triton_cns_is_enabled write_env_file() { - [[ -f .env ]] || { + if [[ -f .env ]] ; then + echo "Env file already exists, not overwriting" + else echo '# Consul discovery via Triton CNS' >> .env - echo CONSUL=consul.svc.${TRITON_ACCOUNT}.${TRITON_DC}.cns.joyent.com >> .env - echo SDC_KEY_ID=${SDC_KEY_ID} >> .env - echo SDC_ACCOUNT=${SDC_ACCOUNT} >> .env - echo SDC_URL=${SDC_URL} >> .env + [[ "${DOCKER_HOST:=unset}" == "*docker.joyent.com*" ]] || { + echo CONSUL="consul.svc.${TRITON_ACCOUNT}.${TRITON_DC}.cns.joyent.com" \ + >> .env + } + echo SDC_KEY_ID=${TRITON_KEY} >> .env + echo SDC_ACCOUNT=${TRITON_ACCOUNT} >> .env + echo SDC_URL=${TRITON_URL} >> .env echo >> .env - } + fi } -export -f write_env_file ensure_prerequisites() { ensure_command docker ensure_command docker-compose ensure_command triton } + +# vim: syntax=sh et ts=2 sts=2 sw=2