-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.sh
executable file
·623 lines (530 loc) · 13.5 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#!/bin/sh -e
# Set up your environment with everything needed for Ansible dev and testing.
process_args() {
while [ "$1" ]; do
case "$1" in
"os")
mode="os"
;;
"pip")
mode="pip"
;;
"brewdo")
mode="brewdo"
;;
"brew")
mode="brew"
;;
"-t" | "--test")
test=1
;;
"-y" | "--assumeyes")
auto="-y"
;;
"-q" | "--quiet")
quiet="-q"
;;
"-h" | "--help")
help=1
;;
"--help-all")
help=1
help_all=1
;;
"--as-platform")
shift
set_platform=1
as_platform="$1"
;;
"--as-version")
shift
set_version=1
as_version="$1"
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
shift
done
if [ ! ${mode} ]; then
help=1
fi
}
show_help() {
if [ ! ${help} ]; then return; fi
cat <<- EOF
Usage: bootstrap.sh command [option ...]
Platform: ${ID:-Not Detected}
Version: ${VERSION_ID:-Not Detected}
Commands:
EOF
if [ ${help_all} ]; then
commands="os pip brewdo brew"
cat <<- EOF
Command availability depends on platform and version.
EOF
elif [ "${ID}" = "" ] || [ "${VERSION_ID}" = "" ]; then
cat <<- EOF
Platform or version not detected. No commands available.
EOF
elif [ "${commands}" = "" ]; then
cat <<- EOF
Platform or version not supported. No commands available.
EOF
else
cat <<- EOF
Commands available for the detected platform and version.
Unless otherwise noted, commands must be run as root.
EOF
fi
for command in ${commands}; do
if [ "${command}" = "os" ]; then
cat <<- EOF
os Install all packages using OS package management.
Python pip will not be used to install any packages.
EOF
fi
if [ "${command}" = "pip" ]; then
cat <<- EOF
pip Install Python packages (except crypto) using pip.
Install non-Python packages with OS package management.
EOF
fi
if [ "${command}" = "brewdo" ]; then
cat <<- EOF
brewdo Install using a combination of brewdo and pip.
EOF
fi
if [ "${command}" = "brew" ]; then
cat <<- EOF
brew Install using a combination of brew and pip.
Must be run as a non-root administrator.
EOF
fi
done
if [ "${commands_note}" != "" ]; then
cat <<- EOF
NOTE: ${commands_note}
EOF
fi
cat <<- EOF
Options:
-y, --assumeyes Assume yes for all questions and do not prompt.
-q, --quiet Show minimal output.
-h, --help Show this help message and exit.
--help-all Show help for all commands and exit.
-t, --test Clone the Ansible GitHub repository and run tests.
EOF
exit 0
}
detect_platform() {
if [ -f /etc/os-release ]; then
. /etc/os-release
elif [ -f /etc/centos-release ]; then
# detect CentOS 6 and earlier
ID=centos
VERSION_ID=$(grep -o '[0-9]' /etc/centos-release | head -n 1)
elif [ -f /etc/redhat-release ]; then
# detect RHEL 6 and earlier
ID=rhel
VERSION_ID=$(grep -o '[0-9]' /etc/redhat-release | head -n 1)
elif VERSION_ID=$(sw_vers -productVersion 2> /dev/null); then
ID=osx
elif VERSION_ID=$(freebsd-version -u 2> /dev/null); then
ID=freebsd
fi
if [ ${set_platform} ]; then
ID="${as_platform}"
fi
if [ ${set_version} ]; then
VERSION_ID="${as_version}"
fi
if [ "$(id -u)" -eq 0 ]; then
have_root=1
fi
}
review_platform() {
case "${ID}" in
ubuntu)
major_version=$(echo "${VERSION_ID}" | awk -F "." '{print $1}')
if [ "${major_version}" -ge 14 ]; then
commands="os pip"
fi
;;
debian)
if [ "${VERSION_ID}" = "" ]; then
VERSION_ID="none" # sid/unstable has no version number
commands="os pip"
elif [ "${VERSION_ID}" -ge 8 ]; then
commands="os pip"
elif [ "${VERSION_ID}" -ge 7 ]; then
commands="pip"
commands_note="OS packages are too old, use of pip required."
fi
;;
fedora)
commands="os pip"
;;
centos)
if [ "${VERSION_ID}" -ge 7 ]; then
commands="os pip"
elif [ "${VERSION_ID}" -ge 6 ]; then
commands="pip"
commands_note="OS packages are too old, use of pip required."
fi
;;
rhel)
if [ "${VERSION_ID}" -ge 7 ]; then
commands="os pip"
elif [ "${VERSION_ID}" -ge 6 ]; then
commands="pip"
commands_note="OS packages are too old, use of pip required."
fi
;;
osx)
major_version=$(echo "${VERSION_ID}" | awk -F "." '{print $1}')
minor_version=$(echo "${VERSION_ID}" | awk -F "." '{print $2}')
if [ "${major_version}" -eq 10 ] && [ "${minor_version}" -ge 9 ]; then
brewdo > /dev/null 2>&1 && have_brewdo=1
brew --version > /dev/null 2>&1 && have_brew=1
if [ ${have_brewdo} ]; then
commands="brewdo"
commands_note="Only brewdo is supported when already installed."
elif [ ${have_brew} ]; then
commands="brew"
commands_note="Only brew is supported when already installed."
else
commands="brewdo brew"
fi
fi
;;
freebsd)
commands="pip"
;;
esac
}
check_platform() {
for command in ${commands}; do
if [ "${mode}" = "${command}" ]; then
valid_mode=1
fi
done
if [ ! ${valid_mode} ]; then
cat <<- EOF
Command '${mode}' is not valid for this platform or version.
See available commands with: bootstrap.sh --help
EOF
exit 1
fi
case "${mode}" in
brew)
if [ ${have_root} ]; then
echo "Command '${mode}' cannot be run as root."
exit 1
fi
;;
*)
if [ ! ${have_root} ]; then
echo "Command '${mode}' must be run as root."
exit 1
fi
;;
esac
pip --version > /dev/null 2>&1 && have_pip=1
curl --version > /dev/null 2>&1 && have_curl=1
yum="yum"
if [ ${mode} = "pip" ] && [ ! ${have_pip} ] && [ ! ${have_curl} ]; then
install_curl=1
fi
}
osx_setup() {
if xcode-select --print-path > /dev/null 2>&1; then return; fi
# Install CLI tools without any GUI prompts.
# Based on the solution found on Stack Exchange here:
# http://apple.stackexchange.com/questions/107307
# Create file checked by CLI updates' .dist code in Apple's SUS catalog.
# Command Line Tools will not appear in the update list without this.
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
echo "Finding Command Line Tools software update ..."
update=$(softwareupdate -l \
| grep '^ *\* *Command Line Tools ' \
| head -n 1 \
| sed -e 's/^ *\* *//')
if [ ! ${have_root} ]; then
softwareupdate_wrapper="sudo"
fi
echo "Installing software update: ${update} ..."
${softwareupdate_wrapper} softwareupdate --install "${update}"
}
apt_setup() {
if [ "${install_curl}" ]; then curl_package="curl"; fi
packages="
make
git
${curl_package}
python
python-crypto
"
# shellcheck disable=SC2086
{
echo "Updating apt package index ..."
apt-get ${quiet} ${auto} update
echo "Installing required OS packages:" ${packages}
apt-get ${quiet} ${auto} install ${packages}
}
}
yum_setup() {
install_epel="$1"
crypto_version="$2"
if [ "${install_curl}" ]; then curl_package="curl"; fi
if [ "${install_epel}" ]; then epel_package="epel-release"; fi
if [ ! "${crypto_version}" ]; then crypto_package="python-crypto"; fi
packages="
${epel_package}
which
make
git
${curl_package}
python
${crypto_package}
"
# shellcheck disable=SC2086
{
echo "Updating yum package index ..."
${yum} ${quiet} ${auto} makecache fast
echo "Installing required OS packages:" ${packages}
${yum} ${quiet} ${auto} install ${packages} | tee
}
if [ "${crypto_version}" ]; then
# EPEL must be installed before the updated python-crypto version
${yum} ${quiet} ${auto} install "python-crypto${crypto_version}" | tee
# The EPEL python-crypto package isn't usable after installation.
# A symlink is needed to support "import Crypto".
# A symlink is needed to make the package visible to "pip list".
pc_path=$(rpm -q -l "python-crypto${crypto_version}" | grep '/Crypto$')
pc_rpath=$(echo "${pc_path}" | sed 's|^.*/site-packages/||')
pkg_path=$(echo "${pc_path}" | sed 's|/site-packages/.*$|/site-packages/|')
crypto_path="${pkg_path}Crypto"
pkginfo_path=$(echo "${pc_rpath}" | sed 's|/Crypto$||')"/EGG-INFO/PKG-INFO"
egginfo_path=$(echo "${pc_path}" | sed 's|/Crypto$||')"-info"
if [ ! -e "${crypto_path}" ] &&
[ ! -e "${egginfo_path}" ] &&
[ -d "${pkg_path}/${pc_rpath}" ] &&
[ -f "${pkg_path}/${pkginfo_path}" ]; then
echo "Creating symlinks for crypto module version ${crypto_version}."
ln -s "${pc_rpath}" "${crypto_path}"
ln -s "${pkginfo_path}" "${egginfo_path}"
fi
fi
}
yum_epel_setup() {
if [ "${VERSION_ID}" -le 6 ]; then
# EPEL required for python-crypto without a C compiler
install_epel=1
crypto_version="2.6"
elif [ ${mode} = "os" ]; then
# EPEL required for the necessary OS packages for Python
install_epel=1
fi
yum_setup "${install_epel}" "${crypto_version}"
}
pkg_setup() {
if [ "${install_curl}" ]; then curl_package="curl"; fi
packages="
gmake
git
${curl_package}
python
py27-pycrypto
"
if [ "${auto}" != "" ]; then
assume_yes="ASSUME_ALWAYS_YES=YES"
fi
# shellcheck disable=SC2086
{
echo "Installing required OS packages:" ${packages}
env ${assume_yes} pkg bootstrap
pkg install ${quiet} ${auto} ${packages}
}
}
apt_packages() {
if [ ${mode} != "os" ]; then return; fi
echo "Checking available OS packages ... "
packages=$(apt-cache ${quiet} show \
python-setuptools \
python-six \
python-yaml \
python-jinja2 \
python-nose \
python-mock \
python-coverage \
python-redis \
python-memcache \
python-passlib \
python-paramiko \
python-systemd \
| grep '^Package: ' \
| sed 's/^Package: //')
# shellcheck disable=SC2086
{
echo "Installing OS packages:" ${packages}
apt-get ${quiet} ${auto} install ${packages}
}
}
yum_packages() {
if [ ${mode} != "os" ]; then return; fi
echo "Checking available OS packages ... "
packages=$(${yum} ${quiet} ${auto} info \
python-setuptools \
python-six \
PyYAML \
python-jinja2 \
python-nose \
python-mock \
python2-mock \
python-coverage \
python-redis \
python-memcached \
python-passlib \
python-paramiko \
systemd-python \
| grep '^Name *: ' \
| sed 's/^Name *: //')
# shellcheck disable=SC2086
{
echo "Installing OS packages:" ${packages}
${yum} ${quiet} ${auto} install ${packages} | tee
}
}
brewdo_setup() {
if [ ${mode} != "brewdo" ]; then return; fi
if [ ! ${have_brewdo} ]; then
git clone https://github.com/zigg/brewdo
(
cd brewdo
./brewdo install
./brewdo "do" make install
)
rm -rf brewdo
fi
brewdo brew install python
have_pip=1
pip_wrapper="brewdo do"
mode="pip"
}
brew_setup() {
if [ ${mode} != "brew" ]; then return; fi
if [ ! ${have_brew} ]; then
url="https://raw.githubusercontent.com/Homebrew/install/master/install"
if [ "${auto}" != "" ]; then
ruby -e "$(curl -fsSL "${url}")" < /dev/null
else
ruby -e "$(curl -fsSL "${url}")"
fi
fi
brew install python
have_pip=1
mode="pip"
}
pip_setup() {
if [ ${mode} != "pip" ]; then return; fi
if [ ! ${have_pip} ]; then
if [ ${quiet} ]; then
silent="--silent"
show_error="--show-error"
fi
url="https://bootstrap.pypa.io/get-pip.py"
echo "Downloading and installing pip..."
curl ${silent} ${show_error} ${url} | python
fi
python_version=$(python --version 2>&1 | grep -o '[0-9]\.[0-9]')
if [ "${python_version}" = "2.6" ]; then unittest2_package="unittest2"; fi
packages="
six
PyYAML
Jinja2
nose
mock
coverage
redis
python-memcached
passlib
paramiko
${unittest2_package}
${pycrypto_package}
"
# shellcheck disable=SC2086
{
echo "Installing pip packages:" ${packages}
${pip_wrapper} pip ${quiet} install ${packages}
}
}
os_setup() {
case "${ID}" in
ubuntu)
apt_setup
apt_packages
;;
debian)
apt_setup
apt_packages
;;
fedora)
if [ "${VERSION_ID}" -ge 22 ]; then yum="dnf"; fi
yum_setup
yum_packages
;;
centos)
yum_epel_setup
yum_packages
;;
rhel)
yum_epel_setup
yum_packages
;;
osx)
osx_setup
brew_setup
brewdo_setup
# pycrypto needs to be installed via pip
pycrypto_package="pycrypto"
;;
freebsd)
pkg_setup
;;
esac
}
test_ansible() {
if [ ! ${test} ]; then return; fi
git clone https://github.com/ansible/ansible --recursive
cd ansible
. hacking/env-setup
make tests
}
success_message() {
cat <<- EOF
You're almost ready to start hacking on Ansible...
If you haven't already, clone the Ansible repository:
git clone https://github.com/ansible/ansible --recursive
To update your Ansible code, run the following from your ansible directory:
hacking/update.sh
In each shell you use, run the following from your ansible directory:
source hacking/env-setup
That's it!
EOF
}
main() {
process_args "$@"
detect_platform
review_platform
show_help
check_platform
os_setup
pip_setup
success_message
test_ansible
}
main "$@"
exit 0