-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·220 lines (186 loc) · 5.4 KB
/
bootstrap.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
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
#!/bin/bash
#
#
# Bootstrap Ansible on a host. After this script is run, you can run
# Ansible playbooks to finalize the host setup
#
# This script will work and has been tested on Linux
# TODO: Test on OS X and FreeBSD.
#
# run with sudo -H
# $ sudo -H bootstrap.sh
#
# Author: Brian A. Onn ([email protected])
# Date: Sat Apr 29 16:30:15 UTC 2017
# License: MIT
trap cleanup EXIT SIGHUP SIGINT SIGQUIT SIGTERM
trapfiles=""
cleanup () {
rm -rf ${trapfiles}
}
addtrapfile () {
trapfile="${trapfile} $1"
}
#########################################
# pathname tilde expansion
# supports ~ ~/path and ~user only
# ~+ ~- and digits are not supported and
# don't make sense in a script anyways
#########################################
expandpath () {
local path="$1"
local homedir expath user rest
case "${path}" in
'~') expath="${HOME}" ;;
'~'/*) expath="${HOME}/${path##'~/'}" ;;
'~'*) user=${path%%/*}; rest=${path##$user}; user=${user##'~'}
if [ -x /usr/bin/dscacheutil ]; then ## OS X
set 1 $(dscacheutil -q user -a name "$user" | grep -e '^dir:')
homedir="$3"
else
IFS=: set 1 $(getent passwd "$user") ## Linux
homedir="$7"
fi
[ -z "${homedir}" ] && expath="${path}" || expath="${homedir}$rest"
;;
*) expath="${path}" ;;
esac
echo "${expath}"
}
#########################################
# tempdir, logging and stderr redirection
#########################################
# prefer TMP, use TEMP if TMP is not set, finally use /tmp as a default
# also do ~ expansion on TMP and TEMP
tmp="$(expandpath "${TMP:=${TEMP:-/tmp}}")"
tmplog=$(mktemp "${tmp}/ansible-bootstrap.XXXXX.log")
addtrapfile "${tmplog}"
echolog() {
echo "$*" >> $tmplog
return 0
}
# close stdout
#exec 1<&-
# close stderr
#exec 2<&-
# re-open stdout to the tty and logfile,
# and send stderr only to the logfile
#exec 3>&1 &>${tmplog} 1> >(tee >(cat >&3))
exec 2>>${tmplog} 1> >(tee -a ${tmplog} >&1)
#########################################
# local vars and utility functions here
#########################################
bold="$(tput bold)"
norm="$(tput cnorm;tput sgr0)"
red="$(tput setaf 1)"
grn="$(tput setaf 2)"
redmsg () {
echo " $bold$red*** $1 ***$norm"
}
grnmsg () {
echo " $bold$grn*** $1 ***$norm"
}
is_installed() {
type $1 2>/dev/null >/dev/null && return 0 || return 1
}
shacmd="echo no"
is_installed shasum && shacmd="shasum -p -a 256"
is_installed sha256 && shacmd=sha256
is_installed sha256sum && shacmd=sha256sum
srcpkgs="ansible sshpass" # src pkgs are built from source on OS X
binpkgs="git python curl" # linux will apt-get all these +src
pippkgs="paramiko PyYAML jinja Sphinx pycrypto cryptography" # python packages installed via pip
system="$(uname -s|tr 'A-Z' 'a-z')"
getpip () {
url="https://bootstrap.pypa.io/get-pip.py"
getpip=$(mktemp /tmp/get-pip.XXXXX.py)
sha256="19dae841a150c86e2a09d475b5eb0602861f2a5b7761ec268049a662dbd2bd0c"
echo "Downloading get-pip.py from '$url'"
curl -m 300 --retry 3 -o "${getpip}" "${url}" >> $tmplog 2>&1
dlsha256=$(${shacmd} ${getpip} | cut -f1 -d' ')
if [ "${sha256}" = "${dlsha256}" ]; then
echo "SHA256 sum is correct: $sha256"
echo "Running get-pip.py to install pip for python"
python "${getpip}"
echo "Running pip updater"
pip install -U pip
return 0
else
redmsg "The get-pip.py command at:"
redmsg "${url}"
redmsg "does not match the known sha256 checksum"
return 1
fi
}
if [ $(id -u) != 0 ]; then
redmsg "Sorry, this script must run as root"
redmsg "Use 'sudo -H' to bootstrap ansible"
exit 255
fi
echo "Platform: ${system}"
case ${system} in
linux)
apt-add-repository -y ppa:ansible/ansible
apt-get -qq -y update
for pkg in ${binpkgs} ${srcpkgs}; do
if is_installed $pkg; then ## assumes the package name is also the binary name
echo $pkg is already installed
else
echo -n "Installing $bold$pkg$norm ... "
apt-get -qy install $pkg 2>/dev/null >/dev/null
echo "[OK]"
fi
done
;;
darwin)
echo "OS X support is incomplete and untested"
#install packages which have brew formulas
brew install ${binpkgs}
# on OSX we build ansible and sshpass from src
echo "Building Ansible from source"
# requires: xcode, terminal and command-line utilites be already installed
# get ansible source here and build it
repo=$(mktemp -d /tmp/repo.XXXXX)
addtrapfile "${repo}"
git clone git://github.com/ansible/ansible.git "${repo}"
( cd ${repo} && make install )
echo "Building sshpass"
# sshpass="https://git.io/sshpass.rb"
sshpass="file://sshpass.rb"
brew install ${sshpass}
;;
bsd)
echo "I don't know bsd yet."
;;
esac
# do the pip install
getpip && echo "Running 'pip install ${pippkgs}'" && pip install ${pippkgs}
if test $? != 0; then
e1="Python package manager pip failed to install"
e2="Ansible will not work without python packages"
err=1
st="Ansible is not installed"
else
err=0
e1= e2=
st="Ansible is installed"
fi
if [ ${err} = 1 ]; then
echo
redmsg "${e1}"
redmsg "${e2}"
echo
redmsg "${st}"
else
echo
grnmsg "${st}"
fi
logfile="${tmpdir}/ansible-bootstrap-$(date -u '+%Y%m%d%H%M%S').log"
mv ${tmplog} ${logfile}
echo
echo Logfile: ${logfile}
echo
#########
# Step 2 -- Create the ansible user and home dir
#########
exit 0