Merge "Autodeployer support for ODL Plugin installation + Fuel 6.1"
[genesis.git] / fuel / build / f_isoroot / f_predeployment / pre-deploy.sh
1 #!/bin/bash -e
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # stefan.k.berg@ericsson.com
5 # jonas.bjurel@ericsson.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12
13 error_exit () {
14     echo "$@" >&2
15     exit 1
16 }
17
18 get_env() {
19    local env_id=${1:-""}
20
21    if [ -z $env_id ]; then
22       local n_envs=$(fuel env --list | grep -v -E "^id|^--|^ *$" | wc -l)
23       if [ $n_envs -ne 1 ]; then
24           echo "Usage: $0 [<env-id>]" >&2
25           error_exit "If only a single environment is present it can be left" \
26                      "out. Otherwise the environment must be selected"
27       fi
28       env_id=$(fuel env --list | grep -v -E "^id|^--" | awk '{print $1}')
29    else
30       if ! fuel --env $env_id environment 2>/dev/null grep -v -E "^id|^--" | \
31            grep -q ^$env_id; then
32          error_exit "No such environment ID: $env_id"
33       fi
34    fi
35    echo $env_id
36 }
37
38 get_node_uid () {
39     cat $1 | grep "^uid: " | sed "s/^uid: '//" | sed "s/'$//"
40 }
41
42 get_node_role () {
43     cat $1 | grep "^role: " | sed "s/^role: //"
44 }
45
46 get_next_cic () {
47     file=$1
48
49     last=`cat $file | sed 's/.*://' | grep "cic-" | sed 's/cic\-.*sl//' | sort -n | tail -1`
50     if [ -z "$last" ]; then
51         next=1
52     else
53         next=$[$last + 2]
54     fi
55     echo $next
56 }
57
58 get_next_compute () {
59     file=$1
60
61     last=`cat $file | sed 's/.*://' | grep "cmp-" | sed 's/cmp\-.*sl//' | sort -n | tail -1`
62     if [ -z "$last" ]; then
63         next=7
64     else
65         next=$[$last + 2]
66     fi
67     echo $next
68 }
69
70 modify_hostnames () {
71     env=$1
72     file=$2
73     for line in `cat $file`
74     do
75         old=`echo $line | sed 's/:.*//'`
76         new=`echo $line | sed 's/.*://'`
77         echo "Applying: $old -> $new"
78
79         for dfile in deployment_$env/*.yaml
80         do
81             sed -i "s/$old/$new/g" $dfile
82         done
83
84         for pfile in provisioning_$env/*.yaml
85         do
86             sed -i "s/$old/$new/g" $pfile
87         done
88     done
89 }
90
91 setup_hostnames () {
92     ENV=$1
93     cd ${CONFIGDIR}
94     touch hostnames.$ENV
95
96     for dfile in deployment_$ENV/*.yaml
97     do
98         uid=`get_node_uid $dfile`
99         hostname=`grep "^node-$uid:" hostnames.$ENV | sed 's/.*://'`
100         if [ -z $hostname ]; then
101
102             pfile=provisioning_$ENV/node-$uid.yaml
103             role=`get_node_role $dfile`
104
105             case $role in
106                 primary-controller)
107                     hostname="cic-pod0-sh0-sl`get_next_cic hostnames.$ENV`"
108                     ;;
109                 controller)
110                     hostname="cic-pod0-sh0-sl`get_next_cic hostnames.$ENV`"
111                     ;;
112                 compute)
113                     hostname="cmp-pod0-sh0-sl`get_next_compute hostnames.$ENV`"
114                     ;;
115                 *)
116                     echo "Unknown node type for UID $uid"
117                     exit 1
118                     ;;
119             esac
120
121             echo "node-$uid:$hostname" >> hostnames.$ENV
122         else
123             echo "Already got hostname $hostname for node-$uid"
124
125         fi
126     done
127
128     rm -f hostnames.$ENV.old
129     mv hostnames.$ENV hostnames.$ENV.old
130     sort hostnames.$ENV.old | uniq > hostnames.$ENV
131     modify_hostnames $ENV hostnames.$ENV
132 }
133
134
135
136 get_provisioning_info () {
137     ENV=$1
138     mkdir -p ${CONFIGDIR}
139     cd ${CONFIGDIR}
140     rm -Rf provisioning_$ENV
141     echo "Getting provisioning info..."
142     fuel --env $ENV provisioning --default
143     if [ $? -ne 0 ]; then
144         echo "Error: Could not get provisioning info for env $ENV">&2
145         exit 1
146     fi
147 }
148
149 get_deployment_info () {
150     ENV=$1
151     mkdir -p ${CONFIGDIR}
152     cd ${CONFIGDIR}
153     rm -Rf deployment_$ENV
154     echo "Getting deployment info..."
155     fuel --env $ENV deployment --default
156     if [ $? -ne 0 ]; then
157         echo "Error: Could not get deployment info for env $ENV">&2
158         exit 1
159     fi
160 }
161
162 transform_yaml () {
163     ENV=$1
164     cd ${CONFIGDIR}
165     for dfile in deployment_$ENV/*.yaml
166     do
167         /opt/opnfv/transform_yaml.py $dfile
168     done
169 }
170
171 commit_changes () {
172     ENV=$1
173     cd ${CONFIGDIR}
174
175     fuel --env $ENV deployment --upload
176     fuel --env $ENV provisioning --upload
177 }
178
179 add_yaml_fragment () {
180     ENV=$1
181     FRAGMENT=${CONFIGDIR}/fragment.yaml.$ENV
182
183     cd ${CONFIGDIR}
184     for dfile in deployment_$ENV/*.yaml
185     do
186         cnt=`grep "^opnfv:" $dfile | wc -l `
187         if [ $cnt -eq 0 ]; then
188             echo "Adding fragment to $dfile"
189             cat $FRAGMENT >> $dfile
190        else
191             echo "Already have fragment in $dfile"
192        fi
193     done
194 }
195
196
197 ip_valid() {
198     IP_ADDRESS="$1"
199     # Check if the format looks right_
200     echo "$IP_ADDRESS" | egrep -qE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' || return 1
201     #check that each octect is less than or equal to 255:
202     echo $IP_ADDRESS | awk -F'.' '$1 <=255 && $2 <= 255 && $3 <=255 && $4 <= 255 {print "Y" } ' | grep -q Y || return 1
203     return 0
204 }
205
206
207 generate_ntp_entry() {
208     FILE=$1
209     read -p "NTP server:" NTP_SERVER
210     if [ -z "$NTP_SERVER" ]; then
211         return 1
212     elif confirm_yes "Are you sure you want to add this entry (y/n): "; then
213         echo "Confirmed"
214         echo "      server $NTP_SERVER" >> $FILE
215     fi
216 }
217
218 generate_hostfile_entry() {
219     FILE=$1
220     read -p "Name:" HOST_NAME
221     if [ -z "$HOST_NAME" ]; then
222         return 1
223     else
224         read -p "FQDN:" HOST_FQDN
225         read -p "IP:  " HOST_IP
226         while ! ip_valid "$HOST_IP"
227         do
228             echo "This is not a valid IP! Try again."
229             read -p "IP:  " HOST_IP
230         done
231     fi
232     if confirm_yes "Are you sure you want to add this entry (y/n): "; then
233         echo "Confirmed"
234         echo "  - name: $HOST_NAME" >> $FILE
235         echo "    address: $HOST_IP" >> $FILE
236         echo "    fqdn: $HOST_FQDN" >> $FILE
237     else
238         echo "Not confirmed"
239     fi
240     return 0
241 }
242
243 generate_dns_entry() {
244     FILE=$1
245     PROMPT=$2
246     read -p "${PROMPT}:" DNS_IP
247     if [ -z "$DNS_IP" ]; then
248         return 1
249     else
250         while ! ip_valid "$DNS_IP"
251         do
252             echo "This is not a valid IP! Try again."
253             read -p "${PROMPT}: " DNS_IP
254         done
255     fi
256     if confirm_yes "Are you sure you want to add this entry (y/n): "; then
257         echo "Confirmed"
258         echo "    - $DNS_IP" >> $FILE
259     else
260         echo "Not confirmed"
261     fi
262     return 0
263 }
264
265 confirm_yes() {
266     prompt=$1
267     while true
268     do
269         read -p "$prompt" YESNO
270         case $YESNO in
271             [Yy])
272                 return 0
273                 ;;
274             [Nn])
275                 return 1
276                 ;;
277         esac
278     done
279 }
280
281 generate_yaml_fragment() {
282     ENV=$1
283     FRAGMENT=${CONFIGDIR}/fragment.yaml.$ENV
284
285     if [ -f $FRAGMENT ]; then
286         echo "Manual configuration already performed, reusing previous data from $FRAGMENT."
287         echo "Press return to continue or ^C to stop."
288         read ans
289         return
290     fi
291
292     echo "opnfv:" > ${FRAGMENT}
293
294     clear
295     echo -e "\n\nPre-deployment configuration\n\n"
296
297     echo -e "\n\nIPs for the DNS servers to go into /etc/resolv.conf. You will be"
298     echo -e "prompted for one IP at the time. Press return on an empty line"
299     echo -e "to complete your input. If no DNS server is specified, the IP of"
300     echo -e "the Fuel master will be used instead.\n"
301
302     DNSCICYAML=${CONFIGDIR}/cicdns.yaml.$ENV
303     rm -f $DNSCICYAML
304
305     echo -e "\n\n"
306
307     while generate_dns_entry $DNSCICYAML "IP for CIC name servers"
308     do
309         :
310     done
311
312     if [ -f $DNSCICYAML ]; then
313         echo "  dns:" >> $FRAGMENT
314         echo "    controller:" >> $FRAGMENT
315         cat $DNSCICYAML >> $FRAGMENT
316     fi
317
318
319     DNSCMPYAML=${CONFIGDIR}/cmpdns.yaml.$ENV
320     rm -f $DNSCMPYAML
321
322     echo -e "\n\n"
323
324     while generate_dns_entry $DNSCMPYAML "IP for compute node name servers"
325     do
326         :
327     done
328
329
330     if [ -f $DNSCMPYAML ]; then
331         if [ ! -f $DNSCICYAML ]; then
332             echo "  dns:" >> $FRAGMENT
333         fi
334         echo "    compute:" >> $FRAGMENT
335         cat $DNSCMPYAML >> $FRAGMENT
336     fi
337
338     echo -e "\n\nHosts file additions for controllers and compute nodes. You will be"
339     echo -e "prompted for name, FQDN and IP for each entry. Press return when prompted"
340     echo -e "for a name when you have completed your input.\n"
341
342
343     HOSTYAML=${CONFIGDIR}/hosts.yaml.$ENV
344     rm -f $HOSTYAML
345     while generate_hostfile_entry $HOSTYAML
346     do
347         :
348     done
349
350     if [ -f $HOSTYAML ]; then
351         echo "  hosts:" >> $FRAGMENT
352         cat $HOSTYAML >> $FRAGMENT
353     fi
354
355     echo -e "\n\nNTP upstream configuration for controllers.You will be"
356     echo -e "prompted for a NTP server each entry. Press return when prompted"
357     echo -e "for a NTP serverwhen you have completed your input.\n"
358
359
360     NTPYAML=${CONFIGDIR}/ntp.yaml.$ENV
361     rm -f $NTPYAML
362     while generate_ntp_entry $NTPYAML
363     do
364         :
365     done
366
367     if [ -f $NTPYAML ]; then
368         echo "  ntp:" >> $FRAGMENT
369         echo "    controller: |" >> $FRAGMENT
370         cat $NTPYAML >> $FRAGMENT
371
372         echo "    compute: |" >> $FRAGMENT
373         for ctl in `find $CONFIGDIR/deployment_$ENV -name '*controller*.yaml'`
374         do
375            fqdn=`grep "^fqdn:" $ctl | sed 's/fqdn: *//'`
376            echo "      server $fqdn" >> $FRAGMENT
377         done
378     fi
379
380     # If nothing added make sure we get an empty opnfv hash
381     # instead of a NULL hash.
382     if [ $(wc -l $FRAGMENT | awk '{print $1}') -le 1 ]; then
383         echo "opnfv: {}" >$FRAGMENT
384     fi
385 }
386
387 ENV=$(get_env "$@")
388
389 CONFIGDIR="/var/lib/opnfv"
390 mkdir -p $CONFIGDIR
391
392 get_deployment_info $ENV
393 # Uncomment the below to enable the control_bond example
394 #transform_yaml $ENV
395 get_provisioning_info $ENV
396 generate_yaml_fragment $ENV
397 # The feature to change hostnames from node-<n> to cmp- or cic- is disabled.
398 # To turn it on, uncomment the following line.
399 #setup_hostnames $ENV
400 add_yaml_fragment $ENV
401 commit_changes $ENV