-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup
executable file
·1623 lines (1410 loc) · 41.5 KB
/
setup
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# Setup script for https://github.com/markosamuli/linux-machine and
# https://github.com/markosamuli/macos-machine Ansible playbooks.
#
# 2020-09-06:
# - Upgrade outdated Ansible Homebrew formula
# - Detect installed Ansible Homebrew path correctly during install
#
# 2020-09-04:
# - Re-install Xcode Command Line Tools (CLT) after upgrade
# - Wait for Xcode CLT installation to complete in the background
#
# 2020-08-31:
# - Detect WSL1 / WSL2 version and pass it to the Ansible playbook
# - Use 'auto' for detecting Python in Ubuntu 20.04 LTS
# - Added debug() function that logs only if DEBUG environment variable is
# defined
#
# 2020-08-30:
# - Use Ansible 2.9 as the default version
# - Update APT cache before installing packages
#
# 2020-08-29:
# - Fix issue with the script detecting pyenv when .python-version file exists
# even if pyenv was not installed on the system
# - Do not install Ansible from PPA on Ubuntu 20.04 LTS
# - Detect Linux distribution release name
#
# 2020-07-01:
# - Use the new Bash Homebrew installer
#
# 2020-02-16:
# - Require minimum Ansible version 2.8 (snap module for Ubuntu)
# - Added '--no-install-ansible' option
# - Added '--update-roles' option
#
# 2019-11-24:
# - Fix Ansible version checking on macOS
#
# 2019-11-22:
# - Rework on the setup script for improved Ansible installation when using
# pyenv or virtualenv or calling Ansible with any non-system paths
# - Use bash executable instead of sh with Ansible on WSL environments
#
# 2019-11-21:
# - Require minimum Ansible version 2.7
# - Install Ansible 2.8 as the default version
# - Support for installing Ansible in a local virtualenv from PyPI
# - Allow setting the default Ansible version with MACHINE_ANSIBLE_VERSION
# environment variable
# - Check that we're not using Ansible v2.8.6
# - Support for uninstalling existing Ansible installations
# - Added new long command line options in setup script
#
# 2019-11-20:
# - Check for outdated Homebrew packages on macOS
#
# 2019-11-09:
# - Check the current user is not root
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# Commands
run_playbook=1 # Run playbook as default command
syntax_check=0 # Run command to check syntax
list_tags=0 # Run command to list tags
# Setup options
check_os_upgrades=1 # Check for OS upgrades
install_roles=1 # Install Ansible roles
install_ansible=1 # Install Ansible
install_roles_force=0 # Force overwriting existing Ansible roles
update_roles=0 # Update Ansible roles to the latest versions
print_versions=0 # Print Python and Ansible versions
upgrade_ansible=0 # Upgrade Ansible to the latest version
# System properties
is_macos="" # macOS
is_linux="" # Linux
is_ubuntu="" # Ubuntu
is_pengwin="" # Pengwin
is_wsl="" # Windows Subsystem for Linux
distrib_release="" # OS release codename
wsl_version="" # WSL1 / WSL2
# macOS specific setup options
install_homebrew=1 # Install Homebrew
install_xcode=1 # Install Xcode
# Environment configuration
deploy_env="local"
# Local variables
local_vars_file="machine.yaml"
# Require sudo
require_sudo=0
# Ansible playbook options
ansible_playbook="playbooks/main.yml"
ansible_tags=""
ansible_skip_tags=""
ansible_verbose=0
ansible_become=0
# Ansible pyenv version / environment name
ansible_pyenv=""
# Ansible virtualenv path
ansible_virtualenv="${DIR}/.venv"
# Ansible version test
required_ansible_version="2.10"
# Ansible version to install
ansible_version="${MACHINE_ANSIBLE_VERSION:-2.10}"
# Ansible PPA to use
ansible_ppa="ansible/ansible-${ansible_version}"
ansible_ppa_key_id="93C4A3FD7BB9C367"
# Ansible installation options
ansible_pypi_enabled="${MACHINE_ANSIBLE_PYPI}"
ansible_apt_enabled="${MACHINE_ANSIBLE_APT}"
ansible_homebrew_enabled="${MACHINE_ANSIBLE_HOMEBREW}"
ansible_virtualenv_enabled="${MACHINE_ANSIBLE_VIRTUALENV}"
ansible_pyenv_enabled="${MACHINE_ANSIBLE_PYENV}"
error() {
echo "$@" 1>&2
}
debug() {
if [ -z "${DEBUG}" ]; then
return 0
fi
echo "DEBUG: $*" 1>&2
}
# Print usage help
show_help() {
echo "Usage: $0"
cat <<END_OF_OPTIONS
-h Show this help
-f Force overwriting existing Ansible roles
-v Verbose mode
-n Do not run Ansible playbook
-q Do not install Ansible requirements
-l List available Ansible tags
-s Ansible playbook syntax check
-u Update Ansible roles to the latest versions
-t TAGS Run Ansible with the given comma separated list of tags
-i TAGS Skip the given list of tags
--reinstall-ansible
Remove and re-install Ansible
--enable-ansible-pypi
Enable Ansible installation from PyPI into a virtualenv
--disable-ansible-pypi
Disable Ansible installation from PyPI into a virtualenv
--install-ansible
Enable Ansible installation
--no-run-playbook
Disable Ansible playbooks from being run
--no-install-roles
Disable Ansible roles from being installed or updated
--upgrade-ansible
Upgrade outdated Ansible version (macOS with Homebrew only)
--verbose
Enable verbose Ansible output
--print-versions
Print Ansible and Python versions
END_OF_OPTIONS
}
# Get Ansible version
get_ansible_version() {
local version
version=$(ansible_exec ansible --version | grep "^ansible" 2>/dev/null)
if [ -z "$version" ]; then
return
fi
if [[ $version =~ core ]]; then
echo "${version}" | grep -E -o "([0-9.]*)"
else
echo "${version}" | awk '{ print $2 }'
fi
}
# Check installed Ansible version meets the requirements
check_installed_ansible_version() {
local required="${required_ansible_version}"
local installed
installed="$(get_ansible_version)"
# Fail if we don't get a version number
if [ -z "${installed}" ]; then
error "Ansible not installed"
exit 1
fi
# Print installed Ansible version
if is_true "${print_versions}"; then
echo "*** Ansible version ${installed} installed"
fi
# Check that the installed major and minor version match the require version
if ! compare_ansible_version "${required}" "${installed}"; then
error "Ansible ${required} required"
exit 1
fi
# Check that we're not using Ansible v2.8.6
if [ "${installed}" == "2.8.6" ]; then
error "Ansible v2.8.6 is not supported, try upgrading to Ansible v2.8.7"
return 1
fi
}
# Check Ansible major and minor version matches the required version
compare_ansible_version() {
local IFS=.
# shellcheck disable=SC2206
local required=($1)
# shellcheck disable=SC2206
local installed=($2)
# shellcheck disable=SC2004
if ((${installed[0]} < ${required[0]})); then
return 1
fi
# shellcheck disable=SC2004
if ((${installed[1]} < ${required[1]})); then
return 1
fi
return 0
}
# Install dependencies on Linux
install_linux_dependencies() {
debug "install Linux dependencies"
if is_true "${install_ansible}"; then
install_ansible
fi
}
# Install dependencies on macOS
install_macos_dependencies() {
if is_true "${install_xcode}"; then
install_xcode
fi
if is_true "${install_homebrew}"; then
install_homebrew
fi
if is_true "${install_ansible}"; then
install_ansible
fi
if is_true "${upgrade_ansible}"; then
upgrade_ansible
fi
}
# Install Xcode Command Line Tools
install_xcode() {
echo "*** Installing Xcode Command Line Tools..."
xcode-select --install ||
{
error "Failed to install Xcode Command Line Tools"
exit 1
}
echo "Follow instructions on the dialog to complete the installation."
echo "Press [CTRL+C] to stop."
printf "Waiting for install to complete"
while is_true "${install_xcode}"; do
printf "."
sleep 5
configure_xcode
done
}
# Install Homebrew
install_homebrew() {
if command -v brew >/dev/null; then
return 0
fi
local url="https://raw.githubusercontent.com"
url="${url}/Homebrew/install/master/install.sh"
echo "*** Installing Homebrew..."
/bin/bash -c "$(curl -fsSL ${url})" ||
{
error "Failed to install Homebrew"
exit 1
}
}
# Install Ansible
install_ansible() {
if is_true "${is_macos}"; then
install_ansible_on_macos
elif is_true "${is_linux}"; then
install_ansible_on_linux
else
error "Your system is not supported"
exit 1
fi
}
# Upgrade existing Ansible installation to the latest version
upgrade_ansible() {
if is_true "${is_macos}"; then
upgrade_ansible_on_macos
elif is_true "${is_linux}"; then
error "upgrading Ansible on Linux is not supported"
else
error "Your system is not supported"
exit 1
fi
}
# Is the installed Ansible version outdated
is_ansible_outdated() {
if ! is_ansible_installed; then
debug "ansible not installed, not marking it as outdated"
return 1
fi
# Check Homebrew installation
if is_ansible_homebrew_enabled; then
local outdated
outdated=$(brew outdated -q ansible)
if [ -n "${outdated}" ]; then
debug "outdated Ansible Homebrew formula found"
return 0
fi
fi
# Not supporting Linux or PyPI version checking
return 1
}
# Is Ansible installed on the PATH or in the custom location
is_ansible_installed() {
# Check Homebrew installation
if is_ansible_homebrew_enabled; then
# Fail if Ansible formula is not found
if [ "$(brew --prefix ansible)" == "" ]; then
debug "brew --prefix ansible is empty"
return 1
fi
debug "ansible Homebrew formula found"
fi
# Fail if 'ansible --version' doesn't work
local version
version=$(ansible_exec ansible --version 2>/dev/null)
if [ -z "${version}" ]; then
debug "ansible --version is empty"
return 1
fi
}
# Install Ansible on Linux
install_ansible_on_linux() {
if is_ansible_installed; then
return 0
fi
echo "*** Install Ansible on Linux..."
# Enable APT installation as the default option if neither APT or
# PyPI installation option have been set or if PyPI has been disabled.
if [ -z "${ansible_apt_enabled}" ]; then
if [ -z "${ansible_pypi_enabled}" ]; then
ansible_apt_enabled=true
elif is_ansible_pypi_disabled; then
ansible_apt_enabled=true
fi
fi
# Install Ansible with APT
if is_ansible_apt_enabled; then
if is_true "${is_ubuntu}"; then
if install_ansible_with_apt_on_ubuntu; then
return 0
fi
elif is_true "${is_pengwin}"; then
if install_ansible_with_apt_on_debian; then
return 0
fi
else
error "Your system is not supported"
exit 1
fi
fi
# Fallback to PyPI installation unless explicitly disabled
if [ -z "${ansible_pypi_enabled}" ]; then
echo "*** Enabled PyPI installation"
ansible_pypi_enabled=true
fi
# Install Ansible from PyPI package
if is_ansible_pypi_enabled; then
if install_ansible_with_pip; then
return 0
fi
fi
error "Failed to install Ansible"
exit 1
}
# Upgrade exisging Ansible installation on macOS
upgrade_ansible_on_macos() {
debug "upgrade Ansible on macOS"
configure_ansible_on_macos
if ! is_ansible_outdated; then
debug "outdated Ansible version not detected"
return 0
fi
if is_ansible_pypi_enabled; then
error "upgrading Ansible PyPI packages is not supported"
return 1
fi
if ! is_ansible_homebrew_enabled; then
error "upgrading Ansible on macOS requires Homebrew"
return 1
fi
if ! upgrade_ansible_with_homebrew; then
error "failed to upgrade Ansible"
return 1
fi
}
# Upgrade Ansible installed with Homebrew
upgrade_ansible_with_homebrew() {
echo "*** Upgrading Ansible with Homebrew..."
brew upgrade ansible ||
{
error "Failed to upgrade Ansible with Homebrew"
exit 1
}
}
# Configure Ansible on macOS
configure_ansible_on_macos() {
debug "configure Ansible on macOS"
# Enable Homebrew installation as the default option if neither Homebrew or
# PyPI installation option have been set or if PyPI has been disabled.
if [ -z "${ansible_homebrew_enabled}" ]; then
if [ -z "${ansible_pypi_enabled}" ]; then
debug "PyPI installation is not enabled"
ansible_homebrew_enabled=true
elif is_ansible_pypi_disabled; then
debug "PyPI installation is disabled"
ansible_homebrew_enabled=true
fi
fi
}
# Install Ansible on macOS
install_ansible_on_macos() {
debug "install Ansible on macOS"
configure_ansible_on_macos
if is_ansible_installed; then
debug "existing Ansible version found, not installing..."
return 0
fi
echo "Install Ansible on macOS..."
# Install Ansible from Homebrew
if is_ansible_homebrew_enabled; then
debug "install Ansible from Homebrew"
if is_true "${is_macos}"; then
if install_ansible_with_homebrew; then
return 0
fi
else
error "Your system is not supported"
exit 1
fi
fi
# Fallback to PyPI installation unless explicitly disabled
if [ -z "${ansible_pypi_enabled}" ]; then
echo "*** Enabled PyPI installation"
ansible_pypi_enabled=true
fi
# Install Ansible from PyPI package
if is_ansible_pypi_enabled; then
if install_ansible_with_pip; then
return 0
fi
fi
error "Failed to install Ansible"
exit 1
}
# Install Ansible with Homebrew
install_ansible_with_homebrew() {
echo "*** Installing Ansible with Homebrew..."
brew install ansible ||
{
error "Failed to install Ansible with Homebrew"
exit 1
}
}
# Uninstall Ansible from Homebrew
uninstall_ansible_with_homebrew() {
echo "*** Uninstalling Ansible with Homebrew..."
brew uninstall ansible ||
{
error "Failed to uninstall Ansible with Homebrew"
exit 1
}
}
# Get APT package install candidate
apt_install_candidate() {
local package=$1
local candidate
candidate=$(apt-cache policy "${package}" | grep 'Candidate:' | sed 's/[[:space:]]*Candidate:[[:space:]]*//')
if [ -n "${candidate}" ]; then
echo "${candidate}" | cut -d- -f1
fi
}
# Instal Ansible with PIP
install_ansible_with_pip() {
local package_version
if [ "${ansible_version}" == "2.7" ]; then
package_version="<2.8"
elif [ "${ansible_version}" == "2.8" ]; then
package_version="!=2.8.6,<2.9"
elif [ "${ansible_version}" == "2.9" ]; then
package_version="<2.10"
else
error "Unsupported Ansible version ${ansible_version}"
return 1
fi
if is_true "${ansible_pyenv_enabled}"; then
if [ -n "${ansible_pyenv}" ]; then
install_ansible_in_pyenv \
"${package_version}" \
"${ansible_pyenv}" || return 1
return 0
fi
fi
if is_true "${ansible_virtualenv_enabled}"; then
if [ -n "${ansible_virtualenv}" ]; then
install_ansible_in_virtualenv \
"${package_version}" \
"${ansible_virtualenv}" || return 1
return 0
fi
fi
error "Couldn't find local pyenv or virtualenv environments"
return 1
}
# Install Ansible in the given pyenv version
install_ansible_in_pyenv() {
local package_version="$1"
local pyenv_version="$2"
if ! command -v pyenv >/dev/null; then
error "pyenv not found"
return 1
fi
echo "*** Install Ansible PyPI (ansible${package_version}) in ${pyenv_version}"
pyenv_exec "${pyenv_version}" \
pip install "ansible${package_version}" || {
error "Failed to install Ansible in ${pyenv_version}"
return 1
}
PYENV_VERSION="${pyenv_version}" pyenv rehash
echo "*** Ansible ${ansible_version} installed in ${pyenv_version}"
ansible_pypi_enabled=true
}
# Check if virtualenv command is installed
is_virtualenv_installed() {
if ! command -v virtualenv >/dev/null; then
return 1
fi
if [ -z "$(virtualenv --version 2>/dev/null)" ]; then
return 1
fi
}
# Create virtualenv in given directory if it doesn't already exist
create_virtualenv() {
local virtualenv_path="$2"
if [ ! -d "${virtualenv_path}" ]; then
if ! is_virtualenv_installed; then
error "Python virtuelenv command not found"
return 1
fi
virtualenv "${virtualenv_path}" || {
error "Failed to create virtualenv in ${virtualenv_path}"
return 1
}
fi
}
# Install Ansible in a virtualenv
install_ansible_in_virtualenv() {
local package_version="$1"
local virtualenv_path="$2"
create_virtualenv "${virtualenv_path}" || return 1
echo "*** Install Ansible PyPI (ansible${package_version}) in ${virtualenv_path}"
virtualenv_exec "${virtualenv_path}" \
pip install "ansible${package_version}" || {
error "Failed to install Ansible in ${virtualenv_path}"
return 1
}
echo "*** Ansible ${ansible_version} installed into ${virtualenv_path}"
ansible_pypi_enabled=true
}
# Install missing APT package
install_apt_package() {
local package="$1"
if ! is_apt_package_installed "${package}"; then
echo "*** Install ${package}..."
sudo apt-get install -y "${package}" || {
error "Failed to install ${package}"
return 1
}
fi
}
# Remove APT package if installed
remove_apt_package() {
local package="$1"
if is_apt_package_installed "${package}"; then
echo "*** Remove ${package} APT package..."
sudo apt-get remove -y "${package}" || {
error "Failed to remove ${package}"
return 1
}
fi
}
# Remove Ansible installations
remove_ansible_installations() {
# Skip if not enabled
if ! is_true "${remove_ansible}"; then
return
fi
# Remove any local virtuelenv directories
remove_ansible_virtualenv
# Find local pyenv version
if command -v pyenv >/dev/null; then
local pyenv_version
if [ -n "${ansible_pyenv}" ]; then
pyenv_version="${ansible_pyenv}"
elif [ -e ".python-version" ]; then
# Check .python-version file even if PyPi install is disabled
pyenv_version=$(cat .python-version)
fi
fi
# Uninstall Ansible package from the pyenv environment
if [ -n "${pyenv_version}" ]; then
local pyenv_installed
pyenv_installed=$(pyenv_exec "${pyenv_version}" pip freeze | grep "^ansible==")
if [ -n "${pyenv_installed}" ]; then
echo "*** Uninstall ansible PyPI from pyenv version ${pyenv_version}"
pyenv_exec "${pyenv_version}" pip uninstall -y ansible
fi
fi
# Remove Homebrew installations on macOS
if is_true "${is_macos}"; then
uninstall_ansible_with_homebrew
fi
# Remove APT installations on Ubuntu and Debian-based systems
if is_true "${is_ubuntu}" || is_true "${is_pengwin}"; then
remove_apt_package "ansible"
remove_ansible_apt_sources_list
fi
}
# Install Ansible PPA on Ubuntu
install_ansible_ppa_on_ubuntu() {
# Do not add PPA if it already exists
if compgen -G "/etc/apt/sources.list.d/ansible*.list" >/dev/null; then
local deb_url="http://ppa.launchpad.net/${ansible_ppa}/ubuntu"
if grep -q "^deb ${deb_url}" /etc/apt/sources.list.d/ansible*.list; then
return 0
fi
fi
install_apt_package "software-properties-common"
echo "*** Add ${ansible_ppa} PPA repository..."
sudo apt-add-repository -y "ppa:${ansible_ppa}"
add_ansible_ppa_key
}
# Remove all Ansible sources list files
remove_ansible_apt_sources_list() {
local update_apt_cache
for apt_list in /etc/apt/sources.list.d/ansible*.list; do
if [ -e "${apt_list}" ]; then
echo "Remove ${apt_list}"
sudo rm -f "${apt_list}" || {
error "Failed to remove ${apt_list}"
exit 1
}
update_apt_cache=1
fi
done
if [ "${update_apt_cache}" == "1" ]; then
echo "*** Update APT cache..."
sudo apt-get update || {
error "failed to update APT cache"
exit 1
}
fi
}
# Remove local virtualenv when using OS installation method
remove_ansible_virtualenv() {
# Deactivate current virtualenv before deleting it
if [ -n "${VIRTUAL_ENV}" ]; then
if [ "$(type -f deactivate)" == "function" ]; then
deactivate
fi
fi
## Remove virtualenv directory
if [ -d "${ansible_virtualenv}" ]; then
echo "*** Removing virtualenv in '${ansible_virtualenv}'"
rm -rf "${ansible_virtualenv}"
fi
}
# Install Ansible with APT on Ubuntu
install_ansible_with_apt_on_ubuntu() {
if [ "${distrib_release}" == "focal" ]; then
echo "*** Do not use PPA on Ubuntu 20.04 LTS"
else
install_ansible_ppa_on_ubuntu
fi
if ! install_ansible_apt "${ansible_version}"; then
error "Couldn't install Ansible ${ansible_version} from APT"
return 1
fi
echo "*** Ansible ${ansible_version} installed"
remove_ansible_virtualenv
}
# Install Ansible PPA on Debian
install_ansible_ppa_on_debian() {
local deb_url="http://ppa.launchpad.net/${ansible_ppa}/ubuntu"
local apt_list="/etc/apt/sources.list.d/ansible.list"
if [ -e "${apt_list}" ]; then
# Do nothing if the correct PPA URL exists in the APT sources list file
if grep -q "^deb ${deb_url}" "${apt_list}"; then
return 0
fi
echo "*** Updating ${ansible_ppa} PPA repository..."
sudo sh -c "echo 'deb ${deb_url} trusty main' > ${apt_list}"
else
echo "*** Add ${ansible_ppa} PPA repository..."
sudo sh -c "echo 'deb ${deb_url} trusty main' > ${apt_list}"
fi
add_ansible_ppa_key
}
# Install Ansible with APT on Debian
install_ansible_with_apt_on_debian() {
install_ansible_ppa_on_debian
if ! install_ansible_apt "${ansible_version}"; then
error "Couldn't install Ansible ${ansible_version} from APT"
return 1
fi
echo "*** Ansible ${ansible_version} installed"
remove_ansible_virtualenv
}
# Install Ansible APT package
install_ansible_apt() {
local version=$1
echo "*** Update APT cache..."
sudo apt-get update || {
error "failed to update APT cache"
exit 1
}
local candidate
candidate=$(apt_install_candidate "ansible")
if [ "${candidate}" == "2.8.6" ]; then
error "WARNING: Not installing Ansible version ${candidate} from APT"
return 1
fi
echo "*** Installing Ansible ${version} from APT..."
sudo apt-get install -y "ansible=${version}*" || {
error "Failed to install ansible=${version}*"
return 1
}
}
# Check if an APT package has been installed
is_apt_package_installed() {
local package="$1"
status=$(dpkg-query --show --showformat='${db:Status-Status}\n' "${package}")
if [ "${status}" == "installed" ]; then
return 0
else
return 1
fi
}
# Add Ansible PPA signing key
add_ansible_ppa_key() {
echo "*** Add Ansible PPA signing key..."
gpg --keyserver keyserver.ubuntu.com --recv "${ansible_ppa_key_id}"
gpg --export --armor "${ansible_ppa_key_id}" | sudo apt-key add -
}
# Remove Ansible PPA signing key
remove_ansible_ppa_key() {
echo "*** Remove Ansible PPA signing key..."
sudo apt-key del "${ansible_ppa_key_id}"
}
# Install Ansible roles
install_ansible_roles() {
# Skip if install_roles is not enabled
if ! is_true "${install_roles}"; then
return
fi
local ansible_roles_opts=""
if is_true "${install_roles_force}"; then
ansible_roles_opts="${ansible_roles_opts} --force"
fi
echo "*** Installing Ansible roles..."
# shellcheck disable=SC2086
ansible_exec ansible-galaxy install \
-r requirements.yml \
${ansible_roles_opts} ||
{
error "Failed to install Ansible roles"
exit 1
}
}
# Update Ansible roles
update_ansible_roles() {
# Skip if update_roles is not enabled
if ! is_true "${update_roles}"; then
return
fi
echo "*** Installing Python requirements..."
pip install -r requirements.txt || {
error "Failed to install required packages"
exit 1
}
echo "*** Updating Ansible roles..."
python -m machine.scripts.update_roles || {
error "Failed to update Ansible roles"
exit 1
}
}
# Detect if we're running macOS or Linux
detect_os() {
if test "$(uname)" = "Darwin"; then
is_macos=1
is_linux=0
elif test "$(uname)" = "Linux"; then
is_linux=1
is_macos=0
detect_ubuntu
detect_pengwin
detect_wsl
else
error "Your system is not supported"
exit 1
fi
}
# Detect Windows Subsystem for Linux
detect_wsl() {
if [[ -d "/run/WSL" ]]; then
echo "*** Windows Subsystem for Linux (WSL2) detected"
is_wsl=1
wsl_version='WSL2'
return
fi
if grep -q Microsoft /proc/version; then
echo "*** Windows Subsystem for Linux (WSL1) detected"
is_wsl=1
wsl_version='WSL1'
return
fi
is_wsl=0
}
# Detect Ubuntu
detect_ubuntu() {
if ! test -f /etc/lsb-release; then
is_ubuntu=0
return
fi
if ! grep -q Ubuntu /etc/lsb-release; then
is_ubuntu=0
return
fi
echo "*** Ubuntu detected"
is_ubuntu=1
local codename
codename=$(grep "DISTRIB_CODENAME=" /etc/lsb-release | cut -d= -f2)
if [ -n "${codename}" ]; then
# shellcheck disable=SC2034
distrib_release="${codename}"
fi
}
# Detect Pengwin
detect_pengwin() {
if test -f /etc/os-release; then
if grep -q Pengwin /etc/os-release; then
echo "*** Pengwin detected"
is_pengwin=1
else
is_pengwin=0
fi
else
is_pengwin=0
fi
}
# Execute command with pyenv version
pyenv_exec() {
local args=("$@")
# The first argument is the pyenv version
local pyenv_version="${args[0]}"
if ! command -v pyenv >/dev/null; then
error "pyenv not installed"
exit 1
fi