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