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