2021-12-08 17:51:53 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -uo pipefail
|
|
|
|
|
|
|
|
####################################
|
|
|
|
# IMPORTANT! #
|
|
|
|
# EC2 instance should have #
|
|
|
|
# `github:runner-type` tag #
|
|
|
|
# set accordingly to a runner role #
|
|
|
|
####################################
|
|
|
|
|
|
|
|
echo "Running init script"
|
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
|
|
export RUNNER_HOME=/home/ubuntu/actions-runner
|
|
|
|
|
|
|
|
export RUNNER_URL="https://github.com/ClickHouse"
|
|
|
|
# Funny fact, but metadata service has fixed IP
|
2022-06-20 13:31:20 +00:00
|
|
|
INSTANCE_ID=$(ec2metadata --instance-id)
|
2021-12-08 17:51:53 +00:00
|
|
|
export INSTANCE_ID
|
|
|
|
|
2022-08-01 16:27:17 +00:00
|
|
|
# Add cloudflare DNS as a fallback
|
|
|
|
# Get default gateway interface
|
2022-08-02 00:23:05 +00:00
|
|
|
IFACE=$(ip --json route list | jq '.[]|select(.dst == "default").dev' --raw-output)
|
2022-08-01 16:27:17 +00:00
|
|
|
# `Link 2 (eth0): 172.31.0.2`
|
|
|
|
ETH_DNS=$(resolvectl dns "$IFACE") || :
|
2022-08-02 00:23:53 +00:00
|
|
|
CLOUDFLARE_NS=1.1.1.1
|
|
|
|
if [[ "$ETH_DNS" ]] && [[ "${ETH_DNS#*: }" != *"$CLOUDFLARE_NS"* ]]; then
|
2022-08-01 16:27:17 +00:00
|
|
|
# Cut the leading legend
|
|
|
|
ETH_DNS=${ETH_DNS#*: }
|
|
|
|
# shellcheck disable=SC2206
|
2022-08-02 00:23:53 +00:00
|
|
|
new_dns=(${ETH_DNS} "$CLOUDFLARE_NS")
|
2022-08-01 16:27:17 +00:00
|
|
|
resolvectl dns "$IFACE" "${new_dns[@]}"
|
|
|
|
fi
|
|
|
|
|
2021-12-08 17:51:53 +00:00
|
|
|
# combine labels
|
2022-06-20 13:31:20 +00:00
|
|
|
RUNNER_TYPE=$(/usr/local/bin/aws ec2 describe-tags --filters "Name=resource-id,Values=$INSTANCE_ID" --query "Tags[?Key=='github:runner-type'].Value" --output text)
|
2021-12-08 17:51:53 +00:00
|
|
|
LABELS="self-hosted,Linux,$(uname -m),$RUNNER_TYPE"
|
|
|
|
export LABELS
|
|
|
|
|
2022-06-20 13:31:20 +00:00
|
|
|
# Refresh CloudWatch agent config
|
|
|
|
aws ssm get-parameter --region us-east-1 --name AmazonCloudWatch-github-runners --query 'Parameter.Value' --output text > /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
|
|
|
|
systemctl restart amazon-cloudwatch-agent.service
|
|
|
|
|
2022-06-27 12:45:50 +00:00
|
|
|
# Refresh teams ssh keys
|
|
|
|
TEAM_KEYS_URL=$(aws ssm get-parameter --region us-east-1 --name team-keys-url --query 'Parameter.Value' --output=text)
|
|
|
|
curl "${TEAM_KEYS_URL}" > /home/ubuntu/.ssh/authorized_keys2
|
|
|
|
chown ubuntu: /home/ubuntu/.ssh -R
|
|
|
|
|
2022-06-20 13:31:20 +00:00
|
|
|
|
2022-11-23 14:29:52 +00:00
|
|
|
# Create a pre-run script that will provide diagnostics info
|
2022-06-20 13:31:20 +00:00
|
|
|
mkdir -p /tmp/actions-hooks
|
2022-11-23 14:29:52 +00:00
|
|
|
cat > /tmp/actions-hooks/pre-run.sh << EOF
|
2022-06-20 13:31:20 +00:00
|
|
|
#!/bin/bash
|
2022-11-23 14:29:52 +00:00
|
|
|
set -uo pipefail
|
2022-06-20 13:31:20 +00:00
|
|
|
|
2022-06-27 12:46:28 +00:00
|
|
|
echo "Runner's public DNS: $(ec2metadata --public-hostname)"
|
2022-11-23 14:29:52 +00:00
|
|
|
echo "Runner's labels: ${LABELS}"
|
2022-06-20 13:31:20 +00:00
|
|
|
EOF
|
|
|
|
|
2022-11-23 14:29:52 +00:00
|
|
|
# Create a post-run script that will restart docker daemon before the job started
|
2022-06-20 13:31:20 +00:00
|
|
|
cat > /tmp/actions-hooks/post-run.sh << 'EOF'
|
|
|
|
#!/bin/bash
|
|
|
|
set -xuo pipefail
|
|
|
|
|
2022-06-29 10:51:36 +00:00
|
|
|
terminate-and-exit() {
|
|
|
|
echo "Going to terminate the runner"
|
2022-06-20 13:31:20 +00:00
|
|
|
INSTANCE_ID=$(ec2metadata --instance-id)
|
2022-06-28 15:51:47 +00:00
|
|
|
# We execute it with at to not have it as an orphan process
|
|
|
|
# GH Runners kill all remain processes
|
2023-02-22 23:45:51 +00:00
|
|
|
echo "sleep 10; aws ec2 terminate-instances --instance-ids $INSTANCE_ID" | at now || \
|
|
|
|
aws ec2 terminate-instances --instance-ids "$INSTANCE_ID" # workaround for complete out of space
|
2022-06-20 13:31:20 +00:00
|
|
|
exit 0
|
2022-06-29 10:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Free KiB, free percents
|
|
|
|
ROOT_STAT=($(df / | awk '/\// {print $4 " " int($4/$2 * 100)}'))
|
|
|
|
if [[ ${ROOT_STAT[0]} -lt 3000000 ]] || [[ ${ROOT_STAT[1]} -lt 5 ]]; then
|
|
|
|
echo "The runner has ${ROOT_STAT[0]}KiB and ${ROOT_STAT[1]}% of free space on /"
|
|
|
|
terminate-and-exit
|
2022-06-20 13:31:20 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# shellcheck disable=SC2046
|
2022-09-17 18:24:56 +00:00
|
|
|
docker ps --quiet | xargs --no-run-if-empty docker kill ||:
|
2022-06-20 13:31:20 +00:00
|
|
|
# shellcheck disable=SC2046
|
2022-09-17 18:24:56 +00:00
|
|
|
docker ps --all --quiet | xargs --no-run-if-empty docker rm -f ||:
|
2022-06-20 13:31:20 +00:00
|
|
|
|
|
|
|
# If we have hanged containers after the previous commands, than we have a hanged one
|
|
|
|
# and should restart the daemon
|
2022-09-17 18:24:56 +00:00
|
|
|
if [ "$(docker ps --all --quiet)" ]; then
|
2022-06-29 10:51:36 +00:00
|
|
|
# Systemd service of docker has StartLimitBurst=3 and StartLimitInterval=60s,
|
|
|
|
# that's why we try restarting it for long
|
|
|
|
for i in {1..25};
|
2022-06-20 13:31:20 +00:00
|
|
|
do
|
|
|
|
sudo systemctl restart docker && break || sleep 5
|
|
|
|
done
|
|
|
|
|
|
|
|
for i in {1..10}
|
|
|
|
do
|
|
|
|
docker info && break || sleep 2
|
|
|
|
done
|
2022-06-29 10:51:36 +00:00
|
|
|
# Last chance, otherwise we have to terminate poor instance
|
|
|
|
docker info 1>/dev/null || { echo Docker unable to start; terminate-and-exit; }
|
2022-06-20 13:31:20 +00:00
|
|
|
fi
|
|
|
|
EOF
|
|
|
|
|
2021-12-08 17:51:53 +00:00
|
|
|
while true; do
|
|
|
|
runner_pid=$(pgrep run.sh)
|
|
|
|
echo "Got runner pid $runner_pid"
|
|
|
|
|
|
|
|
cd $RUNNER_HOME || exit 1
|
|
|
|
if [ -z "$runner_pid" ]; then
|
|
|
|
echo "Receiving token"
|
|
|
|
RUNNER_TOKEN=$(/usr/local/bin/aws ssm get-parameter --name github_runner_registration_token --with-decryption --output text --query Parameter.Value)
|
|
|
|
|
|
|
|
echo "Will try to remove runner"
|
|
|
|
sudo -u ubuntu ./config.sh remove --token "$RUNNER_TOKEN" ||:
|
|
|
|
|
|
|
|
echo "Going to configure runner"
|
|
|
|
sudo -u ubuntu ./config.sh --url $RUNNER_URL --token "$RUNNER_TOKEN" --name "$INSTANCE_ID" --runnergroup Default --labels "$LABELS" --work _work
|
|
|
|
|
|
|
|
echo "Run"
|
2022-06-20 13:31:20 +00:00
|
|
|
sudo -u ubuntu \
|
|
|
|
ACTIONS_RUNNER_HOOK_JOB_STARTED=/tmp/actions-hooks/pre-run.sh \
|
|
|
|
ACTIONS_RUNNER_HOOK_JOB_COMPLETED=/tmp/actions-hooks/post-run.sh \
|
|
|
|
./run.sh &
|
2021-12-08 17:51:53 +00:00
|
|
|
sleep 15
|
|
|
|
else
|
|
|
|
echo "Runner is working with pid $runner_pid, nothing to do"
|
|
|
|
sleep 10
|
|
|
|
fi
|
|
|
|
done
|