2022-05-12 15:48:29 +03:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
2022-12-02 19:48:32 +00:00
|
|
|
set -e
|
2022-05-12 15:48:29 +03:00
|
|
|
set -o pipefail
|
|
|
|
|
|
2022-12-29 22:57:10 +00:00
|
|
|
METADATA_URL="GARM_METADATA_URL"
|
2022-05-12 15:48:29 +03:00
|
|
|
CALLBACK_URL="GARM_CALLBACK_URL"
|
|
|
|
|
BEARER_TOKEN="GARM_CALLBACK_TOKEN"
|
|
|
|
|
DOWNLOAD_URL="GH_DOWNLOAD_URL"
|
2022-12-05 21:29:30 +00:00
|
|
|
DOWNLOAD_TOKEN="GH_TEMP_DOWNLOAD_TOKEN"
|
2022-05-12 15:48:29 +03:00
|
|
|
FILENAME="GH_FILENAME"
|
|
|
|
|
TARGET_URL="GH_TARGET_URL"
|
|
|
|
|
RUNNER_NAME="GH_RUNNER_NAME"
|
|
|
|
|
RUNNER_LABELS="GH_RUNNER_LABELS"
|
2022-12-05 21:29:30 +00:00
|
|
|
TEMP_TOKEN=""
|
|
|
|
|
|
2022-12-29 22:57:10 +00:00
|
|
|
|
|
|
|
|
if [ -z "$METADATA_URL" ];then
|
|
|
|
|
echo "no token is available and METADATA_URL is not set"
|
|
|
|
|
exit 1
|
2022-12-05 21:29:30 +00:00
|
|
|
fi
|
2022-05-12 15:48:29 +03:00
|
|
|
|
|
|
|
|
function call() {
|
|
|
|
|
PAYLOAD="$1"
|
2022-12-06 20:03:16 +00:00
|
|
|
curl --fail -s -X POST -d "${PAYLOAD}" -H 'Accept: application/json' -H "Authorization: Bearer ${BEARER_TOKEN}" "${CALLBACK_URL}" || echo "failed to call home: exit code ($?)"
|
2022-05-12 15:48:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sendStatus() {
|
|
|
|
|
MSG="$1"
|
|
|
|
|
call "{\"status\": \"installing\", \"message\": \"$MSG\"}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function success() {
|
|
|
|
|
MSG="$1"
|
2022-06-29 16:23:01 +00:00
|
|
|
ID=$2
|
|
|
|
|
call "{\"status\": \"idle\", \"message\": \"$MSG\", \"agent_id\": $ID}"
|
2022-05-12 15:48:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fail() {
|
|
|
|
|
MSG="$1"
|
|
|
|
|
call "{\"status\": \"failed\", \"message\": \"$MSG\"}"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 22:57:10 +00:00
|
|
|
if [ ! -z "$DOWNLOAD_TOKEN" ]; then
|
|
|
|
|
TEMP_TOKEN="Authorization: Bearer $DOWNLOAD_TOKEN"
|
|
|
|
|
fi
|
|
|
|
|
|
2022-05-12 15:48:29 +03:00
|
|
|
sendStatus "downloading tools from ${DOWNLOAD_URL}"
|
2022-12-06 20:03:16 +00:00
|
|
|
curl --fail -L -H "${TEMP_TOKEN}" -o "/home/runner/${FILENAME}" "${DOWNLOAD_URL}" || fail "failed to download tools"
|
2022-05-12 15:48:29 +03:00
|
|
|
|
|
|
|
|
mkdir -p /home/runner/actions-runner || fail "failed to create actions-runner folder"
|
|
|
|
|
|
|
|
|
|
sendStatus "extracting runner"
|
|
|
|
|
tar xf "/home/runner/${FILENAME}" -C /home/runner/actions-runner/ || fail "failed to extract runner"
|
|
|
|
|
chown runner:runner -R /home/runner/actions-runner/ || fail "failed to change owner"
|
|
|
|
|
|
|
|
|
|
sendStatus "installing dependencies"
|
|
|
|
|
cd /home/runner/actions-runner
|
|
|
|
|
sudo ./bin/installdependencies.sh || fail "failed to install dependencies"
|
|
|
|
|
|
2022-12-29 22:57:10 +00:00
|
|
|
sendStatus "fetching runner registration token"
|
|
|
|
|
GITHUB_TOKEN=$(curl --fail -s -X GET -H 'Accept: application/json' -H "Authorization: Bearer ${BEARER_TOKEN}" "${METADATA_URL}" || fail "failed to get runner registration token")
|
|
|
|
|
|
2022-05-12 15:48:29 +03:00
|
|
|
sendStatus "configuring runner"
|
2022-12-29 22:57:10 +00:00
|
|
|
sudo -u runner -- ./config.sh --unattended --url "${TARGET_URL}" --token "${GITHUB_TOKEN}" --name "${RUNNER_NAME}" --labels "${RUNNER_LABELS}" --ephemeral || fail "failed to configure runner"
|
2022-05-12 15:48:29 +03:00
|
|
|
|
|
|
|
|
sendStatus "installing runner service"
|
|
|
|
|
./svc.sh install runner || fail "failed to install service"
|
|
|
|
|
|
|
|
|
|
sendStatus "starting service"
|
|
|
|
|
./svc.sh start || fail "failed to start service"
|
|
|
|
|
|
2022-06-29 16:23:01 +00:00
|
|
|
set +e
|
2022-10-21 10:05:26 +03:00
|
|
|
AGENT_ID=$(grep "agentId" /home/runner/actions-runner/.runner | tr -d -c 0-9)
|
2022-06-29 16:23:01 +00:00
|
|
|
if [ $? -ne 0 ];then
|
|
|
|
|
fail "failed to get agent ID"
|
|
|
|
|
fi
|
|
|
|
|
set -e
|
2022-05-12 15:48:29 +03:00
|
|
|
|
2022-12-29 22:57:10 +00:00
|
|
|
success "runner successfully installed" $AGENT_ID
|