Revision 2a0ab295

b/snf-image-helper/common.sh
17 17
# 02110-1301, USA.
18 18

  
19 19
RESULT=/dev/ttyS1
20
MONITOR=/dev/ttyS2
21

  
20 22
FLOPPY_DEV=/dev/fd0
21 23
PROGNAME=$(basename $0)
22 24

  
......
35 37
CHNTPW=chntpw
36 38

  
37 39
CLEANUP=( )
40
ERRORS=( )
41
WARNINGS=( )
38 42

  
39 43
add_cleanup() {
40 44
    local cmd=""
......
43 47
}
44 48

  
45 49
log_error() {
50
    ERRORS+=("$@")
46 51
    echo "ERROR: $@" | tee $RESULT >&2
47 52
    exit 1
48 53
}
49 54

  
50 55
warn() {
56
    WARNINGS+=("$@")
51 57
    echo "Warning: $@" >&2
52 58
}
53 59

  
60
report_start_task() {
61

  
62
    local id="$SNF_IMAGE_HOSTNAME"
63
    local type="ganeti-start-task"
64
    local timestamp=$(date +%s)
65
    local name="${PROGNAME}"
66

  
67
    report="{\"id\":\"$id\","
68
    report+="\"type\":\"$type\"," \
69
    report+="\"timestamp\":$(date +%s)," \
70
    report+="\"name\":\"$name\"}"
71

  
72
    echo "$report" > "$MONITOR"
73
}
74

  
75
json_list() {
76
    declare -a items=("${!1}")
77
    report="["
78
    for item in "${items[@]}"; do
79
        report+="\"$(sed 's/"/\\"/g' <<< "$item")\","
80
    done
81
    if [ ${#report} -gt 1 ]; then
82
        # remove last comma(,)
83
        report="${report%?}"
84
    fi
85
    report+="]"
86

  
87
    echo "$report"
88
}
89

  
90
report_end_task() {
91

  
92
    local id="$SNF_IMAGE_HOSTNAME"
93
    local type="ganeti-end-task"
94
    local timestam=$(date +%s)
95
    local name=${PROGNAME}
96
    local warnings=$(json_list WARNINGS[@])
97

  
98
    report="{\"id\":\"$id\","
99
    report+="\"type\":\"$type\"," \
100
    report+="\"timestamp\":$(date +%s)," \
101
    report+="\"name\":\"$name\"," \
102
    report+="\"warnings\":\"$warnings\"}"
103

  
104
    echo "$report" > "$MONITOR"
105
}
106

  
107
report_error() {
108
    local id="$SNF_IMAGE_HOSTNAME"
109
    local type="ganeti-error"
110
    local timestamp=$(date +%s)
111
    local location="${PROGNAME}"
112
    local errors=$(json_list ERRORS[@])
113
    local warnings=$(json_list WARNINGS[@])
114
    local stderr="$(cat "$STDERR_FILE" | sed 's/"/\\"/g')"
115

  
116
    report="{\"id\":\"$id\","
117
    report+="\"type\":\"$type\"," \
118
    report+="\"timestamp\":$(date +%s)," \
119
    report+="\"location\":\"$location\"," \
120
    report+="\"errors\":$errors," \
121
    report+="\"warnings\":$warnings," \
122
    report+="\"stderr\":\"$stderr\"}"
123

  
124
    echo "$report" > "$MONITOR"
125
}
126

  
54 127
get_base_distro() {
55 128
    local root_dir=$1
56 129

  
......
329 402
  fi
330 403
}
331 404

  
405
task_cleanup() {
406
    rc=$?
407

  
408
    if [ $rc -eq 0 ]; then
409
       report_end_task 
410
    else
411
       report_error
412
    fi
413

  
414
    cleanup
415
}
416

  
332 417
check_if_excluded() {
333 418

  
334 419
    local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
......
343 428
trap cleanup EXIT
344 429
set -o pipefail
345 430

  
431
STDERR_FILE=$(mktemp)
432
add_cleanup rm -f "$STDERR_FILE"
433
exec 2> >(tee -a "$STDERR_FILE" >&2)
434

  
346 435
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
b/snf-image-helper/tasks/10FixPartitionTable.in
26 26
set -e
27 27
. "@commondir@/common.sh"
28 28

  
29
trap task_cleanup EXIT
30
report_start_task
29 31
# Check if the task should be prevented from running.
30 32
check_if_excluded
31 33

  
b/snf-image-helper/tasks/20FilesystemResizeUnmounted.in
27 27
set -e
28 28
. "@commondir@/common.sh"
29 29

  
30
trap task_cleanup EXIT
31
report_start_task
30 32
# Check if the task should be prevented from running.
31 33
check_if_excluded
32 34

  
b/snf-image-helper/tasks/30MountImage.in
26 26
set -e
27 27
. "@commondir@/common.sh"
28 28

  
29
trap task_cleanup EXIT
30
report_start_task
31

  
29 32
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
30 33
    log_error "Target dir:\`$SNF_IMAGE_TARGET' is missing"
31 34
fi
b/snf-image-helper/tasks/40AddSwap.in
27 27
set -e
28 28
. "@commondir@/common.sh"
29 29

  
30
trap task_cleanup EXIT
31
report_start_task
32

  
30 33
# Check if the task should be prevented from running.
31 34
check_if_excluded
32 35

  
b/snf-image-helper/tasks/40DeleteSSHKeys.in
27 27
set -e
28 28
. "@commondir@/common.sh"
29 29

  
30
trap task_cleanup EXIT
31
report_start_task
32

  
30 33
# Check if the task should be prevented from running.
31 34
check_if_excluded
32 35

  
b/snf-image-helper/tasks/40DisableRemoteDesktopConnections.in
34 34
set -e
35 35
. "@commondir@/common.sh"
36 36

  
37
trap task_cleanup EXIT
38
report_start_task
39

  
37 40
# Check if the task should be prevented from running.
38 41
check_if_excluded
39 42

  
b/snf-image-helper/tasks/40InstallUnattend.in
27 27
set -e
28 28
. "@commondir@/common.sh"
29 29

  
30
trap task_cleanup EXIT
31
report_start_task
32

  
30 33
if [ -z "$SNF_IMAGE_TARGET" ]; then
31 34
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"
32 35
fi
b/snf-image-helper/tasks/40SELinuxAutorelabel.in
27 27
set -e
28 28
. "@commondir@/common.sh"
29 29

  
30
trap task_cleanup EXIT
31
report_start_task
32

  
30 33
# Check if the task should be prevented from running.
31 34
check_if_excluded
32 35

  
b/snf-image-helper/tasks/50AssignHostname.in
27 27
set -e
28 28
. "@commondir@/common.sh"
29 29

  
30
trap task_cleanup EXIT
31
report_start_task
32

  
30 33
# Check if the task should be prevented from running.
31 34
check_if_excluded
32 35

  
b/snf-image-helper/tasks/50ChangePassword.in
27 27
set -e
28 28
. "@commondir@/common.sh"
29 29

  
30
trap task_cleanup EXIT
31
report_start_task
32

  
30 33
# Check if the task should be prevented from running.
31 34
check_if_excluded
32 35

  
b/snf-image-helper/tasks/50EnforcePersonality.in
28 28

  
29 29
. "@commondir@/common.sh"
30 30

  
31
trap task_cleanup EXIT
32
report_start_task
33

  
31 34
# Check if the task should be prevented from running.
32 35
check_if_excluded
33 36

  
b/snf-image-helper/tasks/50FilesystemResizeMounted.in
27 27
set -e
28 28
. "@commondir@/common.sh"
29 29

  
30
trap task_cleanup EXIT
31
report_start_task
32

  
30 33
# Check if the task should be prevented from running.
31 34
check_if_excluded
32 35

  
b/snf-image-helper/tasks/80UmountImage.in
27 27
set -e
28 28
. "@commondir@/common.sh"
29 29

  
30
trap task_cleanup EXIT
31
report_start_task
32

  
30 33
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
31 34
	log_error "Target dir:\`$SNF_IMAGE_TARGET' is missing"
32 35
fi
b/snf-image-host/create
149 149
$TIMELIMIT -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \
150 150
    kvm -runas "$HELPER_USER" -drive file="$snapshot" \
151 151
    -drive file="$blockdev",format=raw,if=virtio,cache=none \
152
    -boot c -serial stdio -serial file:"$result_file" \
152
    -boot c -serial stdio -serial "file:$(printf "%q" "$result_file")" \
153
    -serial "file:$(printf "%q" "$monitor_pipe")" \
153 154
    -fda "$floppy" -vga none -nographic -parallel none -monitor null \
154 155
    -kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
155 156
    -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8 snf_image_activate_helper" \

Also available in: Unified diff