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 | |
| 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 | |
Gary Wu | a8aed9b | 2019-06-21 12:08:42 -0700 | [diff] [blame] | 112 | if ! hash openstack jq |
| 113 | then |
| 114 | echo "ERROR: Required commands not found; please install openstack CLI and jq." |
| 115 | exit 2 |
| 116 | fi |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 117 | |
Gary Wu | 28c30b5 | 2019-04-05 14:01:10 -0700 | [diff] [blame] | 118 | SO_ENCRYPTION_KEY=aa3871669d893c7fb8abbcda31b88b4f |
| 119 | export OS_PASSWORD_ENCRYPTED_FOR_ROBOT=$(echo -n "$OS_PASSWORD" | openssl aes-128-ecb -e -K "$SO_ENCRYPTION_KEY" -nosalt | xxd -c 256 -p) |
| 120 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 121 | #Use new encryption method |
| 122 | pushd $WORKSPACE/deployment/heat/onap-rke/scripts |
| 123 | javac Crypto.java |
Gary Wu | 28c30b5 | 2019-04-05 14:01:10 -0700 | [diff] [blame] | 124 | #SO_ENCRYPTION_KEY=aa3871669d893c7fb8abbcda31b88b4f |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 125 | export OS_PASSWORD_ENCRYPTED=$(java Crypto "$OS_PASSWORD" "$SO_ENCRYPTION_KEY") |
| 126 | popd |
| 127 | |
| 128 | for n in $(seq 1 5); do |
| 129 | if [ $full_deletion = true ] ; then |
Gary Wu | a8aed9b | 2019-06-21 12:08:42 -0700 | [diff] [blame] | 130 | $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] | 131 | else |
Gary Wu | a8aed9b | 2019-06-21 12:08:42 -0700 | [diff] [blame] | 132 | $WORKSPACE/deployment/heat/onap-rke/scripts/teardown-onap.sh -n $stack_name |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 133 | fi |
| 134 | |
| 135 | cd $WORKSPACE/deployment/heat/onap-rke |
| 136 | envsubst < $ENV_FILE > $ENV_FILE~ |
| 137 | if [ -z "$vm_num" ]; then |
| 138 | cp onap-oom.yaml onap-oom.yaml~ |
| 139 | else |
| 140 | ./scripts/gen-onap-oom-yaml.sh $vm_num > onap-oom.yaml~ |
| 141 | fi |
| 142 | |
| 143 | if ! openstack stack create -t ./onap-oom.yaml~ -e $ENV_FILE~ $stack_name --parameter docker_manifest=$docker_manifest --parameter portal_hostname=$portal_hostname; then |
| 144 | break |
| 145 | fi |
| 146 | |
| 147 | while [ "CREATE_IN_PROGRESS" == "$(openstack stack show -c stack_status -f value $stack_name)" ]; do |
| 148 | sleep 20 |
| 149 | done |
| 150 | |
| 151 | STATUS=$(openstack stack show -c stack_status -f value $stack_name) |
| 152 | echo $STATUS |
| 153 | if [ "CREATE_COMPLETE" != "$STATUS" ]; then |
| 154 | break |
| 155 | fi |
| 156 | |
| 157 | for i in $(seq 1 30); do |
| 158 | sleep 30 |
| 159 | RANCHER_IP=$(openstack stack output show $stack_name rancher_vm_ip -c output_value -f value) |
| 160 | K8S_IP=$(openstack stack output show $stack_name k8s_01_vm_ip -c output_value -f value) |
| 161 | timeout 1 ping -c 1 "$RANCHER_IP" && break |
| 162 | done |
| 163 | |
| 164 | timeout 1 ping -c 1 "$RANCHER_IP" && break |
| 165 | |
| 166 | echo Error: OpenStack infrastructure issue: unable to reach rancher "$RANCHER_IP" |
| 167 | sleep 10 |
| 168 | done |
| 169 | |
| 170 | if ! timeout 1 ping -c 1 "$RANCHER_IP"; then |
| 171 | exit 2 |
| 172 | fi |
| 173 | |
| 174 | # wait until all k8s VMs have fully initialized |
| 175 | for VM_NAME in $(grep _vm: ./onap-oom.yaml~ | cut -d: -f1); do |
| 176 | echo $VM_NAME |
| 177 | VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value) |
| 178 | ssh-keygen -R $VM_IP |
| 179 | until ssh -o StrictHostKeychecking=no -i $SSH_KEY ubuntu@$VM_IP ls -ad /dockerdata-nfs/.git; do |
| 180 | sleep 1m |
| 181 | done |
| 182 | done |
| 183 | |
| 184 | cat > ./cluster.yml~ <<EOF |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 185 | # GENERATED for $stack_name |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 186 | nodes: |
| 187 | EOF |
| 188 | |
| 189 | for VM_NAME in $(grep -E 'k8s_.+_vm:' ./onap-oom.yaml~ | cut -d: -f1); do |
| 190 | echo $VM_NAME |
| 191 | VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value) |
| 192 | VM_PRIVATE_IP=$(openstack stack output show $stack_name ${VM_NAME}_private_ip -c output_value -f value) |
| 193 | VM_HOSTNAME=$stack_name-$(echo $VM_NAME | tr '_' '-' | cut -d- -f1,2) |
| 194 | cat >> ./cluster.yml~ <<EOF |
| 195 | - address: $VM_IP |
| 196 | port: "22" |
| 197 | internal_address: $VM_PRIVATE_IP |
| 198 | role: |
| 199 | - worker |
| 200 | hostname_override: "$VM_HOSTNAME" |
| 201 | user: ubuntu |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 202 | ssh_key_path: "$SSH_KEY" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 203 | EOF |
| 204 | done |
| 205 | |
| 206 | for VM_NAME in $(grep -E 'orch_.+_vm:' ./onap-oom.yaml~ | cut -d: -f1); do |
| 207 | echo $VM_NAME |
| 208 | VM_IP=$(openstack stack output show $stack_name ${VM_NAME}_ip -c output_value -f value) |
| 209 | VM_PRIVATE_IP=$(openstack stack output show $stack_name ${VM_NAME}_private_ip -c output_value -f value) |
| 210 | VM_HOSTNAME=$stack_name-$(echo $VM_NAME | tr '_' '-' | cut -d- -f1,2) |
| 211 | cat >> ./cluster.yml~ <<EOF |
| 212 | - address: $VM_IP |
| 213 | port: "22" |
| 214 | internal_address: $VM_PRIVATE_IP |
| 215 | role: |
| 216 | - controlplane |
| 217 | - etcd |
| 218 | hostname_override: "$VM_HOSTNAME" |
| 219 | user: ubuntu |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 220 | ssh_key_path: "$SSH_KEY" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 221 | EOF |
| 222 | done |
| 223 | |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 224 | DOCKER_PROXY=$(openstack stack output show $stack_name docker_proxy -c output_value -f value) |
| 225 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 226 | cat >> ./cluster.yml~ <<EOF |
| 227 | services: |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 228 | kube-api: |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 229 | service_cluster_ip_range: 10.43.0.0/16 |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 230 | pod_security_policy: false |
| 231 | always_pull_images: false |
| 232 | kube-controller: |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 233 | cluster_cidr: 10.42.0.0/16 |
| 234 | service_cluster_ip_range: 10.43.0.0/16 |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 235 | kubelet: |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 236 | cluster_domain: cluster.local |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 237 | cluster_dns_server: 10.43.0.10 |
| 238 | fail_swap_on: false |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 239 | network: |
| 240 | plugin: canal |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 241 | authentication: |
| 242 | strategy: x509 |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 243 | ssh_key_path: "$SSH_KEY" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 244 | ssh_agent_auth: false |
| 245 | authorization: |
| 246 | mode: rbac |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 247 | ignore_docker_version: false |
Gary Wu | 137e2b8 | 2019-04-24 11:16:24 -0700 | [diff] [blame] | 248 | kubernetes_version: "v1.13.5-rancher1-2" |
Gary Wu | 0bc6983 | 2019-03-27 13:58:46 -0700 | [diff] [blame] | 249 | private_registries: |
| 250 | - url: $DOCKER_PROXY |
| 251 | is_default: true |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 252 | cluster_name: "$stack_name" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 253 | restore: |
| 254 | restore: false |
| 255 | snapshot_name: "" |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 256 | EOF |
| 257 | |
| 258 | rm -rf ./target |
| 259 | mkdir -p ./target |
| 260 | cp ./cluster.yml~ ./target/cluster.yml |
| 261 | pushd ./target |
| 262 | |
Gary Wu | 137e2b8 | 2019-04-24 11:16:24 -0700 | [diff] [blame] | 263 | wget https://github.com/rancher/rke/releases/download/v0.2.1/rke_linux-amd64 |
| 264 | mv rke_linux-amd64 rke |
| 265 | chmod +x rke |
| 266 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 267 | # spin up k8s with RKE |
Gary Wu | 137e2b8 | 2019-04-24 11:16:24 -0700 | [diff] [blame] | 268 | until ./rke up; do |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 269 | sleep 1m |
Gary Wu | 137e2b8 | 2019-04-24 11:16:24 -0700 | [diff] [blame] | 270 | ./rke remove |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 271 | done |
| 272 | |
Gary Wu | 8ca8538 | 2019-05-14 08:10:00 -0700 | [diff] [blame] | 273 | scp -i $SSH_KEY ./kube_config_cluster.yml root@$RANCHER_IP:/root/.kube/config |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 274 | popd |
| 275 | |
| 276 | |
| 277 | sleep 2m |
| 278 | 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)" |
| 279 | |
Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 280 | exit 0 |