Skip to content

Commit

Permalink
Enable Docker install, enable parameters para1=a para2=b ...
Browse files Browse the repository at this point in the history
  • Loading branch information
igorpecovnik committed Jan 9, 2025
1 parent a32dc56 commit 13e2e8a
Showing 1 changed file with 59 additions and 38 deletions.
97 changes: 59 additions & 38 deletions tools/modules/system/module_armbian_runners.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module_options+=(
["module_armbian_runners,author"]="@igorpecovnik"
["module_armbian_runners,feature"]="module_armbian_runners"
["module_armbian_runners,desc"]="Manage self hosted runners"
["module_armbian_runners,example"]="install remove status help"
["module_armbian_runners,example"]="install remove remove_online help"
["module_armbian_runners,install"]="gh_token runner_name start stop label_primary label_secondary organisation owner repository"
["module_armbian_runners,port"]=""
["module_armbian_runners,status"]="Active"
["module_armbian_runners,arch"]=""
Expand All @@ -12,18 +13,29 @@ module_options+=(
# Module Armbian self hosted Github runners
#
function module_armbian_runners () {

local title="runners"
local condition=$(which "$title" 2>/dev/null)

local gh_token=$2
local runner_name="${3:-armbian}"
local start="${4:-01}"
local stop="${5:-01}"
local label_primary="${6:-alfa}"
local label_secondary="${7:-fast,images}"
local organisation="${8:-armbian}"
local owner="${9}"
local repository="${10}"
# read parameters from command install
local parameter
IFS=' ' read -r -a parameter <<< "${1}"
for feature in ${module_options["module_armbian_runners,install"]}; do
for selected in ${parameter[@]}; do
IFS='=' read -r -a split <<< "${selected}"
[[ ${split[0]} == $feature ]] && eval "$feature=${split[1]}"
done
done

local gh_token="${gh_token}"
local runner_name="${runner_name:-armbian}"
local start="${start:-01}"
local stop="${stop:-01}"
local label_primary="${label_primary:-alfa}"
local label_secondary="${label_secondary:-fast,images}"
local organisation="${organisation:-armbian}"
local owner="${owner}"
local repository="${repository}"

# we can generate per org or per repo
local registration_url="${organisation}"
Expand All @@ -36,20 +48,27 @@ function module_armbian_runners () {
local commands
IFS=' ' read -r -a commands <<< "${module_options["module_armbian_runners,example"]}"

case "$1" in
case "${parameter[0]}" in
"${commands[0]}")
#pkg_installed docker-ce || module_docker install
pkg_update
pkg_install libxml2-utils jq curl

if [[ -z $gh_token ]]; then
echo "Error: Github token is mandatory"
${module_options["module_armbian_runners,feature"]} ${commands[6]}
exit 1
fi

# Docker preinstall is needed for our build framework
# pkg_installed docker-ce || module_docker install
# pkg_update
# pkg_install libxml2-utils jq curl

# download latest runner package
local temp_dir=$(mktemp -d)
local LATEST=$(curl -sL https://github.com/actions/runner/releases/ \
| xmllint -html -xpath '//a[contains(@href, "release")]/text()' - 2> /dev/null \
| grep -P '^v' | head -n1 | sed "s/v//g")
curl --create-dir --output-dir ${temp_dir} -o \
[[ "$ARCH" == "x86_64" ]] && local arch=x64 || local arch=arm64
local LATEST=$(curl -sL https://api.github.com/repos/actions/runner/tags | jq -r '.[0].zipball_url' | rev | cut -d"/" -f1 | rev | sed "s/v//g")
curl --progress-bar --create-dir --output-dir ${temp_dir} -o \
actions-runner-linux-${ARCH}-${LATEST}.tar.gz -L \
https://github.com/actions/runner/releases/download/v${LATEST}/actions-runner-linux-${ARCH}-${LATEST}.tar.gz
https://github.com/actions/runner/releases/download/v${LATEST}/actions-runner-linux-${arch}-${LATEST}.tar.gz

# make runners each under its own user
for i in $(seq -w $start $stop)
Expand All @@ -61,6 +80,8 @@ function module_armbian_runners () {
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/${prefix}/${registration_url}/actions/runners/registration-token | jq -r .token)

${module_options["module_armbian_runners,feature"]} ${commands[1]} ${runner_name}

adduser --quiet --disabled-password --shell /bin/bash \
--home /home/actions-runner-${i} --gecos "actions-runner-${i}" actions-runner-${i}

Expand Down Expand Up @@ -88,23 +109,22 @@ function module_armbian_runners () {
echo "$start $stop $label_primary $label_secondary $organisation"
;;
"${commands[1]}")
for i in $(seq -w $start $stop); do
# delete if previous already exists
if id "actions-runner-${i}" >/dev/null 2>&1; then
runner_delete "$NAME-${i}"
runner_home=$(getent passwd "actions-runner-${i}" | cut -d: -f6)
sh -c "cd ${runner_home} ; \
sudo ./svc.sh stop actions-runner-${i} >/dev/null; \
sudo ./svc.sh uninstall actions-runner-${i} >/dev/null"
sudo userdel -r -f actions-runner-${i} 2>/dev/null
sudo groupdel actions-runner-${i} 2>/dev/null
sudo sed -i "/^actions-runner-${i}.*/d" /etc/sudoers
sudo rm -rf "${runner_home}"
fi
done
# delete if previous already exists
if id "actions-runner-${i}" >/dev/null 2>&1; then
echo "Removing runner ${i} on GitHub"
${module_options["module_armbian_runners,feature"]} ${commands[2]} "$2-${i}"
echo "Removing runner ${i} locally"
runuser -l actions-runner-${i} -c "sudo ./svc.sh stop actions-runner-${i} >/dev/null;"
runuser -l actions-runner-${i} -c "sudo ./svc.sh uninstall actions-runner-${i} >/dev/null;"
runner_home=$(getent passwd "actions-runner-${i}" | cut -d: -f6)
userdel -r -f actions-runner-${i} 2>/dev/null
groupdel actions-runner-${i} 2>/dev/null
sed -i "/^actions-runner-${i}.*/d" /etc/sudoers
rm -rf "${runner_home}"
fi
;;
"${commands[2]}")
DELETE=$1
DELETE=$2
x=1
while [ $x -le 9 ] # need to do it different as it can be more then 9 pages
do
Expand All @@ -118,8 +138,8 @@ function module_armbian_runners () {

while IFS= read -r DATA; do
RUNNER_ID=$(echo $DATA | cut -d"," -f1)
RUNNER_NAME=$(echo $DATA | cut -d"," -f2)
# deleting a runner
RUNNER_NAME=$(echo $DATA | cut -d"," -f2)
# deleting a runner
if [[ $RUNNER_NAME == ${DELETE} ]]; then
echo "Delete existing: $RUNNER_NAME"
curl -s -L \
Expand All @@ -137,8 +157,9 @@ function module_armbian_runners () {
echo -e "\nUsage: ${module_options["module_armbian_runners,feature"]} <command>"
echo -e "Commands: ${module_options["module_armbian_runners,example"]}"
echo "Available commands:"
echo -e "\tinstall\t- Install $title."
echo -e "\t\t token [runner_name] [start] [stop] [labels_primary] [labels_secondary] [organisation]/[owner repository]"
echo -e "\tinstall\t- Install $title. Parameters:"
echo -e "\t\t ${module_options["module_armbian_runners,install"]}"
echo -e "\t\t organisation and owner + repository are mutually exclusive"
echo -e "\tremove\t- Remove $title."
echo -e "\tstatus\t- Installation status $title."
echo
Expand Down

0 comments on commit 13e2e8a

Please sign in to comment.