From 01ee47cc47cac3dd34c7d814878ed72d7e34e8f0 Mon Sep 17 00:00:00 2001 From: Andre Marcelo-Tanner Date: Sat, 30 Apr 2016 12:00:57 +0800 Subject: [PATCH 1/3] Create deploy-cluster script Runs `deploy` on multiple configurations which are grouped by a cluster name like `[Group1:AppA]` and `[Group1:AppB]` Running `deploy-cluster Group1` will run `deploy` on both `[Group1:AppA]` and `[Group1:AppB]` --- Makefile | 6 +- bin/deploy-cluster | 209 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+), 2 deletions(-) create mode 100755 bin/deploy-cluster diff --git a/Makefile b/Makefile index 91193b8..00ef677 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,12 @@ PREFIX ?= /usr/local -install: bin/deploy - @cp -p $< $(PREFIX)/$< +install: + @cp -p bin/deploy $(PREFIX)/bin/deploy + @cp -p bin/deploy-cluster $(PREFIX)/bin/deploy-cluster uninstall: rm -f $(PREFIX)/bin/deploy + rm -f $(PREFIX)/bin/deploy-cluster .PHONY: install uninstall \ No newline at end of file diff --git a/bin/deploy-cluster b/bin/deploy-cluster new file mode 100755 index 0000000..9b42cc0 --- /dev/null +++ b/bin/deploy-cluster @@ -0,0 +1,209 @@ +#!/usr/bin/env bash + +# +# deploy-custer(1) - Shell script that augments deploy with features for deploying on multiple servers +# Released under the MIT License. +# +# https://github.com/kzap/deploy +# + +VERSION="0.1.0" +DEPLOY_CMD=deploy +CONFIG=./deploy.conf +LOG=/tmp/deploy-cluster.log +TEST=1 +REF= +CLUSTER= + +# +# Output usage information. +# + +usage() { + cat <<-EOF + + Usage: deploy-cluster [options] [command] + + Options: + + -C, --chdir change the working directory to + -c, --config set config path. defaults to ./deploy.conf + -T, --no-tests ignore test hook + -V, --version output program version + -h, --help output help information + + Commands: + + setup run remote setup commands + update update deploy to the latest release + revert [n] revert to [n]th last deployment or 1 + config [key] output config file or [key] + curr[ent] output current release commit + prev[ious] output previous release commit + exec|run execute the given + console open an ssh session to the host + list list previous deploy commits + [ref] deploy to [ref], the 'ref' setting, or latest tag + +EOF +} + +# +# Abort with +# + +abort() { + echo + echo " $@" 1>&2 + echo + exit 1 +} + +# +# Log . +# + +log() { + echo " ○ $@" +} + +# +# Set configuration file . +# + +set_config_path() { + test -f $1 || abort invalid --config path + CONFIG=$1 +} + +# +# Check if config
exists. +# + +config_section() { + grep "^\[$1:" $CONFIG &> /dev/null +} + +get_envs() { + local cluster + cluster=`grep "^\[${CLUSTER}:" $CONFIG` + # remove brackets + cluster=${cluster//\[/} + cluster=${cluster//\]/} + + echo $cluster +} + + +# Output version. +# + +version() { + echo "deploy-cluster $VERSION" + + log `deploy_command` --version + log `run_deploy --version`; +} + +# +# Return the deploy command to run. +# + +deploy_command() { + #local key="`config_get key`" + #test -n "$key" && local identity="-i $key" + + echo "$DEPLOY_CMD" +} + +# +# Run the given deploy . +# + +run_deploy() { + local deploy="`deploy_command`" + local envs="`get_envs`" + + echo $deploy -c $CONFIG "\"$@\"" >> $LOG + $deploy -c $CONFIG "$@" +} + +# +# Run the given deploy on all in the . +# + +run_deploy_cluster() { + local deploy="`deploy_command`" + local envs="`get_envs`" + + for env in $envs; do + log Running: $deploy $env $@ + echo $deploy -c $CONFIG $env "\"$@\"" >> $LOG + $deploy -c $CONFIG $env $@ + done + +} + +# +# Require environment arg. +# + +require_cluster() { + test -z "$CLUSTER" && abort " required" + config_section $CLUSTER || abort "[$CLUSTER:] cluster config section not defined" +} + +# +# Output config or [key]. +# + +config() { + if test $# -eq 0; then + cat $CONFIG + else + require_cluster + run_deploy_cluster config $1 + fi +} + +# +# Update deploy. +# + +update() { + log "updating deploy(1)" + rm -fr /tmp/deploy + git clone git://github.com/kzap/deploy.git \ + --depth 0 \ + /tmp/deploy \ + &> /tmp/deploy.log \ + && cd /tmp/deploy \ + && make install \ + && log "updated $VERSION -> `./bin/deploy --version`" +} + +# parse argv + +while test $# -ne 0; do + arg=$1; shift + case $arg in + #-h|--help) usage; exit ;; + -V|--version) version; exit ;; + -c|--config) set_config_path $1; shift ;; + -C|--chdir) log cd $1; cd $1; shift ;; + update) update; exit ;; + config) config $@; exit ;; + *) + if test -z "$CLUSTER"; then + CLUSTER=$arg; + require_cluster + run_deploy_cluster $@; + exit; + else + require_cluster + run_deploy_cluster $@; + fi + ;; + esac +done + From 0abfdb3b572039f11cca470db783603ea7cb032e Mon Sep 17 00:00:00 2001 From: Andre Marcelo-Tanner Date: Sat, 30 Apr 2016 14:59:23 +0800 Subject: [PATCH 2/3] show cluster required error if no args --- bin/deploy-cluster | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/deploy-cluster b/bin/deploy-cluster index 9b42cc0..583d953 100755 --- a/bin/deploy-cluster +++ b/bin/deploy-cluster @@ -202,8 +202,11 @@ while test $# -ne 0; do else require_cluster run_deploy_cluster $@; + exit; fi ;; esac done +require_cluster + From 862e4f04abdb7998abf41f9918c3adf07bc09807 Mon Sep 17 00:00:00 2001 From: Andre Marcelo-Tanner Date: Sat, 30 Apr 2016 15:20:23 +0800 Subject: [PATCH 3/3] updating Readme --- Readme.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Readme.md b/Readme.md index f0ce18b..a35ac65 100644 --- a/Readme.md +++ b/Readme.md @@ -127,6 +127,39 @@ test ./something + +# deploy-cluster + + A wrapper script that runs `deploy(1)` for multiple environments from the config file + +## Usage + + + Usage: deploy-cluster [options] [command] + + Options: + + -V, --version output program version + + Commands: + + All the same commands as deploy(1) as it is just a wrapper for deploy(1) + +## Configuration + + Use the semi colon `:` to group environments into clusters + + [develop:appA] + key /path/to/some.pem + + [develop:appB] + key /path/to/some.pem + +## Sample + + deploy-cluster develop + + ## License (The MIT License)