Deployment Script made more flexible
[integration.git] / deployment / heat / onap-oom / scripts / deploy.sh
1 #!/bin/bash
2 #
3 # Copyright 2018 Huawei Technologies Co., Ltd.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11
12 stack_name="oom"
13 full_deletion=false
14
15 if [ -z "$WORKSPACE" ]; then
16     export WORKSPACE=`git rev-parse --show-toplevel`
17 fi
18
19 usage() {
20     echo "Usage: $0 [ -n <number of VMs {2-15}> ] [ -s <stack name> ][ -b <branch name> ][ -r ] <env>" 1>&2;
21
22     echo "n:    Set the number of VM's that will be installed. This number must be between 2 and 15" 1>&2;
23     echo "s:    Set the name to be used for stack. This name will be used for naming of resources" 1>&2;
24     echo "r:    Delete all resources relating to ONAP within enviroment." 1>&2;
25     echo "q:    Quiet Delete of all ONAP resources." 1>&2;
26
27     exit 1;
28 }
29
30
31 while getopts ":n:s:rq" o; do
32     case "${o}" in
33         n)
34             if [[ ${OPTARG} =~ ^[0-9]+$ ]];then
35                 if [ ${OPTARG} -ge 2 -a ${OPTARG} -le 15 ]; then
36                     vm_num=${OPTARG}
37                 else
38                     usage
39                 fi
40             else
41                 usage
42             fi
43             ;;
44         s)
45             if [[ ! ${OPTARG} =~ ^[0-9]+$ ]];then
46                 stack_name=${OPTARG}
47             else
48                 usage
49             fi
50             ;;
51         r)
52             echo "The following command will delete all information relating to onap within your enviroment"
53             read -p "Are you certain this is what you want? (type y to confirm):" answer
54
55             if [ $answer = "y" ] || [ $answer = "Y" ] || [ $answer = "yes" ] || [ $answer = "Yes"]; then
56                 echo "This may delete the work of other colleages within the same enviroment"
57                 read -p "Are you certain this is what you want? (type y to confirm):" answer2
58
59                 if [ $answer2 = "y" ] || [ $answer2 = "Y" ] || [ $answer2 = "yes" ] || [ $answer2 = "Yes"]; then
60                     full_deletion=true
61                 else
62                     echo "Ending program"
63                     exit 1
64                 fi
65             else
66                 echo "Ending program"
67                 exit 1
68             fi
69             ;;
70         q)
71             full_deletion=true
72             ;;
73         *)
74             usage
75             ;;
76     esac
77 done
78 shift $((OPTIND-1))
79
80 if [ "$#" -ne 1 ]; then
81    usage
82 fi
83
84 ENV_FILE=$1
85
86 if [ ! -f $ENV_FILE ];then
87     echo ENV file does not exist or was not given
88     exit 1
89 fi
90
91 set -x
92
93 SSH_KEY=~/.ssh/onap_key
94
95 source $WORKSPACE/test/ete/scripts/install_openstack_cli.sh
96
97 SO_ENCRYPTION_KEY=aa3871669d893c7fb8abbcda31b88b4f
98 export OS_PASSWORD_ENCRYPTED=$(echo -n "$OS_PASSWORD" | openssl aes-128-ecb -e -K "$SO_ENCRYPTION_KEY" -nosalt | xxd -c 256 -p)
99
100 for n in $(seq 1 5); do
101     if [ $full_deletion = true ] ; then
102         $WORKSPACE/test/ete/scripts/teardown-onap.sh -n $stack_name -q
103     else
104         $WORKSPACE/test/ete/scripts/teardown-onap.sh -n $stack_name
105     fi
106
107     cd $WORKSPACE/deployment/heat/onap-oom
108     envsubst < $ENV_FILE > $ENV_FILE~
109     if [ -z "$vm_num" ]; then
110         cp onap-oom.yaml onap-oom.yaml~
111     else
112         ./scripts/gen-onap-oom-yaml.sh $vm_num > onap-oom.yaml~
113     fi
114
115     if ! openstack stack create -t ./onap-oom.yaml~ -e $ENV_FILE~ $stack_name; then
116         break
117     fi
118
119     while [ "CREATE_IN_PROGRESS" == "$(openstack stack show -c stack_status -f value $stack_name)" ]; do
120         sleep 20
121     done
122
123     STATUS=$(openstack stack show -c stack_status -f value $stack_name)
124     echo $STATUS
125     if [ "CREATE_COMPLETE" != "$STATUS" ]; then
126         break
127     fi
128
129     for i in $(seq 1 30); do
130         sleep 30
131         RANCHER_IP=$(openstack stack output show $stack_name rancher_vm_ip -c output_value -f value)
132         K8S_IP=$(openstack stack output show $stack_name k8s_1_vm_ip -c output_value -f value)
133         timeout 1 ping -c 1 "$RANCHER_IP" && break
134     done
135
136     timeout 1 ping -c 1 "$RANCHER_IP" && break
137
138     echo Error: OpenStack infrastructure issue: unable to reach rancher "$RANCHER_IP"
139     sleep 10
140 done
141
142 if ! timeout 1 ping -c 1 "$RANCHER_IP"; then
143     exit 2
144 fi
145
146 ssh-keygen -R $RANCHER_IP
147
148 ssh -o StrictHostKeychecking=no -i $SSH_KEY ubuntu@$RANCHER_IP "sed -u '/Cloud-init.*finished/q' <(tail -n+0 -f /var/log/cloud-init-output.log)"
149
150 for n in $(seq 1 6); do
151     echo "Wait count $n of 6"
152     sleep 15m
153     timeout 15m ssh -i $SSH_KEY ubuntu@$RANCHER_IP  'sudo su -l root -c "/root/oom/kubernetes/robot/ete-k8s.sh onap health"'
154     RESULT=$?
155     if [ $RESULT -eq 0 ]; then
156         break
157     fi
158 done
159 ROBOT_POD=$(ssh -i $SSH_KEY ubuntu@$RANCHER_IP 'sudo su -c "kubectl --namespace onap get pods"' | grep robot | sed 's/ .*//')
160 if [ "$ROBOT_POD" == "" ]; then
161     exit 1
162 fi
163
164 LOG_DIR=$(echo "kubectl exec -n onap $ROBOT_POD -- ls -1t /share/logs | grep health | head -1" | ssh -i $SSH_KEY ubuntu@$RANCHER_IP sudo su)
165 if [ "$LOG_DIR" == "" ]; then
166     exit 1
167 fi
168
169 echo "kubectl cp -n onap $ROBOT_POD:share/logs/$LOG_DIR /tmp/robot/logs/$LOG_DIR" | ssh -i $SSH_KEY ubuntu@$RANCHER_IP sudo su
170 rsync -e "ssh -i $SSH_KEY" -avtz ubuntu@$RANCHER_IP:/tmp/robot/logs/$LOG_DIR/ $WORKSPACE/archives/
171
172 echo "Browse Robot results at http://$K8S_IP:30209/logs/$LOG_DIR/"
173
174 exit 0