Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 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 | |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 12 | stack_name="onap" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 13 | portal_hostname="portal.api.simpledemo.onap.org" |
| 14 | full_deletion=false |
| 15 | |
Gary Wu | 17314cd | 2019-06-27 07:19:36 -0700 | [diff] [blame] | 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 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 22 | if [ -z "$WORKSPACE" ]; then |
Gary Wu | 17314cd | 2019-06-27 07:19:36 -0700 | [diff] [blame] | 23 | export WORKSPACE=$(git rev-parse --show-toplevel) |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 24 | fi |
| 25 | |
Gary Wu | 17314cd | 2019-06-27 07:19:36 -0700 | [diff] [blame] | 26 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 27 | usage() { |
Brian Freeman | a2b0671 | 2019-10-31 12:28:25 -0500 | [diff] [blame] | 28 | 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 Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 29 | |
Brian Freeman | a2b0671 | 2019-10-31 12:28:25 -0500 | [diff] [blame] | 30 | echo "b: branch for staging image override This must be staging to trigger staging image override." 1>&2; |
Gary Wu | d154504 | 2019-06-27 10:30:52 -0700 | [diff] [blame] | 31 | 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 Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 38 | |
| 39 | exit 1; |
| 40 | } |
| 41 | |
| 42 | |
Brian Freeman | a2b0671 | 2019-10-31 12:28:25 -0500 | [diff] [blame] | 43 | while getopts ":b:n:s:d:i:o:rq" o; do |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 44 | case "${o}" in |
Brian Freeman | a2b0671 | 2019-10-31 12:28:25 -0500 | [diff] [blame] | 45 | b) |
| 46 | if [[ ! ${OPTARG} =~ ^[0-9]+$ ]];then |
| 47 | branch=${OPTARG} |
| 48 | else |
| 49 | branch=master |
| 50 | fi |
| 51 | ;; |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 52 | 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 Wu | 17314cd | 2019-06-27 07:19:36 -0700 | [diff] [blame] | 77 | i) |
| 78 | integration_gerrit_branch=${OPTARG} |
| 79 | ;; |
| 80 | o) |
| 81 | oom_gerrit_branch=${OPTARG} |
| 82 | ;; |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 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 | |
Gary Wu | 17314cd | 2019-06-27 07:19:36 -0700 | [diff] [blame] | 127 | if ! hash openstack jq java |
Gary Wu | a8aed9b | 2019-06-21 12:08:42 -0700 | [diff] [blame] | 128 | then |
Gary Wu | 17314cd | 2019-06-27 07:19:36 -0700 | [diff] [blame] | 129 | echo "ERROR: Required commands not found; please install openstack CLI, jq, java." |
Gary Wu | a8aed9b | 2019-06-21 12:08:42 -0700 | [diff] [blame] | 130 | exit 2 |
| 131 | fi |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 132 | |
Gary Wu | 28c30b5 | 2019-04-05 14:01:10 -0700 | [diff] [blame] | 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 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 136 | #Use new encryption method |
| 137 | pushd $WORKSPACE/deployment/heat/onap-rke/scripts |
| 138 | javac Crypto.java |
Gary Wu | 28c30b5 | 2019-04-05 14:01:10 -0700 | [diff] [blame] | 139 | #SO_ENCRYPTION_KEY=aa3871669d893c7fb8abbcda31b88b4f |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 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 |
Gary Wu | a8aed9b | 2019-06-21 12:08:42 -0700 | [diff] [blame] | 145 | $WORKSPACE/deployment/heat/onap-rke/scripts/teardown-onap.sh -n $stack_name -q |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 146 | else |
Gary Wu | a8aed9b | 2019-06-21 12:08:42 -0700 | [diff] [blame] | 147 | $WORKSPACE/deployment/heat/onap-rke/scripts/teardown-onap.sh -n $stack_name |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 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 | |
Brian Freeman | a2b0671 | 2019-10-31 12:28:25 -0500 | [diff] [blame] | 158 | if [ "$branch" == "staging" ] ; then |
Brian Freeman | 4b4735f | 2019-11-01 10:37:33 -0500 | [diff] [blame] | 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 |
Brian Freeman | a2b0671 | 2019-10-31 12:28:25 -0500 | [diff] [blame] | 160 | break |
Brian Freeman | 4b4735f | 2019-11-01 10:37:33 -0500 | [diff] [blame] | 161 | fi |
Brian Freeman | a2b0671 | 2019-10-31 12:28:25 -0500 | [diff] [blame] | 162 | else |
| 163 | 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 |
| 164 | break |
Brian Freeman | 4b4735f | 2019-11-01 10:37:33 -0500 | [diff] [blame] | 165 | fi |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 166 | fi |
| 167 | |
| 168 | while [ "CREATE_IN_PROGRESS" == "$(openstack stack show -c stack_status -f value $stack_name)" ]; do |
| 169 | sleep 20 |
| 170 | done |
| 171 | |
| 172 | STATUS=$(openstack stack show -c stack_status -f value $stack_name) |
| 173 | echo $STATUS |
| 174 | if [ "CREATE_COMPLETE" != "$STATUS" ]; then |
| 175 | break |
| 176 | fi |
| 177 | |
| 178 | for i in $(seq 1 30); do |
| 179 | sleep 30 |
Gary Wu | d95bf2b | 2019-06-21 15:35:18 -0700 | [diff] [blame] | 180 | NFS_IP=$(openstack stack output show $stack_name nfs_vm_ip -c output_value -f value) |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 181 | K8S_IP=$(openstack stack output show $stack_name k8s_01_vm_ip -c output_value -f value) |
Gary Wu | d95bf2b | 2019-06-21 15:35:18 -0700 | [diff] [blame] | 182 | timeout 1 ping -c 1 "$NFS_IP" && break |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 183 | done |
| 184 | |
Gary Wu | d95bf2b | 2019-06-21 15:35:18 -0700 | [diff] [blame] | 185 | timeout 1 ping -c 1 "$NFS_IP" && break |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 186 | |
Gary Wu | d95bf2b | 2019-06-21 15:35:18 -0700 | [diff] [blame] | 187 | echo Error: OpenStack infrastructure issue: unable to reach NFS server "$NFS_IP" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 188 | sleep 10 |
Brian Freeman | 4b4735f | 2019-11-01 10:37:33 -0500 | [diff] [blame] | 189 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 190 | done |
| 191 | |
Gary Wu | d95bf2b | 2019-06-21 15:35:18 -0700 | [diff] [blame] | 192 | if ! timeout 1 ping -c 1 "$NFS_IP"; then |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 193 | exit 2 |
| 194 | fi |
| 195 | |
| 196 | # wait until all k8s VMs have fully initialized |
| 197 | for VM_NAME in $(grep _vm: ./onap-oom.yaml~ | cut -d: -f1); do |
| 198 | echo $VM_NAME |
| 199 | VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value) |
| 200 | ssh-keygen -R $VM_IP |
| 201 | until ssh -o StrictHostKeychecking=no -i $SSH_KEY ubuntu@$VM_IP ls -ad /dockerdata-nfs/.git; do |
| 202 | sleep 1m |
| 203 | done |
| 204 | done |
| 205 | |
| 206 | cat > ./cluster.yml~ <<EOF |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 207 | # GENERATED for $stack_name |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 208 | nodes: |
| 209 | EOF |
| 210 | |
| 211 | for VM_NAME in $(grep -E 'k8s_.+_vm:' ./onap-oom.yaml~ | cut -d: -f1); do |
| 212 | echo $VM_NAME |
| 213 | VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value) |
| 214 | VM_PRIVATE_IP=$(openstack stack output show $stack_name ${VM_NAME}_private_ip -c output_value -f value) |
| 215 | VM_HOSTNAME=$stack_name-$(echo $VM_NAME | tr '_' '-' | cut -d- -f1,2) |
| 216 | cat >> ./cluster.yml~ <<EOF |
| 217 | - address: $VM_IP |
| 218 | port: "22" |
| 219 | internal_address: $VM_PRIVATE_IP |
| 220 | role: |
| 221 | - worker |
| 222 | hostname_override: "$VM_HOSTNAME" |
| 223 | user: ubuntu |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 224 | ssh_key_path: "$SSH_KEY" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 225 | EOF |
| 226 | done |
| 227 | |
| 228 | for VM_NAME in $(grep -E 'orch_.+_vm:' ./onap-oom.yaml~ | cut -d: -f1); do |
| 229 | echo $VM_NAME |
| 230 | VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value) |
| 231 | VM_PRIVATE_IP=$(openstack stack output show $stack_name ${VM_NAME}_private_ip -c output_value -f value) |
| 232 | VM_HOSTNAME=$stack_name-$(echo $VM_NAME | tr '_' '-' | cut -d- -f1,2) |
| 233 | cat >> ./cluster.yml~ <<EOF |
| 234 | - address: $VM_IP |
| 235 | port: "22" |
| 236 | internal_address: $VM_PRIVATE_IP |
| 237 | role: |
| 238 | - controlplane |
| 239 | - etcd |
| 240 | hostname_override: "$VM_HOSTNAME" |
| 241 | user: ubuntu |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 242 | ssh_key_path: "$SSH_KEY" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 243 | EOF |
| 244 | done |
| 245 | |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 246 | DOCKER_PROXY=$(openstack stack output show $stack_name docker_proxy -c output_value -f value) |
| 247 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 248 | cat >> ./cluster.yml~ <<EOF |
| 249 | services: |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 250 | kube-api: |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 251 | service_cluster_ip_range: 10.43.0.0/16 |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 252 | pod_security_policy: false |
| 253 | always_pull_images: false |
| 254 | kube-controller: |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 255 | cluster_cidr: 10.42.0.0/16 |
| 256 | service_cluster_ip_range: 10.43.0.0/16 |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 257 | kubelet: |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 258 | cluster_domain: cluster.local |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 259 | cluster_dns_server: 10.43.0.10 |
| 260 | fail_swap_on: false |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 261 | network: |
| 262 | plugin: canal |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 263 | authentication: |
| 264 | strategy: x509 |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 265 | ssh_key_path: "$SSH_KEY" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 266 | ssh_agent_auth: false |
| 267 | authorization: |
| 268 | mode: rbac |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 269 | ignore_docker_version: false |
Marco Platania | 190a2f4 | 2019-08-29 13:25:50 -0400 | [diff] [blame] | 270 | kubernetes_version: "v1.15.3-rancher1-1" |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 271 | private_registries: |
| 272 | - url: $DOCKER_PROXY |
| 273 | is_default: true |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 274 | cluster_name: "$stack_name" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 275 | restore: |
| 276 | restore: false |
| 277 | snapshot_name: "" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 278 | EOF |
| 279 | |
| 280 | rm -rf ./target |
| 281 | mkdir -p ./target |
| 282 | cp ./cluster.yml~ ./target/cluster.yml |
| 283 | pushd ./target |
| 284 | |
Marco Platania | 190a2f4 | 2019-08-29 13:25:50 -0400 | [diff] [blame] | 285 | wget https://github.com/rancher/rke/releases/download/v0.2.8/rke_linux-amd64 |
Gary Wu | 137e2b8 | 2019-04-24 11:16:24 -0700 | [diff] [blame] | 286 | mv rke_linux-amd64 rke |
| 287 | chmod +x rke |
| 288 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 289 | # spin up k8s with RKE |
Gary Wu | 137e2b8 | 2019-04-24 11:16:24 -0700 | [diff] [blame] | 290 | until ./rke up; do |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 291 | sleep 1m |
Gary Wu | 137e2b8 | 2019-04-24 11:16:24 -0700 | [diff] [blame] | 292 | ./rke remove |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 293 | done |
| 294 | |
Gary Wu | d95bf2b | 2019-06-21 15:35:18 -0700 | [diff] [blame] | 295 | scp -i $SSH_KEY ./kube_config_cluster.yml root@$NFS_IP:/root/.kube/config |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 296 | popd |
| 297 | |
| 298 | |
| 299 | sleep 2m |
Gary Wu | d95bf2b | 2019-06-21 15:35:18 -0700 | [diff] [blame] | 300 | 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)" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 301 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 302 | exit 0 |