New test case: gen versus swap via gateway
[samplevnf.git] / rapidvm / dib / build-image.sh
1 #!/usr/bin/env bash
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # A shell script to build the PROX VM image using diskimage-builder
16 #
17 usage() {
18     echo "Usage: $0 [-i image_name] [-g gs-url] [-v]"
19     echo "   -i image_appendix    image name to be pushed to google storage)"
20     echo "   -g gs_url            url to store the image"
21     echo "   -v                   verify only (build but do not push to google storage)"
22     echo "   -w cache             cache directory for disk-image-create"
23     exit 1
24 }
25
26 # set -e
27 #default values
28 image_appendix="test"
29 workspace="/home/jenkins-ci/opnfv/slave_root/workspace"
30 gs_url="artifacts.opnfv.org/samplevnf/images"
31 verify_only=0
32 while getopts i:g:vw: flag
33 do
34     case "${flag}" in
35         i) image_appendix=${OPTARG};;
36         g) gs_url=${OPTARG};;
37         v) verify_only=1;;
38         w) workspace=${OPTARG};;
39         *) usage;exit 1;;
40     esac
41 done
42 echo "gs_url: $gs_url";
43 echo "Verify only: $verify_only";
44 image_name=rapid-${image_appendix}
45 echo "image name: $image_name.qcow2"
46 echo "workspace: $workspace"
47
48  install diskimage-builder
49 python3 -m venv dib-rapid-venv
50 . dib-rapid-venv/bin/activate
51 pip3 install --upgrade pip
52 pip3 install six
53 pip3 install diskimage-builder
54 pip3 install gsutil
55
56 echo "Checking if image exists in google storage..."
57 if  command -v gsutil >/dev/null; then
58     if gsutil -q stat gs://$gs_url/$image_name.qcow2; then
59         echo "Image already exists at http://$gs_url/$image_name.qcow2"
60     fi
61     echo "Starting build..."
62     echo
63 else
64     echo "Cannot check image availability in OPNFV artifact repository (gsutil not available)"
65 fi
66
67 # Add rapid elements directory to the DIB elements path
68 export ELEMENTS_PATH=`pwd`/elements
69 # canned user/password for direct login
70 export DIB_DEV_USER_USERNAME=prox
71 export DIB_DEV_USER_PASSWORD=prox
72 export DIB_DEV_USER_PWDLESS_SUDO=Y
73 # Set the data sources to have ConfigDrive only
74 export DIB_CLOUD_INIT_DATASOURCES="Ec2, ConfigDrive, OpenStack"
75 # Use ELRepo to have latest kernel
76 export DIB_USE_ELREPO_KERNEL=True
77 echo "Building $image_name.qcow2..."
78 cache=$workspace/cache
79 mkdir $cache
80 time disk-image-create -o $image_name --image-cache $cache centos7 cloud-init rapid vm
81
82 ls -l $image_name.qcow2
83
84
85 if [ $verify_only -eq 1 ]; then
86     echo "Image verification SUCCESS"
87     echo "NO upload to google storage (-v)"
88 else
89     if command -v gsutil >/dev/null; then
90         echo "Uploading $image_name.qcow2..."
91         gsutil cp $image_name.qcow2 gs://$gs_url/$image_name.qcow2
92         echo "You can access image at http://$gs_url/$image_name.qcow2"
93     else
94         echo "Cannot upload new image to the OPNFV artifact repository (gsutil not available)"
95         exit 1
96     fi
97 fi
98 deactivate
99 rm -r dib-rapid-venv