This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkvmjob.sh
More file actions
executable file
·341 lines (265 loc) · 7.98 KB
/
kvmjob.sh
File metadata and controls
executable file
·341 lines (265 loc) · 7.98 KB
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
#!/bin/bash
# This script is executed on the compute node
#SBATCH -p compile # partition (queue)
#SBATCH -N 1 # number of nodes
#SBATCH -n 1 # number of cores
#SBATCH --mem 100 # memory pool for all cores
#SBATCH -t 0-4:00 # time (D-HH:MM)
#SBATCH -o /var/lib/ci/%N.job.%j.out # STDOUT
#SBATCH -e /var/lib/ci/%N.job.%j.err # STDERR
set -e
set -x
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
if [ -z "$SLURM_JOB_ID" ]
then
{
echo "WARNING : Job not launched by Slurm"
echo " If you are doing this for testing for testing purpose it can be fine"
echo " If not, there is maybe something wrong somewhere !"
} >&2
SLURM_JOB_ID=$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32)
fi
git_url=$1
git_branch=$2
machine_name="ubuntu"
vmTemplate="/var/lib/kvm/templates/$machine_name.xml"
sourceImage="/var/lib/kvm/images/$machine_name.img"
jobDir="/var/lib/ci/$SLURM_JOB_ID"
sourcesDir=${jobDir}/sources
artifactsDir=${jobDir}/artifacts
vmImage="/var/lib/kvm/vms/${SLURM_JOB_ID}.img"
vmConfig="${jobDir}/vm.xml"
vmName="job-$SLURM_JOB_ID"
vmUser="sds"
vmIP=
mkdir -p ${jobDir}
#
# Function call by a trap when script exits
#
cleanupAndExit() {
if [ $(virsh list | grep ${vmName} | wc -l) -eq 1 ] ; then
log "Destroying virtual machine..."
virsh destroy ${vmName} >&2
fi
log "Cleanup..."
rm -f ${vmImage}
if [ ! -e ${jobDir}/status ] ; then
echo 1 > ${jobDir}/status
fi
local status=$(cat ${jobDir}/status)
local message=$(cat ${jobDir}/error_msg)
if [ "${status}" -ne 0 ] ; then
log ""
if [ -n "${message}" ] ; then
log "${message}"
fi
log "Build failed with status code ${status}"
else
log "Success !"
fi
}
trap cleanupAndExit EXIT
cp ${sourceImage} ${vmImage}
log() {
echo "$(date '+[%Y-%m-%d %H:%M:%S]') ${1}"
}
#
# Put SSH public key to be able to log into the VM and set the user as a sudoer
#
configureImage() {
local mountDir=/tmp/mnt.$SLURM_JOB_ID
mkdir -p ${mountDir}
sudo guestmount -a ${vmImage} -m /dev/sda1 ${mountDir}
sudo mkdir -p ${mountDir}/home/${vmUser}/.ssh
local sudoersFile=$(mktemp)
echo "${vmUser} ALL=(ALL) NOPASSWD: ALL" >> ${sudoersFile}
sudo mv ${sudoersFile} ${mountDir}/etc/sudoers.d/${vmUser}
sudo chown -f root:root ${mountDir}/etc/sudoers.d/${vmUser}
sudo bash -c "cat $HOME/.ssh/id_rsa.pub >> ${mountDir}/home/${vmUser}/.ssh/authorized_keys"
sudo chown -Rf 1000:1000 ${mountDir}/home/${vmUser}/.ssh
sudo guestunmount ${mountDir}
sudo rm -rf ${mountDir}
}
#
# Generate XML configuration for virsh from a template
#
generateVMConfiguration() {
cat /var/images/ubuntu.xml \
| sed "s/%SLURM_VM_NAME%/${vmName}/" \
| sed "s/%SLURM_VM_UUID%/$(uuidgen)/" \
| sed "s/%SLURM_VM_IMAGE%/$(echo ${vmImage} | sed 's/\//\\\//g')/" > ${vmConfig}
}
#
# Find IP address for VM from its name
#
# $1 - Machine name
#
findVMIP() {
local name="$1"
arp -an | grep "`virsh dumpxml ${name} | grep "mac address" | sed "s/.*'\(.*\)'.*/\1/g"`" | awk '{ gsub(/[\(\)]/,"",$2); print $2 }'
}
#
# Run virtual machine from XML file descriptor
#
runVM() {
virsh create ${vmConfig} >&2
}
#
# Function call by a trap when script exits
#
cleanupAndExit() {
if [ $(virsh list | grep ${vmName} | wc -l) -eq 1 ] ; then
log "Destroying virtual machine..."
virsh destroy ${vmName} >&2
fi
log "Cleanup..."
rm -f ${vmImage}
if [ ! -e ${jobDir}/status ] ; then
echo 1 > ${jobDir}/status
fi
local status=$(cat ${jobDir}/status)
local message=$(cat ${jobDir}/error_msg)
if [ "${status}" -ne 0 ] ; then
log ""
if [ -n "${message}" ] ; then
log "${message}"
fi
log "Build failed with status code ${status}"
else
log "Success !"
fi
}
#
# Wait for machine to get an IP address. When the IP address is found, it is written
# in file ${jobDir}/vm_ip. If this file is empty when the function returns, it probably
# means there's an issue somewhere
#
waitForVMToGetIP() {
local max_retry=60
local vmIP=""
while [ -z "${vmIP}" ] && [ "${max_retry}" -gt 0 ]
do
sleep 5
vmIP=$(findVMIP ${vmName})
max_retry=$((max_retry-1))
done
echo ${vmIP} > ${jobDir}/vm_ip
}
#
# Wait for the VM to be reachable on SSH port
#
waitForVMToBeReachable() {
local is_ssh_running=
local max_retry=60
while [ -z "${is_ssh_running}" ]
do
sleep 5
is_ssh_running=$(nmap -p22 $vmIP | grep -i open)
done
if [ -n "$is_ssh_running" ]
then
echo true > ${jobDir}/vm_ssh
fi
}
log "=== Initialize building sandbox =============================================="
log "Setting up virtual machine configuration..."
configureImage
generateVMConfiguration
log "Running virtual machine..."
runVM
log "Waiting for virtual machine network..."
waitForVMToGetIP
vmIP=$(cat ${jobDir}/vm_ip)
if [ "$vmIP" == "" ]; then
# We got an error the VM doesn't have networking
log "ERROR : No IP detected"
exit 1
fi
log "Waiting for virtual machine accessible from SSH..."
waitForVMToBeReachable
if [ "$(cat ${jobDir}/vm_ssh)" != "true" ]
then
# ssh server is not running
log "ERROR - SSH server not running in VM"
exit 1
fi
### Get sources from Git
log "Fetching source code from git repository '${git_url}'..."
if [ -n "${git_branch}" ]; then
git_branch="-b ${git_branch}"
fi
git clone --depth 1 ${git_branch} ${git_url} ${sourcesDir}
### Check Repository have de CI file descriptor
log "Reading CI descriptor .ci.yml"
if [ ! -f ${sourcesDir}/.ci.yml ] ; then
echo 1 > status
echo "No build descriptor .ci.yml can be found in the source code repository." > error_msg
destroyVM
exit 0
fi
### Generate bash script from YAML descriptor
cat ${sourcesDir}/.ci.yml | yq .script | jq -r .[] > ${sourcesDir}/.ci.sh
### Copy source git repository into sandbox
rsync -ae ssh ${sourcesDir} ${vmUser}@${vmIP}:
### Install additionnal tools into sandbox
log "Install additionnal tools into sandbox"
ssh -t ${vmUser}@${vmIP} >&2 <<-'EOF'
set -x
sudo apt update
sudo apt install -y moreutils
EOF
log "=== Running scripts =========================================================="
echo "<% USER LOG PLACEHOLDER %>"
ssh -t ${vmUser}@${vmIP} >&2 <<-'EOF'
set -x
mkdir ci
touch ci/status
touch ci/error_msg
touch ci/log
EOF
#
# TODO Do something smarter to share files between host and VM
#
GetLogFileFromVM() {
while true ; do
scp ${vmUser}@${vmIP}:ci/log ${jobDir} < /dev/null > /dev/null
sleep 10
done
}
GetLogFileFromVM &
### Run build
ssh -t ${vmUser}@${vmIP} >&2 <<-'EOF'
set -x
cd sources
bash .ci.sh > ../ci/log 2>&1
status_code=$?
echo ${status_code} > ~/ci/status
exit 0
EOF
log "=== Finish job ==============================================================="
log "Reading build information from sandbox..."
### Get job status and log files and stdout stderr outputs
scp ${vmUser}@${vmIP}:ci/* ${jobDir}
status_code=$(cat ${jobDir}/status)
if [[ -n "${status_code}" && "${status_code}" -ne 0 ]] ; then
log "ERROR : Job failed with status code ${status_code}" > ${jobDir}/error_msg
exit ${status_code}
fi
### Extract build artifacts from the VM
log "Extract build artifacts from sandbox..."
mkdir ${artifactsDir}
yaml_artifacts=$(cat ${sourcesDir}/.ci.yml | yq .artifacts)
if [ "${yaml_artifacts}" != "null" ] ; then
for artifact in $(echo ${yaml_artifacts} | jq -r .[]) ; do
# Check artifact exists
if [ "$(ssh ${vmUser}@${vmIP} ls sources/${artifact} | wc -l)" -eq 0 ] ; then
echo "ERROR : Job failed because artifact \"${artifact}\" cannot be found" > ${jobDir}/error_msg
echo 1 > ${jobDir}/status
exit 1
fi
# Copy artifact from VM
log "Getting build artifacts '${artifact}'..."
scp ${vmUser}@${vmIP}:sources/${artifact} ${artifactsDir}/${artifact}
done
fi