Adding port 5240 along with MAAS_URL needed for non x86
[joid.git] / ci / deploy.sh
1 #!/bin/bash
2
3 set -ex
4
5 #need to put multiple 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=noha
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 <noha|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:5240/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
145         #create json file which is missing in case of new deployment after maas and git tree cloned freshly.
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     waitstatus=$1
184     retval=0
185     timeoutiter=0
186
187     echo -n "executing the relationship within charms ."
188     while [ $retval -eq 0 ]; do
189         if juju status | grep -q $waitstatus; then
190            echo -n '.'
191            if [ $timeoutiter -ge 120 ]; then
192                echo 'timed out'
193                retval=1
194            else
195                sleep 30
196            fi
197            timeoutiter=$((timeoutiter+1))
198        else
199            echo 'done'
200            retval=1
201        fi
202     done
203
204     if [[ "$opnfvmodel" = "openstack" ]]; then
205         juju expose ceph-radosgw || true
206         #juju ssh ceph/0 \ 'sudo radosgw-admin user create --uid="ubuntu" --display-name="Ubuntu Ceph"'
207     fi
208
209     echo "...... deployment finishing ......."
210  }
211
212 # In the case of a virtual deployment
213 if [ "$virtinstall" -eq 1 ]; then
214     ./clean.sh || true
215 fi
216
217 echo "...... deployment started ......"
218 deploy
219
220 check_status executing
221
222 echo "...... deployment finished  ......."
223
224
225 echo "...... configuring public access  ......."
226
227 # translate bundle.yaml to json
228 python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < bundles.yaml > bundles.json
229 # get services list having a public interface
230 srv_list=$(cat bundles.json | jq -r ".services | to_entries[] | {\"key\": .key, \"value\": .value[\"bindings\"]} | select (.value!=null) | select(.value[] | contains(\"public-api\"))".key)
231 # get cnt list from service list
232 cnt_list=$(for cnt in $srv_list; do juju status $cnt --format=json | jq -r ".machines[].containers | to_entries[]".key; done)
233 # get public network gateway (supposing it is the first ip of the network)
234 public_api_gw=$(cat labconfig.json | jq --raw-output ".opnfv.spaces[] | select(.type==\"public\")".gateway)
235 admin_gw=$(cat labconfig.json | jq --raw-output ".opnfv.spaces[] | select(.type==\"admin\")".gateway)
236
237 if ([ $admin_gw ] && [ $admin_gw != "null" ]); then
238     # set default gateway to public api gateway
239     for cnt in $cnt_list; do
240         echo "changing default gw on $cnt"
241         if ([ $public_api_gw ] && [ $public_api_gw != "null" ]); then
242             juju ssh $cnt "sudo ip r d default && sudo ip r a default via $public_api_gw";
243             juju ssh $cnt "gw_dev=\$(ip  r l | grep 'via $public_api_gw' | cut -d \  -f5) &&\
244                    sudo cp /etc/network/interfaces /etc/network/interfaces.bak &&\
245                    echo 'removing old default gateway' &&\
246                    sudo perl -i -pe 's/^\ *gateway $admin_gw\n$//' /etc/network/interfaces &&\
247                    sudo perl -i -pe \"s/iface \$gw_dev inet static/iface \$gw_dev inet static\\n  gateway $public_api_gw/\" /etc/network/interfaces \
248                    ";
249         fi
250     done
251 fi
252
253 echo "...... configure  ......."
254
255 if ([ $opnfvmodel == "openstack" ]); then
256     ./openstack.sh "$opnfvsdn" "$opnfvlab" "$opnfvdistro" "$openstack" || true
257
258     # creating heat domain after pushing the public API into /etc/hosts
259     if [[ "$jujuver" > "2" ]]; then
260         status=`juju run-action heat/0 domain-setup`
261         echo $status
262     else
263         status=`juju action do heat/0 domain-setup`
264         echo $status
265     fi
266
267     sudo ../juju/get-cloud-images || true
268     ../juju/joid-configure-openstack || true
269
270     if grep -q 'openbaton' bundles.yaml; then
271         juju add-relation openbaton keystone
272     fi
273
274 elif ([ $opnfvmodel == "kubernetes" ]); then
275     ./k8.sh
276 fi
277
278 # expose the juju gui-url to login into juju gui
279
280 echo " ...... JUJU GUI can be access using the below URL ...... "
281 juju gui --show-credentials --no-browser
282
283 echo "...... finished  ......."