forked from rpedde/fakecloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
virt.sh
executable file
·500 lines (398 loc) · 12.6 KB
/
virt.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
489
490
491
492
493
494
495
496
497
498
499
500
#!/bin/bash
_RETVAL=""
function init() {
# $1 - job file (if using a job file)
if [ "${1-}" != "" ]; then
JOBFILE=${1-}
local basefile="$(dirname ${JOBFILE})/$(basename ${JOBFILE})"
LOGFILE="${basefile}.log"
STATUSFILE="${basefile}.status"
COMPLETEFILE="${basefile}.complete"
else
LOGFILE=/tmp/fakecloud.log
fi
init_vars
init_logs
set -x
set -u
mknod /tmp/zero-test-$$ c 1 5
if ! dd if=/tmp/zero-test-$$ bs=512 count=1 of=/dev/zero; then
log "/tmp appears to be mounted nodev. Exiting"
rm /tmp/zero-test-$$
exit 1
fi
rm /tmp/zero-test-$$
trap handle_error SIGINT SIGTERM ERR
trap handle_exit EXIT
update_status "PENDING" "0" "Initializing"
}
# set a status state, percent complete, and user message
# valid states:
# SUCCESS - complete, successful
# FAILURE - complete, unsuccessful
# PENDING - job not yet started (pending pickup, maybe?)
# EXECUTE - job running
function update_status() {
# $1 - state
# $2 - percent
# $3 - message
if [ "${JOBFILE-}" != "" ]; then
echo -e "STATE=${1}\nPERCENT=${2}\nMESSAGE=${3}" > ${STATUSFILE}
# this status file should be posted to upstream
# controller, if the job was initially obtained from
# an upstream controller
fi
}
# set up logging
function init_logs() {
# fix up logging
exec 3>&1
exec >${LOGFILE}
exec 2>&1
log_debug "Initialized with logs to ${LOGFILE}"
}
# Set up environmental variables and paths
# to locations.
function init_vars() {
# set up globals, read from config file, potentially
if [ "${USER-}" != "root" ]; then
echo "Must be running as root (try sudo)."
exit 1
fi
REAL_USER=${USER:-}
[ ! -z "${SUDO_USER}" ] && REAL_USER=${SUDO_USER}
[ -e /etc/fakecloud/fakecloud.conf ] && . /etc/fakecloud/fakecloud.conf
# FIXME: getent, not /home/$USER - this is wrong somewhere else too
REAL_HOMEDIR=${REAL_HOMEDIR:-/home/${REAL_USER}}
[ -e ${REAL_HOMEDIR}/.fakecloudrc ] && . ${REAL_HOMEDIR}/.fakecloudrc
BASE_DIR=${BASE_DIR:-/var/lib/spin}
SHARE_DIR=${SHARE_DIR-$(dirname $0)}
NBD_DEVICE=${NBD_DEVICE:-/dev/nbd2} # qemu-nbd is hacky as crap...
PLUGIN_DIR=${PLUGIN_DIR:-${SHARE_DIR}/plugins}
LIB_DIR=${LIB_DIR:-${SHARE_DIR}/lib}
EXAMPLE_DIR=${EXAMPLE_DIR:-${SHARE_DIR}/examples}
# should these be user specific?
META_DIR=${META_DIR:-${SHARE_DIR}/meta}
POSTINSTALL_DIR=${POSTINSTALL_DIR:-${SHARE_DIR}/post-install}
EVENT_DIR=${EVENT_DIR:-${SHARE_DIR}/events}
# honor null
# EXTRA_PACKAGES=${EXTRA_PACKAGES-emacs23-nox,sudo}
VIRT_TEMPLATE=${VIRT_TEMPLATE:-kvm}
if [ -z "${SSH_KEY:-}" ]; then
if [ ! -z "${SUDO_USER:-}" ]; then
SSH_KEY=/home/${SUDO_USER}/.ssh/id_*pub
else
SSH_KEY=${HOME}/.ssh/id_[rd]sa.pub
fi
fi
}
# handle terminating under error
function handle_error() {
set +x
trap - EXIT ERR SIGTERM SIGINT
exec 1>&-
exec 2>&-
update_status "ERROR" "100" "Exited on error"
log "Exiting on error. Logs are in ${LOGFILE}. Excerpt follows:\n\n"
tail -n20 ${LOGFILE} >&3
exit 1
}
# any global deinits that must happen
function handle_exit() {
error=${?-$1}
trap - EXIT ERR SIGTERM SIGINT
# rm ${LOGFILE}
update_status "SUCCESS" "100" "Build complete"
exit $error
}
function log() {
echo -e "$@" >&3
}
function log_debug {
if [ "${DEBUG-}" != "" ]; then
log "$@"
else
echo "$@"
fi
}
function destroy_instance_by_name() {
# $1 name
local name=${1}
local old_options=$(set +o)
set +e
virsh destroy "${name}"
virsh undefine "${name}"
eval "$old_options" 2> /dev/null
# destroy the disk
if [ -e "${BASE_DIR}/instances/${name}/${name}.vars" ]; then
source "${BASE_DIR}/instances/${name}/${name}.vars"
source "${LIB_DIR}/disk/default"
source "${LIB_DIR}/disk/${DISK_FLAVOR}"
destroy_instance_disk "${name}"
fi
rm -rf "${BASE_DIR}/instances/${name}"
}
function rekick_instance() {
# $1 - name
local name=${1}
local was_running=0
if (virsh list | grep -q ${name}); then
log "Terminating instance."
was_running=1
virsh destroy ${name}
fi
if [ -e "${BASE_DIR}/instances/${name}/${name}.vars" ]; then
source "${BASE_DIR}/instances/${name}/${name}.vars"
source "${LIB_DIR}/disk/default"
source "${LIB_DIR}/disk/${DISK_FLAVOR}"
destroy_instance_disk "${name}"
else
return 1
fi
if [ ! -e ${BASE_DIR}/flavors/size/${flavor} ]; then
log "No instance def for flavor ${flavor}"
return 1
fi
source ${BASE_DIR}/flavors/size/${flavor}
make_instance_drive $distrelease ${FLAVOR[disk]} ${name}
run_plugins ${name} ${distrelease}
if [ ${was_running} -eq 1 ]; then
log "Restarting instance"
virsh start ${name}
fi
log "Done"
}
function spin_instance() {
# $1 name
# $2 flavor
# $3 dist-release
local name=${1}
local flavor=${2}
local distrelease=${3}
function spin_instance_cleanup() {
log_debug "Cleaning up spin_instance()"
if [ "${NOCLEAN-0}" -eq 1 ]; then
log_debug "Not cleaning up spun instances"
else
virsh destroy ${name}
virsh undefine ${name}
[ -e "${BASE_DIR}/instances/${name}" ] && rm -rf "${BASE_DIR}/instances/${name}"
fi
return 1
}
maybe_make_default_flavors
if [ -e "${BASE_DIR}/instances/${name}" ]; then
log "Instance already exists."
exit 1
fi
if [ ! -e ${BASE_DIR}/flavors/size/${flavor} ]; then
log "No instance definition for flavor \"${flavor}\""
exit 1
fi
trap "spin_instance_cleanup; return 1" ERR SIGINT SIGTERM
log "Spinning instance of flavor \"${flavor}\""
source ${BASE_DIR}/flavors/size/${flavor}
[ -z "${NETWORK_FLAVOR:-}" ] && NETWORK_FLAVOR=$(brctl show | grep -v "bridge name" | cut -f1 | head -n1)
if [ ! -e ${BASE_DIR}/flavors/network/${NETWORK_FLAVOR} ]; then
BRIDGE=${BRIDGE:-br0}
else
source ${BASE_DIR}/flavors/network/${NETWORK_FLAVOR}
fi
FLAVOR+=([bridge]=${BRIDGE})
make_instance_drive $distrelease ${FLAVOR[disk]} ${name}
local disk_image=${BASE_DIR}/instances/${name}/${name}.disk
FLAVOR+=([disk_image]=${disk_image})
FLAVOR+=([name]=$name)
# get the qemu disk type
get_qemu_type
FLAVOR+=([disk_type]=$_RETVAL)
log "name: ${FLAVOR[name]}"
log "disk size: ${FLAVOR[disk]}G"
log "disk type: ${FLAVOR[disk_type]}"
log "memory: ${FLAVOR[memory]}"
log "vcpus: ${FLAVOR[vcpu]}"
log "disk: ${FLAVOR[disk_image]}"
log "bridge: ${FLAVOR[bridge]}"
# let's drop a descriptor file so we know what disk types, etc
rm -f "${BASE_DIR}/instances/${name}/${name}.vars"
for var in DISK_FLAVOR NETWORK_FLAVOR BRIDGE distrelease name flavor; do
typeset -p ${var} >> "${BASE_DIR}/instances/${name}/${name}.vars"
done
# now, we have to generate the template xml...
eval "echo \"$(< ${BASE_DIR}/flavors/template/${VIRT_TEMPLATE})\"" > ${BASE_DIR}/instances/${name}/${name}.xml
log_debug "running plugins"
virsh define ${BASE_DIR}/instances/${name}/${name}.xml
run_plugins ${name} ${distrelease}
log "Starting instance..."
virsh start ${name}
if [ "${POST_INSTALL-}" != "" ]; then
log_debug "Waiting for instance spin-up..."
# wait for instance to spin up, then run post_install
count=0
while [ ${count} -lt 10 ]; do
# wait for port 22
count=$((count + 1))
sleep 5
if (nc ${name}.local 22 -w 1 -q 0 < /dev/null ); then
break;
fi
done
sleep 5
log_debug "Instance spun... starting postinstall script (${POST_INSTALL})"
# 22 is open (or we died)
if [ -e ${POSTINSTALL_DIR}/${POST_INSTALL} ]; then
log "Running post-install script ${POST_INSTALL}..."
if ! ( ${POSTINSTALL_DIR}/${POST_INSTALL} ${name}.local ${distrelease} xx ); then
log "Error..."
handle_exit
else
log "Success!"
fi
fi
fi
}
#
# run ordered plugins from the plugin directory
#
function run_plugins() {
# $1 - name
# $2 - distrelease
local use_kpartx=0
function run_plugins_cleanup() {
log_debug "Cleaning up run_plugins()"
[ -e ${tmpdir}/mnt ] && umount ${tmpdir}/mnt
[ $use_kpartx -eq 1 ] && kpartx -d ${NBD_DEVICE}
qemu-nbd -d ${NBD_DEVICE}
[ -e /dev/mapper/$(basename ${NBD_DEVICE})p1 ] && dmsetup remove /dev/mapper/$(basename ${NBD_DEVICE})p1
rm -rf ${tmpdir}
return 1
}
trap 'run_plugins_cleanup; return 1' ERR SIGINT SIGTERM
tmpdir=$(mktemp -d)
mkdir -p ${tmpdir}/mnt
modprobe nbd
qemu-nbd -c $NBD_DEVICE ${BASE_DIR}/instances/${name}/${name}.disk
sleep 2
if [ ! -e ${NBD_DEVICE}p1 ]; then
kpartx -a ${NBD_DEVICE}
use_kpartx=1
fi
if [ -e ${NBD_DEVICE}p1 ]; then
mount ${NBD_DEVICE}p1 ${tmpdir}/mnt
else
# we'll fail and unmap if this doesn't exist
mount /dev/mapper/$(basename ${NBD_DEVICE})p1 ${tmpdir}/mnt
fi
for plugin in $(ls ${PLUGIN_DIR} | sort); do
log_debug "Running plugin \"${plugin}\"..."
if ! ( /bin/bash -x ${PLUGIN_DIR}/${plugin} "${1}" "${2}" "${tmpdir}/mnt" > /tmp/plugins.log 2>&1 ); then
log_debug "Plugin \"${plugin}\": failure"
else
log_debug "Plugin \"${plugin}\": success"
fi
done
umount ${tmpdir}/mnt
if [ $use_kpartx -eq 1 ]; then
kpartx -d ${NBD_DEVICE}
fi
qemu-nbd -d $NBD_DEVICE
if [ -e /dev/mapper/$(basename ${NBD_DEVICE})p1 ]; then
dmsetup remove /dev/mapper/$(basename ${NBD_DEVICE})p1
fi
}
# if there is a disk image already of this size, then
# qcow it, otherwise resize and qcow *that*
function make_instance_drive() {
local distrelease=${1}
local size=${2}
local name=${3}
DISK_FLAVOR=${DISK_FLAVOR-qcow}
source ${LIB_DIR}/disk/default
source ${LIB_DIR}/disk/${DISK_FLAVOR}
make_bootable_drive ${distrelease} ${size} ${name}
}
function maybe_make_dist_image() {
# $1 - distrelease (ubuntu-natty, etc)
dist=${1%%-*}
release=${1##*-}
arch=amd64
tmpdir=$(mktemp -d)
function maybe_make_dist_image_cleanup() {
rm -rf ${tmpdir}
return 1
}
trap "maybe_make_dist_image_cleanup; return 1" SIGINT SIGTERM ERR
for l in ${LIB_DIR}/os/{default,$dist/default,$dist/$release}; do
if [ -f $l ]; then
log_debug "Sourcing $l"
source $l
fi
done
if [ ! -e ${BASE_DIR}/minibase ]; then
mkdir -p ${BASE_DIR}/minibase
fi
log_debug "checking for dist image for ${1}"
log_debug "Using: $(declare -f validate_image)"
if ! validate_image ${1}; then
log "No valid dist image yet. Creating."
log_debug "Using: $(declare -f make_dist_image)"
make_dist_image ${1}
fi
}
function maybe_make_default_flavors() {
# check to see if there is a flavors dir, and populate
# it if not
if [ ! -e ${BASE_DIR}/flavors ]; then
mkdir -p ${BASE_DIR}/flavors
rsync -av ${EXAMPLE_DIR}/flavors/ ${BASE_DIR}/flavors/
[ -e ${BASE_DIR}/flavors/network ] || mkdir ${BASE_DIR}/flavors/network
#default network flavor will handle this case in future
for bridge in $(brctl show | grep -v "bridge name" | cut -f1); do
cat > ${BASE_DIR}/flavors/network/${bridge} <<EOF
BRIDGE=${bridge}
EOF
done
fi
}
# files need to be in a format like this:
# Basically, just a transcription of fakecloud command line
#
# --< cut >--
# ACTION=create
# NAME=servername
# DISTRELEASE=ubuntu-natty
# ... other metavars
# --< cut >--
function dispatch_file() {
# $1 - file
file=${1}
JOB_FILE=${file}
if [ ! -e ${file} ]; then
return 0
fi
source ${file}
dispatch_job
}
function dispatch_job() {
case $ACTION in
create)
spin_instance ${NAME} ${FLAVOR-tiny} ${DISTRELEASE}
;;
destroy)
destroy_instance_by_name $NAME
;;
reboot)
virsh destroy ${NAME}
virsh start ${NAME}
;;
stop)
virsh destroy ${NAME}
;;
start)
virsh start ${NAME}
;;
*)
log "Bad command ${ACTION}"
;;
esac
}