Deploy cloud-init script to S3 for following usage

This commit is contained in:
Mikhail f. Shiryaev 2023-11-14 12:48:27 +01:00
parent 5f9704a3c1
commit b4cc55ea34
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4
3 changed files with 125 additions and 0 deletions

1
tests/ci/worker/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
generated_*init_runner.sh

View File

@ -0,0 +1,85 @@
#!/usr/bin/env bash
usage() {
echo "Usage: $0 ENVIRONMENT" >&2
echo "Valid values for ENVIRONMENT: staging, production" >&2
exit 1
}
case "$1" in
staging|production)
ENVIRONMENT="$1" ;;
--help)
usage ;;
*)
echo "Invalid argument" >&2
usage ;;
esac
cd "$(dirname "$0")" || exit 1
SOURCE_SCRIPT='init_runner.sh'
check_response() {
# Are we even in the interactive shell?
[ -t 1 ] || return 1
local request
request="$1"
read -rp "$request (y/N): " response
case "$response" in
[Yy])
return 0
# Your code to continue goes here
;;
*)
return 1
;;
esac
}
check_dirty() {
if [ -n "$(git status --porcelain=v2 "$SOURCE_SCRIPT")" ]; then
echo "The $SOURCE_SCRIPT has uncommited changes, won't deploy it" >&2
exit 1
fi
}
GIT_HASH=$(git log -1 --format=format:%H)
header() {
cat << EOF
#!/usr/bin/env bash
echo 'The $ENVIRONMENT script is generated from $SOURCE_SCRIPT, commit $GIT_HASH'
EOF
}
body() {
local first_line
first_line=$(sed -n '/^# THE SCRIPT START$/{=;q}' "$SOURCE_SCRIPT")
if [ -z "$first_line" ]; then
echo "The pattern '# THE SCRIPT START' is not found in $SOURCE_SCRIPT" >&2
exit 1
fi
tail "+$first_line" "$SOURCE_SCRIPT"
}
GENERATED_FILE="generated_${ENVIRONMENT}_${SOURCE_SCRIPT}"
{ header && body; } > "$GENERATED_FILE"
echo "The file $GENERATED_FILE is generated"
if check_response "Display the content of $GENERATED_FILE?"; then
if [ -z "$PAGER" ]; then
less "$GENERATED_FILE"
else
$PAGER "$GENERATED_FILE"
fi
fi
check_dirty
S3_OBJECT=${S3_OBJECT:-s3://github-runners-data/cloud-init/${ENVIRONMENT}.sh}
if check_response "Deploy the generated script to $S3_OBJECT?"; then
aws s3 mv "$GENERATED_FILE" "$S3_OBJECT"
fi

View File

@ -1,4 +1,43 @@
#!/usr/bin/env bash
cat > /dev/null << 'EOF'
The following content is embedded into the s3 object via the script
deploy-runner-init.sh {staging,production}
with additional helping information
In the `user data` you should define as the following
with appropriate <ENVIRONMENT> as 'staging' or 'production':
### COPY AFTER
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
aws s3 cp s3://github-runners-data/cloud-init/<ENVIRONMENT>.sh /tmp/cloud-init.sh
chmod 0700 /tmp/cloud-init.sh
exec bash /tmp/cloud-init.sh
--//
### COPY BEFORE
EOF
# THE SCRIPT START
set -uo pipefail
####################################