63b0b6cf6c7617bf25cfada49608c9dd95d2cea2
[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     exit 1
23 }
24
25 # set -e
26 #default values
27 image_appendix="test"
28 gs_url="artifacts.opnfv.org/samplevnf/images"
29 verify_only=0
30 while getopts i:g:v flag
31 do
32     case "${flag}" in
33         i) image_appendix=${OPTARG};;
34         g) gs_url=${OPTARG};;
35         v) verify_only=1;;
36         *) usage;exit 1;;
37     esac
38 done
39 echo "gs_url: $gs_url";
40 echo "Verify only: $verify_only";
41 image_name=rapid-${image_appendix}
42 echo "image name: $image_name.qcow2"
43
44 # install diskimage-builder
45
46 python3 -m venv dib-rapid-venv
47 . dib-rapid-venv/bin/activate
48 pip3 install --upgrade pip
49 pip3 install six
50 pip3 install diskimage-builder
51 pip3 install gsutil
52
53 echo "Checking if image exists in google storage..."
54 if  command -v gsutil >/dev/null; then
55     if gsutil -q stat gs://$gs_url/$image_name.qcow2; then
56         echo "Image already exists at http://$gs_url/$image_name.qcow2"
57     fi
58     echo "Starting build..."
59     echo
60 else
61     echo "Cannot check image availability in OPNFV artifact repository (gsutil not available)"
62 fi
63
64 # Add rapid elements directory to the DIB elements path
65 export ELEMENTS_PATH=`pwd`/elements
66 # canned user/password for direct login
67 export DIB_DEV_USER_USERNAME=prox
68 export DIB_DEV_USER_PASSWORD=prox
69 export DIB_DEV_USER_PWDLESS_SUDO=Y
70 # Set the data sources to have ConfigDrive only
71 export DIB_CLOUD_INIT_DATASOURCES="Ec2, ConfigDrive, OpenStack"
72 # Use ELRepo to have latest kernel
73 export DIB_USE_ELREPO_KERNEL=True
74 echo "Building $image_name.qcow2..."
75 time disk-image-create -o $image_name centos7 cloud-init rapid vm
76
77 ls -l $image_name.qcow2
78
79
80 if [ $verify_only -eq 1 ]; then
81     echo "Image verification SUCCESS"
82     echo "NO upload to google storage (-v)"
83 else
84     if command -v gsutil >/dev/null; then
85         echo "Uploading $image_name.qcow2..."
86         gsutil cp $image_name.qcow2 gs://$gs_url/$image_name.qcow2
87         echo "You can access image at http://$gs_url/$image_name.qcow2"
88     else
89         echo "Cannot upload new image to the OPNFV artifact repository (gsutil not available)"
90         exit 1
91     fi
92 fi