generated from snek-at/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·292 lines (240 loc) · 9.88 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
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
#!/usr/bin/env bash
# shellcheck disable=SC1090
# It is highly recommended to use set -eEuo pipefail for every setup script
set -o errexit # Used to exit upon error, avoiding cascading errors
set -o errtrace # Activate traps to catch errors on exit
set -o pipefail # Unveils hidden failures
set -o nounset # Exposes unset variables
#### SPECIAL FUNCTIONS #####
# Functions with the purpose of making coding more convenient and
# debugging a bit easier.
# Disclaimer: "SPECIAL FUNCTIONS" are not test functions!
#
# NOTE: SPECIAL FUNCTIONS start with three CAPS letter.
#
# IF YOU ARE AWARE OF A BETTER FORM OF NAMING FEEL FREE TO OPEN A ISSUE
# OTHERWISE PLEASE USE THIS AS A GUIDELINE FOR ANY COMMIT.
EOS_string(){
# allows to store EOFs in strings
IFS=$'\n' read -r -d '' $1 || true;
return $?
} 2>/dev/null
source /etc/os-release # source os release environment variables
# SYSTEM / USER VARIABLES
readonly __DISTRO="${ID}" # get distro id from /etc/os-release
readonly __DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # workdir
readonly __FILE="${__DIR}/$(basename "${BASH_SOURCE[0]}")" # self
readonly __BASE="$(basename ${__FILE})" # workdir/self
readonly __ROOT="$(cd "$(dirname "${__DIR}")" && pwd)" # homedir
# DEPENDENCY / LOGS VARIABLES
# GINAvbs has currently one dependency that needs to be installed
readonly __GINA_DEPS=(python3 python3-pip)
# Location of installation logs
readonly __GINA_LOGS="${__DIR}/install.log"
# SETUP VARIABLES
# Define and set default for enviroment variables
SSHKEY=${GINA_SSHKEY:-""}
HOSTS=${SETUP_HOSTS:-""}
USER=${GINA_USER:-""}
PASSWORD=${GINA_PASSWORD:-""}
# COLOR / FORMAT VARIABLES
# Set some colors because without them ain't fun
COL_NC='\e[0m' # default color
COL_LIGHT_GREEN='\e[1;32m' # green
COL_LIGHT_RED='\e[1;31m' # red
COL_LIGHT_MAGENTA='\e[1;95m' # magenta
TICK="[${COL_LIGHT_GREEN}✓${COL_NC}]" # green thick
CROSS="[${COL_LIGHT_RED}✗${COL_NC}]" # red cross
INFO="[i]" # info sign
# shellcheck disable=SC2034
DONE="${COL_LIGHT_GREEN} done!${COL_NC}" # a small motivation ^^
OVER="\\r\\033[K" # back to line start
# LOGO / LICENSE / MANPAGE VARIABLES
# Our temporary logo, might be updated in the future
EOS_string LOGO <<-'EOS'
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ ___________ _____ __ +
+ / ____/ _/ | / / |_ __/ /_ _____ +
+ / / __ / // |/ / /| | | / / __ \/ ___/ +
+ / /_/ // // /| / ___ | |/ / /_/ (__ ) +
+ \____/___/_/ |_/_/ |_|___/_.___/____/ +
+ +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOS
# Licensing, recommedations and warnings
EOS_string LICENSE <<-'EOS'
+ # MIT License
+ #
+ # Copyright (c) 2022 Florian Herbert Kleber IT
+ #
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
+ # of this software and associated documentation files (the "Software"), to deal
+ # in the Software without restriction, including without limitation the rights
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ # copies of the Software, and to permit persons to whom the Software is
+ # furnished to do so, subject to the following conditions:
+ #
+ # The above copyright notice and this permission notice shall be included in all
+ # copies or substantial portions of the Software.
+ #
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ # SOFTWARE.
+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOS
# This line serves decorative purposes only
EOS_string COOL_LINE <<-'EOS'
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOS
######## FUNCTIONS #########
#
# FUNCTIONS are written in lowercase
#
# IF YOU ARE AWARE OF A BETTER NAMING SCHEME FEEL FREE TO OPEN AN ISSUE
# OTHERWISE PLEASE USE THIS AS A GUIDELINE FOR ANY COMMIT
install() {
# Install GINAvbs and dependency packages passed in via an argument array
declare -a _argArray1=(${!1})
declare -a _installArray=("")
# Debian based package install - debconf will download the entire package
# list so we just create an array of any packages missing to
# cut down on the amount of download traffic.
for i in "${_argArray1[@]}"; do
echo -ne "+ ${INFO} Checking for ${i}..."
if [[ $(which "${i}" 2>/dev/null) ]]; then
echo -e "+ [${TICK}] Checking for ${i} (is installed)"
else
echo -e "+ ${INFO} Checking for ${i} (will be installed)"
_installArray+=("${i}")
fi 2>/dev/null
done
case ${__DISTRO} in
'alpine')
if [[ ${_installArray[@]} ]]; then
# Installing Packages
apk add --force ${_installArray[@]}
# Cleaning cached files
rm -rf /var/cache/apk/* /var/cache/distfiles/*
echo -e "+ [${TICK}] All dependencies are now installed"
fi
;;
'arch'|'manjaro')
if [[ ${_installArray[@]} ]]; then
# Installing Packages if script was started as root
if [[ $(pacman -S --noconfirm ${_installArray[@]}) ]]; then
# Cleaning cached files
pacman -Scc --noconfirm
# Installing if sudo is installed
elif [[ $(sudo pacman -S --noconfirm ${_installArray[@]}) ]]; then
# Cleaning cached files
sudo pacman -Scc --noconfirm
# Try again as root
else
echo "+ ${INFO} retry as root again"
return 43
fi
echo -e "+ [${TICK}] All dependencies are now installed"
fi
;;
'debian'|'ubuntu'|'mint'|'kali')
if [[ ${_installArray[@]} ]]; then
# Installing Packages if the script was started as root
if [[ $(apt-get install ${_installArray[@]} -y) ]]; then
# Cleaning cached files
apt-get clean -y
# Installing if sudo is installed
elif [[ $(sudo apt-get install ${_installArray[@]} -y) ]]; then
# Cleaning cached files
sudo apt-get clean -y
# Try again as root
else
echo "+ ${INFO} retry as root again"
return 43
fi
echo -e "+ [${TICK}] All dependencies are now installed"
fi
;;
'fedora')
if [[ ${_installArray[@]} ]]; then
# Installing Packages if the script was started as root
if [[ $(dnf install ${_installArray[@]} -y) ]]; then
# Cleaning cached files
dnf clean all
# Installing if sudo is installed
elif [[ $(sudo dnf install ${_installArray[@]} -y) ]]; then
# Cleaning cached files
dnf clean all
# Try again as root
else
echo "+ ${INFO} retry as root again"
return 43
fi
echo -e "+ [${TICK}] All dependencies are now installed"
fi
;;
*) return 1;;
esac
# curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
# python3 get-pip.py
python3 -m pip install ansible
return $?
} 2>/dev/null
exit_handler(){
# Copy the temp log file into final log location for storage
#copy_to_install_log # TODO logging still doesn't working like expected
local error_code=$?
if [[ ${error_code} == 0 ]]; then
echo -e "+"
echo -e "${COL_LIGHT_MAGENTA}${COOL_LINE}"
echo -e "+"
echo "+ Thanks for using GINAvbs"
echo -e "+"
echo -e "${COOL_LINE}"
echo -e "${COL_NC}"
return ${error_code};
fi
echo -e "+"
echo -e "${COL_LIGHT_RED}${COOL_LINE}"
echo -e "+"
error_handler ${error_code}
echo -e "+"
echo -e "${COOL_LINE}"
echo -e "${COL_NC}"
exit ${error_code}
} 2>/dev/null
######## ENTRYPOINT #########
main(){
echo -e "${COL_LIGHT_MAGENTA}${LOGO}"
echo -e "+"
echo -e "${COL_LIGHT_GREEN}${LICENSE}"
echo -e "${COL_NC}+"
set -o xtrace
# Install packages used by this installation script
install __GINA_DEPS[@]
local _hosts="${HOSTS}"
local _hostsArray=()
if ! [[ ${_hosts} ]]; then
read -p 'Hostnames: ' _hosts
if ! [[ ${_hosts} ]]; then
# Check if username or password and/or sshkey was added
return 44
fi
fi
_hosts="${_hosts// /}"
_hostsArray=(${_hosts//,/ })
python3 -m ansible playbook -i , -e "hosts=${_hosts}" playbooks/local_setup.yml --ask-vault-pass
export ANSIBLE_HOST_KEY_CHECKING=False
for i in "${_hostsArray[@]}"; do
python3 -m ansible playbook -i ${i}, -u root -k playbooks/remote_setup.yml --ask-vault-pass
done
return $?
}
# Traps everything
trap exit_handler 0 1 2 3 13 15 # EXIT HUP INT QUIT PIPE TERM
main "$@" 3>&1 1>&2 2>&3
exit 0