Created
August 30, 2018 16:52
-
-
Save tareqy/b56bdd875eece46696250f8bf427d20d to your computer and use it in GitHub Desktop.
BigPanda deploy shell script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function start { | |
if [ -z "$BP_API_TOKEN" ]; then | |
echo "$0: Please set \$BP_API_TOKEN." >&2 | |
exit 1 | |
fi | |
cat >$TMPFILE <<EOF | |
{ | |
"component": "$1", | |
"version": "$2", | |
"source_system": "bash", | |
EOF | |
[ -n "$4" ] && echo " \"env\": \"$4\"," >> $TMPFILE | |
[ -n "$5" ] && echo " \"description\": \"$5\"," >> $TMPFILE | |
[ -n "$6" ] && echo " \"owner\": \"$6\"," >> $TMPFILE | |
cat >>$TMPFILE <<EOF | |
"hosts": [ $3 ] | |
} | |
EOF | |
api_call "$BASE_URL/data/events/deployments/start" >/dev/null | |
rm $TMPFILE | |
} | |
function success { | |
if [ -z "$BP_API_TOKEN" ]; then | |
echo "$0: Please set \$BP_API_TOKEN." >&2 | |
exit 1 | |
fi | |
cat >$TMPFILE <<EOF | |
{ "component": "$1", | |
"version": "$2", | |
"hosts": [ $3 ], | |
"status": "success" | |
} | |
EOF | |
api_call "$BASE_URL/data/events/deployments/end" >/dev/null | |
rm $TMPFILE | |
} | |
function fail { | |
if [ -z "$BP_API_TOKEN" ]; then | |
echo "$0: Please set \$BP_API_TOKEN." >&2 | |
exit 1 | |
fi | |
cat >$TMPFILE <<EOF | |
{ "component": "$1", | |
"version": "$2", | |
"hosts": [ $3 ], | |
EOF | |
[ -n "$4" ] && echo \"errorMessage\": \"$4\", >> $TMPFILE | |
cat >>$TMPFILE <<EOF | |
"status": "failure" | |
} | |
EOF | |
api_call "$BASE_URL/data/events/deployments/end" >/dev/null | |
rm $TMPFILE | |
} | |
function help { | |
case "$1" in | |
start) | |
echo "Usage: start [-e ENV] [-d DESCRIPTION] [-o OWNER] APP_NAME VERSION HOST [HOST [...]]" | |
echo | |
echo "APP_NAME Application being deployed" | |
echo "VERSION Version of application being deployed" | |
echo "HOST List of hostnames that are included in the deployment" | |
echo "ENV Optional environment name" | |
echo "DESCRIPTION Optional deployment description" | |
echo "OWNER Optional deployment owner" | |
;; | |
success) | |
echo "Usage: success APP_NAME VERSION HOST [HOST [...]]" | |
echo | |
echo "APP_NAME Application being deployed" | |
echo "VERSION Version of application being deployed" | |
echo "HOST List of hostnames the deployment succeeded on" | |
;; | |
fail) | |
echo "Usage: fail [-m MESSAGE] APP_NAME VERSION HOST [HOST [...]]" | |
echo | |
echo "APP_NAME Application being deployed" | |
echo "VERSION Version of application being deployed" | |
echo "HOST List of hostnames the deployment failed on" | |
echo "MESSAGE Optional error message" | |
;; | |
'') | |
echo "$0: Send deployment info to BigPanda" | |
echo | |
echo "List of commands:" | |
echo "start Notify a deployment has started" | |
echo "success Notify a deployment has succeeded" | |
echo "fail Notify a deployment has failed" | |
echo "help Get help about commands." | |
echo | |
echo "Run '$0 help COMMAND' for info about that command" | |
;; | |
*) | |
echo "Usage: help COMMAND"; | |
echo | |
echo "COMMAND can be start, success or fail" | |
;; | |
esac | |
} | |
function usage { | |
echo "Usage: $0 COMMAND arguments" | |
echo | |
echo "Run '$0 help' for more info" | |
} | |
function parse_args { | |
while [ -n "$1" ]; do | |
case "$1" in | |
-h|--help) | |
help $command | |
exit | |
;; | |
-e) | |
shift | |
if [ -z "$1" ]; then | |
echo "$0: missing environment for -e" | |
exit 1 | |
fi | |
ENV=$1 | |
;; | |
-d) | |
shift | |
if [ -z "$1" ]; then | |
echo "$0: missing description for -d" | |
exit 1 | |
fi | |
DESCRIPTION=$1 | |
;; | |
-o) | |
shift | |
if [ -z "$1" ]; then | |
echo "$0: missing owner for -o" | |
exit 1 | |
fi | |
OWNER=$1 | |
;; | |
-m) | |
shift | |
if [ -z "$1" ]; then | |
echo "$0: missing message for -m" | |
exit 1 | |
fi | |
MESSAGE=$1 | |
;; | |
-*) | |
echo "$0: Illegal option: $1" | |
exit 1 | |
;; | |
*) | |
if [ -z "$APP_NAME" ]; then | |
APP_NAME=$1 | |
elif [ -z "$VERSION" ]; then | |
VERSION=$1 | |
elif [ -z "$HOSTS" ]; then | |
HOSTS="\"$1\"" | |
else | |
HOSTS="$HOSTS,\"$1\"" | |
fi | |
esac | |
shift | |
done | |
if [ -z "$APP_NAME" -o -z "$VERSION" -o -z "$HOSTS" ]; then | |
echo "$0: missing component, version or host" >&2 | |
exit 1 | |
fi | |
} | |
function api_call { | |
curl -# -f -i -X POST -H "Authorization: Bearer $BP_API_TOKEN" -H "Content-Type: application/json" -d "@$TMPFILE" "$@" | |
} | |
TMPFILE=/tmp/bigpanda_deployment | |
BASE_URL="https://api.bigpanda.io" | |
APP_NAME= | |
VERSION= | |
HOSTS= | |
ENV= | |
MESSAGE= | |
OWNER= | |
DESCRIPTION= | |
command=$1 | |
shift | |
case "$command" in | |
start) | |
parse_args "$@" | |
start "$APP_NAME" "$VERSION" "$HOSTS" "$ENV" "$DESCRIPTION" "$OWNER";; | |
success) | |
parse_args "$@" | |
success "$APP_NAME" "$VERSION" "$HOSTS" ;; | |
fail) | |
parse_args "$@" | |
fail "$APP_NAME" "$VERSION" "$HOSTS" "$MESSAGE" ;; | |
help) | |
help "$@" >&2 ;; | |
*) | |
usage >&2 | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment