From c7567f38386970b94a7b10fec036e78954ab1131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81rgio=20Ramos?= Date: Fri, 26 May 2017 11:17:00 +0100 Subject: [PATCH] chore: initial circleci config --- bin/deploy | 30 ++++++++++++++ bin/docker-login | 10 +++++ bin/setup | 23 +++++++++++ bin/setup-tools | 105 +++++++++++++++++++++++++++++++++++++++++++++++ circle.yml | 49 ++++++++++++++++++++++ 5 files changed, 217 insertions(+) create mode 100755 bin/deploy create mode 100755 bin/docker-login create mode 100755 bin/setup create mode 100755 bin/setup-tools create mode 100644 circle.yml diff --git a/bin/deploy b/bin/deploy new file mode 100755 index 00000000..31ee54af --- /dev/null +++ b/bin/deploy @@ -0,0 +1,30 @@ +#! /usr/bin/env bash + +# +# Prelude - make bash behave sanely +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +# +set -euo pipefail + +# Beware of CDPATH gotchas causing cd not to work correctly when a user +# has set this in their environment +# https://bosker.wordpress.com/2012/02/12/bash-scripters-beware-of-the-cdpath/ +unset CDPATH + +readonly INCLUDE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +# shellcheck source=bin/setup-tools +. "${INCLUDE}"/setup-tools + +echo ">> running triton deploy with docker-compose" +ensure_command triton +ensure_triton_cns_is_enabled +get_triton_details +write_env_file + +export DOCKER_HOST=$_DOCKER_HOST +export DOCKER_CERT_PATH=$_DOCKER_CERT_PATH +# Do not TLS verify for now, incompatibilities between circleci and joyent +export DOCKER_TLS_VERIFY= + +docker-compose pull +COMPOSE_PROJECT_NAME=${CIRCLE_BRANCH} docker-compose up -d diff --git a/bin/docker-login b/bin/docker-login new file mode 100755 index 00000000..4021d1f3 --- /dev/null +++ b/bin/docker-login @@ -0,0 +1,10 @@ +#! /usr/bin/env bash + +# +# Prelude - make bash behave sanely +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +# +set -euo pipefail + +echo ">> Logging into $_DOCKER_REGISTRY" +docker login -e="." -u="$_DOCKER_LOGIN_USERNAME" -p="$_DOCKER_LOGIN_PASSWORD" "$_DOCKER_REGISTRY" diff --git a/bin/setup b/bin/setup new file mode 100755 index 00000000..1cae372c --- /dev/null +++ b/bin/setup @@ -0,0 +1,23 @@ +#! /usr/bin/env bash + +# +# Prelude +# +set -euo pipefail +IFS=$'\n\t' +# Beware of CDPATH gotchas causing cd not to work correctly when a user +# has set this in their environment +# https://bosker.wordpress.com/2012/02/12/bash-scripters-beware-of-the-cdpath/ +unset CDPATH + +readonly INCLUDE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +# shellcheck source=bin/setup-tools +. "${INCLUDE}"/setup-tools + +# +# Main +# +ensure_prerequisites +get_triton_details +check_docker_config +write_env_file diff --git a/bin/setup-tools b/bin/setup-tools new file mode 100755 index 00000000..9d2e65dc --- /dev/null +++ b/bin/setup-tools @@ -0,0 +1,105 @@ +#! /usr/bin/env bash + +# setup.sh - Checks that all the required tools are present and that they are +# appropriately configured for deploying to Triton. +# +# Adapted from https://github.com/autopilotpattern/mysql/blob/master/setup.sh +# + +# +# Prelude +# +set -euo pipefail +IFS=$'\n\t' + +# +# Utilities +# +die() { + local msg="$*" + [[ -z "${msg}" ]] || { + echo + tput setaf 1 # red + tput bold + echo "${msg}" + tput sgr0 # reset + } + exit 1 +} + +# +# Check functions +# +ensure_command() { + local cmd="$1" + + command -v "${cmd}" > /dev/null 2>&1 || { + die "Couldn't find required command: ${cmd}" + } +} + +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 +} + +ensure_docker_config_matches_triton_config_and_capture_triton_details() { + 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 + [[ ! "$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 config does not match your Docker configuration." + } + + return 0; +} + +ensure_triton_cns_is_enabled() { + 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." + } +} + +write_env_file() { + if [[ -f .env ]] ; then + echo "Env file already exists, not overwriting" + else + echo '# Consul discovery via Triton CNS' >> .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 +} + +ensure_prerequisites() { + ensure_command docker + ensure_command docker-compose + ensure_command triton +} + +# vim: syntax=sh et ts=2 sts=2 sw=2 diff --git a/circle.yml b/circle.yml new file mode 100644 index 00000000..e94291c4 --- /dev/null +++ b/circle.yml @@ -0,0 +1,49 @@ +# Customize the test machine +machine: + pre: + - git config --global user.email "circleci@joyent.zone" + - git config --global user.name "circlebot" + - curl -sSL https://s3.amazonaws.com/circle-downloads/install-circleci-docker.sh | bash -s -- 1.10.0 + services: + - docker + node: + version: 7.7.3 + +dependencies: + pre: + # install docker-compose + - sudo curl -L https://github.com/docker/compose/releases/download/1.8.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose + - sudo chmod +x /usr/local/bin/docker-compose + # install and setup triton + - yarn global add triton@4.15.0 || cat /home/ubuntu/.yarn-config/global/yarn-error.log + - echo '{"url":"https://eu-ams-1.api.joyent.com","account":"'$SDC_ACCOUNT'","keyId":"c3:30:35:9b:85:48:73:44:31:cc:4b:2e:6a:00:16:e2","name":"eu-ams-1","curr":true}' | triton profile create -f - + - triton env --docker eu-ams-1 + # setup tap report + - mkdir -p ${CIRCLE_TEST_REPORTS}/tap-xunit/ + override: + # install dependencies + - yarn install --prefer-offline + +test: + override: + # lint + - yarn run lint + # test + - yarn run test + +# deployment: +# development: +# branch: master +# commands: +# - ./bin/docker-login +# - make -j2 build +# - make -j2 push +# - ./bin/deploy +# production: +# tag: /production-*/ +# commands: +# - CIRCLE_BRANCH=staging ./bin/docker-login +# - CIRCLE_BRANCH=staging ./bin/on-changes-publish-ui +# - CIRCLE_BRANCH=staging make -j2 build +# - CIRCLE_BRANCH=staging make -j2 push +# - CIRCLE_BRANCH=staging ./bin/deploy