-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_vms.bash
executable file
·173 lines (156 loc) · 5.38 KB
/
build_vms.bash
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
#!/bin/bash
# build_vms.bash
# shellcheck source=/dev/null
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
source "$SCRIPT_DIR/env.bash"
export LIBVIRT_DEFAULT_URI="${LIBVIRT_DEFAULT_URI}?no_verify=1"
function set_hostname() {
echo setting host_name "${1}"
local ip_address
ip_address="$(virsh domifaddr "${1}" | tail -2 | head -1 | awk '{print $4}')"
if [ "${ip_address:0:$(("${#SUBNET_FRAGMENT}"))}" != "${SUBNET_FRAGMENT}" ]
then
echo no dhcp lease yet, trying again for "${1}"
sleep 15 && set_hostname "${1}"
else
local host_name
host_name="$(ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" 'cat /etc/hostname' 2>/dev/null)"
echo host_name is found to be "${host_name}"
if [ "${host_name}" != "${1}" ]
then
echo "${host_name}" is wrong, changing to "${1}"
# shellcheck disable=SC2027,SC2086
ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" "echo "${1}" > /etc/hostname"
# shellcheck disable=SC2027,SC2086
ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" "sed -i 's/"${ORIGINAL}"/"${1}"/g' /etc/hosts 2>/dev/null"
ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" 'systemctl reboot'
sleep 25
fi
fi
}
function confirm_hostname() {
echo confirming host_name "${1}"
local ip_address
ip_address="$(virsh domifaddr "${1}" | tail -2 | head -1 | awk '{print $4}')"
if [ "${ip_address:0:$(("${#SUBNET_FRAGMENT}"))}" != "${SUBNET_FRAGMENT}" ]
then
sleep 5
confirm_hostname "${1}"
else
local host_name
host_name="$(ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" 'hostname' 2>/dev/null)"
if [ "${host_name}" != "${1}" ]
then
set_hostname "${1}"
sleep 1
confirm_hostname "${1}"
fi
fi
}
function confirm_hostname_in_hosts() {
echo confirming host_name_in_hosts "${1}"
local ip_address
ip_address="$(virsh domifaddr "${1}" | tail -2 | head -1 | awk '{print $4}')"
if [ "${ip_address:0:$(("${#SUBNET_FRAGMENT}"))}" != "${SUBNET_FRAGMENT}" ]
then
sleep 5
confirm_hostname_in_hosts "${1}"
else
local host_is_in_hosts
# shellcheck disable=SC2027,SC2086
host_is_in_hosts="$(ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" "grep "${1}" -c /etc/hosts")"
if [ "${host_is_in_hosts}" == "0" ]
then
# shellcheck disable=SC2027,SC2086
ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" "sed -i 's/"${ORIGINAL}"/"${1}"/g' /etc/hosts 2>/dev/null"
confirm_hostname_in_hosts "${1}"
fi
fi
}
function reset_machine_id() {
echo reset_machine_id "${1}"
local ip_address
ip_address="$(virsh domifaddr "${1}" | tail -2 | head -1 | awk '{print $4}')"
if [ "${ip_address:0:$(("${#SUBNET_FRAGMENT}"))}" != "${SUBNET_FRAGMENT}" ]
then
sleep 2
reset_host_ssh_keys "${1}"
else
local existing_machine_id
existing_machine_id="$(ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" "cat /etc/machine-id")"
if [ "${existing_machine_id}" == "${ORIGINAL_MACHINE_ID}" ]
then
ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" "rm -v /etc/machine-id && rm -v /var/lib/dbus/machine-id"
ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" "dbus-uuidgen --ensure && systemd-machine-id-setup"
reset_machine_id "${1}"
else
echo "${1}" has a new machine-id
fi
fi
}
function reset_host_ssh_keys() {
echo reset_host_ssh_keys "${1}"
local ip_address
ip_address="$(virsh domifaddr "${1}" | tail -2 | head -1 | awk '{print $4}')"
if [ "${ip_address:0:$(("${#SUBNET_FRAGMENT}"))}" != "${SUBNET_FRAGMENT}" ]
then
sleep 2
reset_host_ssh_keys "${1}"
else
local existing_ED25519PUB
existing_ED25519PUB="$(ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" "cat /etc/ssh/ssh_host_ed25519_key.pub")"
if [ "${existing_ED25519PUB}" == "${ORIGINAL_SSH_ED25519PUB}" ]
then
ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" "rm -v /etc/ssh/ssh_host_* && ssh-keygen -A"
ssh -o "StrictHostKeyChecking no" "${ip_address:0:-3}" "systemctl restart sshd"
rm ~/.ssh/known_hosts
reset_host_ssh_keys "${1}"
else
echo "${1}" has new ssh host keys
fi
fi
}
function create_vm() {
local exists
exists="$(virsh list --all | awk '{print $2}' | grep "^${1}$" -c)"
if [ "${exists}" == "0" ]
then
virt-clone --original "${ORIGINAL}" --name "${1}" --auto-clone
fi
start_vm "${1}"
}
function reset_hosts_ssh_keys() {
export -f reset_host_ssh_keys
printf "%s\n" "${MACHINES[@]}" | xargs -P 8 -I {} bash -c 'reset_host_ssh_keys "$@"' _ {}
}
function reset_machine_ids() {
export -f reset_machine_id
printf "%s\n" "${MACHINES[@]}" | xargs -P 8 -I {} bash -c 'reset_machine_id "$@"' _ {}
}
function confirm_hostnames_in_hosts() {
export -f confirm_hostname_in_hosts
printf "%s\n" "${MACHINES[@]}" | xargs -P 8 -I {} bash -c 'confirm_hostname_in_hosts "$@"' _ {}
}
function confirm_hostnames() {
export -f confirm_hostname
printf "%s\n" "${MACHINES[@]}" | xargs -P 8 -I {} bash -c 'confirm_hostname "$@"' _ {}
}
function set_hostnames() {
export -f set_hostname
printf "%s\n" "${MACHINES[@]}" | xargs -P 8 -I {} bash -c 'set_hostname "$@"' _ {}
}
function build_vms() {
for vm in "${MACHINES[@]}"
do
create_vm "${vm}"
done
}
check_uid "0"
rm ~/.ssh/known_hosts
build_vms
set_hostnames
confirm_hostnames
confirm_hostnames_in_hosts
reset_hosts_ssh_keys
rm ~/.ssh/known_hosts
reset_machine_ids