Add Centos 7 image to CSIT.
[csit.git] / resources / tools / disk-image-builder / centos / scripts-local / upload-image-to-virl.sh
1 #!/bin/sh
2
3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 if [ "$1" = "" ]
17 then
18   echo "Syntax: $0 <path to image file>"
19   echo
20   echo "Environment variables that are required:"
21   echo " VIRL_USER, VIRL_PASSWORD - VIRL username and password"
22   echo " VIRL_IMAGE_SUBTYPE       - Image subtype to use (most likely 'server')"
23   echo " VIRL_IMAGE_NAME          - The intended name for the image in VIRL"
24   exit 1
25 fi
26
27 VIRL_IMAGE_FILE=$1
28
29 if [ "$VIRL_USER" = "" ] || [ "$VIRL_PASSWORD" = "" ]
30 then
31   echo "VIRL user or password not defined, not uploading image to VIRL."
32   echo "Define VIRL_USER and VIRL_PASSWORD environment variables if image upload"
33   echo "to VIRL is intended."
34   exit 0
35 fi
36
37 if [ "$VIRL_IMAGE_SUBTYPE" = "" ] || [ "$VIRL_IMAGE_NAME" = "" ]
38 then
39   echo "VIRL_IMAGE_SUBTYPE, VIRL_IMAGE_NAME must both be defined"
40   echo "variables must all be set."
41   exit 1
42 fi
43
44 if [ ! -f $VIRL_IMAGE_FILE ]
45 then
46   echo "VIRL image file $VIRL_IMAGE_FILE not found"
47   exit 1
48 fi
49
50 echo Uploading file $VIRL_IMAGE_FILE to VIRL
51 echo as $VIRL_IMAGE_NAME
52
53 export VIRL_IMAGE_NAME
54
55 existing_image_id=$(virl_uwm_client --quiet -u ${VIRL_USER} -p ${VIRL_PASSWORD} \
56   image-info | \
57   grep -E "^              u'name'|^              u'id'" | \
58   grep -B 1 "u'${VIRL_IMAGE_SUBTYPE}-${VIRL_IMAGE_NAME}'" | \
59   grep -E "^              u'id'" | \
60   cut -f 4 -d "'")
61
62 if [ "${existing_image_id}" = "" ]
63 then
64   echo Image does not exist yet
65 else
66   echo Image exists with ID $existing_image_id
67   virl_uwm_client --quiet -u ${VIRL_USER} -p ${VIRL_PASSWORD} image-delete \
68     --id ${existing_image_id}
69 fi
70
71 virl_uwm_client -u ${VIRL_USER} -p ${VIRL_PASSWORD} image-create --subtype ${VIRL_IMAGE_SUBTYPE} --version ${VIRL_IMAGE_NAME} --image-on-server ${VIRL_IMAGE_FILE}