-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CI_SERVICE_alias environment variables to make referencing the FQDN of services fun! #27
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,9 @@ | |
set -euo pipefail | ||
|
||
# trap any error, and mark it as a system failure. | ||
# Also cleans up TMPVARFILE (set in create_temporary_varfile). | ||
trap 'rm -f "$TMPVARFILE"; exit $SYSTEM_FAILURE_EXIT_CODE' ERR | ||
trap 'rm -f "$TMPVARFILE"' EXIT | ||
# Also cleans up TMPMANIFEST(set in create_temporary_manifest). | ||
trap 'rm -f "$TMPMANIFEST"; exit $SYSTEM_FAILURE_EXIT_CODE' ERR | ||
trap 'rm -f "$TMPMANIFEST"' EXIT | ||
|
||
# Prepare a runner executor application in CloudFoundry | ||
|
||
|
@@ -16,18 +16,6 @@ if [ -z "${WORKER_MEMORY-}" ]; then | |
WORKER_MEMORY="512M" | ||
fi | ||
|
||
create_temporary_varfile () { | ||
# A less leak-prone way to share secrets into the worker which will not | ||
# be able to parse VCAP_CONFIGURATION | ||
TMPVARFILE=$(mktemp /tmp/gitlab-runner-worker-manifest.XXXXXXXXXX) | ||
|
||
for v in RUNNER_NAME CACHE_TYPE CACHE_S3_SERVER_ADDRESS CACHE_S3_BUCKET_LOCATION CACHE_S3_BUCKET_NAME CACHE_S3_BUCKET_NAME CACHE_S3_ACCESS_KEY CACHE_S3_SECRET_KEY; do | ||
echo "$v: \"$v\"" >> "$TMPVARFILE" | ||
done | ||
|
||
echo "[cf-driver] [DEBUG] Added $(wc -l "$TMPVARFILE") lines to $TMPVARFILE" | ||
} | ||
|
||
get_registry_credentials () { | ||
image_name="$1" | ||
|
||
|
@@ -51,6 +39,28 @@ get_registry_credentials () { | |
fi | ||
} | ||
|
||
create_temporary_manifest () { | ||
# A less leak-prone way to share secrets into the worker which will not | ||
# be able to parse VCAP_CONFIGURATION | ||
TMPMANIFEST=$(mktemp /tmp/gitlab-runner-worker-manifest.XXXXXXXXXX) | ||
chmod 600 "$TMPMANIFEST" | ||
cat "${currentDir}/worker-manifest.yml" > "$TMPMANIFEST" | ||
|
||
# Align additional environment variables with YAML at end of source manifest | ||
local padding=" " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought (non-blocking): 😨 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, right? 🧌 |
||
|
||
for v in RUNNER_NAME CACHE_TYPE CACHE_S3_SERVER_ADDRESS CACHE_S3_BUCKET_LOCATION CACHE_S3_BUCKET_NAME CACHE_S3_ACCESS_KEY CACHE_S3_SECRET_KEY; do | ||
echo "${padding}${v}: \"${!v}\"" >> "$TMPMANIFEST" | ||
done | ||
|
||
# Add any CI_SERVICE_x variables populated by start_service() | ||
for v in "${!CI_SERVICE_@}"; do | ||
echo "${padding}${v}: \"${!v}\"" >> "$TMPMANIFEST" | ||
done | ||
Comment on lines
+56
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: the indirection is pretty nifty, I haven't used that before |
||
|
||
echo "[cf-driver] [DEBUG] $(wc -l < "$TMPMANIFEST") lines in $TMPMANIFEST" | ||
} | ||
|
||
start_container () { | ||
container_id="$1" | ||
image_name="$CUSTOM_ENV_CI_JOB_IMAGE" | ||
|
@@ -62,9 +72,8 @@ start_container () { | |
|
||
push_args=( | ||
"$container_id" | ||
-f "${currentDir}/worker-manifest.yml" | ||
-f "$TMPMANIFEST" | ||
-m "$WORKER_MEMORY" | ||
--vars-file "$TMPVARFILE" | ||
--docker-image "$image_name" | ||
) | ||
|
||
|
@@ -118,7 +127,32 @@ start_service () { | |
|
||
# TODO - Figure out how to handle non-global memory definition | ||
cf push "${push_args[@]}" | ||
|
||
# Map route and export a FQDN. We assume apps.internal as the domain. | ||
cf map-route "$container_id" apps.internal --hostname "$container_id" | ||
export "CI_SERVICE_${alias_name}"="${container_id}.apps.internal" | ||
} | ||
|
||
start_services () { | ||
container_id_base="$1" | ||
ci_job_services="$2" | ||
|
||
if [ -z "$ci_job_services" ]; then | ||
echo "[cf-driver] No services defined in ci_job_services - Skipping service startup" | ||
return | ||
fi | ||
|
||
for l in $(echo "$ci_job_services" | jq -rc '.[]'); do | ||
# Using jq -er to fail of alias or name are not found | ||
alias_name=$(echo "$l" | jq -er '.alias | select(.)') | ||
container_id="${container_id_base}-svc-${alias_name}" | ||
image_name=$(echo "$l" | jq -er '.name | select(.)') | ||
# Using jq -r to allow entrypoint and command to be empty | ||
container_entrypoint=$(echo "$l" | jq -r '.entrypoint | select(.)') | ||
container_command=$(echo "$l" | jq -r '.command | select(.)') | ||
|
||
start_service "$alias_name" "$container_id" "$image_name" "$container_entrypoint" "$container_command" | ||
done | ||
} | ||
|
||
allow_access_to_service () { | ||
|
@@ -138,25 +172,17 @@ allow_access_to_service () { | |
--protocol "$protocol" --port "$ports" | ||
} | ||
|
||
start_services () { | ||
allow_access_to_services () { | ||
container_id_base="$1" | ||
ci_job_services="$2" | ||
|
||
if [ -z "$ci_job_services" ]; then | ||
echo "[cf-driver] No services defined in ci_job_services - Skipping service startup" | ||
echo "[cf-driver] No services defined in ci_job_services - Skipping service allowance" | ||
return | ||
fi | ||
|
||
for l in $(echo "$ci_job_services" | jq -rc '.[]'); do | ||
# Using jq -er to fail of alias or name are not found | ||
alias_name=$(echo "$l" | jq -er '.alias | select(.)') | ||
container_id="${container_id_base}-svc-${alias_name}" | ||
image_name=$(echo "$l" | jq -er '.name | select(.)') | ||
# Using jq -r to allow entrypoint and command to be empty | ||
container_entrypoint=$(echo "$l" | jq -r '.entrypoint | select(.)') | ||
container_command=$(echo "$l" | jq -r '.command | select(.)') | ||
|
||
start_service "$alias_name" "$container_id" "$image_name" "$container_entrypoint" "$container_command" | ||
allow_access_to_service "$container_id_base" "$container_id" | ||
done | ||
} | ||
|
@@ -188,18 +214,24 @@ install_dependencies () { | |
ln -s /usr/bin/gitlab-runner-helper /usr/bin/gitlab-runner' | ||
} | ||
|
||
echo "[cf-driver] Preparing environment variables for $CONTAINER_ID" | ||
create_temporary_varfile | ||
if [ -n "$CUSTOM_ENV_CI_JOB_SERVICES" ]; then | ||
echo "[cf-driver] Starting services" | ||
start_services "$CONTAINER_ID" "$CUSTOM_ENV_CI_JOB_SERVICES" | ||
fi | ||
|
||
echo "[cf-driver] Preparing manifest for $CONTAINER_ID" | ||
create_temporary_manifest | ||
|
||
echo "[cf-driver] Starting $CONTAINER_ID with image $CUSTOM_ENV_CI_JOB_IMAGE" | ||
start_container "$CONTAINER_ID" | ||
|
||
echo "[cf-driver] Installing dependencies into $CONTAINER_ID" | ||
install_dependencies "$CONTAINER_ID" | ||
|
||
# Allowing access last so services and the worker are all present | ||
if [ -n "$CUSTOM_ENV_CI_JOB_SERVICES" ]; then | ||
echo "[cf-driver] Starting services" | ||
start_services "$CONTAINER_ID" "$CUSTOM_ENV_CI_JOB_SERVICES" | ||
echo "[cf-driver] Enabling access from worker to services" | ||
allow_access_to_services "$CONTAINER_ID" "$CUSTOM_ENV_CI_JOB_SERVICES" | ||
fi | ||
|
||
echo "[cf-driver] $CONTAINER_ID preparation complete" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: good thinking