cleanup old controllers and models for virtual deployments
[joid.git] / ci / deploy.sh
1 #!/bin/bash
2
3 set -ex
4
5 #need to put mutiple cases here where decide this bundle to deploy by default use the odl bundle.
6 # Below parameters are the default and we can according the release
7
8 opnfvsdn=nosdn
9 opnfvtype=nonha
10 openstack=ocata
11 opnfvlab=default
12 opnfvrel=e
13 opnfvfeature=none
14 opnfvdistro=xenial
15 opnfvarch=amd64
16 opnfvmodel=openstack
17 virtinstall=0
18
19 jujuver=`juju --version`
20
21 read_config() {
22     opnfvrel=`grep release: deploy.yaml | cut -d ":" -f2`
23     openstack=`grep openstack: deploy.yaml | cut -d ":" -f2`
24     opnfvtype=`grep type: deploy.yaml | cut -d ":" -f2`
25     opnfvlab=`grep lab: deploy.yaml | cut -d ":" -f2`
26     opnfvsdn=`grep sdn: deploy.yaml | cut -d ":" -f2`
27 }
28
29 usage() { echo "Usage: $0 [-s <nosdn|odl|opencontrail>]
30                          [-t <nonha|ha|tip>]
31                          [-o <juno|liberty>]
32                          [-l <default|intelpod5>]
33                          [-f <ipv6,dpdk,lxd,dvr>]
34                          [-d <trusty|xenial>]
35                          [-a <amd64>]
36                          [-m <openstack|kubernetes>]
37                          [-i <0|1>]
38                          [-r <a|b>]" 1>&2 exit 1; }
39
40 while getopts ":s:t:o:l:h:r:f:d:a:m:i:" opt; do
41     case "${opt}" in
42         s)
43             opnfvsdn=${OPTARG}
44             ;;
45         t)
46             opnfvtype=${OPTARG}
47             ;;
48         o)
49             openstack=${OPTARG}
50             ;;
51         l)
52             opnfvlab=${OPTARG}
53             ;;
54         r)
55             opnfvrel=${OPTARG}
56             ;;
57         f)
58             opnfvfeature=${OPTARG}
59             ;;
60         d)
61             opnfvdistro=${OPTARG}
62             ;;
63         a)
64             opnfvarch=${OPTARG}
65             ;;
66         m)
67             opnfvmodel=${OPTARG}
68             ;;
69         i)
70             virtinstall=${OPTARG}
71             ;;
72         h)
73             usage
74             ;;
75         *)
76             ;;
77     esac
78 done
79
80 #by default maas creates two VMs in case of three more VM needed.
81 createresource() {
82     # TODO: make sure this function run with the same parameters used in 03-maasdeploy.sh
83     PROFILE=${PROFILE:-ubuntu}
84     MAAS_IP=$(grep " ip_address" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //')
85     API_SERVER="http://$MAAS_IP/MAAS/api/2.0"
86     API_KEY=`sudo maas-region apikey --username=ubuntu`
87     maas login $PROFILE $API_SERVER $API_KEY
88
89     for node in node3-control node4-control
90     do
91         node_id=$(maas $PROFILE machines read | \
92                   jq -r ".[] | select(.hostname == \"$node\").system_id")
93         if [[ -z "$node_id" ]]; then
94             sudo virt-install --connect qemu:///system --name $node \
95                 --ram 8192 --cpu host --vcpus 4 \
96                 --disk size=120,format=qcow2,bus=virtio,cache=directsync,io=native,pool=default \
97                 --network bridge=virbr0,model=virtio \
98                 --network bridge=virbr0,model=virtio \
99                 --boot network,hd,menu=off \
100                 --noautoconsole --vnc --print-xml | tee _node.xml
101             node_mac=$(grep "mac address" _node.xml | head -1 | cut -d "'" -f 2)
102             sudo virsh -c qemu:///system define --file _node.xml
103             rm -f _node.xml
104
105             maas $PROFILE nodes new autodetect_nodegroup='yes' name=$node \
106                 tags='control' hostname=$name power_type='virsh' \
107                 mac_addresses=$node3controlmac \
108                 power_parameters_power_address="qemu+ssh://$USER@192.168.122.1/system" \
109                 architecture='amd64/generic' power_parameters_power_id='node3-control'
110             node_id=$(maas $PROFILE machines read | \
111                   jq -r ".[] | select(.hostname == \"$node\").system_id")
112         fi
113         if [[ -z "$node_id" ]]; then
114             echo "Error: failed to create node $node ."
115             exit 1
116         fi
117         maas $PROFILE tag update-nodes control add=$node_id || true
118     done
119 }
120
121 #copy the files and create extra resources needed for HA deployment
122 # in case of default VM labs.
123 deploy() {
124     if [[ "$jujuver" > "2" ]]; then
125         if [ ! -f ./labconfig.yaml ] && [ -e ~/joid_config/labconfig.yaml ]; then
126             cp ~/joid_config/labconfig.yaml ./labconfig.yaml
127
128             if [ ! -f ./deployconfig.yaml ] && [ -e ~/joid_config/deployconfig.yaml ]; then
129                 cp ~/joid_config/deployconfig.yaml ./deployconfig.yaml
130             else
131                 python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
132             fi
133         else
134             if [ -e ./labconfig.yaml ]; then
135                 if [ ! -f ./deployconfig.yaml ] && [ -e ~/joid_config/deployconfig.yaml ]; then
136                     cp ~/joid_config/deployconfig.yaml ./deployconfig.yaml
137                 else
138                     python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml
139                 fi
140             else
141                 echo " MAAS not deployed please deploy MAAS first."
142             fi
143         fi
144 #create json file which is missing in case of new deployment after maas and git tree cloned freshly.
145
146         python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < labconfig.yaml > labconfig.json
147         python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < deployconfig.yaml > deployconfig.json
148
149     else
150         if [ ! -f ./environments.yaml ] && [ -e ~/.juju/environments.yaml ]; then
151             cp ~/.juju/environments.yaml ./environments.yaml
152         elif [ ! -f ./environments.yaml ] && [ -e ~/joid_config/environments.yaml ]; then
153             cp ~/joid_config/environments.yaml ./environments.yaml
154         fi
155         #copy the script which needs to get deployed as part of ofnfv release
156         echo "...... deploying now ......"
157         echo "   " >> environments.yaml
158         echo "        enable-os-refresh-update: false" >> environments.yaml
159         echo "        enable-os-upgrade: false" >> environments.yaml
160         echo "        admin-secret: admin" >> environments.yaml
161         echo "        default-series: $opnfvdistro" >> environments.yaml
162         cp environments.yaml ~/.juju/
163         cp environments.yaml ~/joid_config/
164     fi
165
166     if [[ "$opnfvtype" = "ha" && "$opnfvlab" = "default" ]]; then
167         createresource
168     fi
169
170     #bootstrap the node
171     ./01-bootstrap.sh
172
173     if [[ "$jujuver" > "2" ]]; then
174         juju model-config default-series=$opnfvdistro enable-os-refresh-update=false enable-os-upgrade=false
175     fi
176
177     #case default deploy the opnfv platform:
178     ./02-deploybundle.sh $opnfvtype $openstack $opnfvlab $opnfvsdn $opnfvfeature $opnfvdistro $opnfvmodel
179 }
180
181 #check whether charms are still executing the code even juju-deployer says installed.
182 check_status() {
183     retval=0
184     timeoutiter=0
185
186     echo -n "executing the reltionship within charms ."
187     while [ $retval -eq 0 ]; do
188        sleep 30
189        if juju status | grep -q "executing"; then
190            echo -n '.'
191            if [ $timeoutiter -ge 120 ]; then
192                echo 'timed out'
193                retval=1
194            fi
195            timeoutiter=$((timeoutiter+1))
196        else
197            echo 'done'
198            retval=1
199        fi
200     done
201
202     if [[ "$opnfvmodel" = "openstack" ]]; then
203         juju expose ceph-radosgw || true
204         #juju ssh ceph/0 \ 'sudo radosgw-admin user create --uid="ubuntu" --display-name="Ubuntu Ceph"'
205     fi
206     echo "...... deployment finishing ......."
207 }
208
209 # In the case of a virtual deployment
210 if [ "$virtinstall" -eq 1 ]; then
211     ./clean.sh || true
212 fi
213
214 echo "...... deployment started ......"
215 deploy
216
217 check_status
218
219 echo "...... deployment finished  ......."
220
221
222 echo "...... configuring public access  ......."
223
224 # translate bundle.yaml to json
225 python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < bundles.yaml > bundles.json
226 # get services list having a public interface
227 srv_list=$(cat bundles.json | jq -r ".services | to_entries[] | {\"key\": .key, \"value\": .value[\"bindings\"]} | select (.value!=null) | select(.value[] | contains(\"public-api\"))".key)
228 # get cnt list from service list
229 cnt_list=$(for cnt in $srv_list; do juju status $cnt --format=json | jq -r ".machines[].containers | to_entries[]".key; done)
230 # get public network gateway (supposing it is the first ip of the network)
231 public_api_gw=$(cat labconfig.json | jq --raw-output ".opnfv.spaces[] | select(.type==\"public\")".gateway)
232 admin_gw=$(cat labconfig.json | jq --raw-output ".opnfv.spaces[] | select(.type==\"admin\")".gateway)
233
234 if ([ $admin_gw ] && [ $admin_gw != "null" ]); then
235     # set default gateway to public api gateway
236     for cnt in $cnt_list; do
237         echo "changing default gw on $cnt"
238         if ([ $public_api_gw ] && [ $public_api_gw != "null" ]); then
239             juju ssh $cnt "sudo ip r d default && sudo ip r a default via $public_api_gw";
240             juju ssh $cnt "gw_dev=\$(ip  r l | grep 'via $public_api_gw' | cut -d \  -f5) &&\
241                    sudo cp /etc/network/interfaces /etc/network/interfaces.bak &&\
242                    echo 'removing old default gateway' &&\
243                    sudo perl -i -pe 's/^\ *gateway $admin_gw\n$//' /etc/network/interfaces &&\
244                    sudo perl -i -pe \"s/iface \$gw_dev inet static/iface \$gw_dev inet static\\n  gateway $public_api_gw/\" /etc/network/interfaces \
245                    ";
246         fi
247     done
248 fi
249
250 echo "...... configure  ......."
251
252 if ([ $opnfvmodel == "openstack" ]); then
253     ./openstack.sh "$opnfvsdn" "$opnfvlab" "$opnfvdistro" "$openstack" || true
254
255     # creating heat domain after puching the public API into /etc/hosts
256     if [[ "$jujuver" > "2" ]]; then
257         status=`juju run-action heat/0 domain-setup`
258         echo $status
259     else
260         status=`juju action do heat/0 domain-setup`
261         echo $status
262     fi
263
264     sudo ../juju/get-cloud-images || true
265     ../juju/joid-configure-openstack || true
266
267 elif ([ $opnfvmodel == "kubernetes" ]); then
268     ./k8.sh
269 fi
270
271 # expose the juju gui-url to login into juju gui
272
273 echo " ...... JUJU GUI can be access using the below URL ...... "
274 juju gui --show-credentials --no-browser
275
276 echo "...... finished  ......."