blob: 935e69424288a9d0d5e1beec4ab3004bb514221e [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() {
Brian Freemana2b06712019-10-31 12:28:25 -050028 echo "Usage: $0 [-b staging] [ -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
Brian Freemana2b06712019-10-31 12:28:25 -050030 echo "b: branch for staging image override This must be staging to trigger staging image override." 1>&2;
Gary Wud1545042019-06-27 10:30:52 -070031 echo "n: Number of worker VMs to deploy. This number must be between 2 and 15." 1>&2;
32 echo "s: Stack name. This name will be used for naming of resources." 1>&2;
33 echo "d: Base domain name to be used in portal UI URLs." 1>&2;
34 echo "i: Branch of integration repo to clone." 1>&2;
35 echo "o: Branch of oom repo to clone." 1>&2;
36 echo "r: Delete all ONAP resource within tenant." 1>&2;
37 echo "q: Quiet delete of all ONAP resources within tenant." 1>&2;
Gary Wu950a3232019-03-26 13:08:29 -070038
39 exit 1;
40}
41
42
Brian Freemana2b06712019-10-31 12:28:25 -050043while getopts ":b:n:s:d:i:o:rq" o; do
Gary Wu950a3232019-03-26 13:08:29 -070044 case "${o}" in
Brian Freemana2b06712019-10-31 12:28:25 -050045 b)
46 if [[ ! ${OPTARG} =~ ^[0-9]+$ ]];then
47 branch=${OPTARG}
48 else
49 branch=master
50 fi
51 ;;
Gary Wu950a3232019-03-26 13:08:29 -070052 n)
53 if [[ ${OPTARG} =~ ^[0-9]+$ ]];then
54 if [ ${OPTARG} -ge 2 -a ${OPTARG} -le 15 ]; then
55 vm_num=${OPTARG}
56 else
57 usage
58 fi
59 else
60 usage
61 fi
62 ;;
63 s)
64 if [[ ! ${OPTARG} =~ ^[0-9]+$ ]];then
65 stack_name=${OPTARG}
66 else
67 usage
68 fi
69 ;;
70 d)
71 if [[ ! ${OPTARG} =~ ^[0-9]+$ ]];then
72 portal_hostname=${OPTARG}
73 else
74 usage
75 fi
76 ;;
Gary Wu17314cd2019-06-27 07:19:36 -070077 i)
78 integration_gerrit_branch=${OPTARG}
79 ;;
80 o)
81 oom_gerrit_branch=${OPTARG}
82 ;;
Gary Wu950a3232019-03-26 13:08:29 -070083 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
109done
110shift $((OPTIND-1))
111
112if [ "$#" -ne 1 ]; then
113 usage
114fi
115
116ENV_FILE=$1
117
118if [ ! -f $ENV_FILE ];then
119 echo ENV file does not exist or was not given
120 exit 1
121fi
122
123set -x
124
125SSH_KEY=~/.ssh/onap_key
126
Gary Wu17314cd2019-06-27 07:19:36 -0700127if ! hash openstack jq java
Gary Wua8aed9b2019-06-21 12:08:42 -0700128then
Gary Wu17314cd2019-06-27 07:19:36 -0700129 echo "ERROR: Required commands not found; please install openstack CLI, jq, java."
Gary Wua8aed9b2019-06-21 12:08:42 -0700130 exit 2
131fi
Gary Wu950a3232019-03-26 13:08:29 -0700132
Gary Wu28c30b52019-04-05 14:01:10 -0700133SO_ENCRYPTION_KEY=aa3871669d893c7fb8abbcda31b88b4f
134export OS_PASSWORD_ENCRYPTED_FOR_ROBOT=$(echo -n "$OS_PASSWORD" | openssl aes-128-ecb -e -K "$SO_ENCRYPTION_KEY" -nosalt | xxd -c 256 -p)
135
Gary Wu950a3232019-03-26 13:08:29 -0700136#Use new encryption method
137pushd $WORKSPACE/deployment/heat/onap-rke/scripts
138javac Crypto.java
Gary Wu28c30b52019-04-05 14:01:10 -0700139#SO_ENCRYPTION_KEY=aa3871669d893c7fb8abbcda31b88b4f
Gary Wu950a3232019-03-26 13:08:29 -0700140export OS_PASSWORD_ENCRYPTED=$(java Crypto "$OS_PASSWORD" "$SO_ENCRYPTION_KEY")
141popd
142
143for n in $(seq 1 5); do
144 if [ $full_deletion = true ] ; then
Gary Wua8aed9b2019-06-21 12:08:42 -0700145 $WORKSPACE/deployment/heat/onap-rke/scripts/teardown-onap.sh -n $stack_name -q
Gary Wu950a3232019-03-26 13:08:29 -0700146 else
Gary Wua8aed9b2019-06-21 12:08:42 -0700147 $WORKSPACE/deployment/heat/onap-rke/scripts/teardown-onap.sh -n $stack_name
Gary Wu950a3232019-03-26 13:08:29 -0700148 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
Brian Freemana2b06712019-10-31 12:28:25 -0500158 if [ "$branch" == "staging" ] ; then
159 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 --parameter additional_override="~/integration/deployment/heat/onap-rke/staging-image-override.yaml" ; then
160 break
161 else
162 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
163 break
Gary Wu950a3232019-03-26 13:08:29 -0700164 fi
165
166 while [ "CREATE_IN_PROGRESS" == "$(openstack stack show -c stack_status -f value $stack_name)" ]; do
167 sleep 20
168 done
169
170 STATUS=$(openstack stack show -c stack_status -f value $stack_name)
171 echo $STATUS
172 if [ "CREATE_COMPLETE" != "$STATUS" ]; then
173 break
174 fi
175
176 for i in $(seq 1 30); do
177 sleep 30
Gary Wud95bf2b2019-06-21 15:35:18 -0700178 NFS_IP=$(openstack stack output show $stack_name nfs_vm_ip -c output_value -f value)
Gary Wu950a3232019-03-26 13:08:29 -0700179 K8S_IP=$(openstack stack output show $stack_name k8s_01_vm_ip -c output_value -f value)
Gary Wud95bf2b2019-06-21 15:35:18 -0700180 timeout 1 ping -c 1 "$NFS_IP" && break
Gary Wu950a3232019-03-26 13:08:29 -0700181 done
182
Gary Wud95bf2b2019-06-21 15:35:18 -0700183 timeout 1 ping -c 1 "$NFS_IP" && break
Gary Wu950a3232019-03-26 13:08:29 -0700184
Gary Wud95bf2b2019-06-21 15:35:18 -0700185 echo Error: OpenStack infrastructure issue: unable to reach NFS server "$NFS_IP"
Gary Wu950a3232019-03-26 13:08:29 -0700186 sleep 10
187done
188
Gary Wud95bf2b2019-06-21 15:35:18 -0700189if ! timeout 1 ping -c 1 "$NFS_IP"; then
Gary Wu950a3232019-03-26 13:08:29 -0700190 exit 2
191fi
192
193# wait until all k8s VMs have fully initialized
194for VM_NAME in $(grep _vm: ./onap-oom.yaml~ | cut -d: -f1); do
195 echo $VM_NAME
196 VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value)
197 ssh-keygen -R $VM_IP
198 until ssh -o StrictHostKeychecking=no -i $SSH_KEY ubuntu@$VM_IP ls -ad /dockerdata-nfs/.git; do
199 sleep 1m
200 done
201done
202
203cat > ./cluster.yml~ <<EOF
Gary Wu0bc69832019-03-27 13:58:46 -0700204# GENERATED for $stack_name
Gary Wu950a3232019-03-26 13:08:29 -0700205nodes:
206EOF
207
208for VM_NAME in $(grep -E 'k8s_.+_vm:' ./onap-oom.yaml~ | cut -d: -f1); do
209 echo $VM_NAME
210 VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value)
211 VM_PRIVATE_IP=$(openstack stack output show $stack_name ${VM_NAME}_private_ip -c output_value -f value)
212 VM_HOSTNAME=$stack_name-$(echo $VM_NAME | tr '_' '-' | cut -d- -f1,2)
213 cat >> ./cluster.yml~ <<EOF
214- address: $VM_IP
215 port: "22"
216 internal_address: $VM_PRIVATE_IP
217 role:
218 - worker
219 hostname_override: "$VM_HOSTNAME"
220 user: ubuntu
Gary Wu0bc69832019-03-27 13:58:46 -0700221 ssh_key_path: "$SSH_KEY"
Gary Wu950a3232019-03-26 13:08:29 -0700222EOF
223done
224
225for VM_NAME in $(grep -E 'orch_.+_vm:' ./onap-oom.yaml~ | cut -d: -f1); do
226 echo $VM_NAME
227 VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value)
228 VM_PRIVATE_IP=$(openstack stack output show $stack_name ${VM_NAME}_private_ip -c output_value -f value)
229 VM_HOSTNAME=$stack_name-$(echo $VM_NAME | tr '_' '-' | cut -d- -f1,2)
230 cat >> ./cluster.yml~ <<EOF
231- address: $VM_IP
232 port: "22"
233 internal_address: $VM_PRIVATE_IP
234 role:
235 - controlplane
236 - etcd
237 hostname_override: "$VM_HOSTNAME"
238 user: ubuntu
Gary Wu0bc69832019-03-27 13:58:46 -0700239 ssh_key_path: "$SSH_KEY"
Gary Wu950a3232019-03-26 13:08:29 -0700240EOF
241done
242
Gary Wu0bc69832019-03-27 13:58:46 -0700243DOCKER_PROXY=$(openstack stack output show $stack_name docker_proxy -c output_value -f value)
244
Gary Wu950a3232019-03-26 13:08:29 -0700245cat >> ./cluster.yml~ <<EOF
246services:
Gary Wu950a3232019-03-26 13:08:29 -0700247 kube-api:
Gary Wu950a3232019-03-26 13:08:29 -0700248 service_cluster_ip_range: 10.43.0.0/16
Gary Wu950a3232019-03-26 13:08:29 -0700249 pod_security_policy: false
250 always_pull_images: false
251 kube-controller:
Gary Wu950a3232019-03-26 13:08:29 -0700252 cluster_cidr: 10.42.0.0/16
253 service_cluster_ip_range: 10.43.0.0/16
Gary Wu950a3232019-03-26 13:08:29 -0700254 kubelet:
Gary Wu950a3232019-03-26 13:08:29 -0700255 cluster_domain: cluster.local
Gary Wu950a3232019-03-26 13:08:29 -0700256 cluster_dns_server: 10.43.0.10
257 fail_swap_on: false
Gary Wu950a3232019-03-26 13:08:29 -0700258network:
259 plugin: canal
Gary Wu950a3232019-03-26 13:08:29 -0700260authentication:
261 strategy: x509
Gary Wu0bc69832019-03-27 13:58:46 -0700262ssh_key_path: "$SSH_KEY"
Gary Wu950a3232019-03-26 13:08:29 -0700263ssh_agent_auth: false
264authorization:
265 mode: rbac
Gary Wu950a3232019-03-26 13:08:29 -0700266ignore_docker_version: false
Marco Platania190a2f42019-08-29 13:25:50 -0400267kubernetes_version: "v1.15.3-rancher1-1"
Gary Wu0bc69832019-03-27 13:58:46 -0700268private_registries:
269- url: $DOCKER_PROXY
270 is_default: true
Gary Wu950a3232019-03-26 13:08:29 -0700271cluster_name: "$stack_name"
Gary Wu950a3232019-03-26 13:08:29 -0700272restore:
273 restore: false
274 snapshot_name: ""
Gary Wu950a3232019-03-26 13:08:29 -0700275EOF
276
277rm -rf ./target
278mkdir -p ./target
279cp ./cluster.yml~ ./target/cluster.yml
280pushd ./target
281
Marco Platania190a2f42019-08-29 13:25:50 -0400282wget https://github.com/rancher/rke/releases/download/v0.2.8/rke_linux-amd64
Gary Wu137e2b82019-04-24 11:16:24 -0700283mv rke_linux-amd64 rke
284chmod +x rke
285
Gary Wu950a3232019-03-26 13:08:29 -0700286# spin up k8s with RKE
Gary Wu137e2b82019-04-24 11:16:24 -0700287until ./rke up; do
Gary Wu950a3232019-03-26 13:08:29 -0700288 sleep 1m
Gary Wu137e2b82019-04-24 11:16:24 -0700289 ./rke remove
Gary Wu950a3232019-03-26 13:08:29 -0700290done
291
Gary Wud95bf2b2019-06-21 15:35:18 -0700292scp -i $SSH_KEY ./kube_config_cluster.yml root@$NFS_IP:/root/.kube/config
Gary Wu950a3232019-03-26 13:08:29 -0700293popd
294
295
296sleep 2m
Gary Wud95bf2b2019-06-21 15:35:18 -0700297ssh -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 -0700298
Gary Wu950a3232019-03-26 13:08:29 -0700299exit 0