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