Using disk image builder to create rapid VM
[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 [-v]"
19     echo "   -v    verify only (build but do not push to google storage)"
20     exit 1
21 }
22
23 # Takes only 1 optional argument
24 if [ $# -gt 1 ]; then
25    usage
26 fi
27 verify_only=0
28
29 if [ $# -eq 1 ]; then
30    if [ $1 = "-v" ]; then
31         verify_only=1
32     else
33         usage
34     fi
35 fi
36 set -e
37
38 # Artifact URL
39 gs_url=artifacts.opnfv.org/samplevnf/images
40
41 # image version number
42 __version__=0.01
43 image_name=rapid-$__version__
44
45 # if image exists skip building
46 echo "Checking if image exists in google storage..."
47 if  command -v gsutil >/dev/null; then
48     if gsutil -q stat gs://$gs_url/$image_name.qcow2; then
49         echo "Image already exists at http://$gs_url/$image_name.qcow2"
50         echo "Build is skipped"
51         exit 0
52     fi
53     echo "Image does not exist in google storage, starting build..."
54     echo
55 else
56     echo "Cannot check image availability in OPNFV artifact repository (gsutil not available)"
57 fi
58
59 # check if image is already built locally
60 if [ -f $image_name.qcow2 ]; then
61     echo "Image $image_name.qcow2 already exists locally"
62 else
63
64     # install diskimage-builder
65     if [ -d dib-venv ]; then
66         . dib-venv/bin/activate
67     else
68         virtualenv dib-venv
69         . dib-venv/bin/activate
70         pip install diskimage-builder
71     fi
72     # Add rapid elements directory to the DIB elements path
73     export ELEMENTS_PATH=`pwd`/elements
74     # canned user/password for direct login
75     export DIB_DEV_USER_USERNAME=prox
76     export DIB_DEV_USER_PASSWORD=prox
77     export DIB_DEV_USER_PWDLESS_SUDO=Y
78     # Set the data sources to have ConfigDrive only
79     export DIB_CLOUD_INIT_DATASOURCES="Ec2, ConfigDrive, OpenStack"
80     # Use ELRepo to have latest kernel
81     export DIB_USE_ELREPO_KERNEL=True
82     echo "Building $image_name.qcow2..."
83     time disk-image-create -o $image_name centos7 cloud-init rapid vm
84 fi
85
86 ls -l $image_name.qcow2
87
88
89 if [ $verify_only -eq 1 ]; then
90     echo "Image verification SUCCESS"
91     echo "NO upload to google storage (-v)"
92 else
93     if command -v gsutil >/dev/null; then
94         echo "Uploading $image_name.qcow2..."
95         gsutil cp $image_name.qcow2 gs://$gs_url/$image_name.qcow2
96         echo "You can access to image at http://$gs_url/$image_name.qcow2"
97     else
98         echo "Cannot upload new image to the OPNFV artifact repository (gsutil not available)"
99         exit 1
100     fi
101 fi