NFVBENCH-196: New NFVbench image for generator part (nfvbench and TRex codes inside VM)
[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 function build_image {
57     # if image exists skip building
58     echo "Checking if image exists in google storage..."
59     if  command -v gsutil >/dev/null; then
60         if gsutil -q stat gs://$gs_url/$1.qcow2; then
61             echo "Image already exists at http://$gs_url/$1.qcow2"
62             echo "Build is skipped"
63             exit 0
64         fi
65         echo "Image does not exist in google storage, starting build..."
66         echo
67     else
68         echo "Cannot check image availability in OPNFV artifact repository (gsutil not available)"
69     fi
70
71     # check if image is already built locally
72     if [ -f $1.qcow2 ]; then
73         echo "Image $1.qcow2 already exists locally"
74     else
75
76         # install diskimage-builder
77         if [ -d dib-venv ]; then
78            . dib-venv/bin/activate
79         else
80            virtualenv dib-venv
81            . dib-venv/bin/activate
82            pip install diskimage-builder
83         fi
84
85         # Add nfvbenchvm_centos elements directory to the DIB elements path
86         export ELEMENTS_PATH=`pwd`/elements
87
88         # canned user/password for direct login
89         export DIB_DEV_USER_USERNAME=nfvbench
90         export DIB_DEV_USER_PASSWORD=nfvbench
91         export DIB_DEV_USER_PWDLESS_SUDO=Y
92
93         # Set the data sources to have ConfigDrive only
94         export DIB_CLOUD_INIT_DATASOURCES="ConfigDrive"
95
96         # Configure VPP REPO
97         export DIB_YUM_REPO_CONF=$ELEMENTS_PATH/nfvbenchvm/fdio-release.repo
98
99         # Use ELRepo to have latest kernel
100         # only for loop vm image
101         if [ $1 = $loopvm_image_name ]; then
102            export DIB_USE_ELREPO_KERNEL=True
103            export DIB_DEV_IMAGE=loopvm
104         else
105            export DIB_USE_ELREPO_KERNEL=False
106            export DIB_DEV_IMAGE=generator
107            # get current git branch to build image with current code
108            export GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
109            # retrieve TREX_VER from Dockerfile
110            export TREX_VER=$(awk '/ENV TREX_VER/ {print $3}' ../../docker/Dockerfile | sed 's/"//g' | sed 's/\r//g')
111         fi
112
113         echo "Building $1.qcow2..."
114         time disk-image-create -o $1 centos7 nfvbenchvm
115     fi
116
117     ls -l $1.qcow2
118
119     if [ $verify_only -eq 1 ]; then
120         echo "Image verification SUCCESS"
121         echo "NO upload to google storage (-v)"
122     else
123         if command -v gsutil >/dev/null; then
124             echo "Uploading $1.qcow2..."
125             gsutil cp $1.qcow2 gs://$gs_url/$1.qcow2
126             echo "You can access to image at http://$gs_url/$1.qcow2"
127         else
128             echo "Cannot upload new image to the OPNFV artifact repository (gsutil not available)"
129             exit 1
130         fi
131     fi
132 }
133
134
135 if [ ! $generator_only -eq 1 ] && [ ! $loopvm_only -eq 1 ]; then
136    echo "Build loop VM image"
137    build_image $loopvm_image_name
138    echo "Build generator image"
139    build_image $generator_image_name
140 else
141     if [ $loopvm_only -eq 1 ]; then
142        echo "Build loop VM image"
143        build_image $loopvm_image_name
144     fi
145     if [ $generator_only -eq 1 ]; then
146        echo "Build generator image"
147        build_image $generator_image_name
148     fi
149 fi