Skip to content

Commit

Permalink
Rework openstack attach/detach handling
Browse files Browse the repository at this point in the history
Now VM_IMAGE/VM_SWAP either contains the volume or the device.
The __not_attached__ flag is put in a different _STATE variable.
This makes the build log output nicer to read.
  • Loading branch information
mlschroe committed Feb 16, 2017
1 parent 9530ffb commit 80cd648
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 77 deletions.
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ fi

if test -n "$KILL" ; then
test -z "$SRCDIR" || usage
if test -n "$VM_IMAGE" -a -n "$VM_SWAP" -a -n "$VM_TYPE" -a "$VM_SWAP" != __not_attached__ ; then
if test -n "$VM_IMAGE" -a -n "$VM_SWAP" -a -n "$VM_TYPE" -a "$VM_SWAP_STATE" != __not_attached__ ; then
# mark job as failed so that we don't extract packages
if test "$VM_TYPE" != zvm ; then
echo -n "BUILDSTATUS1" >"$VM_SWAP"
Expand Down
20 changes: 12 additions & 8 deletions build-vm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ VM_SWAPDEV=/dev/hda2
VM_TYPE=
VM_IMAGE=
VM_SWAP=
VM_IMAGE_STATE=
VM_SWAP_STATE=
VM_KERNEL=
VM_INITRD=
VM_WORKER=
Expand All @@ -57,7 +59,6 @@ VMDISK_SWAPSIZE=1024
VMDISK_FILESYSTEM=
VMDISK_MOUNT_OPTIONS=__default
VMDISK_CLEAN=
VM_VOLUME_GRUB=
VM_VOLUME_NAME=
VM_VOLUME_SWAP=
VM_HOSTNAME=
Expand All @@ -72,6 +73,9 @@ VM_CUSTOMOPT=
# emulator specific?
EMULATOR_SCRIPT=

# openstack specific
VM_OPENSTACK_FLAVOR=

