From 8e78c7ba11618c855913dfd64fcf20151462f917 Mon Sep 17 00:00:00 2001 From: Bryan MacFarlane Date: Wed, 15 Apr 2020 09:51:53 -0400 Subject: [PATCH] start remove script --- .../{latest-svc.sh => create-latest-svc.sh} | 16 ++--- scripts/remove-svc.sh | 61 +++++++++++++++++++ 2 files changed, 70 insertions(+), 7 deletions(-) rename scripts/{latest-svc.sh => create-latest-svc.sh} (86%) create mode 100755 scripts/remove-svc.sh diff --git a/scripts/latest-svc.sh b/scripts/create-latest-svc.sh similarity index 86% rename from scripts/latest-svc.sh rename to scripts/create-latest-svc.sh index 0a31df7b1..d7cf3b537 100755 --- a/scripts/latest-svc.sh +++ b/scripts/create-latest-svc.sh @@ -7,14 +7,15 @@ set -e # Configures as a service # # Examples: -# RUNNER_CFG_PAT= ./nix-svc.sh myuser/myrepo -# RUNNER_CFG_PAT= ./nix-svc.sh myorg +# RUNNER_CFG_PAT= ./create-latest-svc.sh myuser/myrepo +# RUNNER_CFG_PAT= ./create-latest-svc.sh myorg # # Usage: # export RUNNER_CFG_PAT= -# ./nix-svc.sh scope [name] [user] +# ./create-latest-svc scope [name] [user] # # scope required repo (:owner/:repo) or org (:organization) +# name optional defaults to hostname # user optional user svc will run as. defaults to current # # Notes: @@ -66,13 +67,12 @@ echo echo "Generating a registration token..." # if the scope has a slash, it's an repo runner +base_api_url="https://api.github.com/orgs" if [[ "$runner_scope" == *\/* ]]; then - base_api_url="https://api.github.com/repos/${runner_scope}" -else - base_api_url="https://api.github.com/orgs/${runner_scope}" + base_api_url="https://api.github.com/repos" fi -export RUNNER_TOKEN=$(curl -s -X POST ${base_api_url}/actions/runners/registration-token -H "accept: application/vnd.github.everest-preview+json" -H "authorization: token ${RUNNER_CFG_PAT}" | jq -r '.token') +export RUNNER_TOKEN=$(curl -s -X POST ${base_api_url}/${runner_scope}/actions/runners/registration-token -H "accept: application/vnd.github.everest-preview+json" -H "authorization: token ${RUNNER_CFG_PAT}" | jq -r '.token') if [ -z "$RUNNER_TOKEN" ]; then fatal "Failed to get a token"; fi @@ -128,3 +128,5 @@ echo echo "Configuring as a service ..." ./svc.sh install ${svc_user} ./svc.sh start + + diff --git a/scripts/remove-svc.sh b/scripts/remove-svc.sh new file mode 100755 index 000000000..3ecc24965 --- /dev/null +++ b/scripts/remove-svc.sh @@ -0,0 +1,61 @@ +# +# Removes a runner running as a service +# Must be run on the machine where the service is run +# +# Examples: +# RUNNER_CFG_PAT= ./remove-svc.sh myuser/myrepo +# RUNNER_CFG_PAT= ./remove-svc.sh myorg +# +# Usage: +# export RUNNER_CFG_PAT= +# ./remove-svc scope name +# +# scope required repo (:owner/:repo) or org (:organization) +# name optional defaults to hostname. name to uninstall and remove +# +# Notes: +# PATS over envvars are more secure +# Should be used on VMs and not containers +# Works on OSX and Linux +# Assumes x64 arch +# + +runner_scope=${1} +runner_name=${2:-$(hostname)} + +echo "Uninstalling runner ${runner_name} @ ${runner_scope}" +sudo echo + + + +#--------------------------------------- +# Stop and uninstall the service +#--------------------------------------- +echo +echo "Uninstall the service ..." +./svc.sh stop +./svc.sh uninstall + +#-------------------------------------- +# Get a removal token +#-------------------------------------- +echo +echo "Generating a removal registration token..." + +# if the scope has a slash, it's an repo runner +base_api_url="https://api.github.com/orgs" +if [[ "$runner_scope" == *\/* ]]; then + base_api_url="https://api.github.com/repos" +fi + +export REMOVE_TOKEN=$(curl -s -X POST ${base_api_url}/${runner_scope}/actions/runners/remove-token -H "accept: application/vnd.github.everest-preview+json" -H "authorization: token ${RUNNER_CFG_PAT}" | jq -r '.token') + +if [ -z "$REMOVE_TOKEN" ]; then fatal "Failed to get a token"; fi + +#-------------------------------------- +# Remove the runner +#-------------------------------------- +# DELETE /orgs/:organization/actions/runners/:runner_id +curl -s -X DELETE ${base_api_url}/${runner_scope}/actions/runners/remove-token -H "authorization: token ${REMOVE_TOKEN}" + +echo "Done."