c595f6b123d24ec1fbc20df89e5494741967252d
[nfvbench.git] / nfvbenchvm / dib / build-image.sh
1 #!/bin/bash
2 #
3 # A shell script to build the VPP VM image or NFVbench+TRex VM image using diskinage-builder
4 #
5 # The following packages must be installed prior to using this script:
6 # sudo apt-get -y install python-virtualenv qemu-utils kpartx
7
8 usage() {
9     echo "Usage: $0 [-l] [-g] [-v]"
10     echo "   -l    build NFVbench loop VM image"
11     echo "   -g    build NFVbench generator image"
12     echo "   -v    verify only (build but do not push to google storage)"
13     exit 1
14 }
15
16 verify_only=0
17 generator_only=0
18 loopvm_only=0
19 __prefix__=""
20 # ----------------------------------------------------------------------------
21 # Parse command line options and configure the script
22 # ----------------------------------------------------------------------------
23
24 while getopts ":hglv" opt; do
25     case $opt in
26         h)
27             usage
28             exit 0
29             ;;
30         g)
31             generator_only=1
32             ;;
33         l)
34             loopvm_only=1
35             ;;
36         v)
37             verify_only=1
38             ;;
39         ?)
40             usage
41             exit 1
42             ;;
43     esac
44 done
45
46 set -e
47
48 # Artifact URL
49 gs_url=artifacts.opnfv.org/nfvbench/images
50
51 # image version number
52 __version__=0.13
53 loopvm_image_name=nfvbenchvm_centos-$__version__
54 generator_image_name=nfvbenchvm_centos-generator-$__version__
55
56
57
58 function cleanup_image {
59     # if image exists skip building
60     echo "Checking if image exists in google storage..."
61     if  command -v gsutil >/dev/null; then
62         if gsutil -q stat gs://$gs_url/$1.qcow2; then
63             gsutil rm http://$gs_url/$1.qcow2
64             echo "Image is deleted"
65             exit 0
66         fi
67         echo "Image does not exist in google storage, starting build..."
68         echo
69     else
70         echo "Cannot check image availability in OPNFV artifact repository (gsutil not available)"
71     fi
72 }
73
74
75 function build_image {
76     # if image exists skip building
77     echo "Checking if image exists in google storage..."
78     if  command -v gsutil >/dev/null; then
79         if gsutil -q stat gs://$gs_url/$1.qcow2; then
80             echo "Image already exists at http://$gs_url/$1.qcow2"
81             echo "Build is skipped"
82             exit 0
83         fi
84         echo "Image does not exist in google storage, starting build..."
85         echo
86     else
87         echo "Cannot check image availability in OPNFV artifact repository (gsutil not available)"
88     fi
89
90     # check if image is already built locally
91     if [ -f $1.qcow2 ]; then
92         echo "Image $1.qcow2 already exists locally"
93     else
94
95         # install diskimage-builder
96         if [ -d dib-venv ]; then
97            . dib-venv/bin/activate
98         else
99            virtualenv dib-venv
100            . dib-venv/bin/activate
101            pip install diskimage-builder
102         fi
103
104         # Add nfvbenchvm_centos elements directory to the DIB elements path
105         export ELEMENTS_PATH=`pwd`/elements
106
107         # canned user/password for direct login
108         export DIB_DEV_USER_USERNAME=nfvbench
109         export DIB_DEV_USER_PASSWORD=nfvbench
110         export DIB_DEV_USER_PWDLESS_SUDO=Y
111
112         # Set the data sources to have ConfigDrive only
113         export DIB_CLOUD_INIT_DATASOURCES="ConfigDrive"
114
115         # Configure VPP REPO
116         export DIB_YUM_REPO_CONF=$ELEMENTS_PATH/nfvbenchvm/fdio-release.repo
117
118         # Use ELRepo to have latest kernel
119         # only for loop vm image
120         if [ $1 = $loopvm_image_name ]; then
121            export DIB_USE_ELREPO_KERNEL=True
122            export DIB_DEV_IMAGE=loopvm
123         else
124            export DIB_USE_ELREPO_KERNEL=False
125            export DIB_DEV_IMAGE=generator
126            # get current git branch to build image with current code
127            export GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
128            # retrieve TREX_VER from Dockerfile
129            export TREX_VER=$(awk '/ENV TREX_VER/ {print $3}' ../../docker/Dockerfile | sed 's/"//g' | sed 's/\r//g')
130         fi
131
132         echo "Building $1.qcow2..."
133         time disk-image-create -o $1 centos7 nfvbenchvm
134     fi
135
136     ls -l $1.qcow2
137
138     if [ $verify_only -eq 1 ]; then
139         echo "Image verification SUCCESS"
140         echo "NO upload to google storage (-v)"
141     else
142         if command -v gsutil >/dev/null; then
143             echo "Uploading $1.qcow2..."
144             gsutil cp $1.qcow2 gs://$gs_url/$1.qcow2
145             echo "You can access to image at http://$gs_url/$1.qcow2"
146         else
147             echo "Cannot upload new image to the OPNFV artifact repository (gsutil not available)"
148             exit 1
149         fi
150     fi
151 }
152
153
154 if [ ! $generator_only -eq 1 ] && [ ! $loopvm_only -eq 1 ]; then
155    echo "cleanup old loop VM image (wrong content)"
156    cleanup_image $loopvm_image_name
157    echo "Build loop VM image"
158    build_image $loopvm_image_name
159    echo "cleanup old generator VM image (wrong content)"
160    cleanup_image $generator_image_name
161    echo "Build generator image"
162    build_image $generator_image_name
163 else
164     if [ $loopvm_only -eq 1 ]; then
165        echo "Build loop VM image"
166        build_image $loopvm_image_name
167     fi
168     if [ $generator_only -eq 1 ]; then
169        echo "Build generator image"
170        build_image $generator_image_name
171     fi
172 fi