for i in ec2 emulator kvm lxc openstack qemu uml xen zvm docker pvm; do
. "$BUILD_DIR/build-vm-$i"
done
Expand Down Expand Up @@ -265,9 +269,9 @@ vm_parse_options() {
VM_CUSTOMOPT="$ARG"
shift
;;
-os-flavor)
-openstack-flavor)
needarg
VM_OS_FLAVOR="$ARG"
VM_OPENSTACK_FLAVOR="$ARG"
shift
;;
-*)
Expand Down Expand Up @@ -347,10 +351,10 @@ vm_img_create() {
vm_img_wipe() {
vm_wipe_$VM_TYPE "$@"

if test -n "$VM_IMAGE" -a "$VM_IMAGE" != __not_attached__ -a ! -b "$VM_IMAGE" ; then
if test -n "$VM_IMAGE" -a "$VM_IMAGE_STATE" != __not_attached__ -a ! -b "$VM_IMAGE" ; then
rm -f "$VM_IMAGE"
fi
if test -n "$VM_SWAP" -a "$VM_SWAP" != __not_attached__ -a ! -b "$VM_SWAP" ; then
if test -n "$VM_SWAP" -a "$VM_SWAP_STATE" != __not_attached__ -a ! -b "$VM_SWAP" ; then
rm -f "$VM_SWAP"
fi
}
Expand Down Expand Up @@ -625,11 +629,11 @@ vm_setup() {
test "$VM_TYPE" = xen && vm_purge_xen
if test -n "$VMDISK_CLEAN" ; then
# delete old root/swap to get rid of the old blocks
if test -n "$VM_IMAGE" -a "$VM_IMAGE" != __not_attached__ -a -f "$VM_IMAGE" ; then
if test -n "$VM_IMAGE" -a "$VM_IMAGE_STATE" != __not_attached__ -a -f "$VM_IMAGE" ; then
echo "Deleting old $VM_IMAGE"
rm -rf "$VM_IMAGE"
fi
if test -n "$VM_SWAP" -a "$VM_SWAP" != __not_attached__ -a -f "$VM_SWAP" ; then
if test -n "$VM_SWAP" -a "$VM_SWAP_STATE" != __not_attached__ -a -f "$VM_SWAP" ; then
echo "Deleting old $VM_SWAP"
rm -rf "$VM_SWAP"
fi
Expand All @@ -640,7 +644,7 @@ vm_setup() {
if test -z "$CLEAN_BUILD" ; then
vm_img_mkfs "$VMDISK_FILESYSTEM" "$VM_IMAGE"
fi
if test -n "$VM_SWAP" -a "$VM_SWAP" != __not_attached__ -a ! -b "$VM_SWAP" ; then
if test -n "$VM_SWAP" -a "$VM_SWAP_STATE" != __not_attached__ -a ! -b "$VM_SWAP" ; then
vm_img_create "$VM_SWAP" "$VMDISK_SWAPSIZE"
fi
if test ! -e "$VM_IMAGE" ; then
Expand Down
123 changes: 55 additions & 68 deletions build-vm-openstack
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@


OPENSTACK_BUILD_ID=
OPENSTACK_VOLUME_GRUB=

OPENSTACK_IMAGE_ATTACH_INFO=
OPENSTACK_SWAP_ATTACH_INFO=

openstack_get_field() {
echo -n "$1"|perl -n -e "\$_ =~ /^\|\s+$2\s+\|\s+(\S*)\s+\|\$/ && print \$1"
}

openstack_get_disk_state() {
OUT=$($CINDERCLIENT show "$1")
local OUT=$($CINDERCLIENT show "$1")
openstack_get_field "$OUT" status
}

openstack_cinder_volume2id() {
VM_VOL_NAME=$1
OUT=$($CINDERCLIENT show $VM_VOL_NAME)
local VM_VOL_NAME=$1
local OUT=$($CINDERCLIENT show $VM_VOL_NAME)
openstack_get_field "$OUT" id
}

Expand All @@ -54,16 +58,16 @@ cloud_volume_attach_openstack() {
# | serverId | 175e470c-5869-4425-988a-6b334a2fa655 |
# | volumeId | 793e9a04-7068-4cf1-86e7-26509f709b54 |
# +----------+--------------------------------------+
OUT=$($NOVACLIENT volume-attach "$VM_SERVER" "$VM_VOL_ID")
local OUT=$($NOVACLIENT volume-attach "$VM_SERVER" "$VM_VOL_ID")

if [ $? -gt 0 ] ; then
echo "ERROR: nova attach failed. $?" >&2
return 3
fi
device_path=`openstack_get_field "$OUT" device`
serverId=`openstack_get_field "$OUT" serverId`
while true; do
state=`openstack_get_disk_state "$VM_VOL_NAME"`
local device_path=`openstack_get_field "$OUT" device`
local serverId=`openstack_get_field "$OUT" serverId`
while true ; do
local state=`openstack_get_disk_state "$VM_VOL_NAME"`
test "$state" = "in-use" && break
if test -z "$state" ; then
echo "ERROR: unable to find state of volume $VM_VOL_NAME" >&2
Expand All @@ -82,61 +86,42 @@ cloud_volume_attach_openstack() {
sleep 3
done

# Example:
# return attach info. Example:
# 793e9a04-7068-4cf1-86e7-26509f709b54:175e470c-5869-4425-988a-6b334a2fa655:/dev/vdb
echo "$VM_VOL_ID:$serverId:$device_path"

}

cloud_volume_detach_openstack() {

if [ "$1" = "__not_attached__" ];then
echo "Skipping detach because device is not attached to any server"
return 0
fi

local VM_SERVER=${1:37:36}
local VM_VOL_ID=${1:0:36}

if [ -z "$VM_SERVER" -o -z "$VM_VOL_ID" ];then
# Cannot proceed anyway - guess volume never got attached
echo "__not_attached__"
return 0
fi
test -z "$VM_SERVER" -o -z "$VM_VOL_ID" && cleanup_and_exit 1 "cloud_volume_detach_openstack: bad attach info"

# needed at all?
$NOVACLIENT volume-detach "$VM_SERVER" "$VM_VOL_ID"

state=`openstack_get_disk_state $VM_VOL_ID`

while [ "$state" = detaching ];do
while test "$state" = detaching ; do
sleep 1
state=`openstack_get_disk_state $VM_VOL_ID`
done

if [ "$state" = "available" ];then
echo "__not_attached__"
if test "$state" = "available" ; then
return 0
fi
# umount seems not to be enough
sync

if ! $NOVACLIENT volume-detach "$VM_SERVER" "$VM_VOL_ID"; then
echo "ERROR: nova detach of $VM_VOL_ID failed." >&2
return 3
cleanup_and_exit 3 "ERROR: nova detach of $VM_VOL_ID failed."
fi
while [ "$state" != "available" ]; do
while test "$state" != "available" ; do
state=`openstack_get_disk_state $VM_VOL_ID`
sleep 3
done
return 0

}

vm_verify_options_openstack() {

# Checking for required tools (nova and cinder)

NOVACLIENT=`type -p nova`
if test -z "$NOVACLIENT" ; then
cleanup_and_exit 3 "ERROR: nova not installed. Please install nova and try again"
Expand All @@ -148,7 +133,6 @@ vm_verify_options_openstack() {
fi

# verify options

if test -z "$OS_AUTH_URL" ; then
cleanup_and_exit 3 "ERROR: No openstack environment set. This vm-type works only inside of an openstack VM."
fi
Expand All @@ -168,55 +152,60 @@ vm_verify_options_openstack() {
cleanup_and_exit 3 "ERROR: No VM worker node name specified (the instance to be created)."
fi

if test -z "$VM_OS_FLAVOR" ; then
if test -z "$VM_OPENSTACK_FLAVOR" ; then
cleanup_and_exit 3 "ERROR: No VM openstack flavor set (--os-flavor <FLAVOR-NAME|FLAVOR-ID>)."
fi

# set default values

VM_ROOTDEV="LABEL=obsrootfs"
VM_SWAPDEV="LABEL=obsswapfs"


qemu_rootdev=/dev/vda
VM_VOLUME_GRUB="$VM_KERNEL"
VM_KERNEL=__not_attached__
VM_VOLUME_NAME="$VM_IMAGE"
VM_IMAGE=__not_attached__
VM_VOLUME_SWAP="$VM_SWAP"
VM_SWAP=__not_attached__
# hack: mis-use kernel parameter
OPENSTACK_VOLUME_GRUB="$VM_KERNEL"
VM_KERNEL=

VM_IMAGE_STATE=__not_attached__
VM_SWAP_STATE=__not_attached__
}

vm_attach_root_openstack() {
TMP=`cloud_volume_attach_openstack "$VM_SERVER" "$VM_VOLUME_NAME"`
VM_IMAGE=`echo "$TMP"|cut -d: -f3`
test "${VM_IMAGE:0:5}" = "/dev/" || cleanup_and_exit 3
VM_IMAGE_ATTACH_INFO=${TMP:0:73}
test -n "$OPENSTACK_IMAGE_ATTACH_INFO" && cleanup_and_exit 1 "root is already attached"
TMP=`cloud_volume_attach_openstack "$VM_SERVER" "$VM_IMAGE"`
test "${TMP:74:5}" = "/dev/" || cleanup_and_exit 3
OPENSTACK_IMAGE_ATTACH_INFO=$TMP
VM_IMAGE_STATE=$VM_IMAGE
VM_IMAGE=${TMP:74}
}

vm_attach_swap_openstack() {
TMP=`cloud_volume_attach_openstack "$VM_SERVER" "$VM_VOLUME_SWAP"`
VM_SWAP=`echo "$TMP"|cut -d: -f3`
test "${VM_SWAP:0:5}" = "/dev/" || cleanup_and_exit 3
VM_SWAP_ATTACH_INFO=${TMP:0:73}
test -n "$OPENSTACK_SWAP_ATTACH_INFO" && cleanup_and_exit 1 "root is already attached"
TMP=`cloud_volume_attach_openstack "$VM_SERVER" "$VM_SWAP"`
test "${TMP:74:5}" = "/dev/" || cleanup_and_exit 3
OPENSTACK_SWAP_ATTACH_INFO=$TMP
VM_SWAP_STATE=$VM_SWAP
VM_SWAP=${TMP:74}
}

vm_detach_root_openstack() {
VM_IMAGE=`cloud_volume_detach_openstack "$VM_IMAGE_ATTACH_INFO"`
VM_IMAGE_ATTACH_INFO=__not_attached__
VM_IMAGE=__not_attached__
test -z "$OPENSTACK_IMAGE_ATTACH_INFO" && cleanup_and_exit 1 "root is not attached"
cloud_volume_detach_openstack "$OPENSTACK_IMAGE_ATTACH_INFO" || cleanup_and_exit 3
VM_IMAGE_ATTACH_INFO=
VM_IMAGE=$VM_IMAGE_STATE
VM_IMAGE_STATE=__not_attached__
}

vm_detach_swap_openstack() {
VM_SWAP=`cloud_volume_detach_openstack "$VM_SWAP_ATTACH_INFO"`
VM_SWAP_ATTACH_INFO=__not_attached__
VM_SWAP=__not_attached__
test -z "$OPENSTACK_SWAP_ATTACH_INFO" && cleanup_and_exit 1 "swap is not attached"
cloud_volume_detach_openstack "$OPENSTACK_SWAP_ATTACH_INFO" || cleanup_and_exit 3
VM_SWAP_ATTACH_INFO=
VM_SWAP=$VM_SWAP_STATE
VM_SWAP_STATE=__not_attached__
}

vm_cleanup_openstack() {
vm_detach_root_openstack
vm_detach_swap_openstack
test -n "$OPENSTACK_IMAGE_ATTACH_INFO" && vm_detach_root_openstack
test -n "$OPENSTACK_SWAP_ATTACH_INFO" && vm_detach_swap_openstack
}

vm_fixup_openstack() {
Expand All @@ -238,27 +227,25 @@ vm_wipe_openstack() {
vm_kill_openstack() {
if $NOVACLIENT show "$VM_WORKER" >/dev/null 2>&1 ; then
if ! $NOVACLIENT delete "$VM_WORKER" ; then
cleanup_and_exit 1 "could not kill openstack vm build $VM_VOLUME_NAME"
cleanup_and_exit 1 "could not kill openstack vm build $VM_WORKER"
fi
fi
}

openstack_wait_for_delete_instance() {
FOUND=`$NOVACLIENT list|grep $VM_WORKER`
while test -n "$FOUND" ; do
while test -n "$($NOVACLIENT list|grep $VM_WORKER)" ; do
sleep 1
FOUND=`$NOVACLIENT list|grep $VM_WORKER`
done
}

vm_startup_openstack() {
VM_VOL_ROOT_ID=`openstack_cinder_volume2id ${VM_VOLUME_NAME}`
VM_VOL_SWAP_ID=`openstack_cinder_volume2id ${VM_VOLUME_SWAP}`
VM_VOL_BOOT_ID=`openstack_cinder_volume2id ${VM_VOLUME_GRUB}`
local VM_VOL_ROOT_ID=`openstack_cinder_volume2id ${VM_IMAGE}`
local VM_VOL_SWAP_ID=`openstack_cinder_volume2id ${VM_SWAP}`
local VM_VOL_BOOT_ID=`openstack_cinder_volume2id ${OPENSTACK_VOLUME_GRUB}`

OUTPUT=`\
local OUTPUT=`\
$NOVACLIENT boot \
--flavor $VM_OS_FLAVOR \
--flavor $VM_OPENSTACK_FLAVOR \
--block-device source=volume,dest=volume,bootindex=0,id=${VM_VOL_BOOT_ID}\
--block-device source=volume,dest=volume,bootindex=1,id=${VM_VOL_ROOT_ID}\
--block-device source=volume,dest=volume,bootindex=2,id=${VM_VOL_SWAP_ID}\
Expand Down

0 comments on commit 80cd648

Please sign in to comment.