This repository has been archived by the owner on Mar 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmax
executable file
·104 lines (84 loc) · 2.48 KB
/
max
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env bash
SCRIPTNAME=`basename $0`
ROOTDIR="../../../"
CURDIR=`pwd`
ANSIBLE_OPTS="-v"
ANSIBLE_PLAYBOOK="`which ansible-playbook` ${ANSIBLE_OPTS}"
PLBK_PREFIX="plbk-"
export MAX_WHAT=$1; shift
export MAX_ACTION=$1; shift
export MAX_ENV=$1; shift
# shift can result in error, so we move this over here and test MAX_ENV before
# doing anything
set -e
# Extracts image version (tag) from cmdline like "sample-service-go:onbuild"
# if blank, defaults to 'latest'
export MAX_VERSION=$(echo "${MAX_WHAT}" | awk -F':' '{print $2}')
if [ "${MAX_VERSION}" == "" ]; then
export MAX_VERSION="latest"
fi
export MAX_WHAT=$(echo $MAX_WHAT | awk -F':' '{print $1}')
# Reads current user running max.sh and passes that variable into playbooks.
# If this playboook is going to be targeted localy, local user is used, instead
# root is used (on remote servers).
if [[ "${MAX_ENV}" == "dev" ]]; then
export MAX_WHO=$(whoami)
export MAX_SUDO="no"
else
export MAX_WHO=""
export MAX_SUDO=""
fi
# Loads different inventories based on ENV variable captured via cmdline.
ANSIBLE_HOSTS=""
function getEnvironment() {
if [ -f "inventories/${MAX_ENV}" ]; then
export ANSIBLE_HOSTS="inventories/${MAX_ENV}"
elif [ -f "inventories/${MAX_ENV}.ini" ]; then
export ANSIBLE_HOSTS="inventories/${MAX_ENV}.ini"
else
printf "ERROR: unknown Environment!\n"
exit 1;
fi
}
# Prepares the command line statement to be executed by Ansible.
function runPlay() {
getEnvironment
printf "${MAX_ACTION}'ing ${MAX_WHAT} in ${MAX_ENV}...\n"
CMD="${ANSIBLE_PLAYBOOK} -i ${ANSIBLE_HOSTS} ./playbooks/${PLBK_PREFIX}${MAX_WHAT}.yml"
echo ${CMD} $@
[ ! -z "$DEBUG" ] || exec ${CMD} $@
}
function getListofServices {
for i in playbooks/"${PLBK_PREFIX}"*.yml
do
printf " "; basename $i | sed -e "s/${PLBK_PREFIX}//" -e 's/\.ya?ml//'
printf " "; ansible-playbook --list-tags -i inventories/prd-sample $i | grep "TASK TAGS:" | sed -e 's/TASK TAGS://'
done
}
function myhelp() {
cat << EOH
Usage:
./max.sh [WHAT] [ACTION] [ENVIRONMENT]
ie:
./max.sh sample-service-go fetch prd
list of services:
EOH
getListofServices
}
# user passed in all params or we throw him the help in his face
if [ -z "$MAX_ENV" ]; then
myhelp
exit 1
fi
case "${MAX_ACTION}" in
"help" | "" ) # show the help
myhelp
;;
'list') # list services
getListofServices
;;
*) # default stuff
runPlay $@
;;
esac
exit 0;