diff --git a/tools/modules/system/module_armbian_runners.sh b/tools/modules/system/module_armbian_runners.sh index f5cc7c5f..b0ed8bf7 100644 --- a/tools/modules/system/module_armbian_runners.sh +++ b/tools/modules/system/module_armbian_runners.sh @@ -4,6 +4,7 @@ module_options+=( ["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,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"]="" @@ -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}" @@ -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) @@ -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]} + adduser --quiet --disabled-password --shell /bin/bash \ --home /home/actions-runner-${i} --gecos "actions-runner-${i}" actions-runner-${i} @@ -88,20 +109,19 @@ 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]} "$NAME-${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 @@ -118,9 +138,11 @@ function module_armbian_runners () { while IFS= read -r DATA; do RUNNER_ID=$(echo $DATA | cut -d"," -f1) - RUNNER_NAME=$(echo $DATA | cut -d"," -f2) + RUNNER_NAME=$(echo $DATA | cut -d"," -f2) # deleting a runner + echo $DELETE if [[ $RUNNER_NAME == ${DELETE} ]]; then + echo "Delete existing: $RUNNER_NAME" curl -s -L \ -X DELETE \ @@ -137,8 +159,9 @@ function module_armbian_runners () { echo -e "\nUsage: ${module_options["module_armbian_runners,feature"]} " 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