-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_base_image.sh
executable file
·147 lines (113 loc) · 3.14 KB
/
make_base_image.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
#!/bin/bash
PASS=true
LOG_FILE="gitlab-provisioner.log"
POOL_PATH="/var/lib/libvirt/images/base_imgs"
SCRIPT_BASE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
COMMON_SSH_ARGS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
> $LOG_FILE
print_ok() {
CLEAR=$(tput sgr0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
action=$1
ret=1
shift
echo -en "[i] $action\r"
run "$@"
if [[ $? -eq 0 ]]; then
echo -e "[${GREEN}OK${CLEAR}] $action"
ret=0
else
echo -e "[${RED}FAILED${CLEAR}] $action"
ret=1
PASS=false
fi
return $ret
}
log() {
echo "$@" &>> $LOG_FILE
}
log_cmd() {
echo >> $LOG_FILE
printf '=%.0s' {1..80} >> $LOG_FILE; echo >> $LOG_FILE
echo "$ $@" &>> $LOG_FILE
printf '=%.0s' {1..80} >> $LOG_FILE; echo >> $LOG_FILE
}
run() {
log_cmd "$@"
"$@" &>> $LOG_FILE
}
create_libvirt_base_img_pool() {
if ! [[ -d "$POOL_PATH" ]]; then
mkdir "$POOL_PATH"
fi
if ! (virsh --quiet pool-list --all | grep base_imgs &>/dev/null); then
print_ok "Create a new storage pool for base images" \
virsh pool-define-as \
--name base_imgs \
--type dir \
--target "$POOL_PATH"
fi
}
wait_for_domain_status() {
domain=$1
state=$2
timeout=1
while true; do
if [[ $timeout -ge 30 ]]; then
log "Maximum timeout reached waiting for domain $domain"
return 1
fi
if [[ "$state" == "running" ]]; then
if (ping -4 -c 1 $domain &>/dev/null) &&
ssh $COMMON_SSH_ARGS $distro /bin/true; then
break
fi
elif [[ "$state" == "shut off" ]]; then
if [[ $(virsh domstate "$domain") == "$state" ]]; then
break
fi
fi
sleep $timeout
timeout=$(( 2 * timeout ))
done
}
start_domain() {
distro=$1
if ! (virsh list --name --all | grep $distro &>/dev/null); then
log "ERROR: Template image not found, please install it manually..."
return 1
fi
if [[ $(virsh domstate $distro) != "running" ]]; then
run virsh start $distro || return 1
wait_for_domain_status $distro "running"
fi
}
stop_domain() {
distro=$1
if ! (virsh list --name --all | grep $distro &>/dev/null); then
log "ERROR: Template image not found, please install it manually..."
return 1
fi
if [[ $(virsh domstate $distro) != "shut off" ]]; then
run virsh shutdown $distro || return 1
wait_for_domain_status $distro "shut off"
fi
}
prepare_base_image() {
distro=$1
print_ok "Shut down virtual machine [$distro]" stop_domain $distro
print_ok "Create a machine template [$distro]" \
virt-sysprep --operations defaults,-ssh-userdir \
-d "$distro" || return 1
}
tput civis
while [ $# -gt 0 ]; do
distro="$1"
shift
prepare_base_image $distro || continue
done
tput cnorm
if ! $PASS; then
echo "See '$LOG_FILE' for more details about the failures"
fi