blob: b6c5de6488012e0be3b2ccbcc982cfc942598f93 [file] [log] [blame]
tiernof7aa8c42016-09-06 16:43:04 +02001#!/bin/bash
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
tierno9a61c6b2016-09-08 10:57:02 +02005# This file is part of openvim
tiernof7aa8c42016-09-06 16:43:04 +02006# All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
24#This script can be used as a basic test of openvim
25#stopping on an error
26#WARNING: It destroy the database content
27
tiernoc4864732016-09-20 11:07:00 +000028DIRNAME=$(readlink -f ${BASH_SOURCE[0]})
29DIRNAME=$(dirname $DIRNAME )
tiernof7aa8c42016-09-06 16:43:04 +020030
31function usage(){
32 echo -e "usage: ${BASH_SOURCE[0]} [OPTIONS] <action>\n Deletes openvim content and add fake hosts, networks"
33 echo -e " <action> is a list of the following items (by default 'reset create')"
34 echo -e " reset reset the openvim database content"
35 echo -e " create creates fake hosts and networks"
36 echo -e " delete delete created items"
37 echo -e " delete-all delete vms. flavors, images, ..."
38 echo -e " OPTIONS:"
39 echo -e " -f --force : does not prompt for confirmation"
40 echo -e " -d --delete : same to action delete-all"
tiernoc4864732016-09-20 11:07:00 +000041 echo -e " -p --port PORT : port to start openvim service"
42 echo -e " -P --admin-port PORT : administrator port to start openvim service"
43 echo -e " --screen-name NAME : screen name to launch openvim (default vim)"
44 echo -e " --dbname NAME : database name to use (default vim_db)"
tiernof7aa8c42016-09-06 16:43:04 +020045 echo -e " --insert-bashrc insert the created tenant variables at"
46 echo -e " ~/.bashrc to be available by openvim CLI"
47 echo -e " -h --help : shows this help"
48}
49
50function is_valid_uuid(){
51 echo "$1" | grep -q -E '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$' && return 0
52 return 1
53}
54
55
56#detect if is called with a source to use the 'exit'/'return' command for exiting
57[[ ${BASH_SOURCE[0]} != $0 ]] && _exit="return" || _exit="exit"
58
tiernof7aa8c42016-09-06 16:43:04 +020059
tiernoc4864732016-09-20 11:07:00 +000060#process options
tierno49f2e852017-05-05 15:51:47 +020061source ${DIRNAME}/get-options.sh \
62 "force:f delete:d delete-all port:p= admin-port:P= screen-name= help:h dbname= insert-bashrc" $* || $_exit 1
tiernoc4864732016-09-20 11:07:00 +000063
tiernoc4864732016-09-20 11:07:00 +000064#check correct arguments
65action_list=""
66for param in $params
tiernof7aa8c42016-09-06 16:43:04 +020067do
tiernoc4864732016-09-20 11:07:00 +000068 if [[ "$param" == reset ]] || [[ "$param" == create ]] || [[ "$param" == delete ]] || [[ "$param" == delete-all ]]
tiernof7aa8c42016-09-06 16:43:04 +020069 then
tiernob0b9d082016-10-04 08:24:42 +000070 action_list="$action_list $param"
tiernof7aa8c42016-09-06 16:43:04 +020071 continue
tiernoc4864732016-09-20 11:07:00 +000072 else
73 echo "invalid argument '$param'? Type -h for help" >&2 && $_exit 1
tiernof7aa8c42016-09-06 16:43:04 +020074 fi
tiernof7aa8c42016-09-06 16:43:04 +020075done
76
tiernoc4864732016-09-20 11:07:00 +000077#help
78[[ -n "$option_help" ]] && usage && $_exit 0
79
80#check numeric values for port
tierno24ca7a82017-05-08 15:45:32 +020081[[ -n "$option_port" ]] && ( [[ "$option_port" -lt 1 ]] || [[ "$option_port" -gt 65535 ]] ) &&
82 echo "Option '-p' or '--port' requires a valid numeric argument" >&2 && $_exit 1
83[[ -n "$option_admin_port" ]] && ( [[ "$option_admin_port" -lt 1 ]] || [[ "$option_admin_port" -gt 65535 ]] ) &&
84 echo "Option '-P' or '--admin-port' requieres a valid numeric argument" >&2 && $_exit 1
tiernoc4864732016-09-20 11:07:00 +000085
86[[ -n "$option_screen_name" ]] && screen_name="$option_screen_name" && screen_name_param=" --screen-name $screen_name"
87[[ -z "$option_screen_name" ]] && screen_name=vim && screen_name_param="" #default value
88
89[[ -n "$option_delete" ]] && action_list="delete-all $action_list"
90
91openvim_param=" --"
92[[ -n "$option_port" ]] && openvim_param="$openvim_param -p $option_port"
93[[ -n "$option_admin_port" ]] && openvim_param="$openvim_param -P $option_admin_port"
94[[ -n "$option_dbname" ]] && openvim_param="$openvim_param --dbname $option_dbname"
95[[ $openvim_param = " --" ]] && openvim_param=""
96db_name=vim_db #default value
97[[ -n "$option_dbname" ]] && db_name="$option_dbname"
98
tiernof7aa8c42016-09-06 16:43:04 +020099DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
100DIRvim=$(dirname $DIRNAME)
101export OPENVIM_HOST=localhost
tiernoc4864732016-09-20 11:07:00 +0000102[[ -n "$option_port" ]] && export OPENVIM_PORT=$option_port
103[[ -n "$option_admin_port" ]] && export OPENVIM_ADMIN_PORT=$option_admin_port
104
105[[ -n "$option_insert_bashrc" ]] && echo -e "\nexport OPENVIM_HOST=localhost" >> ~/.bashrc
106[[ -n "$option_insert_bashrc" ]] && echo -e "\nexport OPENVIM_PORT=9080" >> ~/.bashrc
tiernof7aa8c42016-09-06 16:43:04 +0200107#by default action should be reset and create
tiernoc4864732016-09-20 11:07:00 +0000108[[ -z "$action_list" ]] && action_list="reset create"
tiernof7aa8c42016-09-06 16:43:04 +0200109
110
111for action in $action_list
112do
113if [[ $action == "reset" ]]
114then
115 #ask for confirmation if argument is not -f --force
116 force_="y"
tiernoc4864732016-09-20 11:07:00 +0000117 [[ -z "$option_force" ]] && read -e -p "WARNING: openvim database content will be lost!!! Continue(y/N)" force_
tiernof7aa8c42016-09-06 16:43:04 +0200118 [[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
119 echo "deleting deployed vm"
120 ${DIRvim}/openvim vm-delete -f | grep -q deleted && sleep 10 #give some time to get virtual machines deleted
tiernoc4864732016-09-20 11:07:00 +0000121 echo "Stopping openvim${screen_name_param}${openvim_param}"
mirabal9cc283f2017-04-26 12:44:14 +0200122 $DIRNAME/service-openvim stop${screen_name_param}${openvim_param}
tiernoc4864732016-09-20 11:07:00 +0000123 echo "Initializing databases $db_name"
124 $DIRvim/database_utils/init_vim_db.sh -u vim -p vimpw -d $db_name
125 echo "Starting openvim${screen_name_param}${openvim_param}"
mirabal9cc283f2017-04-26 12:44:14 +0200126 $DIRNAME/service-openvim start${screen_name_param}${openvim_param}
tiernof7aa8c42016-09-06 16:43:04 +0200127
128elif [[ $action == delete-all ]]
129then
tierno24ca7a82017-05-08 15:45:32 +0200130 for t in `${DIRvim}/openvim tenant-list |
131 awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{printf("%s:%s",$1,$2)}'`
tiernof7aa8c42016-09-06 16:43:04 +0200132 do
133 t_id=${t%%:*}
134 t_name=${t#*:}
135 [[ -z $t_id ]] && continue
136 export OPENVIM_TENANT=${t_id}
137 for what in vm image flavor port net
138 do
139 items=`${DIRvim}/openvim $what-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{print $1}'`
140 if [[ -n $items ]]
141 then
tiernoc4864732016-09-20 11:07:00 +0000142 [[ $option_force == "-" ]] && echo deleting openvim ${what}s from tenant ${t_name}
143 [[ $option_force != "-" ]] && read -e -p "Delete openvim ${what}s from tenant ${t_name}?(y/N) " force_
tiernof7aa8c42016-09-06 16:43:04 +0200144 [[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
145 for item in $items
146 do
147 echo -n "$item "
148 ${DIRvim}/openvim $what-delete -f $item || ! echo "fail" >&2 || $_exit 1
149 done
150 fi
151 done
152 ${DIRvim}/openvim tenant-delete -f $t_id || ! echo "fail" >&2 || $_exit 1
153 for what in host
154 do
155 items=`${DIRvim}/openvim $what-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{print $1}'`
156 if [[ -n $items ]]
157 then
tiernoc4864732016-09-20 11:07:00 +0000158 [[ $option_force == "-" ]] && echo deleting openvim ${what}s
159 [[ $option_force != "-" ]] && read -e -p "Delete openvim ${what}s?(y/N) " force_
tiernof7aa8c42016-09-06 16:43:04 +0200160 [[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
161 for item in $items
162 do
163 echo -n "$item "
164 ${DIRvim}/openvim $what-delete -f $item || ! echo "fail" >&2 || $_exit 1
165 done
166 fi
167 done
168
169 done
170elif [[ $action == "delete" ]]
171then
tierno24ca7a82017-05-08 15:45:32 +0200172 ${DIRvim}/openvim net-delete -f mgmt || echo "fail"
173 # ${DIRvim}/openvim net-delete -f default || echo "fail"
174 # ${DIRvim}/openvim net-delete -f macvtap:em1 || echo "fail"
175 # ${DIRvim}/openvim net-delete -f shared_bridge_net || echo "fail"
176 # ${DIRvim}/openvim net-delete -f data_net || echo "fail"
tiernof7aa8c42016-09-06 16:43:04 +0200177 ${DIRvim}/openvim host-remove -f fake-host-0 || echo "fail"
178 ${DIRvim}/openvim host-remove -f fake-host-1 || echo "fail"
179 ${DIRvim}/openvim host-remove -f fake-host-2 || echo "fail"
180 ${DIRvim}/openvim host-remove -f fake-host-3 || echo "fail"
tierno24ca7a82017-05-08 15:45:32 +0200181 ${DIRvim}/openvim image-delete -f cirros034 || echo "fail"
tierno1817f962017-03-09 11:50:36 +0100182 result=`openvim tenant-list osm`
tiernof7aa8c42016-09-06 16:43:04 +0200183 vimtenant=`echo $result |gawk '{print $1}'`
184 #check a valid uuid is obtained
tierno1817f962017-03-09 11:50:36 +0100185 is_valid_uuid $vimtenant || ! echo "Tenant 'osm' not found. Already delete?" >&2 || $_exit 1
tiernof7aa8c42016-09-06 16:43:04 +0200186 export OPENVIM_TENANT=$vimtenant
tierno1817f962017-03-09 11:50:36 +0100187 ${DIRvim}/openvim tenant-delete -f osm || echo "fail"
tiernof7aa8c42016-09-06 16:43:04 +0200188 echo
189
190elif [[ $action == "create" ]]
191then
192 echo "Adding example hosts"
tierno9a61c6b2016-09-08 10:57:02 +0200193 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example0.yaml || ! echo "fail" >&2 || $_exit 1
194 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example1.yaml || ! echo "fail" >&2 || $_exit 1
195 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example2.yaml || ! echo "fail" >&2 || $_exit 1
196 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example3.yaml || ! echo "fail" >&2 || $_exit 1
tiernof7aa8c42016-09-06 16:43:04 +0200197 echo "Adding example nets"
tierno24ca7a82017-05-08 15:45:32 +0200198 # ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example0.yaml || ! echo "fail" >&2 || $_exit 1
199 # ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example1.yaml || ! echo "fail" >&2 || $_exit 1
200 # ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example2.yaml || ! echo "fail" >&2 || $_exit 1
201 # ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example3.yaml || ! echo "fail" >&2 || $_exit 1
202 ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example4.yaml || ! echo "fail" >&2 || $_exit 1
tiernof7aa8c42016-09-06 16:43:04 +0200203
tierno1817f962017-03-09 11:50:36 +0100204 printf "%-50s" "Creating openvim tenant 'osm': "
tierno24ca7a82017-05-08 15:45:32 +0200205 result=`${DIRvim}/openvim tenant-create '{tenant: {name: osm, description: admin}}'`
tiernof7aa8c42016-09-06 16:43:04 +0200206 vimtenant=`echo $result |gawk '{print $1}'`
207 #check a valid uuid is obtained
208 ! is_valid_uuid $vimtenant && echo "FAIL" && echo " $result" && $_exit 1
209 echo " $vimtenant"
210 export OPENVIM_TENANT=$vimtenant
tiernoc4864732016-09-20 11:07:00 +0000211 [[ -n "$option_insert_bashrc" ]] && echo -e "\nexport OPENVIM_TENANT=$vimtenant" >> ~/.bashrc
tiernof7aa8c42016-09-06 16:43:04 +0200212
tierno24ca7a82017-05-08 15:45:32 +0200213 printf "%-50s" "Adding example image 'cirros034': "
214 result=`${DIRvim}/openvim image-create --name cirros034 \
215 --path http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img \
216 --description "cirros from web"`
217 image=`echo $result |gawk '{print $1}'`
218 ! is_valid_uuid $image && echo "FAIL" && echo " $result" && $_exit 1
219 echo " $image"
220
221
tiernof7aa8c42016-09-06 16:43:04 +0200222 echo
223 #echo "Check virtual machines are deployed"
224 #vms_error=`openvim vm-list | grep ERROR | wc -l`
225 #vms=`openvim vm-list | wc -l`
226 #[[ $vms -ne 8 ]] && echo "WARNING: $vms VMs created, must be 8 VMs" >&2 && $_exit 1
227 #[[ $vms_error -gt 0 ]] && echo "WARNING: $vms_error VMs with ERROR" >&2 && $_exit 1
228fi
229done
230
231echo
232echo DONE
233#echo "Listing VNFs"
234#openvim vnf-list
235#echo "Listing scenarios"
236#openvim scenario-list
237#echo "Listing scenario instances"
238#openvim instance-scenario-list
239
240