From 0bfa9fca88f75194183f5780a854c6c8429d4d25 Mon Sep 17 00:00:00 2001 From: DevTekVE Date: Mon, 23 Dec 2024 09:19:26 +0100 Subject: [PATCH] SP: GitHub runner as service and able to toggle via developer panel (#494) * Add support for GitHub Actions runner management Introduce a new process and configuration to manage a GitHub Actions runner. Added a persistent "EnableGithubRunner" parameter and a script to control the runner service. Integrated the feature into the system's process manager logic. * Restrict GitHub runner usage on metered networks. This update modifies the `use_github_runner` function to include a check for metered networks using `HARDWARE.get_network_metered()`. This ensures the GitHub runner is not enabled when the network is metered, improving network usage efficiency. * Add GitHub runner service toggle to developer panel Introduces a new toggle in the developer panel to enable or disable the GitHub runner service. This provides developers with a convenient way to control the service from the UI. * translations * Refactor network condition check for GitHub runner. Updated `use_github_runner` to include network type in metered network checks. This ensures more accurate validation by considering specific network types when determining metered status. * Mark as executable * Update paths and shebangs for consistency across scripts Modified file paths to align with the new directory structure under `/data/media/0/github` and updated the shebang in `github_runner.sh` for better environment compatibility. Adjusted the `BUILD_DIR` in the GitHub workflow to reflect the new path. * Fix string translation for GitHub runner parameter text Added translation support for the GitHub runner service description text. This ensures consistent localization across the UI. * Remove gitlab_runner.sh from Sunnypilot blacklist. The script is no longer required to be blacklisted, likely due to updates or changes in its usage. This improves the maintainability of the blacklist by removing unnecessary entries. * lang stuff * Update BASE_DIR determination based on mount point Refactored scripts to dynamically set BASE_DIR depending on whether /data/media is a valid mount point. This ensures compatibility with different environments and improves robustness of path resolution. * Refactor GitHub runner logic in process_config. Simplified enabling conditions for the GitHub runner by removing dependency on hardware network checks and adding a logical combination of offroad and runner-related functions. This improves code readability and reduces hardware coupling. --- .github/workflows/sunnypilot-build-model.yaml | 2 +- common/params.cc | 1 + release/ci/install_github_runner.sh | 8 +++- release/ci/uninstall_github_runner.sh | 8 +++- release/release_files.py | 1 - selfdrive/ui/qt/offroad/developer_panel.cc | 3 ++ selfdrive/ui/translations/main_ar.ts | 8 ++++ selfdrive/ui/translations/main_de.ts | 8 ++++ selfdrive/ui/translations/main_es.ts | 8 ++++ selfdrive/ui/translations/main_fr.ts | 8 ++++ selfdrive/ui/translations/main_ja.ts | 8 ++++ selfdrive/ui/translations/main_ko.ts | 8 ++++ selfdrive/ui/translations/main_pt-BR.ts | 8 ++++ selfdrive/ui/translations/main_th.ts | 8 ++++ selfdrive/ui/translations/main_tr.ts | 8 ++++ selfdrive/ui/translations/main_zh-CHS.ts | 8 ++++ selfdrive/ui/translations/main_zh-CHT.ts | 8 ++++ system/manager/github_runner.sh | 40 +++++++++++++++++++ system/manager/process_config.py | 6 +++ 19 files changed, 153 insertions(+), 4 deletions(-) create mode 100755 system/manager/github_runner.sh diff --git a/.github/workflows/sunnypilot-build-model.yaml b/.github/workflows/sunnypilot-build-model.yaml index b01a7a062..9b700e186 100644 --- a/.github/workflows/sunnypilot-build-model.yaml +++ b/.github/workflows/sunnypilot-build-model.yaml @@ -1,7 +1,7 @@ name: Build Model from Upstream env: - BUILD_DIR: "/data/github/openpilot" + BUILD_DIR: "/data/openpilot" OUTPUT_DIR: ${{ github.workspace }}/output SCONS_CACHE_DIR: ${{ github.workspace }}/release/ci/scons_cache UPSTREAM_REPO: "commaai/openpilot" diff --git a/common/params.cc b/common/params.cc index 2011b5213..1ab37ea84 100644 --- a/common/params.cc +++ b/common/params.cc @@ -200,6 +200,7 @@ std::unordered_map keys = { {"UpdaterTargetBranch", CLEAR_ON_MANAGER_START}, {"UpdaterLastFetchTime", PERSISTENT}, {"Version", PERSISTENT}, + {"EnableGithubRunner", PERSISTENT}, }; } // namespace diff --git a/release/ci/install_github_runner.sh b/release/ci/install_github_runner.sh index 3aa6b79dd..1b138996d 100755 --- a/release/ci/install_github_runner.sh +++ b/release/ci/install_github_runner.sh @@ -44,10 +44,16 @@ fi # Set repository URL if not provided REPO_URL="${REPO_URL:-$DEFAULT_REPO_URL}" +# Determine BASE_DIR based on mount point +if mountpoint -q /data/media; then + BASE_DIR="/data/media/0/github" +else + BASE_DIR="/data/github" +fi + # Constants RUNNER_USER="github-runner" USER_GROUPS="comma,gpu,gpio,sudo" -BASE_DIR="/data/github" RUNNER_DIR="${BASE_DIR}/runner" BUILDS_DIR="${BASE_DIR}/builds" LOGS_DIR="${BASE_DIR}/logs" diff --git a/release/ci/uninstall_github_runner.sh b/release/ci/uninstall_github_runner.sh index a7b0ca3c0..5f3acfbaf 100755 --- a/release/ci/uninstall_github_runner.sh +++ b/release/ci/uninstall_github_runner.sh @@ -1,6 +1,12 @@ #!/usr/bin/env bash +# Determine BASE_DIR based on mount point +if mountpoint -q /data/media; then + GITHUB_BASE_DIR="/data/media/0/github" +else + GITHUB_BASE_DIR="/data/github" +fi + # Define directories and user -GITHUB_BASE_DIR="/data/github" BIN_DIR="$GITHUB_BASE_DIR/bin" BUILDS_DIR="$GITHUB_BASE_DIR/builds" OPENPILOT_DIR="$GITHUB_BASE_DIR/openpilot" diff --git a/release/release_files.py b/release/release_files.py index 702255f96..dcebb1972 100755 --- a/release/release_files.py +++ b/release/release_files.py @@ -51,7 +51,6 @@ blacklist = [ # Sunnypilot blacklist sunnypilot_blacklist = [ "system/loggerd/sunnylink_uploader.py", # Temporarily, until we are ready to roll it out widely - "system/manager/gitlab_runner.sh", ".idea/", ".run/", ".*__pycache__/.*", diff --git a/selfdrive/ui/qt/offroad/developer_panel.cc b/selfdrive/ui/qt/offroad/developer_panel.cc index 5e127eb21..bbd69f72c 100644 --- a/selfdrive/ui/qt/offroad/developer_panel.cc +++ b/selfdrive/ui/qt/offroad/developer_panel.cc @@ -24,6 +24,9 @@ DeveloperPanel::DeveloperPanel(SettingsWindow *parent) : ListWidget(parent) { }); addItem(longManeuverToggle); + auto enableGithubRunner = new ParamControl("EnableGithubRunner", tr("Enable GitHub runner service"), tr("Enables or disables the github runner service."), ""); + addItem(enableGithubRunner); + // Joystick and longitudinal maneuvers should be hidden on release branches is_release = params.getBool("IsReleaseBranch"); diff --git a/selfdrive/ui/translations/main_ar.ts b/selfdrive/ui/translations/main_ar.ts index 2315268a7..0b18af4f2 100644 --- a/selfdrive/ui/translations/main_ar.ts +++ b/selfdrive/ui/translations/main_ar.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode وضع المناورة الطولية + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/selfdrive/ui/translations/main_de.ts b/selfdrive/ui/translations/main_de.ts index 2abed6e30..1f40c2e40 100644 --- a/selfdrive/ui/translations/main_de.ts +++ b/selfdrive/ui/translations/main_de.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/selfdrive/ui/translations/main_es.ts b/selfdrive/ui/translations/main_es.ts index 38ba964f1..6a27d4913 100644 --- a/selfdrive/ui/translations/main_es.ts +++ b/selfdrive/ui/translations/main_es.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode Modo de maniobra longitudinal + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/selfdrive/ui/translations/main_fr.ts b/selfdrive/ui/translations/main_fr.ts index f4251bc41..bcaff48bb 100644 --- a/selfdrive/ui/translations/main_fr.ts +++ b/selfdrive/ui/translations/main_fr.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/selfdrive/ui/translations/main_ja.ts b/selfdrive/ui/translations/main_ja.ts index bc83d3f9a..9e16d612f 100644 --- a/selfdrive/ui/translations/main_ja.ts +++ b/selfdrive/ui/translations/main_ja.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/selfdrive/ui/translations/main_ko.ts b/selfdrive/ui/translations/main_ko.ts index 1d2a753e1..4bbcae92a 100644 --- a/selfdrive/ui/translations/main_ko.ts +++ b/selfdrive/ui/translations/main_ko.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode 롱컨 기동 모드 + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/selfdrive/ui/translations/main_pt-BR.ts b/selfdrive/ui/translations/main_pt-BR.ts index a89c85510..ec3d18628 100644 --- a/selfdrive/ui/translations/main_pt-BR.ts +++ b/selfdrive/ui/translations/main_pt-BR.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode Modo Longitudinal Maneuver + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/selfdrive/ui/translations/main_th.ts b/selfdrive/ui/translations/main_th.ts index c2b277183..82e135a85 100644 --- a/selfdrive/ui/translations/main_th.ts +++ b/selfdrive/ui/translations/main_th.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/selfdrive/ui/translations/main_tr.ts b/selfdrive/ui/translations/main_tr.ts index 9a5344927..58da05c4d 100644 --- a/selfdrive/ui/translations/main_tr.ts +++ b/selfdrive/ui/translations/main_tr.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/selfdrive/ui/translations/main_zh-CHS.ts b/selfdrive/ui/translations/main_zh-CHS.ts index f061322c4..ab679b7f0 100644 --- a/selfdrive/ui/translations/main_zh-CHS.ts +++ b/selfdrive/ui/translations/main_zh-CHS.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/selfdrive/ui/translations/main_zh-CHT.ts b/selfdrive/ui/translations/main_zh-CHT.ts index 04e76a8d9..f88b12aba 100644 --- a/selfdrive/ui/translations/main_zh-CHT.ts +++ b/selfdrive/ui/translations/main_zh-CHT.ts @@ -123,6 +123,14 @@ Longitudinal Maneuver Mode + + Enable GitHub runner service + + + + Enables or disables the github runner service. + + DevicePanel diff --git a/system/manager/github_runner.sh b/system/manager/github_runner.sh new file mode 100755 index 000000000..f2170cfc7 --- /dev/null +++ b/system/manager/github_runner.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +# Define the service name +SERVICE_NAME="actions.runner.sunnypilot.$(uname -n)" + +# Function to control the service +control_service() { + local action=$1 # Store the function argument in a local variable + sudo systemctl $action ${SERVICE_NAME} +} + +service_exists_and_is_loaded() { + sudo systemctl status ${SERVICE_NAME} &>/dev/null + if [[ $? -ne 4 ]]; then + return 0 # Service is known to systemd (i.e., loaded) + else + return 1 # Service is unknown to systemd (i.e., not loaded) + fi +} + +# Check for required argument +if [[ -z $1 ]] || { [[ $1 != "start" ]] && [[ $1 != "stop" ]]; }; then + echo "Usage: $0 {start|stop}" + exit 1 +fi + +# Store the script argument in a descriptive variable +ACTION=$1 + +# Trap EXIT signal (Ctrl+C) and stop the service +trap 'control_service stop ; exit' SIGINT SIGKILL EXIT + +# Enter the main loop +while true; do + # Check if the service is actually present on the system + if service_exists_and_is_loaded; then + control_service $ACTION # Call the function with the specified action + fi + sleep 1 # Pause before the next iteration +done \ No newline at end of file diff --git a/system/manager/process_config.py b/system/manager/process_config.py index a25be615f..09b897363 100644 --- a/system/manager/process_config.py +++ b/system/manager/process_config.py @@ -54,6 +54,9 @@ def only_onroad(started: bool, params: Params, CP: car.CarParams) -> bool: def only_offroad(started: bool, params: Params, CP: car.CarParams) -> bool: return not started +def use_github_runner(started, params, CP: car.CarParams) -> bool: + return not PC and params.get_bool("EnableGithubRunner") and not params.get_bool("NetworkMetered") + def or_(*fns): return lambda *args: operator.or_(*(fn(*args) for fn in fns)) @@ -111,4 +114,7 @@ procs = [ PythonProcess("joystick", "tools.joystick.joystick_control", and_(joystick, iscar)), ] +if os.path.exists("./github_runner.sh"): + procs += [NativeProcess("github_runner_start", "system/manager", ["./github_runner.sh", "start"], and_(only_offroad, use_github_runner), sigkill=False)] + managed_processes = {p.name: p for p in procs}