-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
·238 lines (208 loc) · 6.2 KB
/
setup.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
#!/usr/bin/env sh
#
# Script prepares operating system with foundation to run Ansible
# Tools installed depend on operating system and environment
if [ "$TRAVIS" = "true" ]; then
set -ex
elif [ "$CORPORATE" = "true" ]; then
set -x
else
set -e
fi
## Config
#
GIT_REPO_URL="https://github.com/lony/dotFiles.git"
GIT_REPO_BRANCH="main"
GIT_CLONE_FOLDER="$HOME/dotFiles"
ANSIBLE_PLAYBOOK_PATH="ansible/site.yml"
ANSIBLE_PLAYBOOK_CMD="ansible-playbook --ask-become-pass --inventory localhost, ${ANSIBLE_PLAYBOOK_PATH}"
SYSTEM_OS="Unknown"
SYSTEM_OS_VERSION="Unknown"
PACKAGE_MANAGER="Unknown"
ROOT_RUN=""
## Func
#
#######################################
# Helper to print error message
# Globals:
# RED
# RESET
# Arguments:
# $@ Error message text
#######################################
error_print() {
echo ${RED}"Error: $@"${RESET} >&2
}
#######################################
# Print status information about run
# Globals:
# SYSTEM_OS
# SYSTEM_OS_VERSION
# PACKAGE_MANAGER
# ROOT_RUN
# TRAVIS
# Arguments:
# None
#######################################
status_print() {
printf "\n"
printf "### INFORMATION\n"
printf "SYSTEM_OS=$SYSTEM_OS\n"
printf "SYSTEM_OS_VERSION=$SYSTEM_OS_VERSION\n"
printf "PACKAGE_MANAGER=$PACKAGE_MANAGER\n"
printf "ROOT_RUN=$ROOT_RUN\n"
printf "\n"
if [ "$TRAVIS" = "true" ]; then
printf "\n"
printf "df -h\n"
df -h
fi
}
#######################################
# Detects if script is run as root
# Globals:
# ROOT_RUN
# Arguments:
# None
#######################################
root_detect() {
if ! [ $(id -u) -eq 0 ]; then
ROOT_RUN="sudo "
fi
}
#######################################
# Detects if command exists, first
# for sudo then for normal user
# Globals:
# ROOT_RUN
# Arguments:
# $@ Command to test for
#######################################
command_exists() {
$ROOT_RUN command -v "$@" >/dev/null 2>&1
}
#######################################
# Detects if command exists else
# run install command
# Globals:
# ROOT_RUN
# Arguments:
# $1 Command to test for
# $2 Command used to install if missing
#######################################
command_install() {
if ! command_exists $1; then
printf "\n### INSTALL $1 - run '$ROOT_RUN$2'...\n"
$ROOT_RUN$2
if ! command_exists $1; then
error_print "Command '$1' could not be installed!\n"
exit 1
else
printf "\n... successful\n$1 --version\n"
$1 --version
fi
fi
}
#######################################
# Clone git repository if not existing
# Globals:
# GIT_CLONE_FOLDER
# GIT_REPO_URL
# Arguments:
# None
#######################################
git_clone() {
if [ ! -d "$GIT_CLONE_FOLDER" ] ; then
printf "\n### RUN - git clone\n"
git clone --depth=1 --branch "$GIT_REPO_BRANCH" "$GIT_REPO_URL" "$GIT_CLONE_FOLDER"
fi
cd "$GIT_CLONE_FOLDER"
}
#######################################
# Execute ansible with provided recipes
# Globals:
# TRAVIS
# ANSIBLE_PLAYBOOK_CMD
# Arguments:
# None
#######################################
ansible_install_run() {
if [ "$TRAVIS" = "true" ]; then
printf "\n### RUN - ${ANSIBLE_PLAYBOOK_CMD} --syntax-check\n"
${ANSIBLE_PLAYBOOK_CMD} --syntax-check
fi
printf "\n### RUN - ${ANSIBLE_PLAYBOOK_CMD}\n"
if [ "$TRAVIS" = "true" ]; then
${ANSIBLE_PLAYBOOK_CMD} --skip-tags "travis-do-not"
elif [ "$CORPORATE" = "true" ]; then
${ANSIBLE_PLAYBOOK_CMD} --skip-tags "corporate-do-not"
else
${ANSIBLE_PLAYBOOK_CMD}
fi
}
## Main
#
printf "Install prerequisite for Ansible (python, git, etc.).\n"
printf "Please consider after this is finished there will be a prompt!\n"
root_detect
unameOut="$(uname -s)"
case "${unameOut}" in
Darwin*)
SYSTEM_OS="OSX"
SYSTEM_OS_VERSION=$(defaults read loginwindow SystemVersionStampAsString)
PACKAGE_MANAGER="brew install"
status_print
# https://stackoverflow.com/questions/15371925/how-to-check-if-xcode-command-line-tools-are-installed
# https://apple.stackexchange.com/questions/107307/how-can-i-install-the-command-line-tools-completely-from-the-command-line
set +e
xcode-select -p >/dev/null 2>&1
CMD_TOOLS_RETURN_CODE="$?"
set -e
if [ $CMD_TOOLS_RETURN_CODE -ne 0 ]; then
printf "Install Apple Command Line Tools for Xcode, "
printf "which contains 'git' and other tools needed for homebrew.\n"
printf "Please confirm the following dialog with 'Install'.\n"
xcode-select --install
# src https://gist.github.com/phuctm97/946b5ced8cbfabc2f34e489c447456b1
until $(xcode-select --print-path &> /dev/null); do
printf "."
sleep 1
done
printf "Apple Command Line Tools for Xcode has finished installing.\n"
fi
# TODO Install homebrew currently needs admin rights. So make it work without admin privileges
# https://www.reddit.com/r/macsysadmin/comments/yf5jsi/homebrew_install_through_an_mdm_script/
# https://github.com/Honestpuck/homebrew.sh/tree/master
if ! command_exists brew; then
# Tested against: Homebrew 4.1.1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/goetz/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
# Tested against: ansible [core 2.15.2]
# Tested against: Python 3.11.4
ROOT_RUN="" command_install ansible "$PACKAGE_MANAGER ansible"
git_clone
ansible_install_run
;;
Linux*)
SYSTEM_OS="Linux"
SYSTEM_OS_VERSION="\n\n$(cat /etc/os-release)\n"
if [ -x $(command_exists yum) ]; then
PACKAGE_MANAGER="yum install -y"
elif [ -x $(command_exists apt-get) ]; then
PACKAGE_MANAGER="apt-get -y install"
else
error_print "Unknown package manager. Please add support in script!\n"
fi
status_print
command_install git "$PACKAGE_MANAGER git"
command_install pip "$PACKAGE_MANAGER python-pip"
command_install ansible "pip install ansible --upgrade"
git_clone
ansible_install_run
;;
*)
error_print "Unknown operating system: ${unameOut}. Please add support in script!\n"
;;
esac