-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.sh
executable file
·161 lines (134 loc) · 4.6 KB
/
start.sh
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
#!/bin/bash
### startup script for Ansible testing
if [ "${PKG_CMD}" == "" ]; then
echo "ERROR: No PKG_CMD set! eg: 'yum -y install'"
exit 1
fi
[ "${PLAYBOOK}" != "" ] && playbook="${PLAYBOOK}" || playbook=''
[ "${WORKDIR}" != "" ] && wd="${WORKDIR}" || wd='/workspace'
[ "${PYPI}" != "" ] && pypifile="${PYPI}" || pypifile="${wd}/requirements.txt"
[ "${SYSPKGS}" != "" ] && pkgfile="${SYSPKGS}" || pkgfile="${wd}/system_packages.txt"
[ "${VAULTFILE}" != "" ] && vaultfile="${VAULTFILE}" || vaultfile="${wd}/vault-password.txt"
if [ "${GALAXY}" != "" ]; then
galaxyfile="${GALAXY}"
elif [ -f "${wd}/requirements.yml" ]; then
galaxyfile="${wd}/requirements.yml"
elif [ -f "${wd}/roles/requirements.yml" ]; then
galaxyfile="${wd}/roles/requirements.yml"
else
galaxyfile=""
fi
verbosity=''
skip_all=0
skip_playbook=0
cmd="ansible-playbook"
USAGE="""$0 [-x] [-y] [-h] [-*]
Installs pre-reqs and runs an Ansible playbook.
Version $(cat /VERSION)
-x Skip all dependency installs.
-y Skip playbook run.
-h Show this help message
-* Any option supported by ansible-playbook (eg: -e SOMEVAR=someval -i /path/to/inventory)
The following environment variables can be used to modify the playbook run:
WORKDIR Path to code location in the image.
Default: /workspace
PLAYBOOK Path to Ansible playbook.
Default: ${wd}/test.yml > local.yml > playbook.yml > site.yml
GALAXY Path to Ansible Galaxy requirements file.
Default: ${wd}/requirements.yml
PYPI Path to PyPI/pip requirements file
Default: ${wd}/requirements.txt
SYSPKGS Path to a list of system packages to install, one per line.
Default: ${wd}/system_packages.txt
VAULTFILE Path to a plaintext file containing the Ansible Vault password.
Default: ${wd}/vault-password.txt
GPG_PK Unencrypted GPG secret key to use with git-crypt.
"""
# doing this instead of getopts so we can trap "invalid" params and use them as
# part of the ansible-playbook command
while test $# -gt 0; do
if [ "$1" == "-x" ]; then
skip_all=1
elif [ "$1" == "-y" ]; then
skip_playbook=1
elif [ "$1" == "-h" ]; then
echo -e "${USAGE}"; exit 0
else
# this is janky as fuck, but it allows us to use inline json params for ansible
if [[ "$1" == {* ]]; then
cmd="${cmd} '${1}'"
else
cmd="${cmd} ${1}"
fi
fi
shift
done
# print startup banner
source /etc/os-release
seq -s# 60 | tr -d '[:digit:]'
echo "# Launching Ansible Docker container startup script..."
echo "# - Image v$(cat /VERSION)"
echo "# - ${PRETTY_NAME}"
echo "# - $(ansible --version | head -n 1)"
echo "# - $(python --version 2>&1)"
seq -s# 60 | tr -d '[:digit:]'
# prep gpg key if necessary
if [ "${GPG_PK}" != "" ]; then
echo -e "\n### GPG key found, importing..."
eval $(gpg-agent --daemon 2> /dev/null)
echo "${GPG_PK}" > /pk.key
gpg --batch --yes --import /pk.key
git-crypt unlock
fi
# autodetect vault-password.txt
if [ -f "${vaultfile}" ]; then
echo -e "\n### Vault password file found at ${vaultfile}, using it in command."
if [ "${VAULT_FILE_MODE}" != "" ]; then
chmod "${VAULT_FILE_MODE}" "${vaultfile}"
fi
cmd="${cmd} --vault-password-file ${vaultfile}"
else
echo -e "\n### No vault password file found at ${vaultfile}"
fi
# Install ansible-galaxy requirements
if [ -f "${galaxyfile}" ] && [[ $skip_all -eq 0 ]]; then
echo -e "\n### Installing pre-reqs from Ansible Galaxy..."
ansible-galaxy install -r "${galaxyfile}"
else
echo -e "\n### No Ansible Galaxy pre-reqs found at ${galaxyfile}, moving on."
fi
# Install Python requirements
if [ -f "${pypifile}" ] && [[ $skip_all -eq 0 ]]; then
echo -e "\n### Installing pre-reqs from PyPI..."
pip install -r "${pypifile}"
else
echo -e "\n### No Python pre-reqs found at ${pypifile}, moving on."
fi
# Install system packages
if [ -f "${pkgfile}" ] && [[ $skip_all -eq 0 ]]; then
echo -e "\n### Installing system packages..."
pkgs=""
cat $pkgfile | while read line; do
pkgs="${pkgs} ${line}"
done
$PKG_CMD $pkgs
else
echo -e "\n### No system package pre-reqs found at ${pkgfile}, moving on."
fi
# Look for a playbook file
if [ ! -f "${playbook}" ] && [ ! -f "${wd}/${playbook}" ]; then
for pb in 'test.yml' 'local.yml' 'playbook.yml' 'site.yml'; do
if [ -f "${wd}/${pb}" ]; then
playbook="${wd}/${pb}"
echo -e "\n### Found playbook: ${playbook}"
break
fi
done
fi
if [[ $skip_playbook -eq 1 ]]; then
echo -e "\n### Skipping playbook run."
exit 0
fi
# Do the thing.
echo -e "\n### Starting run for playbook ${playbook}..."
eval "${cmd} ${playbook}"