Add new version_consistency_check in configure
[snf-image] / snf-image-helper / common.sh
index 1523cd2..d666e4f 100644 (file)
@@ -35,90 +35,59 @@ BLKID=blkid
 BLOCKDEV=blockdev
 REGLOOKUP=reglookup
 CHNTPW=chntpw
+DATE="date -u" # Time in UTC
+EATMYDATA=eatmydata
 
 CLEANUP=( )
 ERRORS=( )
 WARNINGS=( )
 
+MSG_TYPE_TASK_START="TASK_START"
+MSG_TYPE_TASK_END="TASK_END"
+
+STDERR_LINE_SIZE=10
+
 add_cleanup() {
     local cmd=""
     for arg; do cmd+=$(printf "%q " "$arg"); done
     CLEANUP+=("$cmd")
 }
 
+report_error() {
+    if [ ${#ERRORS[*]} -eq 0 ]; then
+        # No error message. Print stderr
+       local lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
+        echo -n "STDERR:${lines}:" > "$MONITOR"
+        tail --lines=$lines  "$STDERR_FILE" > "$MONITOR"
+    else
+        for line in "${ERRORS[@]}"; do
+            echo "ERROR:$line" > "$MONITOR"
+        done
+    fi
+}
+
 log_error() {
-    ERRORS+=("$@")
+    ERRORS+=("$*")
     echo "ERROR: $@" | tee $RESULT >&2
+    report_error
     exit 1
 }
 
 warn() {
-    WARNINGS+=("$@")
     echo "Warning: $@" >&2
+    echo "WARNING:$@" > "$MONITOR"
 }
 
-report_start_task() {
-
-    local type="start-task"
-    local timestamp=$(date +%s.%N)
-    local name="${PROGNAME}"
-
-    report="{\"id\":\"$id\","
-    report+="\"type\":\"$type\"," \
-    report+="\"timestamp\":$(date +%s.%N)," \
-    report+="\"name\":\"$name\"}"
-
-    echo "$report" > "$MONITOR"
+report_task_start() {
+    echo "$MSG_TYPE_TASK_START:${PROGNAME:2}" > "$MONITOR"
 }
 
-json_list() {
-    declare -a items=("${!1}")
-    report="["
-    for item in "${items[@]}"; do
-        report+="\"$(sed 's/"/\\"/g' <<< "$item")\","
-    done
-    if [ ${#report} -gt 1 ]; then
-        # remove last comma(,)
-        report="${report%?}"
-    fi
-    report+="]"
-
-    echo "$report"
+report_task_end() {
+    echo "$MSG_TYPE_TASK_END:${PROGNAME:2}" > "$MONITOR"
 }
 
-report_end_task() {
-
-    local type="end-task"
-    local timestam=$(date +%s.%N)
-    local name=${PROGNAME}
-    local warnings=$(json_list WARNINGS[@])
-
-    report="{\"id\":\"$id\","
-    report+="\"type\":\"$type\"," \
-    report+="\"timestamp\":$(date +%s)," \
-    report+="\"name\":\"$name\"," \
-    report+="\"warnings\":\"$warnings\"}"
-
-    echo "$report" > "$MONITOR"
-}
-
-report_error() {
-    local type="ganeti-error"
-    local timestamp=$(date +%s.%N)
-    local location="${PROGNAME}"
-    local errors=$(json_list ERRORS[@])
-    local warnings=$(json_list WARNINGS[@])
-    local stderr="$(cat "$STDERR_FILE" | sed 's/"/\\"/g')"
-
-    report="{\"id\":\"$id\","
-    report+="\"type\":\"$type\"," \
-    report+="\"timestamp\":$(date +%s)," \
-    report+="\"location\":\"$location\"," \
-    report+="\"errors\":$errors," \
-    report+="\"warnings\":$warnings," \
-    report+="\"stderr\":\"$stderr\"}"
-
-    echo "$report" > "$MONITOR"
+system_poweroff() {
+    echo o > /proc/sysrq-trigger
 }
 
 get_base_distro() {
@@ -134,6 +103,8 @@ get_base_distro() {
         echo "suse"
     elif [ -e "$root_dir/etc/gentoo-release" ]; then
         echo "gentoo"
+    elif [ -e "$root_dir/etc/arch-release" ]; then
+        echo "arch"
     else
         warn "Unknown base distro."
     fi
@@ -163,6 +134,8 @@ get_distro() {
         echo "suse"
     elif [ -e "$root_dir/etc/gentoo-release" ]; then
         echo "gentoo"
+    elif [ -e "$root_dir/etc/arch-release" ]; then
+        echo "arch"
     else
         warn "Unknown distro."
     fi
@@ -175,7 +148,7 @@ get_partition_table() {
     # secondary gpt is not it the end of the disk, and a warning that has to
     # do with the "Last Usable LBA" entry in gpt.
     if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
-        log_error "Unable to read partition table for device \`${dev}'"
+        log_error "Unable to read partition table for device \`${dev}'. The image seems corrupted."
     fi
 
     echo "$output"
@@ -367,6 +340,18 @@ get_last_free_sector() {
     fi
 }
 
+get_unattend() {
+    local target="$1"
+
+    # Workaround to search for $target/Unattend.xml in an case insensitive way.
+    exists=$(find "$target"/ -maxdepth 1 -iname unattend.xml)
+    if [ $(wc -l <<< "$exists") -gt 1 ]; then
+        log_error "Found multiple Unattend.xml files in the image:" $exists
+    fi
+
+    echo "$exists"
+}
+
 cleanup() {
     # if something fails here, it shouldn't call cleanup again...
     trap - EXIT
@@ -403,19 +388,17 @@ task_cleanup() {
     rc=$?
 
     if [ $rc -eq 0 ]; then
-       report_end_task
-    else
-       report_error
+       report_task_end
     fi
 
     cleanup
 }
 
 check_if_excluded() {
-
-    local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
+    local name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
+    local exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
     if [ -n "${!exclude}" ]; then
-        warn "Task $PROGNAME was excluded and will not run."
+        warn "Task ${PROGNAME:2} was excluded and will not run."
         exit 0
     fi