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