Merge "switch drools pdp image to new one"
[integration.git] / deployment / heat / onap-rke / 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="onap"
13 portal_hostname="portal.api.simpledemo.onap.org"
14 full_deletion=false
15
16 if [ -z "$WORKSPACE" ]; then
17     export WORKSPACE=`git rev-parse --show-toplevel`
18 fi
19
20 usage() {
21     echo "Usage: $0 [ -n <number of VMs {2-15}> ][ -s <stack name> ][ -m <manifest> ][ -d <domain> ][ -r ][ -q ] <env>" 1>&2;
22
23     echo "n:    Set the number of VM's that will be installed. This number must be between 2 and 15" 1>&2;
24     echo "s:    Set the name to be used for stack. This name will be used for naming of resources" 1>&2;
25     echo "d:    Set the base domain name to be used in portal UI URLs" 1>&2;
26     echo "m:    The docker manifest to apply; must be either \"docker-manifest-staging.csv\" or \"docker-manifest.csv\"." 1>&2;
27     echo "r:    Delete all resources relating to ONAP within enviroment." 1>&2;
28     echo "q:    Quiet Delete of all ONAP resources." 1>&2;
29
30     exit 1;
31 }
32
33
34 while getopts ":n:s:d:m:rq" o; do
35     case "${o}" in
36         n)
37             if [[ ${OPTARG} =~ ^[0-9]+$ ]];then
38                 if [ ${OPTARG} -ge 2 -a ${OPTARG} -le 15 ]; then
39                     vm_num=${OPTARG}
40                 else
41                     usage
42                 fi
43             else
44                 usage
45             fi
46             ;;
47         s)
48             if [[ ! ${OPTARG} =~ ^[0-9]+$ ]];then
49                 stack_name=${OPTARG}
50             else
51                 usage
52             fi
53             ;;
54         d)
55             if [[ ! ${OPTARG} =~ ^[0-9]+$ ]];then
56                 portal_hostname=${OPTARG}
57             else
58                 usage
59             fi
60             ;;
61         m)
62             if [ -f $WORKSPACE/version-manifest/src/main/resources/${OPTARG} ]; then
63                 docker_manifest=${OPTARG}
64             else
65                 usage
66             fi
67             ;;
68         r)
69             echo "The following command will delete all information relating to onap within your enviroment"
70             read -p "Are you certain this is what you want? (type y to confirm):" answer
71
72             if [ $answer = "y" ] || [ $answer = "Y" ] || [ $answer = "yes" ] || [ $answer = "Yes"]; then
73                 echo "This may delete the work of other colleages within the same enviroment"
74                 read -p "Are you certain this is what you want? (type y to confirm):" answer2
75
76                 if [ $answer2 = "y" ] || [ $answer2 = "Y" ] || [ $answer2 = "yes" ] || [ $answer2 = "Yes"]; then
77                     full_deletion=true
78                 else
79                     echo "Ending program"
80                     exit 1
81                 fi
82             else
83                 echo "Ending program"
84                 exit 1
85             fi
86             ;;
87         q)
88             full_deletion=true
89             ;;
90         *)
91             usage
92             ;;
93     esac
94 done
95 shift $((OPTIND-1))
96
97 if [ "$#" -ne 1 ]; then
98    usage
99 fi
100
101 ENV_FILE=$1
102
103 if [ ! -f $ENV_FILE ];then
104     echo ENV file does not exist or was not given
105     exit 1
106 fi
107
108 set -x
109
110 SSH_KEY=~/.ssh/onap_key
111
112 source $WORKSPACE/test/ete/scripts/install_openstack_cli.sh
113
114 #Use new encryption method
115 pushd $WORKSPACE/deployment/heat/onap-rke/scripts
116 javac Crypto.java
117 SO_ENCRYPTION_KEY=aa3871669d893c7fb8abbcda31b88b4f
118 export OS_PASSWORD_ENCRYPTED=$(java Crypto "$OS_PASSWORD" "$SO_ENCRYPTION_KEY")
119 popd
120
121 for n in $(seq 1 5); do
122     if [ $full_deletion = true ] ; then
123         $WORKSPACE/test/ete/scripts/teardown-onap.sh -n $stack_name -q
124     else
125         $WORKSPACE/test/ete/scripts/teardown-onap.sh -n $stack_name
126     fi
127
128     cd $WORKSPACE/deployment/heat/onap-rke
129     envsubst < $ENV_FILE > $ENV_FILE~
130     if [ -z "$vm_num" ]; then
131         cp onap-oom.yaml onap-oom.yaml~
132     else
133         ./scripts/gen-onap-oom-yaml.sh $vm_num > onap-oom.yaml~
134     fi
135
136     if ! openstack stack create -t ./onap-oom.yaml~ -e $ENV_FILE~ $stack_name --parameter docker_manifest=$docker_manifest --parameter portal_hostname=$portal_hostname; then
137         break
138     fi
139
140     while [ "CREATE_IN_PROGRESS" == "$(openstack stack show -c stack_status -f value $stack_name)" ]; do
141         sleep 20
142     done
143
144     STATUS=$(openstack stack show -c stack_status -f value $stack_name)
145     echo $STATUS
146     if [ "CREATE_COMPLETE" != "$STATUS" ]; then
147         break
148     fi
149
150     for i in $(seq 1 30); do
151         sleep 30
152         RANCHER_IP=$(openstack stack output show $stack_name rancher_vm_ip -c output_value -f value)
153         K8S_IP=$(openstack stack output show $stack_name k8s_01_vm_ip -c output_value -f value)
154         timeout 1 ping -c 1 "$RANCHER_IP" && break
155     done
156
157     timeout 1 ping -c 1 "$RANCHER_IP" && break
158
159     echo Error: OpenStack infrastructure issue: unable to reach rancher "$RANCHER_IP"
160     sleep 10
161 done
162
163 if ! timeout 1 ping -c 1 "$RANCHER_IP"; then
164     exit 2
165 fi
166
167 # wait until all k8s VMs have fully initialized
168 for VM_NAME in $(grep _vm: ./onap-oom.yaml~ | cut -d: -f1); do
169     echo $VM_NAME
170     VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value)
171     ssh-keygen -R $VM_IP
172     until ssh -o StrictHostKeychecking=no -i $SSH_KEY ubuntu@$VM_IP ls -ad /dockerdata-nfs/.git; do
173         sleep 1m
174     done
175 done
176
177 cat > ./cluster.yml~ <<EOF
178 # GENERATED for $stack_name
179 nodes:
180 EOF
181
182 for VM_NAME in $(grep -E 'k8s_.+_vm:' ./onap-oom.yaml~ | cut -d: -f1); do
183     echo $VM_NAME
184     VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value)
185     VM_PRIVATE_IP=$(openstack stack output show $stack_name ${VM_NAME}_private_ip -c output_value -f value)
186     VM_HOSTNAME=$stack_name-$(echo $VM_NAME | tr '_' '-' | cut -d- -f1,2)
187     cat >> ./cluster.yml~ <<EOF
188 - address: $VM_IP
189   port: "22"
190   internal_address: $VM_PRIVATE_IP
191   role:
192   - worker
193   hostname_override: "$VM_HOSTNAME"
194   user: ubuntu
195   ssh_key_path: "$SSH_KEY"
196 EOF
197 done
198
199 for VM_NAME in $(grep -E 'orch_.+_vm:' ./onap-oom.yaml~ | cut -d: -f1); do
200     echo $VM_NAME
201     VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value)
202     VM_PRIVATE_IP=$(openstack stack output show $stack_name ${VM_NAME}_private_ip -c output_value -f value)
203     VM_HOSTNAME=$stack_name-$(echo $VM_NAME | tr '_' '-' | cut -d- -f1,2)
204     cat >> ./cluster.yml~ <<EOF
205 - address: $VM_IP
206   port: "22"
207   internal_address: $VM_PRIVATE_IP
208   role:
209   - controlplane
210   - etcd
211   hostname_override: "$VM_HOSTNAME"
212   user: ubuntu
213   ssh_key_path: "$SSH_KEY"
214 EOF
215 done
216
217 DOCKER_PROXY=$(openstack stack output show $stack_name docker_proxy -c output_value -f value)
218
219 cat >> ./cluster.yml~ <<EOF
220 services:
221   kube-api:
222     service_cluster_ip_range: 10.43.0.0/16
223     pod_security_policy: false
224     always_pull_images: false
225   kube-controller:
226     cluster_cidr: 10.42.0.0/16
227     service_cluster_ip_range: 10.43.0.0/16
228   kubelet:
229     cluster_domain: cluster.local
230     cluster_dns_server: 10.43.0.10
231     fail_swap_on: false
232 network:
233   plugin: canal
234 authentication:
235   strategy: x509
236 ssh_key_path: "$SSH_KEY"
237 ssh_agent_auth: false
238 authorization:
239   mode: rbac
240 ignore_docker_version: false
241 kubernetes_version: "v1.13.4-rancher1-2"
242 private_registries:
243 - url: $DOCKER_PROXY
244   is_default: true
245 cluster_name: "$stack_name"
246 restore:
247   restore: false
248   snapshot_name: ""
249 EOF
250
251 rm -rf ./target
252 mkdir -p ./target
253 cp ./cluster.yml~ ./target/cluster.yml
254 pushd ./target
255
256 # spin up k8s with RKE
257 until rke up; do
258     sleep 1m
259     rke remove
260 done
261
262 scp ./kube_config_cluster.yml root@$RANCHER_IP:/root/.kube/config
263 popd
264
265
266 sleep 2m
267 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)"
268
269 exit 0