jjb: Gently stop instances before publication
[lttng-ci.git] / pipelines / images / imagebuild.sh
index 8d164ae432c29df9eb2d34ef61fef3388a90bcf2..45632c0514c365dd0811a5eac196c5c5158825c8 100644 (file)
@@ -49,7 +49,7 @@ if [[ ! "${MISSING_VARS}" == "0" ]] ; then
 fi
 
 # Default optional variables
-INSTANCE_START_TIMEOUT="${INSTANCE_START_TIMEOUT:-60}"
+INSTANCE_START_TIMEOUT="${INSTANCE_START_TIMEOUT:-120}"
 NETWORK_SLEEP="${NETWORK_SLEEP:-15}"
 
 # Dependencies
@@ -91,9 +91,9 @@ set +e
 TRIES_MAX=3
 TRIES=0
 while [[ "${TRIES}" -lt "${TRIES_MAX}" ]] ; do
-    if ! INSTANCE_NAME=$(lxc -q launch -e "${VM_ARG[@]}" -p default -p "${LXD_INSTANCE_PROFILE}" "${SOURCE_IMAGE_NAME}/${IMAGE_TYPE}") ; then
+    if ! INSTANCE_NAME=$(lxc -q launch "${VM_ARG[@]}" -p default -p "${LXD_INSTANCE_PROFILE}" "${SOURCE_IMAGE_NAME}/${IMAGE_TYPE}") ; then
         # Try from images
-        if ! INSTANCE_NAME=$(lxc -q launch -e "${VM_ARG[@]}" -p default -p "${LXD_INSTANCE_PROFILE}" images:"${SOURCE_IMAGE_NAME}") ; then
+        if ! INSTANCE_NAME=$(lxc -q launch "${VM_ARG[@]}" -p default -p "${LXD_INSTANCE_PROFILE}" images:"${SOURCE_IMAGE_NAME}") ; then
             TRIES=$((TRIES + 1))
             echo "Failed to deployed ephemereal instance attempt ${TRIES}/${TRIES_MAX}"
             if [[ "${TRIES}" -lt  "${TRIES_MAX}" ]] ; then
@@ -111,6 +111,7 @@ INSTANCE_NAME="$(echo "${INSTANCE_NAME}" | cut -d ':' -f 2 | tr -d ' ')"
 set -e
 
 CLEANUP+=(
+    "lxc delete -f ${INSTANCE_NAME}"
     "lxc stop ${INSTANCE_NAME}"
 )
 
@@ -132,7 +133,10 @@ done
 
 # Wait for cloud-init to finish
 if [[ "${VARIANT}" == "cloud" ]] ; then
-    lxc exec "${INSTANCE_NAME}" -- cloud-init status -w
+    # It's possible for cloud-init to fail, but to still be able to continue.
+    # Eg., a profile asks for netplan.io on a system that doesn't have that
+    # package available.
+    lxc exec "${INSTANCE_NAME}" -- cloud-init status -w || true
 fi
 
 # Wait for instance to have an ip address (@TODO: is there a better approach?)
@@ -193,22 +197,29 @@ LANG=C ANSIBLE_STRATEGY=linear ansible-playbook \
        playbooks/post-imagebuild-clean.yml \
        -l "${INSTANCE_IP}" -i fake-inventory
 
+# Graceful shutdown
+lxc stop "${INSTANCE_NAME}"
+
 # Publish
-lxc publish "${INSTANCE_NAME}" --alias "${TARGET_IMAGE_NAME}" -f
+if FINGERPRINT=$(lxc publish "${INSTANCE_NAME}" 2>&1 | grep -E -o '[A-Fa-f0-9]{64}') ; then
+    echo "Published instance with fingerprint '${FINGERPRINT}'"
+else
+    fail 1 "No fingerprint for published instance"
+fi
 
 TRIES=0
 
 if [[ "${TEST}" == "true" ]] ; then
     set +e
     while [[ "${TRIES}" -lt "${TRIES_MAX}" ]] ; do
-        if ! INSTANCE_NAME=$(lxc -q launch -e "${VM_ARG[@]}" -p default -p "${LXD_INSTANCE_PROFILE}" "${TARGET_IMAGE_NAME}")  ; then
+        if ! INSTANCE_NAME=$(lxc -q launch -e "${VM_ARG[@]}" -p default -p "${LXD_INSTANCE_PROFILE}" "${FINGERPRINT}")  ; then
             TRIES=$((TRIES + 1))
             echo "Failed to launch instance try ${TRIES}/${TRIES_MAX}"
             if [[ "${TRIES}" -lt "${TRIES_MAX}" ]] ; then
                 sleep $((1 + RANDOM % 10))
                 continue
             fi
-            fail 1 "Failed to launch an instance using newly published image '${TARGET_IMAGE_NAME}'"
+            fail 1 "Failed to launch an instance using newly published image '${FINGERPRINT}'"
         else
             INSTANCE_NAME="$(echo "${INSTANCE_NAME}" | cut -d':' -f2 | tr -d ' ')"
             CLEANUP+=(
@@ -219,3 +230,6 @@ if [[ "${TEST}" == "true" ]] ; then
     done
     set -e
 fi
+
+lxc image alias delete "${TARGET_IMAGE_NAME}" || true
+lxc image alias create "${TARGET_IMAGE_NAME}" "${FINGERPRINT}"
This page took 0.026857 seconds and 4 git commands to generate.