Initial openvim v0.4.6 upload
[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 openmano
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
29 function usage(){
30 echo -e "usage: ${BASH_SOURCE[0]} [OPTIONS] <action>\n Deletes openvim content and add fake hosts, networks"
31 echo -e " <action> is a list of the following items (by default 'reset create')"
32 echo -e " reset reset the openvim database content"
33 echo -e " create creates fake hosts and networks"
34 echo -e " delete delete created items"
35 echo -e " delete-all delete vms. flavors, images, ..."
36 echo -e " OPTIONS:"
37 echo -e " -f --force : does not prompt for confirmation"
38 echo -e " -d --delete : same to action delete-all"
39 echo -e " --insert-bashrc insert the created tenant variables at"
40 echo -e " ~/.bashrc to be available by openvim CLI"
41 echo -e " -h --help : shows this help"
42 }
43
44 function is_valid_uuid(){
45 echo "$1" | grep -q -E '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$' && return 0
46 return 1
47 }
48
49
50 #detect if is called with a source to use the 'exit'/'return' command for exiting
51 [[ ${BASH_SOURCE[0]} != $0 ]] && _exit="return" || _exit="exit"
52
53 #check correct arguments
54 force=""
55 action_list=""
56 insert_bashrc=""
57
58 while [[ $# -gt 0 ]]
59 do
60 argument="$1"
61 shift
62 if [[ $argument == reset ]] || [[ $argument == create ]] || [[ $argument == delete ]] || [[ $argument == delete-all ]]
63 then
64 action_list="$action_list $argument"
65 continue
66 #short options
67 elif [[ ${argument:0:1} == "-" ]] && [[ ${argument:1:1} != "-" ]] && [[ ${#argument} -ge 2 ]]
68 then
69 index=0
70 while index=$((index+1)) && [[ $index -lt ${#argument} ]]
71 do
72 [[ ${argument:$index:1} == h ]] && usage && $_exit 0
73 [[ ${argument:$index:1} == f ]] && force=y && continue
74 [[ ${argument:$index:1} == d ]] && action_list="delete-all $action_list" && continue
75 echo "invalid option '${argument:$index:1}'? Type -h for help" >&2 && $_exit 1
76 done
77 continue
78 fi
79 #long options
80 [[ $argument == --help ]] && usage && $_exit 0
81 [[ $argument == --force ]] && force=y && continue
82 [[ $argument == --delete ]] && action_list="delete-all $action_list" && continue
83 [[ $argument == --insert-bashrc ]] && insert_bashrc=y && continue
84 echo "invalid argument '$argument'? Type -h for help" >&2 && $_exit 1
85 done
86
87 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
88 DIRvim=$(dirname $DIRNAME)
89 export OPENVIM_HOST=localhost
90 export OPENVIM_PORT=9080
91 [[ $insert_bashrc == y ]] && echo -e "\nexport OPENVIM_HOST=localhost" >> ~/.bashrc
92 [[ $insert_bashrc == y ]] && echo -e "\nexport OPENVIM_PORT=9080" >> ~/.bashrc
93 #by default action should be reset and create
94 [[ -z $action_list ]] && action_list="reset create"
95
96
97 for action in $action_list
98 do
99 if [[ $action == "reset" ]]
100 then
101 #ask for confirmation if argument is not -f --force
102 force_="y"
103 [[ $force != y ]] && read -e -p "WARNING: openvim database content will be lost!!! Continue(y/N)" force_
104 [[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
105 echo "deleting deployed vm"
106 ${DIRvim}/openvim vm-delete -f | grep -q deleted && sleep 10 #give some time to get virtual machines deleted
107 echo "Stopping openvim"
108 $DIRNAME/service-openvim.sh stop
109 echo "Initializing databases"
110 $DIRvim/database_utils/init_vim_db.sh -u vim -p vimpw
111 echo "Starting openvim"
112 $DIRNAME/service-openvim.sh start
113
114 elif [[ $action == delete-all ]]
115 then
116 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)}'`
117 do
118 t_id=${t%%:*}
119 t_name=${t#*:}
120 [[ -z $t_id ]] && continue
121 export OPENVIM_TENANT=${t_id}
122 for what in vm image flavor port net
123 do
124 items=`${DIRvim}/openvim $what-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{print $1}'`
125 if [[ -n $items ]]
126 then
127 [[ $force == y ]] && echo deleting openvim ${what}s from tenant ${t_name}
128 [[ $force != y ]] && read -e -p "Delete openvim ${what}s from tenant ${t_name}?(y/N) " force_
129 [[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
130 for item in $items
131 do
132 echo -n "$item "
133 ${DIRvim}/openvim $what-delete -f $item || ! echo "fail" >&2 || $_exit 1
134 done
135 fi
136 done
137 ${DIRvim}/openvim tenant-delete -f $t_id || ! echo "fail" >&2 || $_exit 1
138 for what in host
139 do
140 items=`${DIRvim}/openvim $what-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{print $1}'`
141 if [[ -n $items ]]
142 then
143 [[ $force == y ]] && echo deleting openvim ${what}s
144 [[ $force != y ]] && read -e -p "Delete openvim ${what}s?(y/N) " force_
145 [[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
146 for item in $items
147 do
148 echo -n "$item "
149 ${DIRvim}/openvim $what-delete -f $item || ! echo "fail" >&2 || $_exit 1
150 done
151 fi
152 done
153
154 done
155 elif [[ $action == "delete" ]]
156 then
157 ${DIRvim}/openvim net-delete -f default || echo "fail"
158 ${DIRvim}/openvim net-delete -f macvtap:em1 || echo "fail"
159 ${DIRvim}/openvim net-delete -f shared_bridge_net || echo "fail"
160 ${DIRvim}/openvim net-delete -f data_net || echo "fail"
161 ${DIRvim}/openvim host-remove -f fake-host-0 || echo "fail"
162 ${DIRvim}/openvim host-remove -f fake-host-1 || echo "fail"
163 ${DIRvim}/openvim host-remove -f fake-host-2 || echo "fail"
164 ${DIRvim}/openvim host-remove -f fake-host-3 || echo "fail"
165 result=`openvim tenant-list TEST-admin`
166 vimtenant=`echo $result |gawk '{print $1}'`
167 #check a valid uuid is obtained
168 is_valid_uuid $vimtenant || ! echo "Tenant TEST-admin not found. Already delete?" >&2 || $_exit 1
169 export OPENVIM_TENANT=$vimtenant
170 ${DIRvim}/openvim tenant-delete -f TEST-admin || echo "fail"
171 echo
172
173 elif [[ $action == "create" ]]
174 then
175 echo "Adding example hosts"
176 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example0.json || ! echo "fail" >&2 || $_exit 1
177 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example1.json || ! echo "fail" >&2 || $_exit 1
178 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example2.json || ! echo "fail" >&2 || $_exit 1
179 ${DIRvim}/openvim host-add $DIRvim/test/hosts/host-example3.json || ! echo "fail" >&2 || $_exit 1
180 echo "Adding example nets"
181 ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example0.yaml || ! echo "fail" >&2 || $_exit 1
182 ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example1.yaml || ! echo "fail" >&2 || $_exit 1
183 ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example2.yaml || ! echo "fail" >&2 || $_exit 1
184 ${DIRvim}/openvim net-create $DIRvim/test/networks/net-example3.yaml || ! echo "fail" >&2 || $_exit 1
185
186 printf "%-50s" "Creating openvim tenant 'TEST-admin': "
187 result=`openvim tenant-create '{"tenant": {"name":"TEST-admin", "description":"admin"}}'`
188 vimtenant=`echo $result |gawk '{print $1}'`
189 #check a valid uuid is obtained
190 ! is_valid_uuid $vimtenant && echo "FAIL" && echo " $result" && $_exit 1
191 echo " $vimtenant"
192 export OPENVIM_TENANT=$vimtenant
193 [[ $insert_bashrc == y ]] && echo -e "\nexport OPENVIM_TENANT=$vimtenant" >> ~/.bashrc
194
195 echo
196 #echo "Check virtual machines are deployed"
197 #vms_error=`openvim vm-list | grep ERROR | wc -l`
198 #vms=`openvim vm-list | wc -l`
199 #[[ $vms -ne 8 ]] && echo "WARNING: $vms VMs created, must be 8 VMs" >&2 && $_exit 1
200 #[[ $vms_error -gt 0 ]] && echo "WARNING: $vms_error VMs with ERROR" >&2 && $_exit 1
201 fi
202 done
203
204 echo
205 echo DONE
206 #echo "Listing VNFs"
207 #openvim vnf-list
208 #echo "Listing scenarios"
209 #openvim scenario-list
210 #echo "Listing scenario instances"
211 #openvim instance-scenario-list
212
213