-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.sh
executable file
·488 lines (442 loc) · 18.6 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
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
#!/bin/bash
#github-action genshdoc
# @setting-header setup.sh quickstart script for qubinode_navigator
# @setting ./setup.sh
# Uncomment for debugging
#export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
#set -x
# @global ANSIBLE_SAFE_VERSION this is the ansible safe version
# @global INVENTORY this is the inventory file name and path Example: inventories/localhost
export ANSIBLE_SAFE_VERSION="0.0.9"
export GIT_REPO="https://github.com/tosin2013/qubinode_navigator.git"
if [ -z "$CICD_PIPELINE" ]; then
export CICD_PIPELINE="false"
export INVENTORY="localhost"
fi
if [ -z "$USE_HASHICORP_VAULT" ]; then
export USE_HASHICORP_VAULT="false"
else
if [[ -z "$VAULT_ADDRESS" && -z "$VAULT_ADDRESS" && -z ${SECRET_PATH} ]]; then
echo "VAULT enviornment variables are not passed is not set"
exit 1
fi
fi
# @setting The function get_rhel_version function will determine the version of RHEL
function get_rhel_version() {
if cat /etc/redhat-release | grep "Red Hat Enterprise Linux release 9.[0-9]" > /dev/null 2>&1; then
export BASE_OS="RHEL9"
elif cat /etc/redhat-release | grep "Red Hat Enterprise Linux release 8.[0-9]" > /dev/null 2>&1; then
export BASE_OS="RHEL8"
elif cat /etc/redhat-release | grep "Rocky Linux release 8.[0-9]" > /dev/null 2>&1; then
export BASE_OS="ROCKY8"
elif cat /etc/redhat-release | grep 7.[0-9] > /dev/null 2>&1; then
export BASE_OS="RHEL7"
elif cat /etc/redhat-release | grep "CentOS Stream release 9" > /dev/null 2>&1; then
export BASE_OS="CENTOS9"
elif cat /etc/redhat-release | grep "CentOS Stream release 8" > /dev/null 2>&1; then
export BASE_OS="CENTOS8"
elif cat /etc/redhat-release | grep "Fedora" > /dev/null 2>&1; then
export BASE_OS="FEDORA"
else
echo "Operating System not supported"
echo "You may put a pull request to add support for your OS"
fi
echo ${BASE_OS}
}
function install_packages() {
# Check if packages are already installed
echo "Installing packages"
echo "*******************"
for package in openssl-devel bzip2-devel libffi-devel wget vim podman ncurses-devel sqlite-devel firewalld make gcc git unzip sshpass lvm lvm2 python3 python3-pip leapp-upgrade cockpit-leapp; do
if rpm -q "${package}" >/dev/null 2>&1; then
echo "Package ${package} already installed"
else
echo "Installing package ${package}"
sudo dnf install "${package}" -y
fi
done
# Install necessary groups and updates
if dnf group info "Development Tools" >/dev/null 2>&1; then
echo "Package group Development Tools already installed"
else
echo "Installing package group Development Tools"
sudo dnf groupinstall "Development Tools" -y
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'
dnf check-update
sudo dnf install code -y
fi
sudo dnf update -y
}
# @description This function get_qubinode_navigator function will clone the qubinode_navigator repo
function get_qubinode_navigator() {
echo "Cloning qubinode_navigator"
if [ -d $1/qubinode_navigator ]; then
echo "Qubinode Installer already exists"
git -C "$HOME/qubinode_navigator" pull
else
git clone ${GIT_REPO}
ln -s /root/qubinode_navigator /opt/qubinode_navigator
fi
}
# @description This function configure_navigator function will configure the ansible-navigator
function configure_navigator() {
echo "Configuring ansible navigator"
echo "****************"
if [ -d $1/qubinode_navigator ]; then
cd $1/qubinode_navigator
if ! command -v ansible-navigator &> /dev/null; then
if [ ${BASE_OS} == "RHEL8" ]; then
# Enable the Python 3.9 Module
sudo dnf module install -y python39
sudo dnf install -y python39 python39-devel python39-pip
sudo dnf module enable -y python39
# Make sure the Python 3.6 Module is disabled
sudo dnf module disable -y python36
# Set the default Python version to 3.9
sudo alternatives --set python /usr/bin/python3.9
sudo alternatives --set python3 /usr/bin/python3.9
# Non-root podman hacks
sudo chmod 4755 /usr/bin/newgidmap
sudo chmod 4755 /usr/bin/newuidmap
sudo dnf reinstall -yq shadow-utils
cat > /tmp/xdg_runtime_dir.sh <<EOF
export XDG_RUNTIME_DIR="\$HOME/.run/containers"
EOF
sudo mv /tmp/xdg_runtime_dir.sh /etc/profile.d/xdg_runtime_dir.sh
sudo chmod a+rx /etc/profile.d/xdg_runtime_dir.sh
sudo cp /etc/profile.d/xdg_runtime_dir.sh /etc/profile.d/xdg_runtime_dir.zsh
cat > /tmp/ping_group_range.conf <<EOF
net.ipv4.ping_group_range=0 2000000
EOF
sudo mv /tmp/ping_group_range.conf /etc/sysctl.d/ping_group_range.conf
sudo sysctl --system
# Install needed Pip modules
# - For Ansible-Navigator
curl -sSL https://raw.githubusercontent.com/ansible/ansible-navigator/main/requirements.txt | sudo python3 -m pip install -r /dev/stdin
sudo python3 -m pip install -r $HOME/qubinode_navigator/bash-aliases/bastion-requirements.txt
else
make install-ansible-navigator
fi
make copy-navigator
# Check if running as root
if [ "$EUID" -eq 0 ]; then
sed -i 's|/home/admin/qubinode_navigator/inventories/localhost|/root/qubinode_navigator/inventories/'${INVENTORY}'|g' ~/.ansible-navigator.yml
else
sed -i 's|/home/admin/qubinode_navigator/inventories/localhost|/home/'$USER'/qubinode_navigator/inventories/'${INVENTORY}'|g' ~/.ansible-navigator.yml
fi
fi
else
echo "Qubinode Installer does not exist"
fi
cd $1/qubinode_navigator
sudo pip3 install -r requirements.txt
echo "Load variables"
echo "**************"
if [ $CICD_PIPELINE == "false" ];
then
python3 load-variables.py
else
if [[ -z "$ENV_USERNAME" && -z "$DOMAIN" && -z "$FORWARDER" && -z "$ACTIVE_BRIDGE" && -z "$INTERFACE" && -z "$DISK" ]]; then
echo "Error: One or more environment variables are not set"
exit 1
fi
python3 load-variables.py --username ${ENV_USERNAME} --domain ${DOMAIN} --forwarder ${FORWARDER} --bridge ${ACTIVE_BRIDGE} --interface ${INTERFACE} --disk ${DISK}
fi
}
# @description This function configure_vault function will configure the ansible-vault it will download ansible vault and ansiblesafe
function configure_vault() {
echo "Configuring vault using ansible safe"
echo "****************"
if [ -d $1/qubinode_navigator ]; then
cd $1/qubinode_navigator
if ! command -v ansible-vault &> /dev/null; then
sudo dnf install ansible-core -y
fi
if ! command -v ansiblesafe &> /dev/null; then
curl -OL https://github.com/tosin2013/ansiblesafe/releases/download/v${ANSIBLE_SAFE_VERSION}/ansiblesafe-v${ANSIBLE_SAFE_VERSION}-linux-amd64.tar.gz
tar -zxvf ansiblesafe-v${ANSIBLE_SAFE_VERSION}-linux-amd64.tar.gz
chmod +x ansiblesafe-linux-amd64
sudo mv ansiblesafe-linux-amd64 /usr/local/bin/ansiblesafe
fi
echo "Configure Ansible Vault password file"
echo "****************"
if [ ! -f ~/qubinode_navigator/ansible_vault_setup.sh ];
then
curl -OL https://gist.githubusercontent.com/tosin2013/022841d90216df8617244ab6d6aceaf8/raw/92400b9e459351d204feb67b985c08df6477d7fa/ansible_vault_setup.sh
chmod +x ansible_vault_setup.sh
fi
rm -f ~/.vault_password
sudo rm -rf /root/.vault_password
if [ $USE_HASHICORP_VAULT == "true" ];
then
echo "$SSH_PASSWORD" > ~/.vault_password
sudo cp ~/.vault_password /root/.vault_password
bash ./ansible_vault_setup.sh
if [ $(id -u) -ne 0 ]; then
if [ ! -f /home/${USER}/qubinode_navigator/inventories/${INVENTORY}/group_vars/control/vault.yml ];
then
ansiblesafe -f /home/${USER}/qubinode_navigator/inventories/${INVENTORY}/group_vars/control/vault.yml -o 4
ansiblesafe -f /home/${USER}/qubinode_navigator/inventories/${INVENTORY}/group_vars/control/vault.yml -o 1
fi
else
if [ ! -f /root/qubinode_navigator/inventories/${INVENTORY}/group_vars/control/vault.yml ];
then
ansiblesafe -f /root/qubinode_navigator/inventories/${INVENTORY}/group_vars/control/vault.yml -o 4
ansiblesafe -f /root/qubinode_navigator/inventories/${INVENTORY}/group_vars/control/vault.yml -o 1
fi
fi
else
read -t 360 -p "Press Enter to continue, or wait 5 minutes for the script to continue automatically" || true
bash ./ansible_vault_setup.sh
if [ $(id -u) -ne 0 ]; then
if [ ! -f /home/${USER}/qubinode_navigator/inventories/${INVENTORY}/group_vars/control/vault.yml ];
then
ansiblesafe -f /home/${USER}/qubinode_navigator/inventories/${INVENTORY}/group_vars/control/vault.yml
fi
else
if [ ! -f /root/qubinode_navigator/inventories/${INVENTORY}/group_vars/control/vault.yml ];
then
ansiblesafe -f /root/qubinode_navigator/inventories/${INVENTORY}/group_vars/control/vault.yml
fi
fi
fi
#ansible-navigator inventory --list -m stdout --vault-password-file $HOME/.vault_password
else
echo "Qubinode Installer does not exist"
fi
}
# @description This function generate_inventory function will generate the inventory
function generate_inventory(){
echo "Generating inventory"
echo "****************"
if [ -d $1/qubinode_navigator ]; then
cd $1/qubinode_navigator
if [ ! -d inventories/${INVENTORY} ]; then
mkdir -p inventories/${INVENTORY}
mkdir -p inventories/${INVENTORY}/group_vars/control
cp -r inventories/${INVENTORY}/group_vars/control/* inventories/${INVENTORY}/group_vars/control/
fi
sed -i 's|export CURRENT_INVENTORY="localhost"|export CURRENT_INVENTORY="'${INVENTORY}'"|g' bash-aliases/random-functions.sh
# set the values
control_host="$(hostname -I | awk '{print $1}')"
# Check if running as root
if [ "$EUID" -eq 0 ]; then
read -p "Enter the target username to ssh into machine: " control_user
else
control_user="$USER"
fi
echo "[control]" > inventories/${INVENTORY}/hosts
echo "control ansible_host=${control_host} ansible_user=${control_user}" >> inventories/${INVENTORY}/hosts
if ! command -v ansible-navigator &> /dev/null; then
sudo pip3 install ansible-navigator
whereis ansible-navigator
ANSIBLE_NAVIAGATOR=$(whereis ansible-navigator | awk '{print $2}')
else
ANSIBLE_NAVIAGATOR="ansible-navigator "
fi
${ANSIBLE_NAVIAGATOR} inventory --list -m stdout --vault-password-file $HOME/.vault_password
else
echo "Qubinode Installer does not exist"
fi
}
# @description This function configure_ssh function will configure the ssh
function configure_ssh(){
echo "Configuring SSH"
echo "****************"
if [ -f ~/.ssh/id_rsa ]; then
echo "SSH key already exists"
else
IP_ADDRESS=$(hostname -I | awk '{print $1}')
ssh-keygen -f ~/.ssh/id_rsa -t rsa -N ''
# Check if running as root
if [ $CICD_PIPELINE == "true" ];
then
if [ "$EUID" -eq 0 ]; then
sshpass -p "$SSH_PASSWORD" ssh-copy-id -o StrictHostKeyChecking=no $control_user@${IP_ADDRESS}
else
sshpass -p "$SSH_PASSWORD" ssh-copy-id -o StrictHostKeyChecking=no $USER@${IP_ADDRESS}
sudo ssh-keygen -f /root/.ssh/id_rsa -t rsa -N ''
fi
else
if [ "$EUID" -eq 0 ]; then
read -p "Enter the target username to ssh into machine: " control_user
ssh-copy-id $control_user@${IP_ADDRESS}
else
ssh-copy-id $USER@${IP_ADDRESS}
sudo ssh-keygen -f /root/.ssh/id_rsa -t rsa -N ''
fi
fi
fi
}
function configure_firewalld() {
echo "Configuring firewalld"
echo "*********************"
if systemctl is-active --quiet firewalld; then
echo "firewalld is already active"
else
echo "starting firewalld"
sudo systemctl start firewalld
sudo systemctl enable firewalld
fi
}
function configure_onedev(){
if [ "$(pwd)" != "/root/qubinode_navigator" ]; then
echo "Current directory is not /root/qubinode_navigator."
echo "Changing to /root/qubinode_navigator..."
cd /root/qubinode_navigator
else
echo "Current directory is /root/qubinode_navigator."
fi
echo "Configuring OneDev"
echo "******************"
./dependancies/onedev/configure-onedev.sh
}
# @description This configure_os function will get the base os and install the required packages
function configure_os(){
if [ ${1} == "ROCKY8" ]; then
sudo dnf install git vim unzip wget bind-utils python3-pip tar util-linux-user gcc python3-devel podman ansible-core make sshpass -y
elif [ ${1} == "FEDORA" ]; then
sudo dnf install git vim unzip wget bind-utils python3-pip tar util-linux-user gcc python3-devel podman ansible-core make sshpass -y
elif [ ${1} == "UBUNTU" ]; then
sudo apt install git vim unzip wget bind-utils python3-pip tar util-linux-user gcc python3-devel podman ansible-core make sshpass -y
elif [ ${1} == "CENTOS8" ]; then
sudo dnf install git vim unzip wget bind-utils python3-pip tar util-linux-user gcc python3-devel podman ansible-core make sshpass -y
elif [ ${1} == "RHEL9" ]; then
sudo dnf update -y
sudo dnf install git vim unzip wget bind-utils python3-pip tar util-linux-user gcc python3-devel podman ansible-core make sshpass -y
elif [ ${1} == "CENTOS9" ]; then
sudo dnf update -y
sudo dnf install git vim unzip wget bind-utils python3-pip tar util-linux-user gcc python3-devel podman ansible-core make sshpass -y
fi
}
function test_inventory(){
echo "Testing inventory"
echo "****************"
if [ -d $1/qubinode_navigator ]; then
cd $1/qubinode_navigator
if ! command -v ansible-navigator &> /dev/null; then
ANSIBLE_NAVIAGATOR=$(whereis ansible-navigator | awk '{print $2}')
else
ANSIBLE_NAVIAGATOR="ansible-navigator "
fi
${ANSIBLE_NAVIAGATOR} inventory --list -m stdout --vault-password-file $HOME/.vault_password || exit 1
else
echo "Qubinode Installer does not exist"
fi
}
function deploy_kvmhost() {
echo "Deploying KVM Host"
echo "******************"
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
cd "$HOME"/qubinode_navigator
if ! command -v ansible-navigator &> /dev/null; then
ANSIBLE_NAVIAGATOR=$(whereis ansible-navigator | awk '{print $2}')
else
ANSIBLE_NAVIAGATOR="ansible-navigator"
fi
${ANSIBLE_NAVIAGATOR} run ansible-navigator/setup_kvmhost.yml \
--vault-password-file "$HOME"/.vault_password -m stdout || exit 1
}
function configure_bash_aliases() {
echo "Configuring bash aliases"
echo "************************"
if [ "$(pwd)" != "/root/qubinode_navigator" ]; then
echo "Current directory is not /root/qubinode_navigator."
echo "Changing to /root/qubinode_navigator..."
cd /root/qubinode_navigator
else
echo "Current directory is /root/qubinode_navigator."
fi
# Source the function definitions
source bash-aliases/functions.sh
# Source the alias definitions
source bash-aliases/aliases.sh
# Source .bash_aliases to apply changes
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# Ensure .bash_aliases is sourced from .bashrc
if ! grep -qF "source ~/.bash_aliases" ~/.bashrc; then
echo "source ~/.bash_aliases" >> ~/.bashrc
fi
}
function setup_kcli_base() {
if [ "$(pwd)" != "/root/qubinode_navigator" ]; then
echo "Current directory is not /root/qubinode_navigator."
echo "Changing to /root/qubinode_navigator..."
cd /root/qubinode_navigator
else
echo "Current directory is /root/qubinode_navigator."
fi
echo "Configuring Kcli"
echo "****************"
source ~/.bash_aliases
qubinode_setup_kcli
kcli_configure_images
}
function show_help() {
echo "Usage: $0 [OPTION]"
echo "Call all functions if nothing is passed."
echo "OPTIONS:"
echo " -h, --help Show this help message and exit"
echo " --deploy-kvmhost Deploy KVM host"
echo " --configure-bash-aliases Configure bash aliases"
echo " --setup-kcli-base Setup Kcli"
echo " --deploy-freeipa Deploy FreeIPA"
}
get_rhel_version
if [ $BASE_OS == "ROCKY8" ];
then
echo "Please run the rocky-linux-hypervisor.sh script"
exit 1
fi
if [ "$EUID" -eq 0 ]; then
MY_DIR="/root"
else
MY_DIR="$HOME"
fi
if [ $# -eq 0 ]; then
configure_os $BASE_OS
install_packages
configure_ssh
configure_firewalld
get_qubinode_navigator $MY_DIR
configure_navigator $MY_DIR
configure_vault $MY_DIR
generate_inventory $MY_DIR
test_inventory $MY_DIR
deploy_kvmhost
configure_bash_aliases $MY_DIR
setup_kcli_base $MY_DIR
configure_onedev
fi
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
--deploy-kvmhost)
deploy_kvmhost
shift
;;
--configure-bash-aliases)
configure_bash_aliases $MY_DIR
shift
;;
--setup-kcli-base)
setup_kcli_base $MY_DIR
shift
;;
--deploy-freeipa)
freeipa-utils deploy
shift
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done