2fb3439a01318df963869937a80d918aa9a146cf
[osm/openvim.git] / scripts / initopenvim.sh
1 #!/bin/bash
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openvim
6 # 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
28 DIRNAME=$(readlink -f ${BASH_SOURCE[0]})
29 DIRNAME=$(dirname $DIRNAME )
30
31 function 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"
41 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)"
45 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
50 function 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
59
60 #process options
61 source ${DIRNAME}/get-options.sh "force:f delete:d delete-all port:p= admin-port:P= screen-name= help:h dbname= insert-bashrc" $* || $_exit 1
62
63 #check correct arguments
64 action_list=""
65 for param in $params
66 do
67 if [[ "$param" == reset ]] || [[ "$param" == create ]] || [[ "$param" == delete ]] || [[ "$param" == delete-all ]]
68 then
69 action_list="$action_list $param"
70 continue
71 else
72 echo "invalid argument '$param'? Type -h for help" >&2 && $_exit 1
73 fi
74 done
75
76 #help
77 [[ -n "$option_help" ]] && usage && $_exit 0
78
79 #check numeric values for port
80 [[ -n "$option_port" ]] && ( [[ "$option_port" -lt 1 ]] || [[ "$option_port" -gt 65535 ]] ) && echo "Option '-p' or '--port' requires a valid numeric argument" >&2 && $_exit 1
81 [[ -n "$option_admin_port" ]] && ( [[ "$option_admin_port" -lt 1 ]] || [[ "$option_admin_port" -gt 65535 ]] ) && echo "Option '-P' or '--admin-port' requieres a valid numeric argument" >&2 && $_exit 1
82
83 [[ -n "$option_screen_name" ]] && screen_name="$option_screen_name" && screen_name_param=" --screen-name $screen_name"
84 [[ -z "$option_screen_name" ]] && screen_name=vim && screen_name_param="" #default value
85
86 [[ -n "$option_delete" ]] && action_list="delete-all $action_list"
87
88 openvim_param=" --"
89 [[ -n "$option_port" ]] && openvim_param="$openvim_param -p $option_port"
90 [[ -n "$option_admin_port" ]] && openvim_param="$openvim_param -P $option_admin_port"
91 [[ -n "$option_dbname" ]] && openvim_param="$openvim_param --dbname $option_dbname"
92 [[ $openvim_param = " --" ]] && openvim_param=""
93 db_name=vim_db #default value
94 [[ -n "$option_dbname" ]] && db_name="$option_dbname"
95
96 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
97 DIRvim=$(dirname $DIRNAME)
98 export OPENVIM_HOST=localhost
99 [[ -n "$option_port" ]] && export OPENVIM_PORT=$option_port
100 [[ -n "$option_admin_port" ]] && export OPENVIM_ADMIN_PORT=$option_admin_port
101
102 [[ -n "$option_insert_bashrc" ]] && echo -e "\nexport OPENVIM_HOST=localhost" >> ~/.bashrc
103 [[ -n "$option_insert_bashrc" ]] && echo -e "\nexport OPENVIM_PORT=9080" >> ~/.bashrc
104 #by default action should be reset and create
105 [[ -z "$action_list" ]] && action_list="reset create"
106
107
108 for action in $action_list
109 do
110 if [[ $action == "reset" ]]
111 then
112 #ask for confirmation if argument is not -f --force
113 force_="y"
114 [[ -z "$option_force" ]] && read -e -p "WARNING: openvim database content will be lost!!! Continue(y/N)" force_
115 [[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
116 echo "deleting deployed vm"
117 ${DIRvim}/openvim vm-delete -f | grep -q deleted && sleep 10 #give some time to get virtual machines deleted
118 echo "Stopping openvim${screen_name_param}${openvim_param}"
119 $DIRNAME/service-openvim stop${screen_name_param}${openvim_param}
120 echo "Initializing databases $db_name"
121 $DIRvim/database_utils/init_vim_db.sh -u vim -p vimpw -d $db_name
122 echo "Starting openvim${screen_name_param}${openvim_param}"
123 $DIRNAME/service-openvim start${screen_name_param}${openvim_param}
124
125 elif [[ $action == delete-all ]]
126 then
127 for t in `${DIRvim}/openvim tenant-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{printf("%s:%s",$1,$2)}'`
128 do
129 t_id=${t%%:*}
130 t_name=${t#*:}
131 [[ -z $t_id ]] && continue
132 export OPENVIM_TENANT=${t_id}
133 for what in vm image flavor port net
134 do
135 items=`${DIRvim}/openvim $what-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{print $1}'`
136 if [[ -n $items ]]
137 then
138 [[ $option_force == "-" ]] && echo deleting openvim ${what}s from tenant ${t_name}
139 [[ $option_force != "-" ]] && read -e -p "Delete openvim ${what}s from tenant ${t_name}?(y/N) " force_
140 [[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
141 for item in $items
142 do
143 echo -n "$item "
144 ${DIRvim}/openvim $what-delete -f $item || ! echo "fail" >&2 || $_exit 1
145 done
146 fi
147 done
148 ${DIRvim}/openvim tenant-delete -f $t_id || ! echo "fail" >&2 || $_exit 1
149 for what in host
150 do
151 items=`${DIRvim}/openvim $what-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{print $1}'`
152 if [[ -n $items ]]
153 then
154 [[ $option_force == "-" ]] && echo deleting openvim ${what}s
155 [[ $option_force != "-" ]] && read -e -p "Delete openvim ${what}s?(y/N) " force_
156 [[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
157 for item in $items
158 do
159 echo -n "$item "
160 ${DIRvim}/openvim $what-delete -f $item || ! echo "fail" >&2 || $_exit 1
161 done
162 fi
163 done
164
165 done
166 elif [[ $action == "delete" ]]
167 then
168 ${DIRvim}/openvim net-delete -f default || echo "fail"
169 ${DIRvim}/openvim net-delete -f macvtap:em1 || echo "fail"
170 ${DIRvim}/openvim net-delete -f shared_bridge_net || echo "fail"
171 ${DIRvim}/openvim net-delete -f data_net || echo "fail"
172 ${DIRvim}/openvim host-remove -f fake-host-0 || echo "fail"
173 ${DIRvim}/openvim host-remove -f fake-host-1 || echo "fail"
174 ${DIRvim}/openvim host-remove -f fake-host-2 || echo "fail"
175 ${DIRvim}/openvim host-remove -f fake-host-3 || echo "fail"
176 result=`openvim tenant-list osm`
177 vimtenant=`echo $result |gawk '{print $1}'`
178 #check a valid uuid is obtained
179 is_valid_uuid $vimtenant || ! echo "Tenant 'osm' not found. Already delete?" >&2 || $_exit 1
180 export OPENVIM_TENANT=$vimtenant
181 ${DIRvim}/openvim tenant-delete -f osm || echo "fail"
182 echo
183
184 elif [[ $action == "create" ]]
185 then
186 echo "Adding example hosts"
187 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example0.yaml || ! echo "fail" >&2 || $_exit 1
188 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example1.yaml || ! echo "fail" >&2 || $_exit 1
189 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example2.yaml || ! echo "fail" >&2 || $_exit 1
190 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example3.yaml || ! echo "fail" >&2 || $_exit 1
191 echo "Adding example nets"
192 ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example0.yaml || ! echo "fail" >&2 || $_exit 1
193 ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example1.yaml || ! echo "fail" >&2 || $_exit 1
194 ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example2.yaml || ! echo "fail" >&2 || $_exit 1
195 ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example3.yaml || ! echo "fail" >&2 || $_exit 1
196
197 printf "%-50s" "Creating openvim tenant 'osm': "
198 result=`openvim tenant-create '{tenant: {name: osm, description: admin}}'`
199 vimtenant=`echo $result |gawk '{print $1}'`
200 #check a valid uuid is obtained
201 ! is_valid_uuid $vimtenant && echo "FAIL" && echo " $result" && $_exit 1
202 echo " $vimtenant"
203 export OPENVIM_TENANT=$vimtenant
204 [[ -n "$option_insert_bashrc" ]] && echo -e "\nexport OPENVIM_TENANT=$vimtenant" >> ~/.bashrc
205
206 echo
207 #echo "Check virtual machines are deployed"
208 #vms_error=`openvim vm-list | grep ERROR | wc -l`
209 #vms=`openvim vm-list | wc -l`
210 #[[ $vms -ne 8 ]] && echo "WARNING: $vms VMs created, must be 8 VMs" >&2 && $_exit 1
211 #[[ $vms_error -gt 0 ]] && echo "WARNING: $vms_error VMs with ERROR" >&2 && $_exit 1
212 fi
213 done
214
215 echo
216 echo DONE
217 #echo "Listing VNFs"
218 #openvim vnf-list
219 #echo "Listing scenarios"
220 #openvim scenario-list
221 #echo "Listing scenario instances"
222 #openvim instance-scenario-list
223
224