Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deploy-cluster script #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] <cluster> [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)
Expand Down
212 changes: 212 additions & 0 deletions bin/deploy-cluster
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
#!/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] <cluster> [command]

Options:

-C, --chdir <path> change the working directory to <path>
-c, --config <path> 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 <cmd> execute the given <cmd>
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 <msg>
#

abort() {
echo
echo " $@" 1>&2
echo
exit 1
}

#
# Log <msg>.
#

log() {
echo " ○ $@"
}

#
# Set configuration file <path>.
#

set_config_path() {
test -f $1 || abort invalid --config path
CONFIG=$1
}

#
# Check if config <section> 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 <cmd>.
#

run_deploy() {
local deploy="`deploy_command`"
local envs="`get_envs`"

echo $deploy -c $CONFIG "\"$@\"" >> $LOG
$deploy -c $CONFIG "$@"
}

#
# Run the given deploy <cmd> on all <env> in the <cluster>.
#

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 "<cluster> 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 $@;
exit;
fi
;;
esac
done

require_cluster