/
ansitheus
executable file
·171 lines (143 loc) · 4.44 KB
/
ansitheus
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/bash
# This script can be used to interact with ansitheus
function find_base_dir() {
local real_path=$(python3 -c "import os;print(os.path.realpath('$0'))")
local dir_name="$(dirname "$real_path")"
# TODO: Check in /usr/lib etc...
BASEDIR="$(dirname ${dir_name})"
}
function usage() {
cat <<EOF
Usage: $0 COMMAND [option]
Options:
--inventory, -i <inventory_path> Specify path to ansible inventory file
--configdir, -c <config_path> Specify path to directory with config.yml
--verbose, -v Increase verbosity of ansible-playbook
--tags, -t <tags> Only run plays and tasks tagged with these values
--limit <host> Specify host to run plays
--help, -h Show this usage information
--skip-gather-fact Skip gather fact
--ask-vault-pass Ask for vault password
--vault-password-file Provide the vault password file
Commands:
precheck Do pre-deployment checks for hosts
deploy Deploy and start all ansitheus containers
pull Pull all images for containers (only pull, no running containers)
destroy Destroy Prometheus containers and service configuration
--include-images to also destroy Prometheus images
--include-volumes to also destroy Prometheus volumes
EOF
}
function process_cmd() {
echo "$ACTION : $CMD"
$CMD
if [[ $? -ne 0 ]]; then
echo "Command failed $CMD"
exit 1
fi
}
SHORT_OPTS="hi:c:t:v"
LONG_OPTS="help,inventory:,configdir:,tags:,limit:,vault-password-file:,verbose,include-images,include-volumes,skip-gather-fact,ask-vault-pass"
ARGS=$(getopt -o "${SHORT_OPTS}" -l "${LONG_OPTS}" --name "$0" -- "$@") || {
usage >&2
exit 2
}
eval set -- "$ARGS"
find_base_dir
INVENTORY="${BASEDIR}/ansible/inventory/all-in-one"
PLAYBOOK="${BASEDIR}/ansible/site.yml"
VERBOSITY=
EXTRA_OPTS=${EXTRA_OPTS}
CONFIG_DIR="/etc/ansitheus"
REMOVE_INCLUDE_IMAGES="no"
REMOVE_INCLUDE_VOLUMES="no"
SKIP_GATHER_FACT="no"
ASK_VAULT_PASS="no"
while [ "$#" -gt 0 ]; do
case "$1" in
--inventory | -i)
INVENTORY="$2"
shift 2
;;
--configdir | -c)
CONFIG_DIR="$2"
shift 2
;;
--verbose | -v)
VERBOSITY="$VERBOSITY --verbose"
shift
;;
--tags | -t)
EXTRA_OPTS="$EXTRA_OPTS --tags $2"
shift 2
;;
--limit)
EXTRA_OPTS="$EXTRA_OPTS --limit $2"
shift 2
;;
--include-images)
REMOVE_INCLUDE_IMAGES="yes"
shift 1
;;
--include-volumes)
REMOVE_INCLUDE_VOLUMES="yes"
shift 1
;;
--skip-gather-fact)
SKIP_GATHER_FACT="yes"
shift 1
;;
--ask-vault-pass)
EXTRA_OPTS="$EXTRA_OPTS --ask-vault-pass"
shift 1
;;
--vault-password-file)
EXTRA_OPTS="$EXTRA_OPTS --vault-password-file $2"
shift 2
;;
--help | -h)
usage
shift
exit 0
;;
--)
shift
break
;;
*)
echo 'error'
exit 3
;;
esac
done
case "$1" in
deploy)
ACTION="Deploying ansitheus containers with docker-compose"
EXTRA_OPTS="${EXTRA_OPTS} -e ansitheus_action=deploy"
EXTRA_OPTS="${EXTRA_OPTS} -e skip_gather_fact=${SKIP_GATHER_FACT}"
;;
pull)
ACTION="Pull all images without running containers"
EXTRA_OPTS="${EXTRA_OPTS} -e ansitheus_action=pull"
EXTRA_OPTS="${EXTRA_OPTS} -e skip_gather_fact=${SKIP_GATHER_FACT}"
;;
precheck)
ACTION="Precheck port and docker installation in target hosts"
EXTRA_OPTS="${EXTRA_OPTS} -e ansitheus_action=precheck"
EXTRA_OPTS="${EXTRA_OPTS} -e skip_gather_fact=${SKIP_GATHER_FACT}"
;;
destroy)
ACTION="Destroy Prometheus containers and service configuration"
EXTRA_OPTS="${EXTRA_OPTS} -e ansitheus_action=destroy"
EXTRA_OPTS="${EXTRA_OPTS} -e skip_gather_fact=${SKIP_GATHER_FACT}"
EXTRA_OPTS="${EXTRA_OPTS} -e destroy_include_images=${REMOVE_INCLUDE_IMAGES}"
EXTRA_OPTS="${EXTRA_OPTS} -e destroy_include_volumes=${REMOVE_INCLUDE_VOLUMES}"
;;
*)
usage
exit 0
;;
esac
CONFIG_OPTS="-e @${CONFIG_DIR}/config.yml"
CMD="ansible-playbook -i $INVENTORY $CONFIG_OPTS $EXTRA_OPTS $PLAYBOOK $VERBOSITY"
process_cmd