Add license header
[releng.git] / jjb / joid / joid-deploy.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2016 Orange and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 set +e
11 set -o nounset
12
13 PWD_FILENAME="passwords.sh"
14
15 ##
16 ##
17 ##
18 function exit_on_error {
19     RES=$1
20     MSG=$2
21     if [ $RES != 0 ]; then
22         echo "FAILED - $MSG"
23         exit $RES
24     fi
25 }
26
27
28 ##
29 ## Create LAB_CONFIG folder if not exists
30 ##
31 mkdir -p $LAB_CONFIG
32
33 ##
34 ## Override default passwords with local settings if needed
35 ##
36
37 if [ -e "$LAB_CONFIG/$PWD_FILENAME" ]; then
38     echo "------ Load local passwords ------"
39     source $LAB_CONFIG/$PWD_FILENAME
40 else
41     export MAAS_USER=ubuntu
42     export MAAS_PASSWORD=ubuntu
43     export OS_ADMIN_PASSWORD=openstack
44 fi
45
46 ##
47 ## Set Joid pod config name
48 ##
49     # This part will be removed when pod names will be synced between jenkins and joid config
50     case $NODE_NAME in
51         orange-fr-pod2)
52             POD=orange-pod2 ;;
53         juniper-us-test-1)
54             POD=juniper-pod1 ;;
55         *virtual*)
56             POD=default ;;
57         *)
58             POD=$NODE_NAME ;;
59     esac
60     export POD_NAME=${POD/-}
61
62 ##
63 ## Parse Network config
64 ##
65
66 EXTERNAL_NETWORK=${EXTERNAL_NETWORK:-}
67 # split EXTERNAL_NETWORK=name;type;first ip;last ip; gateway;network
68 IFS=';' read -r -a EXTNET <<< "$EXTERNAL_NETWORK"
69 EXTNET_NAME=${EXTNET[0]}
70 EXTNET_TYPE=${EXTNET[1]}
71 EXTNET_FIP=${EXTNET[2]}
72 EXTNET_LIP=${EXTNET[3]}
73 EXTNET_GW=${EXTNET[4]}
74 EXTNET_NET=${EXTNET[5]}
75
76 ##
77 ## Redeploy MAAS or recover the previous config
78 ##
79
80 cd $WORKSPACE/ci
81 if [ -e "$LAB_CONFIG/environments.yaml" ] && [ "$MAAS_REINSTALL" == "false" ]; then
82     echo "------ Recover Juju environment to use MAAS ------"
83     cp $LAB_CONFIG/environments.yaml .
84 else
85     MAASCONFIG=$WORKSPACE/ci/maas/${POD/-*}/${POD/*-}/deployment.yaml
86     echo "------ Set MAAS password ------"
87     sed -i -- "s/user: ubuntu/user: $MAAS_USER/" $MAASCONFIG
88     sed -i -- "s/password: ubuntu/password: $MAAS_PASSWORD/" $MAASCONFIG
89     echo "------ Redeploy MAAS ------"
90     ./02-maasdeploy.sh $POD_NAME
91     exit_on_error $? "MAAS Deploy FAILED"
92 fi
93
94 ##
95 ## Configure Joid deployment
96 ##
97
98 # Based on scenario naming we can get joid options
99 # naming convention:
100 #    os-<controller>-<nfvfeature>-<mode>[-<extrastuff>]
101 # With parameters:
102 #    controller=(nosdn|odl_l3|odl_l2|onos|ocl)
103 #       No odl_l3 today
104 #    nfvfeature=(kvm|ovs|dpdk|nofeature)
105 #       '_' list separated.
106 #    mode=(ha|noha)
107 #    extrastuff=(none)
108 #       Optional field - Not used today
109
110 IFS='-' read -r -a DEPLOY_OPTIONS <<< "${DEPLOY_SCENARIO}--"
111 #last -- need to avoid nounset error
112
113 SDN_CONTROLLER=${DEPLOY_OPTIONS[1]}
114 NFV_FEATURES=${DEPLOY_OPTIONS[2]}
115 HA_MODE=${DEPLOY_OPTIONS[3]}
116 EXTRA=${DEPLOY_OPTIONS[4]}
117
118 if [ "$SDN_CONTROLLER" == 'odl_l2' ] || [ "$SDN_CONTROLLER" == 'odl_l3' ]; then
119     SDN_CONTROLLER='odl'
120 fi
121 if [ "$HA_MODE" == 'noha' ]; then
122     HA_MODE='nonha'
123 fi
124 SRCBUNDLE="${WORKSPACE}/ci/${SDN_CONTROLLER}/juju-deployer/"
125 SRCBUNDLE="${SRCBUNDLE}/ovs-${SDN_CONTROLLER}-${HA_MODE}.yaml"
126
127
128 # Modify Bundle
129 echo "------ Set openstack password ------"
130 sed -i -- "s/\"admin-password\": openstack/\"admin-password\": $OS_ADMIN_PASSWORD/" $SRCBUNDLE
131
132 if [ -n "$EXTNET_NAME" ]; then
133     echo "------ Set openstack default network ------"
134     sed -i -- "s/\"neutron-external-network\": ext_net/\"neutron-external-network\": $EXTNET_NAME/" $SRCBUNDLE
135 fi
136
137 echo "------ Set ceph disks ------"
138 CEPH_DISKS_CONTROLLERS=${CEPH_DISKS_CONTROLLERS:-}
139 if [ -z "$CEPH_DISKS_CONTROLLERS" ]; then
140     CEPH_DISKS_CONTROLLERS=$CEPH_DISKS
141 fi
142
143 #Find the first line of osd-devices to change the one for ceph, then the other for ceph-osd
144 CEPH_DEV_LINE=$(grep -nr osd-devices $SRCBUNDLE |head -n1|cut -d: -f1)
145 sed -i -- "${CEPH_DEV_LINE}s@osd-devices: /srv@osd-devices: $CEPH_DISKS@" $SRCBUNDLE
146 sed -i -- "s@osd-devices: /srv@osd-devices: $CEPH_DISKS_CONTROLLERS@" $SRCBUNDLE
147 sed -i -r -- "s/^(\s+osd-reformat: )'no'/\1'$CEPH_REFORMAT'/" $SRCBUNDLE
148
149 ##
150 ## Configure Joid deployment
151 ##
152
153 echo "------ Deploy with juju ------"
154 echo "Execute: ./deploy.sh -t $HA_MODE -o $OS_RELEASE -s $SDN_CONTROLLER -l $POD_NAME"
155
156 ./deploy.sh -t $HA_MODE -o $OS_RELEASE -s $SDN_CONTROLLER -l $POD_NAME
157 exit_on_error $? "Main deploy FAILED"
158
159 ##
160 ## Set Admin RC
161 ##
162 JOID_ADMIN_OPENRC=$LAB_CONFIG/admin-openrc
163 echo "------ Create OpenRC file [$JOID_ADMIN_OPENRC] ------"
164
165 # get Keystone vip
166 KEYSTONE=$(cat bundles.yaml |shyaml get-value openstack-phase2.services.keystone.options.vip)
167
168 # get controller IP
169 case "$SDN_CONTROLLER" in
170     "odl_l2" | "odl_l3")
171         SDN_CONTROLLER_IP=$(juju status odl-controller/0 |grep public-address|sed -- 's/.*\: //')
172         ;;
173     "onos")
174         SDN_CONTROLLER_IP=$(juju status onos-controller/0 |grep public-address|sed -- 's/.*\: //')
175         ;;
176     *)
177         SDN_CONTROLLER_IP='none'
178         ;;
179 esac
180 SDN_PASSWORD='admin'
181
182 # export the openrc file
183 cat << EOF > $JOID_ADMIN_OPENRC
184 export OS_USERNAME=admin
185 export OS_PASSWORD=$OS_ADMIN_PASSWORD
186 export OS_TENANT_NAME=admin
187 export OS_AUTH_URL=http://$KEYSTONE:5000/v2.0
188 export OS_REGION_NAME=Canonical
189 export SDN_CONTROLLER=$SDN_CONTROLLER_IP
190 export SDN_PASSWORD=$SDN_PASSWORD
191 EOF
192
193 ##
194 ## Backup local juju env
195 ##
196
197 echo "------ Backup Juju environment ------"
198 cp environments.yaml $LAB_CONFIG/
199
200 ##
201 ## Basic test to return a realistic result to jenkins
202 ##
203
204 echo "------ Do basic test ------"
205 source $JOID_ADMIN_OPENRC
206 curl -i -sw '%{http_code}' -H "Content-Type: application/json"   -d "
207 { \"auth\": {
208     \"identity\": {
209       \"methods\": [\"password\"],
210       \"password\": {
211         \"user\": {
212           \"name\": \"admin\",
213           \"domain\": { \"id\": \"default\" },
214           \"password\": \"$OS_ADMIN_PASSWORD\"
215         }
216       }
217     }
218   }
219 }"   http://$KEYSTONE:5000/v3/auth/tokens |grep "HTTP/1.1 20" 2>&1 >/dev/null;
220 exit_on_error $? "Deploy FAILED to auth to openstack"
221
222
223 ##
224 ## Create external network if needed
225 ##
226
227 # If we have more information than only the name, try to create it
228 if [ -z "$EXTNET_TYPE" ]; then
229     echo "------ No data for external network creation, pass ------"
230 elif [[ "$DEPLOY_SCENARIO" =~ "onos" ]]; then
231     echo "------ ONOS have created the external network, pass ------"
232 else
233     echo "------ External network creation ------"
234     neutron net-create $EXTNET_NAME --router:external True \
235       --provider:physical_network external --provider:network_type $EXTNET_TYPE
236     exit_on_error $? "External network creation failed"
237     neutron subnet-create $EXTNET_NAME --name $EXTNET_NAME \
238       --allocation-pool start=$EXTNET_FIP,end=$EXTNET_LIP \
239       --disable-dhcp --gateway $EXTNET_GW $EXTNET_NET
240     exit_on_error $? "External subnet creation failed"
241     neutron net-update $EXTNET_NAME --shared
242     exit_on_error $? "External network sharing failed"
243 fi
244
245 ##
246 ## Exit success
247 ##
248
249 echo "Deploy success"
250 exit 0