tc053 kill haproxy wrong mismatch
[yardstick.git] / tools / yardstick-img-lxd-modify
1 #!/bin/bash
2
3 ##############################################################################
4 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # yardstick-img-lxd-modify - download and modify a Ubuntu cloud image
13 #
14 # The actual customization is done by a script passed with an absolute path as
15 # the only single argument. The command needs to be invoked as sudo
16 #
17 # Example invocation:
18 # yardstick-img-lxd-modify /home/yardstick/tools/ubuntu-server-cloudimg-modify.sh
19 #
20 # Warning: the script will create files by default in:
21 #   /tmp/workspace/yardstick
22 # the files will be owned by root!
23 #
24 # TODO: image resize is needed if the base image is too small
25 #
26
27 set -e
28 set -x
29
30 die() {
31     echo "error: $1" >&2
32     exit 1
33 }
34
35 test $# -eq 1 -o $# -eq 2 || die "no image specific script as argument"
36 test $(id -u) -eq 0 || die "should invoke using sudo"
37
38 cmd=$1
39 RELEASE=$2
40 test -x $cmd
41 mountdir="/mnt/yardstick"
42 workspace=${WORKSPACE:-"/tmp/workspace/yardstick"}
43 host=${HOST:-"cloud-images.ubuntu.com"}
44 release=${RELEASE:-"xenial"}
45 image_path="${release}/current/${release}-server-cloudimg-amd64-root.tar.gz"
46 image_url=${IMAGE_URL:-"https://${host}/${image_path}"}
47 sha256sums_path="${release}/current/SHA256SUMS"
48 sha256sums_url=${SHA256SUMS_URL:-"https://${host}/${sha256sums_path}"}
49
50 imgfile="${workspace}/yardstick-image.tar.gz"
51 filename=$(basename $image_url)
52
53 # download and checksum base image, conditionally if local copy is outdated
54 download() {
55     test -d $workspace || mkdir -p $workspace
56     cd $workspace
57     rm -f SHA256SUMS # always download the checksum file to a detect stale image
58     wget $sha256sums_url
59     test -e $filename || wget -nc --progress=dot:giga $image_url
60     grep $filename SHA256SUMS | sha256sum -c ||
61     if [ $? -ne 0 ]; then
62         rm $filename
63         wget -nc --progress=dot:giga $image_url
64         grep $filename SHA256SUMS | sha256sum -c
65     fi
66     cd -
67 }
68
69 # extract files
70 setup() {
71     mkdir -p $mountdir
72
73     cp $cmd $mountdir/$(basename $cmd)
74
75     cd $workspace
76     tar zxvf $filename -C $mountdir
77 }
78
79 # modify image running a script using in a chrooted environment
80 modify() {
81     # resolv.conf does not exist in base image, pass nameserver value from host
82     nameserver_ip=$(grep -m 1 '^nameserver' \
83         /etc/resolv.conf | awk '{ print $2 '})
84
85     # prevent init scripts from running during install
86     echo $'#!/bin/sh\nexit 101' >$mountdir/usr/sbin/policy-rc.d
87     chmod a+x $mountdir/usr/sbin/policy-rc.d
88
89     chroot $mountdir /$(basename $cmd) $nameserver_ip
90
91     rm -rf $mountdir/usr/sbin/policy-rc.d
92
93     tar zcvf $(basename $imgfile) $mountdir/
94 }
95
96 # cleanup the image
97 cleanup() {
98     rm -rf $mountdir
99 }
100
101 exitcode=""
102 error_trap()
103 {
104     local rc=$?
105
106     set +e
107
108     if [ -z "$exitcode" ]; then
109         exitcode=$rc
110     fi
111
112     dmesg -T | tail -50
113
114     cleanup
115
116     echo "Image build failed with $exitcode"
117
118     exit $exitcode
119 }
120
121 main() {
122     cleanup
123
124     trap "error_trap" EXIT SIGTERM
125
126     download
127     setup
128     modify
129
130     trap - EXIT SIGTERM
131     cleanup
132
133     echo "the modified image is found here: $imgfile"
134 }
135
136 main