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() {
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
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
# 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"
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
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