|
1 |
| -#!/bin/bash |
2 |
| -script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
3 |
| -source "${script_dir}/vars.source.sh" |
4 |
| -set -euo pipefail |
| 1 | +#! /usr/bin/env bash |
5 | 2 |
|
6 |
| -function retry(){ |
7 |
| - max_retries=$1 |
8 |
| - shift |
9 |
| - retries=0 |
10 |
| - command="$*" |
11 |
| - until [ "${retries}" -eq "${max_retries}" ] || $command; do |
12 |
| - ((retries=retries+1)) |
13 |
| - echo " - retrying command '${command}' attempt: ${retries}" |
14 |
| - done |
15 |
| - [ "${retries}" -lt "${max_retries}" ] || { echo "ERROR: Command '$*' failed after ${max_retries} attempts"; return 1; } |
| 3 | +# This file is intended to be loaded via the `source`-command. |
| 4 | + |
| 5 | +function step(){ |
| 6 | + echo "# $1" |
16 | 7 | }
|
17 | 8 |
|
| 9 | +function retry(){ |
| 10 | + max_retries=$1 |
| 11 | + shift |
| 12 | + retries=0 |
| 13 | + command="$*" |
| 14 | + until [ "${retries}" -eq "${max_retries}" ] || $command; do |
| 15 | + ((retries=retries+1)) |
| 16 | + echo " - retrying command '${command}' attempt: ${retries}" |
| 17 | + done |
| 18 | + [ "${retries}" -lt "${max_retries}" ] || { echo "ERROR: Command '$*' failed after ${max_retries} attempts"; return 1; } |
| 19 | +} |
18 | 20 |
|
19 |
| -function bosh_login(){ |
20 |
| - step "bosh login" |
21 |
| - if [[ ! -d ${bbl_state_path} ]]; then |
22 |
| - echo "FAILED: Did not find bbl-state folder at ${bbl_state_path}" |
23 |
| - echo "Make sure you have checked out the app-autoscaler-env-bbl-state repository next to the app-autoscaler-release repository to run this target or indicate its location via BBL_STATE_PATH"; |
24 |
| - exit 1; |
25 |
| - fi |
| 21 | +function concourse_login() { |
| 22 | + local -r concourse_aas_release_target="${1}" |
| 23 | + if ! fly targets | rg --only-matching --regexp="^${concourse_aas_release_target}[[:space:]]" > /dev/null |
| 24 | + then |
| 25 | + echo "There is no concourse-target for precicely \"${concourse_aas_release_target}\"." \ |
| 26 | + 'Login required!' |
| 27 | + fly --target="${concourse_aas_release_target}" login \ |
| 28 | + --team-name='app-autoscaler'\ |
| 29 | + --concourse-url='https://concourse.app-runtime-interfaces.ci.cloudfoundry.org' |
| 30 | + fi |
| 31 | +} |
26 | 32 |
|
27 |
| - pushd "${bbl_state_path}" > /dev/null |
28 |
| - eval "$(bbl print-env)" |
29 |
| - popd > /dev/null |
| 33 | +function bosh_login() { |
| 34 | + step "bosh login" |
| 35 | + local -r bbl_state_path="${1}" |
| 36 | + if [[ ! -d "${bbl_state_path}" ]] |
| 37 | + then |
| 38 | + echo "⛔ FAILED: Did not find bbl-state folder at ${bbl_state_path}" |
| 39 | + echo 'Make sure you have checked out the app-autoscaler-env-bbl-state repository next to the app-autoscaler-release repository to run this target or indicate its location via BBL_STATE_PATH' |
| 40 | + exit 1; |
| 41 | + fi |
| 42 | + |
| 43 | + pushd "${bbl_state_path}" > /dev/null |
| 44 | + eval "$(bbl print-env)" |
| 45 | + popd > /dev/null |
30 | 46 | }
|
31 | 47 |
|
32 | 48 | function cf_login(){
|
33 |
| - step "login to cf" |
34 |
| - cf api "https://api.${system_domain}" --skip-ssl-validation |
35 |
| - cf_admin_password="$(credhub get --quiet --name='/bosh-autoscaler/cf/cf_admin_password')" |
36 |
| - cf auth admin "$cf_admin_password" |
| 49 | + step 'login to cf' |
| 50 | + cf api "https://api.${system_domain}" --skip-ssl-validation |
| 51 | + cf_admin_password="$(credhub get --quiet --name='/bosh-autoscaler/cf/cf_admin_password')" |
| 52 | + cf auth admin "$cf_admin_password" |
37 | 53 | }
|
38 | 54 |
|
39 | 55 | function cleanup_acceptance_run(){
|
40 |
| - step "cleaning up from acceptance tests" |
41 |
| - pushd "${ci_dir}/../src/acceptance" > /dev/null |
42 |
| - retry 5 ./cleanup.sh |
43 |
| - popd > /dev/null |
| 56 | + step "cleaning up from acceptance tests" |
| 57 | + pushd "${ci_dir}/../src/acceptance" > /dev/null |
| 58 | + retry 5 ./cleanup.sh |
| 59 | + popd > /dev/null |
44 | 60 | }
|
45 | 61 |
|
46 | 62 | function cleanup_service_broker(){
|
47 |
| - step "deleting service broker for deployment '${deployment_name}'" |
48 |
| - SERVICE_BROKER_EXISTS=$(cf service-brokers | grep -c "${service_broker_name}.${system_domain}" || true) |
49 |
| - if [[ $SERVICE_BROKER_EXISTS == 1 ]]; then |
50 |
| - echo "- Service Broker exists, deleting broker '${deployment_name}'" |
51 |
| - retry 3 cf delete-service-broker "${deployment_name}" -f |
52 |
| - fi |
| 63 | + step "deleting service broker for deployment '${deployment_name}'" |
| 64 | + SERVICE_BROKER_EXISTS=$(cf service-brokers | grep -c "${service_broker_name}.${system_domain}" || true) |
| 65 | + if [[ $SERVICE_BROKER_EXISTS == 1 ]]; then |
| 66 | + echo "- Service Broker exists, deleting broker '${deployment_name}'" |
| 67 | + retry 3 cf delete-service-broker "${deployment_name}" -f |
| 68 | + fi |
53 | 69 | }
|
54 | 70 |
|
55 | 71 | function cleanup_bosh_deployment(){
|
56 |
| - step "deleting bosh deployment '${deployment_name}'" |
57 |
| - retry 3 bosh delete-deployment -d "${deployment_name}" -n |
| 72 | + step "deleting bosh deployment '${deployment_name}'" |
| 73 | + retry 3 bosh delete-deployment -d "${deployment_name}" -n |
58 | 74 | }
|
59 | 75 |
|
60 | 76 | function delete_releases(){
|
61 |
| - step "deleting releases" |
62 |
| - if [ -n "${deployment_name}" ] |
63 |
| - then |
64 |
| - for release in $(bosh releases | grep -E "${deployment_name}\s+" | awk '{print $2}') |
65 |
| - do |
66 |
| - echo "- Deleting bosh release '${release}'" |
67 |
| - bosh delete-release -n "app-autoscaler/${release}" & |
68 |
| - done |
69 |
| - wait |
70 |
| - fi |
| 77 | + step "deleting releases" |
| 78 | + if [ -n "${deployment_name}" ] |
| 79 | + then |
| 80 | + for release in $(bosh releases | grep -E "${deployment_name}\s+" | awk '{print $2}') |
| 81 | + do |
| 82 | + echo "- Deleting bosh release '${release}'" |
| 83 | + bosh delete-release -n "app-autoscaler/${release}" & |
| 84 | + done |
| 85 | + wait |
| 86 | + fi |
71 | 87 | }
|
72 | 88 |
|
73 | 89 | function cleanup_bosh(){
|
74 |
| - step "cleaning up bosh" |
75 |
| - retry 3 bosh clean-up --all -n |
| 90 | + step "cleaning up bosh" |
| 91 | + retry 3 bosh clean-up --all -n |
76 | 92 | }
|
77 | 93 |
|
78 | 94 | function cleanup_credhub(){
|
79 |
| - step "cleaning up credhub: '/bosh-autoscaler/${deployment_name}/*'" |
80 |
| - retry 3 credhub delete --path="/bosh-autoscaler/${deployment_name}" |
| 95 | + step "cleaning up credhub: '/bosh-autoscaler/${deployment_name}/*'" |
| 96 | + retry 3 credhub delete --path="/bosh-autoscaler/${deployment_name}" |
81 | 97 | }
|
82 | 98 |
|
83 | 99 | function cleanup_apps(){
|
84 |
| - step "cleaning up apps" |
85 |
| - local mtar_app |
86 |
| - local space_guid |
87 |
| - |
88 |
| - cf_target "${autoscaler_org}" "${autoscaler_space}" |
89 |
| - |
90 |
| - space_guid="$(cf space --guid "${autoscaler_space}")" |
91 |
| - mtar_app="$(curl --header "Authorization: $(cf oauth-token)" "deploy-service.${system_domain}/api/v2/spaces/${space_guid}/mtas" | jq ". | .[] | .metadata | .id" -r)" |
92 |
| - |
93 |
| - if [ -n "${mtar_app}" ]; then |
94 |
| - set +e |
95 |
| - cf undeploy "${mtar_app}" -f --delete-service-brokers --delete-service-keys --delete-services --do-not-fail-on-missing-permissions |
96 |
| - set -e |
97 |
| - else |
98 |
| - echo "No app to undeploy" |
99 |
| - fi |
100 |
| - |
101 |
| - if cf spaces | grep --quiet --regexp="^${AUTOSCALER_SPACE}$"; then |
102 |
| - cf delete-space -f "${AUTOSCALER_SPACE}" |
103 |
| - fi |
104 |
| - |
105 |
| - if cf orgs | grep --quiet --regexp="^${AUTOSCALER_ORG}$"; then |
106 |
| - cf delete-org -f "${AUTOSCALER_ORG}" |
107 |
| - fi |
| 100 | + step "cleaning up apps" |
| 101 | + local mtar_app |
| 102 | + local space_guid |
| 103 | + |
| 104 | + cf_target "${autoscaler_org}" "${autoscaler_space}" |
| 105 | + |
| 106 | + space_guid="$(cf space --guid "${autoscaler_space}")" |
| 107 | + mtar_app="$(curl --header "Authorization: $(cf oauth-token)" "deploy-service.${system_domain}/api/v2/spaces/${space_guid}/mtas" | jq ". | .[] | .metadata | .id" -r)" |
| 108 | + |
| 109 | + if [ -n "${mtar_app}" ]; then |
| 110 | + set +e |
| 111 | + cf undeploy "${mtar_app}" -f --delete-service-brokers --delete-service-keys --delete-services --do-not-fail-on-missing-permissions |
| 112 | + set -e |
| 113 | + else |
| 114 | + echo "No app to undeploy" |
| 115 | + fi |
| 116 | + |
| 117 | + if cf spaces | grep --quiet --regexp="^${AUTOSCALER_SPACE}$"; then |
| 118 | + cf delete-space -f "${AUTOSCALER_SPACE}" |
| 119 | + fi |
| 120 | + |
| 121 | + if cf orgs | grep --quiet --regexp="^${AUTOSCALER_ORG}$"; then |
| 122 | + cf delete-org -f "${AUTOSCALER_ORG}" |
| 123 | + fi |
108 | 124 | }
|
109 | 125 |
|
110 | 126 |
|
111 | 127 | function unset_vars() {
|
112 |
| - unset PR_NUMBER |
113 |
| - unset DEPLOYMENT_NAME |
114 |
| - unset SYSTEM_DOMAIN |
115 |
| - unset BBL_STATE_PATH |
116 |
| - unset AUTOSCALER_DIR |
117 |
| - unset CI_DIR |
118 |
| - unset SERVICE_NAME |
119 |
| - unset SERVICE_BROKER_NAME |
120 |
| - unset NAME_PREFIX |
121 |
| - unset GINKGO_OPTS |
| 128 | + unset PR_NUMBER |
| 129 | + unset DEPLOYMENT_NAME |
| 130 | + unset SYSTEM_DOMAIN |
| 131 | + unset BBL_STATE_PATH |
| 132 | + unset AUTOSCALER_DIR |
| 133 | + unset CI_DIR |
| 134 | + unset SERVICE_NAME |
| 135 | + unset SERVICE_BROKER_NAME |
| 136 | + unset NAME_PREFIX |
| 137 | + unset GINKGO_OPTS |
122 | 138 | }
|
123 | 139 |
|
124 | 140 | function find_or_create_org(){
|
125 |
| - step "finding or creating org" |
126 |
| - local org_name="$1" |
127 |
| - if ! cf orgs | grep --quiet --regexp="^${org_name}$"; then |
128 |
| - cf create-org "${org_name}" |
129 |
| - fi |
130 |
| - echo "targeting org ${org_name}" |
131 |
| - cf target -o "${org_name}" |
| 141 | + step "finding or creating org" |
| 142 | + local org_name="$1" |
| 143 | + if ! cf orgs | grep --quiet --regexp="^${org_name}$"; then |
| 144 | + cf create-org "${org_name}" |
| 145 | + fi |
| 146 | + echo "targeting org ${org_name}" |
| 147 | + cf target -o "${org_name}" |
132 | 148 | }
|
133 | 149 |
|
134 | 150 | function find_or_create_space(){
|
135 |
| - step "finding or creating space" |
136 |
| - local space_name="$1" |
137 |
| - if ! cf spaces | grep --quiet --regexp="^${space_name}$"; then |
138 |
| - cf create-space "${space_name}" |
139 |
| - fi |
140 |
| - echo "targeting space ${space_name}" |
141 |
| - cf target -s "${space_name}" |
| 151 | + step "finding or creating space" |
| 152 | + local space_name="$1" |
| 153 | + if ! cf spaces | grep --quiet --regexp="^${space_name}$"; then |
| 154 | + cf create-space "${space_name}" |
| 155 | + fi |
| 156 | + echo "targeting space ${space_name}" |
| 157 | + cf target -s "${space_name}" |
142 | 158 | }
|
143 | 159 |
|
144 | 160 | function cf_target(){
|
145 |
| - local org_name="$1" |
146 |
| - local space_name="$2" |
| 161 | + local org_name="$1" |
| 162 | + local space_name="$2" |
147 | 163 |
|
148 |
| - find_or_create_org "${org_name}" |
149 |
| - find_or_create_space "${space_name}" |
| 164 | + find_or_create_org "${org_name}" |
| 165 | + find_or_create_space "${space_name}" |
150 | 166 | }
|
0 commit comments