-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·229 lines (192 loc) · 6.79 KB
/
run
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash -e
EXTRA_PARAMS=''
VENV_PATH=~/.ansible_venv
PATH="$PATH:$(pwd)/bin"
ARA_HTML_PATH=~/ara_html/
progname=$0
export ANSIBLE_FORCE_COLOR=true
export DEBIAN_FRONTEND=noninteractive
export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
echo -e " ==> DevOps CI/CD wrapper <ivan.bogomazov@gmail.com>"
echo Running under user `whoami`
echo Working dir = `pwd`
WORKING_DIR=`pwd`
DIRECTORY_STRUCTURE=(
"bin"
"galaxy-roles"
"library"
"playbooks"
"roles"
)
DEPENDECY_FILES=(
https://raw.githubusercontent.com/arno49/provisioner/master/ansible.cfg
https://raw.githubusercontent.com/arno49/provisioner/master/README.md
https://raw.githubusercontent.com/arno49/provisioner/master/requirements
https://raw.githubusercontent.com/arno49/provisioner/master/run
https://raw.githubusercontent.com/arno49/provisioner/master/Vagrantfile
)
function die() {
CODE=$?
echo -e "\e[31m▓▓▓▓▓ command died with code $CODE ▓▓▓▓▓\e[39m"
exit $CODE
}
function install() {
# - install system packages
# - create virtual env
# - update pip & setuptools
# - install requirements
if [ -f /etc/debian_version ]; then
echo "This is debian based distro"
install_debian
elif [ -f /etc/redhat-release ]; then
echo "This is RedHat based distro"
install_rh
else
echo "This install not yet implemented. Please contact ivan.bogomazov@gmail.com"
fi
source $VENV_PATH/bin/activate
cd $WORKING_DIR
# install python requirements
pip install -U setuptools
pip install -r requirements
# install highlevel ansible requirements
[ -f roles/requirements.yml ] && ansible-galaxy install -r roles/requirements.yml -p ./galaxy-roles/ --force || echo "Local roles not required for dependencies"
if [ $? -ne 0 ]; then
install
fi
}
function install_rh() {
if [ ! -d $VENV_PATH ]; then
echo "Virtualenv in $VENV_PATH doesn't exists, create it"
sudo yum check-update
sudo yum groupinstall -y "development tools"
sudo yum install -y python-devel libffi-devel openssl-devel libxml2-devel python-virtualenv python-pip libyaml-devel python-six coreutils
sudo -H pip install -U pip
virtualenv $VENV_PATH
fi
}
function install_debian() {
if [ ! -d $VENV_PATH ]; then
echo "Virtualenv in $VENV_PATH doesn't exists, create it"
sudo apt-get update
sudo apt-get install -y python-dev libffi-dev libssl-dev libxml2-dev libxslt1-dev python-virtualenv python-pip libyaml-dev python-six aptitude coreutils
sudo locale-gen en_US.UTF-8
sudo -H pip install -U pip
virtualenv $VENV_PATH
fi
}
function parse_cmdb_file(){
PARSED_SETTINGS=$(mktemp); $VENV_PATH/bin/python bin/parser.py $FILE_TO_PARSE > $PARSED_SETTINGS
}
function run_playbook() {
# - check virtual env
# - activate and run ansible-playbook
echo Take off with playbook $PLAYBOOK
if [ ! -d $VENV_PATH ]; then
install
fi
echo Running ansible
echo $PLAYBOOK
echo "Check for playbook requirements"
PLAYBOOK_REQUIREMENTS="$(dirname $(realpath $PLAYBOOK))/requirements.yml"
[ -f $PLAYBOOK_REQUIREMENTS ] && ansible-galaxy install -r $PLAYBOOK_REQUIREMENTS -p ./galaxy-roles/ --force || echo "Playbook $PLAYBOOK have no galaxy requirements"
export ANSIBLE_FORCE_COLOR=True
unset LD_LIBRARY_PATH
if [[ $EXTRA_PARAMS == *-i* ]]; then
if [[ $RUN_PARSER = true ]]; then
parse_cmdb_file
EXTRA_PARAMS="$EXTRA_PARAMS -e @$PARSED_SETTINGS"
fi
source $VENV_PATH/bin/activate && ansible-playbook $PLAYBOOK $EXTRA_PARAMS
else
if [[ $RUN_PARSER = true ]]; then
parse_cmdb_file
EXTRA_PARAMS="$EXTRA_PARAMS -e @$PARSED_SETTINGS"
fi
source $VENV_PATH/bin/activate && ansible-playbook $PLAYBOOK -i localhost, -c local $EXTRA_PARAMS
fi
rc=$?
if [ -d "~/.ara/" ]; then
echo Generating Ansible Run Analyzer HTML files
ara_result=$(ara generate html $ARA_HTML_PATH 2>&1)
if [ $? -ne 0 ]
then
echo -e "\e[31mfail:\e[39m Failed to generate ara report: $ara_result"
fi
fi
exit $rc
}
function init() {
# pwd
# if [ ! -d "~/.git/" ]; then
# echo "that's not vcs directory, init repo first"
# fi
cd $WORKING_DIR
echo "=> creating directory structure"
for directory in ${DIRECTORY_STRUCTURE[*]}; do
mkdir -p $directory
done
echo "=> downloading dependencies"
for filename in ${DEPENDECY_FILES[*]}; do
curl -o `basename $filename` $filename
chmod +x run
done
}
function usage () {
cat <<EOF
Usage: $progname [-p path_to_playbook] [-i] [-lvs] [-t tag,...] [-f vault_file] [-I dynamic_inventory.py]
-i, --install install environment
-l, --local_install local install - sets local_install=True
-t, --tags tags to run (list of tags separated by commas)
-v run ansible in verbose mode
-s, --syntax-check perform a syntax check on the playbook, but do not execute it
-f, --vault-password-file vault password file
-I, --inventory path to dynamic inventory script
-n, --init init file structure
-h, --help print this message
EOF
exit 0
}
# replace long options with short ones as getops doesn't support long options
for arg in "$@"; do
shift
case "$arg" in
"--local_install") set -- "$@" "-l" ;;
"--install") set -- "$@" "-i" ;;
"--help") set -- "$@" "-h" ;;
"--tags") set -- "$@" "-t" ;;
"--syntax-check") set -- "$@" "-s" ;;
"--vault-password-file") set -- "$@" "-f" ;;
"--extra-vars") set -- "$@" "-e" ;;
"--inventory") set -- "$@" "-I" ;;
"--init") set -- "$@" "-n" ;;
*) set -- "$@" "$arg"
esac
done
OPTIND=1
# getopts block
while getopts ":p:inlt:hvsf:e:I:" opt; do
case $opt in
p) PLAYBOOK=$OPTARG ;;
i) echo 'Install was triggered...'; install && exit || die ;;
n) echo 'Init file structure triggered...'; init && exit || die ;;
l) EXTRA_PARAMS=$EXTRA_PARAMS' -e local_install=True' ;;
t) EXTRA_PARAMS=$EXTRA_PARAMS' --tags '$OPTARG ;;
h) usage ;;
v) EXTRA_PARAMS=$EXTRA_PARAMS' -vvvv' ;;
s) EXTRA_PARAMS=$EXTRA_PARAMS' --syntax-check' ;;
f) EXTRA_PARAMS="$EXTRA_PARAMS --vault-password-file=$OPTARG" ;;
e) RUN_PARSER=true; FILE_TO_PARSE=$OPTARG ;;
I) EXTRA_PARAMS="$EXTRA_PARAMS -i $OPTARG" ;;
esac
done
shift $(($OPTIND - 1))
# check if playbook variable is set, otherwise print usage
if [ -z ${PLAYBOOK+x} ]; then
echo "PLAYBOOK is unset"
usage
else
run_playbook
fi