066b457b491ed1c86235e4f6dc2bb220a00c0510
[joid.git] / ci / deploy.sh
1 #!/bin/bash
2
3 set -ex
4
5 source common/tools.sh
6
7 #need to put multiple cases here where decide this bundle to deploy by default use the odl bundle.
8 # Below parameters are the default and we can according the release
9
10 opnfvsdn=nosdn
11 opnfvtype=noha
12 openstack=ocata
13 opnfvlab=default
14 opnfvlabfile=
15 opnfvrel=e
16 opnfvfeature=none
17 opnfvdistro=xenial
18 opnfvarch=amd64
19 opnfvmodel=openstack
20 virtinstall=0
21 maasinstall=0
22
23 jujuver=`juju --version`
24
25 usage() { echo "Usage: $0
26     [-s|--sdn <nosdn|odl|opencontrail>]
27     [-t|--type <noha|ha|tip>]
28     [-o|--openstack <ocata>]
29     [-l|--lab <default|custom>]
30     [-f|--feature <ipv6,dpdk,lxd,dvr,openbaton>]
31     [-d|--distro <xenial>]
32     [-a|--arch <amd64|ppc64el|aarch64>]
33     [-m|--model <openstack|kubernetes>]
34     [-i|--virtinstall <0|1>]
35     [--maasinstall <0|1>]
36     [--labfile <labvonfig.yaml file>]
37     [-r|--release <e>]" 1>&2 exit 1;
38 }
39
40 #A string with command options
41 options=$@
42
43 # An array with all the arguments
44 arguments=($options)
45
46 # Loop index
47 index=0
48
49 for argument in $options
50     do
51         # Incrementing index
52         index=`expr $index + 1`
53
54         # The conditions
55         case $argument in
56             -h|--help )
57                 usage;
58                 ;;
59             -s|--sdn  )
60                 if ([ "arguments[index]" != "" ]); then
61                     opnfvsdn=${arguments[index]}
62                 fi;
63                 ;;
64             -t|--type )
65                 if ([ "arguments[index]" != "" ]); then
66                     opnfvtype=${arguments[index]}
67                 fi;
68                 ;;
69             -o|--openstack )
70                 if ([ "arguments[index]" != "" ]); then
71                     openstack=${arguments[index]}
72                 fi;
73                 ;;
74
75             -l|--lab  )
76                 if ([ "arguments[index]" != "" ]); then
77                     opnfvlab=${arguments[index]}
78                 fi;
79                 ;;
80
81             -r|--release )
82                 if ([ "arguments[index]" != "" ]); then
83                     opnfvrel=${arguments[index]}
84                 fi;
85                 ;;
86
87             -f|--feature )
88                 if ([ "arguments[index]" != "" ]); then
89                     opnfvfeature=${arguments[index]}
90                 fi;
91                 ;;
92
93             -d|--distro )
94                 if ([ "arguments[index]" != "" ]); then
95                     opnfdistro=${arguments[index]}
96                 fi;
97                 ;;
98
99             -a|--arch  )
100                 if ([ "arguments[index]" != "" ]); then
101                     opnfvarch=${arguments[index]}
102                 fi;
103                 ;;
104
105             -m|--model )
106                 if ([ "arguments[index]" != "" ]); then
107                     opnfvmodel=${arguments[index]}
108                 fi;
109                 ;;
110
111             -i|--virtinstall )
112                 if ([ "arguments[index]" != "" ]); then
113                     virtinstall=${arguments[index]}
114                 fi;
115                 ;;
116             --maasinstall )
117                 if ([ "arguments[index]" != "" ]); then
118                     maasinstall=${arguments[index]}
119                 fi;
120                 ;;
121             --labfile )
122                 if ([ "arguments[index]" != "" ]); then
123                     labfile=${arguments[index]}
124                 fi;
125                 ;;
126           esac
127     done
128
129
130 #by default maas creates two VMs in case of three more VM needed.
131 createresource() {
132     # TODO: make sure this function run with the same parameters used in 03-maasdeploy.sh
133     PROFILE=${PROFILE:-ubuntu}
134     MAAS_IP=$(grep " ip_address" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //')
135     API_SERVER="http://$MAAS_IP:5240/MAAS/api/2.0"
136     API_KEY=`sudo maas-region apikey --username=ubuntu`
137     maas login $PROFILE $API_SERVER $API_KEY
138
139     for node in node3-control node4-control
140     do
141         node_id=$(maas $PROFILE machines read | \
142                   jq -r ".[] | select(.hostname == \"$node\").system_id")
143         if [[ -z "$node_id" ]]; then
144             sudo virt-install --connect qemu:///system --name $node \
145                 --ram 8192 --cpu host --vcpus 4 \
146                 --disk size=120,format=qcow2,bus=virtio,cache=directsync,io=native,pool=default \
147                 --network bridge=virbr0,model=virtio \
148                 --network bridge=virbr0,model=virtio \
149                 --boot network,hd,menu=off \
150                 --noautoconsole --vnc --print-xml | tee _node.xml
151             node_mac=$(grep "mac address" _node.xml | head -1 | cut -d "'" -f 2)
152             sudo virsh -c qemu:///system define --file _node.xml
153             rm -f _node.xml
154
155             maas $PROFILE nodes new autodetect_nodegroup='yes' name=$node \
156                 tags='control' hostname=$name power_type='virsh' \
157                 mac_addresses=$node3controlmac \
158                 power_parameters_power_address="qemu+ssh://$USER@192.168.122.1/system" \
159                 architecture='amd64/generic' power_parameters_power_id='node3-control'
160             node_id=$(maas $PROFILE machines read | \
161                   jq -r ".[] | select(.hostname == \"$node\").system_id")
162         fi
163         if [[ -z "$node_id" ]]; then
164             echo_error "Error: failed to create node $node ."
165             exit 1
166         fi
167         maas $PROFILE tag update-nodes control add=$node_id || true
168     done
169 }
170
171 #copy the files and create extra resources needed for HA deployment
172 # in case of default VM labs.
173 deploy() {
174     if [ ! -f ./labconfig.yaml ] && [ -e ~/joid_config/labconfig.yaml ]; then
175         cp ~/joid_config/labconfig.yaml ./labconfig.yaml
176
177         if [ ! -f ./deployconfig.yaml ] && [ -e ~/joid_config/deployconfig.yaml ]; then
178             cp ~/joid_config/deployconfig.yaml ./deployconfig.yaml
179         else
180             python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
181         fi
182     else
183         if [ -e ./labconfig.yaml ]; then
184             if [ ! -f ./deployconfig.yaml ] && [ -e ~/joid_config/deployconfig.yaml ]; then
185                 cp ~/joid_config/deployconfig.yaml ./deployconfig.yaml
186             else
187                 python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
188             fi
189         else
190             echo_error "MAAS not deployed please deploy MAAS first."
191         fi
192     fi
193
194     #create json file which is missing in case of new deployment after maas and git tree cloned freshly.
195     python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < labconfig.yaml > labconfig.json
196     python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < deployconfig.yaml > deployconfig.json
197
198     # Install MAAS and expecting the labconfig.yaml at local directory.
199
200     if [ "$maasinstall" -eq 1 ]; then
201         ./clean.sh || true
202         PROFILE=${PROFILE:-ubuntu}
203         MAAS_IP=$(grep " ip_address" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //')
204         API_SERVER="http://$MAAS_IP:5240/MAAS/api/2.0"
205         if which maas > /dev/null; then
206             API_KEY=`sudo maas-region apikey --username=ubuntu`
207             maas login $PROFILE $API_SERVER $API_KEY
208
209             # make sure there is no machine entry in maas
210             for m in $(maas $PROFILE machines read | jq -r '.[].system_id')
211             do
212                 maas $PROFILE machine delete $m || true
213             done
214             podno=$(maas $PROFILE pods read | jq -r ".[]".id)
215             maas $PROFILE pod delete $podno || true
216         fi
217         ./cleanvm.sh || true
218
219         if [ "$virtinstall" -eq 1 ]; then
220             ./00-maasdeploy.sh virtual
221         else
222             if [ -z "$labfile" ]; then
223                 if [ ! -e ./labconfig.yaml ]; then
224                     echo_error "Labconfig file must be specified when using custom"
225                 else
226                     echo_warning "Labconfig was not specified, using ./labconfig.yaml instead"
227                 fi
228             elif [ ! -e "$labfile" ]; then
229                 echo_warning "Labconfig not found locally, trying download"
230                 wget $labfile -t 3 -T 10 -O ./labconfig.yaml || true
231                 count=`wc -l labconfig.yaml  | cut -d " " -f 1`
232                 if [ $count -lt 10 ]; then
233                     echo_error "Unable to download labconfig"
234                     exit 1
235                 fi
236             else
237                 echo_info "Using $labfile to setup deployment"
238                 cp $labfile ./labconfig.yaml
239             fi
240
241             ./00-maasdeploy.sh custom
242         fi
243     fi
244
245     if [[ "$opnfvtype" = "ha" && "$opnfvlab" = "default" ]]; then
246         createresource
247     fi
248
249     #bootstrap the node
250     ./01-bootstrap.sh
251
252     juju model-config default-series=$opnfvdistro enable-os-refresh-update=false enable-os-upgrade=false
253
254     # case default deploy the opnfv platform:
255     ./02-deploybundle.sh $opnfvtype $openstack $opnfvlab $opnfvsdn $opnfvfeature $opnfvdistro $opnfvmodel
256 }
257
258 #check whether charms are still executing the code even juju-deployer says installed.
259 check_status() {
260     waitstatus=$1
261     retval=0
262     timeoutiter=0
263
264     echo_info "Executing the relationships within charms..."
265     while [ $retval -eq 0 ]; do
266         if juju status | grep -q $waitstatus; then
267            echo_info "Still waiting for $waitstatus units"
268            if [ $timeoutiter -ge 180 ]; then
269                echo_error 'Timed out'
270                retval=1
271            else
272                sleep 30
273            fi
274            timeoutiter=$((timeoutiter+1))
275        else
276            echo_info 'Done executing the relationships'
277            retval=1
278        fi
279     done
280
281     if [[ "$opnfvmodel" = "openstack" ]]; then
282         juju expose ceph-radosgw || true
283         #juju ssh ceph/0 \ 'sudo radosgw-admin user create --uid="ubuntu" --display-name="Ubuntu Ceph"'
284     fi
285
286     echo_info "Deployment finishing..."
287  }
288
289
290 # In the case of a virtual deployment
291 if [ "$virtinstall" -eq 1 ]; then
292     ./clean.sh || true
293 fi
294
295 echo_info "Deployment started"
296 deploy
297
298 check_status executing
299
300 echo_info "Deployment finished"
301
302 echo_info "Configuring public access"
303
304 # translate bundle.yaml to json
305 python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < bundles.yaml > bundles.json
306
307 public_service() {
308     # get services list having a public interface
309     srv_list=$(cat bundles.json | jq -r ".services | to_entries[] | {\"key\": .key, \"value\": .value[\"bindings\"]} | select (.value!=null) | select(.value[] | contains(\"public-api\"))".key)
310     # get cnt list from service list
311     cnt_list=$(for cnt in $srv_list; do juju status $cnt --format=json | jq -r ".machines[].containers | to_entries[]".key; done)
312     # get public network gateway (supposing it is the first ip of the network)
313     public_api_gw=$(cat labconfig.json | jq --raw-output ".opnfv.spaces[] | select(.type==\"public\")".gateway)
314     admin_gw=$(cat labconfig.json | jq --raw-output ".opnfv.spaces[] | select(.type==\"admin\")".gateway)
315
316     if ([ $admin_gw ] && [ $admin_gw != "null" ]); then
317         # set default gateway to public api gateway
318         for cnt in $cnt_list; do
319             echo_info "Changing default gateway on $cnt"
320             if ([ $public_api_gw ] && [ $public_api_gw != "null" ]); then
321                 juju ssh $cnt "sudo ip r d default && sudo ip r a default via $public_api_gw";
322                 juju ssh $cnt "gw_dev=\$(ip  r l | grep 'via $public_api_gw' | cut -d \  -f5) &&\
323                    sudo cp /etc/network/interfaces /etc/network/interfaces.bak &&\
324                    echo 'removing old default gateway' &&\
325                    sudo perl -i -pe 's/^\ *gateway $admin_gw\n$//' /etc/network/interfaces &&\
326                    sudo perl -i -pe \"s/iface \$gw_dev inet static/iface \$gw_dev inet static\\n  gateway $public_api_gw/\" /etc/network/interfaces \
327                    ";
328             fi
329         done
330     fi
331 }
332
333 # Configuring deployment
334 if ([ $opnfvmodel == "openstack" ]); then
335     echo_info "Configuring OpenStack deployment"
336
337     ./openstack.sh "$opnfvsdn" "$opnfvlab" "$opnfvdistro" "$openstack" || true
338
339     # creating heat domain after pushing the public API into /etc/hosts
340     if [[ "$jujuver" > "2" ]]; then
341         status=`juju run-action heat/0 domain-setup`
342         echo $status
343     else
344         status=`juju action do heat/0 domain-setup`
345         echo $status
346     fi
347
348     sudo ../juju/get-cloud-images || true
349     ../juju/joid-configure-openstack || true
350
351     if grep -q 'openbaton' bundles.yaml; then
352         juju add-relation openbaton keystone
353     fi
354
355 elif ([ $opnfvmodel == "kubernetes" ]); then
356     echo_info "Configuring Kubernetes deployment"
357
358     ./k8.sh
359 fi
360
361 # expose the juju gui-url to login into juju gui
362
363 echo_info "Juju GUI can be accessed using the following URL and credentials:"
364 juju gui --show-credentials --no-browser
365
366 echo "Finished deployment and configuration"