blob: 456be2b6789ba2522fbe388ac39430e719bf7c61 [file] [log] [blame]
Gary Wu950a3232019-03-26 13:08:29 -07001#!/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
Gary Wu0bc69832019-03-27 13:58:46 -070012stack_name="onap"
Gary Wu950a3232019-03-26 13:08:29 -070013portal_hostname="portal.api.simpledemo.onap.org"
14full_deletion=false
15
Gary Wu17314cd2019-06-27 07:19:36 -070016# default branch for cloning integration repo
17integration_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
20oom_gerrit_branch=$(git rev-parse --abbrev-ref HEAD)
21
Gary Wu950a3232019-03-26 13:08:29 -070022if [ -z "$WORKSPACE" ]; then
Gary Wu17314cd2019-06-27 07:19:36 -070023 export WORKSPACE=$(git rev-parse --show-toplevel)
Gary Wu950a3232019-03-26 13:08:29 -070024fi
25
Gary Wu17314cd2019-06-27 07:19:36 -070026
Gary Wu950a3232019-03-26 13:08:29 -070027usage() {
Gary Wud1545042019-06-27 10:30:52 -070028 echo "Usage: $0 [ -n <number of VMs {2-15}> ][ -s <stack name> ][ -d <domain> ][ -i <integration_branch> ][ -o <oom_branch> ][ -r ][ -q ] <env>" 1>&2;
Gary Wu950a3232019-03-26 13:08:29 -070029
Gary Wud1545042019-06-27 10:30:52 -070030 echo "n: Number of worker VMs to deploy. This number must be between 2 and 15." 1>&2;
31 echo "s: Stack name. This name will be used for naming of resources." 1>&2;
32 echo "d: Base domain name to be used in portal UI URLs." 1>&2;
33 echo "i: Branch of integration repo to clone." 1>&2;
34 echo "o: Branch of oom repo to clone." 1>&2;
35 echo "r: Delete all ONAP resource within tenant." 1>&2;
36 echo "q: Quiet delete of all ONAP resources within tenant." 1>&2;
Gary Wu950a3232019-03-26 13:08:29 -070037
38 exit 1;
39}
40
41
Gary Wud1545042019-06-27 10:30:52 -070042while getopts ":n:s:d:i:o:rq" o; do
Gary Wu950a3232019-03-26 13:08:29 -070043 case "${o}" in
44 n)
45 if [[ ${OPTARG} =~ ^[0-9]+$ ]];then
46 if [ ${OPTARG} -ge 2 -a ${OPTARG} -le 15 ]; then
47 vm_num=${OPTARG}
48 else
49 usage
50 fi
51 else
52 usage
53 fi
54 ;;
55 s)
56 if [[ ! ${OPTARG} =~ ^[0-9]+$ ]];then
57 stack_name=${OPTARG}
58 else
59 usage
60 fi
61 ;;
62 d)
63 if [[ ! ${OPTARG} =~ ^[0-9]+$ ]];then
64 portal_hostname=${OPTARG}
65 else
66 usage
67 fi
68 ;;
Gary Wu17314cd2019-06-27 07:19:36 -070069 i)
70 integration_gerrit_branch=${OPTARG}
71 ;;
72 o)
73 oom_gerrit_branch=${OPTARG}
74 ;;
Gary Wu950a3232019-03-26 13:08:29 -070075 r)
76 echo "The following command will delete all information relating to onap within your enviroment"
77 read -p "Are you certain this is what you want? (type y to confirm):" answer
78
79 if [ $answer = "y" ] || [ $answer = "Y" ] || [ $answer = "yes" ] || [ $answer = "Yes"]; then
80 echo "This may delete the work of other colleages within the same enviroment"
81 read -p "Are you certain this is what you want? (type y to confirm):" answer2
82
83 if [ $answer2 = "y" ] || [ $answer2 = "Y" ] || [ $answer2 = "yes" ] || [ $answer2 = "Yes"]; then
84 full_deletion=true
85 else
86 echo "Ending program"
87 exit 1
88 fi
89 else
90 echo "Ending program"
91 exit 1
92 fi
93 ;;
94 q)
95 full_deletion=true
96 ;;
97 *)
98 usage
99 ;;
100 esac
101done
102shift $((OPTIND-1))
103
104if [ "$#" -ne 1 ]; then
105 usage
106fi
107
108ENV_FILE=$1
109
110if [ ! -f $ENV_FILE ];then
111 echo ENV file does not exist or was not given
112 exit 1
113fi
114
115set -x
116
117SSH_KEY=~/.ssh/onap_key
118
Gary Wu17314cd2019-06-27 07:19:36 -0700119if ! hash openstack jq java
Gary Wua8aed9b2019-06-21 12:08:42 -0700120then
Gary Wu17314cd2019-06-27 07:19:36 -0700121 echo "ERROR: Required commands not found; please install openstack CLI, jq, java."
Gary Wua8aed9b2019-06-21 12:08:42 -0700122 exit 2
123fi
Gary Wu950a3232019-03-26 13:08:29 -0700124
Gary Wu28c30b52019-04-05 14:01:10 -0700125SO_ENCRYPTION_KEY=aa3871669d893c7fb8abbcda31b88b4f
126export OS_PASSWORD_ENCRYPTED_FOR_ROBOT=$(echo -n "$OS_PASSWORD" | openssl aes-128-ecb -e -K "$SO_ENCRYPTION_KEY" -nosalt | xxd -c 256 -p)
127
Gary Wu950a3232019-03-26 13:08:29 -0700128#Use new encryption method
129pushd $WORKSPACE/deployment/heat/onap-rke/scripts
130javac Crypto.java
Gary Wu28c30b52019-04-05 14:01:10 -0700131#SO_ENCRYPTION_KEY=aa3871669d893c7fb8abbcda31b88b4f
Gary Wu950a3232019-03-26 13:08:29 -0700132export OS_PASSWORD_ENCRYPTED=$(java Crypto "$OS_PASSWORD" "$SO_ENCRYPTION_KEY")
133popd
134
135for n in $(seq 1 5); do
136 if [ $full_deletion = true ] ; then
Gary Wua8aed9b2019-06-21 12:08:42 -0700137 $WORKSPACE/deployment/heat/onap-rke/scripts/teardown-onap.sh -n $stack_name -q
Gary Wu950a3232019-03-26 13:08:29 -0700138 else
Gary Wua8aed9b2019-06-21 12:08:42 -0700139 $WORKSPACE/deployment/heat/onap-rke/scripts/teardown-onap.sh -n $stack_name
Gary Wu950a3232019-03-26 13:08:29 -0700140 fi
141
142 cd $WORKSPACE/deployment/heat/onap-rke
143 envsubst < $ENV_FILE > $ENV_FILE~
144 if [ -z "$vm_num" ]; then
145 cp onap-oom.yaml onap-oom.yaml~
146 else
147 ./scripts/gen-onap-oom-yaml.sh $vm_num > onap-oom.yaml~
148 fi
149
Gary Wu3adfd352019-07-09 11:23:17 -0700150 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 portal_hostname=$portal_hostname; then
Gary Wu950a3232019-03-26 13:08:29 -0700151 break
152 fi
153
154 while [ "CREATE_IN_PROGRESS" == "$(openstack stack show -c stack_status -f value $stack_name)" ]; do
155 sleep 20
156 done
157
158 STATUS=$(openstack stack show -c stack_status -f value $stack_name)
159 echo $STATUS
160 if [ "CREATE_COMPLETE" != "$STATUS" ]; then
161 break
162 fi
163
164 for i in $(seq 1 30); do
165 sleep 30
Gary Wud95bf2b2019-06-21 15:35:18 -0700166 NFS_IP=$(openstack stack output show $stack_name nfs_vm_ip -c output_value -f value)
Gary Wu950a3232019-03-26 13:08:29 -0700167 K8S_IP=$(openstack stack output show $stack_name k8s_01_vm_ip -c output_value -f value)
Gary Wud95bf2b2019-06-21 15:35:18 -0700168 timeout 1 ping -c 1 "$NFS_IP" && break
Gary Wu950a3232019-03-26 13:08:29 -0700169 done
170
Gary Wud95bf2b2019-06-21 15:35:18 -0700171 timeout 1 ping -c 1 "$NFS_IP" && break
Gary Wu950a3232019-03-26 13:08:29 -0700172
Gary Wud95bf2b2019-06-21 15:35:18 -0700173 echo Error: OpenStack infrastructure issue: unable to reach NFS server "$NFS_IP"
Gary Wu950a3232019-03-26 13:08:29 -0700174 sleep 10
175done
176
Gary Wud95bf2b2019-06-21 15:35:18 -0700177if ! timeout 1 ping -c 1 "$NFS_IP"; then
Gary Wu950a3232019-03-26 13:08:29 -0700178 exit 2
179fi
180
181# wait until all k8s VMs have fully initialized
182for VM_NAME in $(grep _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 ssh-keygen -R $VM_IP
186 until ssh -o StrictHostKeychecking=no -i $SSH_KEY ubuntu@$VM_IP ls -ad /dockerdata-nfs/.git; do
187 sleep 1m
188 done
189done
190
191cat > ./cluster.yml~ <<EOF
Gary Wu0bc69832019-03-27 13:58:46 -0700192# GENERATED for $stack_name
Gary Wu950a3232019-03-26 13:08:29 -0700193nodes:
194EOF
195
196for VM_NAME in $(grep -E 'k8s_.+_vm:' ./onap-oom.yaml~ | cut -d: -f1); do
197 echo $VM_NAME
198 VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value)
199 VM_PRIVATE_IP=$(openstack stack output show $stack_name ${VM_NAME}_private_ip -c output_value -f value)
200 VM_HOSTNAME=$stack_name-$(echo $VM_NAME | tr '_' '-' | cut -d- -f1,2)
201 cat >> ./cluster.yml~ <<EOF
202- address: $VM_IP
203 port: "22"
204 internal_address: $VM_PRIVATE_IP
205 role:
206 - worker
207 hostname_override: "$VM_HOSTNAME"
208 user: ubuntu
Gary Wu0bc69832019-03-27 13:58:46 -0700209 ssh_key_path: "$SSH_KEY"
Gary Wu950a3232019-03-26 13:08:29 -0700210EOF
211done
212
213for VM_NAME in $(grep -E 'orch_.+_vm:' ./onap-oom.yaml~ | cut -d: -f1); do
214 echo $VM_NAME
215 VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value)
216 VM_PRIVATE_IP=$(openstack stack output show $stack_name ${VM_NAME}_private_ip -c output_value -f value)
217 VM_HOSTNAME=$stack_name-$(echo $VM_NAME | tr '_' '-' | cut -d- -f1,2)
218 cat >> ./cluster.yml~ <<EOF
219- address: $VM_IP
220 port: "22"
221 internal_address: $VM_PRIVATE_IP
222 role:
223 - controlplane
224 - etcd
225 hostname_override: "$VM_HOSTNAME"
226 user: ubuntu
Gary Wu0bc69832019-03-27 13:58:46 -0700227 ssh_key_path: "$SSH_KEY"
Gary Wu950a3232019-03-26 13:08:29 -0700228EOF
229done
230
Gary Wu0bc69832019-03-27 13:58:46 -0700231DOCKER_PROXY=$(openstack stack output show $stack_name docker_proxy -c output_value -f value)
232
Gary Wu950a3232019-03-26 13:08:29 -0700233cat >> ./cluster.yml~ <<EOF
234services:
Gary Wu950a3232019-03-26 13:08:29 -0700235 kube-api:
Gary Wu950a3232019-03-26 13:08:29 -0700236 service_cluster_ip_range: 10.43.0.0/16
Gary Wu950a3232019-03-26 13:08:29 -0700237 pod_security_policy: false
238 always_pull_images: false
239 kube-controller:
Gary Wu950a3232019-03-26 13:08:29 -0700240 cluster_cidr: 10.42.0.0/16
241 service_cluster_ip_range: 10.43.0.0/16
Gary Wu950a3232019-03-26 13:08:29 -0700242 kubelet:
Gary Wu950a3232019-03-26 13:08:29 -0700243 cluster_domain: cluster.local
Gary Wu950a3232019-03-26 13:08:29 -0700244 cluster_dns_server: 10.43.0.10
245 fail_swap_on: false
Gary Wu950a3232019-03-26 13:08:29 -0700246network:
247 plugin: canal
Gary Wu950a3232019-03-26 13:08:29 -0700248authentication:
249 strategy: x509
Gary Wu0bc69832019-03-27 13:58:46 -0700250ssh_key_path: "$SSH_KEY"
Gary Wu950a3232019-03-26 13:08:29 -0700251ssh_agent_auth: false
252authorization:
253 mode: rbac
Gary Wu950a3232019-03-26 13:08:29 -0700254ignore_docker_version: false
Gary Wu137e2b82019-04-24 11:16:24 -0700255kubernetes_version: "v1.13.5-rancher1-2"
Gary Wu0bc69832019-03-27 13:58:46 -0700256private_registries:
257- url: $DOCKER_PROXY
258 is_default: true
Gary Wu950a3232019-03-26 13:08:29 -0700259cluster_name: "$stack_name"
Gary Wu950a3232019-03-26 13:08:29 -0700260restore:
261 restore: false
262 snapshot_name: ""
Gary Wu950a3232019-03-26 13:08:29 -0700263EOF
264
265rm -rf ./target
266mkdir -p ./target
267cp ./cluster.yml~ ./target/cluster.yml
268pushd ./target
269
Gary Wu137e2b82019-04-24 11:16:24 -0700270wget https://github.com/rancher/rke/releases/download/v0.2.1/rke_linux-amd64
271mv rke_linux-amd64 rke
272chmod +x rke
273
Gary Wu950a3232019-03-26 13:08:29 -0700274# spin up k8s with RKE
Gary Wu137e2b82019-04-24 11:16:24 -0700275until ./rke up; do
Gary Wu950a3232019-03-26 13:08:29 -0700276 sleep 1m
Gary Wu137e2b82019-04-24 11:16:24 -0700277 ./rke remove
Gary Wu950a3232019-03-26 13:08:29 -0700278done
279
Gary Wud95bf2b2019-06-21 15:35:18 -0700280scp -i $SSH_KEY ./kube_config_cluster.yml root@$NFS_IP:/root/.kube/config
Gary Wu950a3232019-03-26 13:08:29 -0700281popd
282
283
284sleep 2m
Gary Wud95bf2b2019-06-21 15:35:18 -0700285ssh -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)"
Gary Wu950a3232019-03-26 13:08:29 -0700286
Gary Wu950a3232019-03-26 13:08:29 -0700287exit 0