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