From 5c73a27e268ff07db294114a29d23e0a5c78a4be Mon Sep 17 00:00:00 2001 From: pika Date: Sat, 12 Apr 2025 11:28:01 +0200 Subject: [PATCH] addet forgejo-runner installation script --- installs/forgejo-runner.sh | 160 +++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100755 installs/forgejo-runner.sh diff --git a/installs/forgejo-runner.sh b/installs/forgejo-runner.sh new file mode 100755 index 0000000..f89d1f5 --- /dev/null +++ b/installs/forgejo-runner.sh @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +# ─< ANSI color codes >─────────────────────────────────────────────────────────────────── +RED='\033[0;31m' +CYAN='\033[0;36m' +YELLOW='\033[0;33m' +LIGHT_GREEN='\033[0;92m' +BOLD='\033[1m' +NC='\033[0m' + +# ─< Initialize storage variables >─────────────────────────────────────────────────────── +_STORED_ERRORS="" +_STORED_WARNINGS="" +_STORED_INFOS="" +_STORED_NOTES="" + +# ─< echo functions that store and display messages >──────────────────────────── +echo_error() { + local message="${RED}$1${NC}\n" + printf "$message" >&2 + _STORED_ERRORS="${_STORED_ERRORS}${message}" +} + +echo_warning() { + local message="${YELLOW}$1${NC}\n" + printf "$message" + _STORED_WARNINGS="${_STORED_WARNINGS}${message}" +} + +echo_info() { + local message="${CYAN}$1${NC}\n" + printf "$message" + _STORED_INFOS="${_STORED_INFOS}${message}" +} + +echo_note() { + local message="${LIGHT_GREEN}$1${NC}\n" + printf "$message" + _STORED_NOTES="${_STORED_NOTES}${message}" +} + +# ─< Improved display function that only shows categories with content >────────────────── +display_stored_messages() { + local has_messages=0 + + # ─< First check if we have any messages at all >───────────────────────────────────────── + if [ -z "$_STORED_ERRORS" ] && [ -z "$_STORED_WARNINGS" ] && [ -z "$_STORED_INFOS" ] && [ -z "$_STORED_NOTES" ]; then + return 0 + fi + + # ─< Now display each non-empty category with proper spacing >──────────────────────────── + if [ -n "$_STORED_ERRORS" ]; then + printf "\n${BOLD}${RED}=== Errors ===${NC}\n" + printf "$_STORED_ERRORS" + has_messages=1 + fi + + if [ -n "$_STORED_WARNINGS" ]; then + [ "$has_messages" -eq 1 ] && printf "\n" + printf "${BOLD}${YELLOW}=== Warnings ===${NC}\n" + printf "$_STORED_WARNINGS" + has_messages=1 + fi + + if [ -n "$_STORED_INFOS" ]; then + [ "$has_messages" -eq 1 ] && printf "\n" + printf "${BOLD}${CYAN}=== Info ===${NC}\n" + printf "$_STORED_INFOS" + has_messages=1 + fi + + if [ -n "$_STORED_NOTES" ]; then + [ "$has_messages" -eq 1 ] && printf "\n" + printf "${BOLD}${LIGHT_GREEN}=== Notes ===${NC}\n" + printf "$_STORED_NOTES" + fi +} + +_exit() { + display_stored_messages + exit 1 +} + +# ─< Check if the given command exists silently >───────────────────────────────────────── +command_exists() { + command -v "$@" >/dev/null 2>&1 +} + +# ─< Check if the user is root and set sudo variable if necessary >─────────────────────── +check_root() { + if [ "$(id -u)" -ne 0 ]; then + if command_exists sudo; then + echo_info "User is not root. Using sudo for privileged operations." + _sudo="sudo" + else + echo_error "No sudo found and you're not root! Can't install packages." + return 1 + fi + else + echo_info "Root access confirmed." + _sudo="" + fi +} + +getRunner() { + # ╭─────────────────────────────────────────────────────────╮ + # │ should output something like this │ + # │ ╭─────────────────────────────────────────────────────╮ │ + # │ │ Good signature from "Forgejo " │ │ + # │ │ aka "Forgejo Releases " │ │ + # │ ╰─────────────────────────────────────────────────────╯ │ + # ╰─────────────────────────────────────────────────────────╯ + # + # ─< get the runner version >───────────────────────────────────────────────────────────── + export RUNNER_VERSION=$(curl -X 'GET' https://data.forgejo.org/api/v1/repos/forgejo/runner/releases/latest | jq .name -r | cut -c 2-) + # ─< get the runner binary >────────────────────────────────────────────────────────────── + wget -O forgejo-runner https://code.forgejo.org/forgejo/runner/releases/download/v${RUNNER_VERSION}/forgejo-runner-${RUNNER_VERSION}-linux-amd64 + # ─< make it executable >───────────────────────────────────────────────────────────────── + chmod +x forgejo-runner + # ─< get the verification hashes >──────────────────────────────────────────────────────── + wget -O forgejo-runner.asc https://code.forgejo.org/forgejo/runner/releases/download/v${RUNNER_VERSION}/forgejo-runner-${RUNNER_VERSION}-linux-amd64.asc + gpg --keyserver keys.openpgp.org --recv EB114F5E6C0DC2BCDD183550A4B61A2DC5923710 + gpg --verify forgejo-runner.asc forgejo-runner +} + +userCreation() { + $_sudo useradd --create-home runner + $_sudo usermod -aG docker runner +} + +envCheck() { + if ! command_exists curl; then + echo_error "You have no curl installed" + _exit + fi + + if ! command_exists wget; then + echo_error "You have no wget installed" + _exit + fi + + if ! command_exists docker; then + echo_warning "Sorry, you have no docker installed.." + _exit + fi + + if command_exists forgejo-runner; then + echo_warning "forgejo-runner binary is already callable.." + _exit + fi +} + +if envCheck; then + getRunner + if ! grep -iq "runner" /etc/passwd; then + userCreation + fi + + forgejo-runner generate-config >config.yml && $_sudo rsync -avP config.yml /home/runner/config.yml +fi