Skip to content

Commit 8587834

Browse files
committed
Automatically login into concourse if required and reuse more code;
1 parent adfb6f3 commit 8587834

10 files changed

+286
-240
lines changed

.envrc

+5-8
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ then
88
# for more details
99
fi
1010

11-
local bbl_state_dir=../app-autoscaler-env-bbl-state/bbl-state
12-
if has bbl && [[ -d "$bbl_state_dir" ]]
13-
then
14-
BBL_STATE_DIRECTORY="$(realpath "$bbl_state_dir")"
15-
export BBL_STATE_DIRECTORY
16-
eval "$(bbl print-env)"
17-
fi
11+
local bbl_state_dir=${BBL_STATE_PATH:-'../app-autoscaler-env-bbl-state/bbl-state'}
12+
source './ci/autoscaler/scripts/common.sh'
13+
bosh_login "${bbl_state_dir}"
14+
echo '🚸 Bosh-login successful! For more execute: `./ci/autoscaler/scripts/os-infrastructure-login.sh`'
1815

19-
export DBURL="postgres://postgres:postgres@localhost/autoscaler"
16+
export DBURL='postgres://postgres:postgres@localhost/autoscaler'

Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,11 @@ cleanup-autoscaler-deployments:
442442
@${CI_DIR}/autoscaler/scripts/cleanup-autoscaler-deployments.sh
443443

444444
.PHONY: cf-login
445-
cf-login: ## Login to OSS CF dev environment
446-
@${CI_DIR}/autoscaler/scripts/cf-login.sh
445+
cf-login:
446+
@echo '⚠️ Please note that this login only works for cf,' \
447+
'in spite of performing a login as well on bosh and credhub.' \
448+
'The necessary changes to the environment get lost when make exits its process.'
449+
@${CI_DIR}/autoscaler/scripts/os-infrastructure-login.sh
447450

448451
.PHONY: setup-performance
449452
setup-performance: build-test-app

ci/Makefile

-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
.PHONY: set-autoscaler-pipeline
32
set-autoscaler-pipeline:
43
@./autoscaler/set-pipeline.sh
@@ -15,10 +14,6 @@ unpause-pipeline:
1514
delete-pipeline:
1615
@./scripts/delete-pipeline.sh
1716

18-
.PHONY: set-target
19-
set-target:
20-
@fly --target app-autoscaler-release login --team-name app-autoscaler --concourse-url https://concourse.app-runtime-interfaces.ci.cloudfoundry.org
21-
2217
.PHONY: lint
2318
lint:
2419
@shellcheck --external-sources --shell='bash' **/scripts/*.sh

ci/autoscaler/scripts/cf-login.sh

-12
This file was deleted.

ci/autoscaler/scripts/common.sh

+122-106
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,166 @@
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
52

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"
167
}
178

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+
}
1820

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+
}
2632

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
3046
}
3147

3248
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"
3753
}
3854

3955
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
4460
}
4561

4662
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
5369
}
5470

5571
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
5874
}
5975

6076
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
7187
}
7288

7389
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
7692
}
7793

7894
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}"
8197
}
8298

8399
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
108124
}
109125

110126

111127
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
122138
}
123139

124140
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}"
132148
}
133149

134150
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}"
142158
}
143159

144160
function cf_target(){
145-
local org_name="$1"
146-
local space_name="$2"
161+
local org_name="$1"
162+
local space_name="$2"
147163

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}"
150166
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#! /usr/bin/env bash
2+
# shellcheck disable=SC2086
3+
set -eu -o pipefail
4+
script_dir=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
5+
source "${script_dir}/vars.source.sh"
6+
source "${script_dir}/common.sh"
7+
8+
bosh_login "${BBL_STATE_PATH}"
9+
concourse_login "${CONCOURSE_AAS_RELEASE_TARGET}"
10+
cf_login
11+
cf_target "${AUTOSCALER_ORG}" "${AUTOSCALER_SPACE}"
12+
13+
14+
cf autoscaling-api "https://autoscaler-${PR_NUMBER}.${SYSTEM_DOMAIN}"

0 commit comments

Comments
 (0)