From aa56e214fd7a1b8c0b45498bad41a9e6a6c12ae8 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Mon, 15 Jan 2024 23:03:21 +0400 Subject: [PATCH] TART_EXECUTOR_INSTALL_GITLAB_RUNNER: deprecate "true" In favor of explicit "brew" and "curl" installation methods. --- README.md | 22 ++++----- ...unner.sh => install-gitlab-runner-auto.sh} | 10 ++++ .../prepare/install-gitlab-runner-brew.sh | 25 ++++++++++ .../prepare/install-gitlab-runner-curl.sh | 47 +++++++++++++++++++ internal/commands/prepare/prepare.go | 33 +++++++++++-- internal/tart/config.go | 2 +- 6 files changed, 124 insertions(+), 15 deletions(-) rename internal/commands/prepare/{install-gitlab-runner.sh => install-gitlab-runner-auto.sh} (78%) create mode 100644 internal/commands/prepare/install-gitlab-runner-brew.sh create mode 100644 internal/commands/prepare/install-gitlab-runner-curl.sh diff --git a/README.md b/README.md index bdda456..1d32cfb 100644 --- a/README.md +++ b/README.md @@ -162,17 +162,17 @@ that required paid sponsorship upon exceeding a free limit. ## Supported environment variables -| Name | Default | Description | -|---------------------------------------|----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `TART_EXECUTOR_SSH_USERNAME` | admin | SSH username to use when connecting to the VM | -| `TART_EXECUTOR_SSH_PASSWORD` | admin | SSH password to use when connecting to the VM | -| `TART_EXECUTOR_HEADLESS` | true | Run the VM in headless mode (`true`) or with GUI (`false`) | -| `TART_EXECUTOR_ALWAYS_PULL` | true | Always pull the latest version of the Tart image (`true`) or only when the image doesn't exist locally (`false`) | -| `TART_EXECUTOR_SOFTNET` | false | Whether to enable [Softnet](https://github.com/cirruslabs/softnet) software networking (`true`) or disable it (`false`) | -| `TART_EXECUTOR_HOST_DIR`1 | false | Whether to mount a temporary directory from the host for performance reasons (`true`) or use a directory inside of a guest (`false`) | -| `TART_EXECUTOR_SHELL` | system default | Alternative [Unix shell](https://en.wikipedia.org/wiki/Unix_shell) to use (e.g. `bash -l`) | -| `TART_EXECUTOR_INSTALL_GITLAB_RUNNER` | false | Automatically install GitLab Runner [via Homebrew](https://docs.gitlab.com/runner/install/osx.html#homebrew-installation-alternative) or [manually using cURL](https://docs.gitlab.com/runner/install/osx.html#manual-installation-official), if Homebrew is not available | -| `TART_EXECUTOR_TIMEZONE` | | Timezone to set in the guest (or `auto` to pick up the timezone from host), see `systemsetup listtimezones` for a list of possible timezones | +| Name | Default | Description | +|---------------------------------------|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `TART_EXECUTOR_SSH_USERNAME` | admin | SSH username to use when connecting to the VM | +| `TART_EXECUTOR_SSH_PASSWORD` | admin | SSH password to use when connecting to the VM | +| `TART_EXECUTOR_HEADLESS` | true | Run the VM in headless mode (`true`) or with GUI (`false`) | +| `TART_EXECUTOR_ALWAYS_PULL` | true | Always pull the latest version of the Tart image (`true`) or only when the image doesn't exist locally (`false`) | +| `TART_EXECUTOR_SOFTNET` | false | Whether to enable [Softnet](https://github.com/cirruslabs/softnet) software networking (`true`) or disable it (`false`) | +| `TART_EXECUTOR_HOST_DIR`1 | false | Whether to mount a temporary directory from the host for performance reasons (`true`) or use a directory inside of a guest (`false`) | +| `TART_EXECUTOR_SHELL` | system default | Alternative [Unix shell](https://en.wikipedia.org/wiki/Unix_shell) to use (e.g. `bash -l`) | +| `TART_EXECUTOR_INSTALL_GITLAB_RUNNER` | | Set to `brew` to automatically install GitLab Runner [via Homebrew](https://docs.gitlab.com/runner/install/osx.html#homebrew-installation-alternative) or to `curl` to install it [automatically using cURL](https://docs.gitlab.com/runner/install/osx.html#manual-installation-official) | +| `TART_EXECUTOR_TIMEZONE` | | Timezone to set in the guest (or `auto` to pick up the timezone from host), see `systemsetup listtimezones` for a list of possible timezones | 1: to use the directory mounting feature, both the host and the guest need to run macOS 13.0 (Ventura) or newer. diff --git a/internal/commands/prepare/install-gitlab-runner.sh b/internal/commands/prepare/install-gitlab-runner-auto.sh similarity index 78% rename from internal/commands/prepare/install-gitlab-runner.sh rename to internal/commands/prepare/install-gitlab-runner-auto.sh index 0d0b627..06a00b8 100644 --- a/internal/commands/prepare/install-gitlab-runner.sh +++ b/internal/commands/prepare/install-gitlab-runner-auto.sh @@ -1,5 +1,15 @@ #!/bin/bash +# Set shell options to enable fail-fast behavior +# +# * -e: fail the script when an error occurs or command fails +# * -u: fail the script when attempting to reference unset parameters +# * -o pipefail: by default an exit status of a pipeline is that of its +# last command, this fails the pipe early if an error in +# any of its commands occurs +# +set -euo pipefail + GITLAB_RUNNER_URL="https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-arm64" GITLAB_RUNNER_PATH="/usr/local/bin/gitlab-runner" diff --git a/internal/commands/prepare/install-gitlab-runner-brew.sh b/internal/commands/prepare/install-gitlab-runner-brew.sh new file mode 100644 index 0000000..3b50749 --- /dev/null +++ b/internal/commands/prepare/install-gitlab-runner-brew.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Set shell options to enable fail-fast behavior +# +# * -e: fail the script when an error occurs or command fails +# * -u: fail the script when attempting to reference unset parameters +# * -o pipefail: by default an exit status of a pipeline is that of its +# last command, this fails the pipe early if an error in +# any of its commands occurs +# +set -euo pipefail + +# Is GitLab Runner already installed? +if type gitlab-runner &> /dev/null +then + echo "GitLab Runner is already installed, skipping installation" + + exit 0 +fi + +echo "Installing GitLab Runner via Homebrew..." + +brew install gitlab-runner + +echo "GitLab Runner was successfully installed!" diff --git a/internal/commands/prepare/install-gitlab-runner-curl.sh b/internal/commands/prepare/install-gitlab-runner-curl.sh new file mode 100644 index 0000000..9d91071 --- /dev/null +++ b/internal/commands/prepare/install-gitlab-runner-curl.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# Set shell options to enable fail-fast behavior +# +# * -e: fail the script when an error occurs or command fails +# * -u: fail the script when attempting to reference unset parameters +# * -o pipefail: by default an exit status of a pipeline is that of its +# last command, this fails the pipe early if an error in +# any of its commands occurs +# +set -euo pipefail + +GITLAB_RUNNER_URL="https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-arm64" +GITLAB_RUNNER_PATH="/usr/local/bin/gitlab-runner" + +# Is GitLab Runner already installed? +if type gitlab-runner &> /dev/null +then + echo "GitLab Runner is already installed, skipping installation" + + exit 0 +fi + +echo "Installing GitLab Runner using cURL..." + +if ! sudo -n true &>/dev/null +then + echo "Failed to install GitLab Runner using cURL: passwordless sudo is required, but is not configured" + + exit 1 +fi + +# /usr/local is empty on fresh macOS installations +# (for example, try ghcr.io/cirruslabs/macos-ventura-vanilla:latest) +if test ! -e /usr/local/bin +then + echo "Creating /usr/local/bin because it doesn't exist yet..." + sudo mkdir -p /usr/local/bin +fi + +echo "Downloading GitLab Runner from $GITLAB_RUNNER_URL to $GITLAB_RUNNER_PATH..." +sudo curl --output "$GITLAB_RUNNER_PATH" "$GITLAB_RUNNER_URL" + +echo "Making $GITLAB_RUNNER_PATH executable..." +sudo chmod +x "$GITLAB_RUNNER_PATH" + +echo "GitLab Runner was successfully installed!" diff --git a/internal/commands/prepare/prepare.go b/internal/commands/prepare/prepare.go index 5769266..fda7bcd 100644 --- a/internal/commands/prepare/prepare.go +++ b/internal/commands/prepare/prepare.go @@ -4,6 +4,7 @@ import ( "bytes" "context" _ "embed" + "errors" "fmt" "github.com/alecthomas/units" "github.com/cirruslabs/gitlab-tart-executor/internal/gitlab" @@ -17,8 +18,16 @@ import ( "strconv" ) -//go:embed install-gitlab-runner.sh -var installGitlabRunnerScript string +var ErrFailed = errors.New("\"prepare\" stage failed") + +//go:embed install-gitlab-runner-auto.sh +var installGitlabRunnerScriptAuto string + +//go:embed install-gitlab-runner-brew.sh +var installGitlabRunnerBrewScript string + +//go:embed install-gitlab-runner-curl.sh +var installGitlabRunnerCurlScript string var concurrency uint64 var cpuOverrideRaw string @@ -107,7 +116,25 @@ func runPrepareVM(cmd *cobra.Command, args []string) error { log.Println("Was able to SSH!") - if config.InstallGitlabRunner { + var installGitlabRunnerScript string + + switch config.InstallGitlabRunner { + case "brew": + installGitlabRunnerScript = installGitlabRunnerBrewScript + case "curl": + installGitlabRunnerScript = installGitlabRunnerCurlScript + case "true", "yes", "on": + log.Printf("%q value for TART_EXECUTOR_INSTALL_GITLAB_RUNNER will deprecated "+ + "in next version, please use either \"brew\" or \"curl\"", config.InstallGitlabRunner) + installGitlabRunnerScript = installGitlabRunnerScriptAuto + case "": + // do not set installGitlabRunnerScript to avoid installing GitLab Runner + default: + return fmt.Errorf("%w: TART_EXECUTOR_INSTALL_GITLAB_RUNNER only accepts "+ + "\"brew\" or \"curl\" arguments, got %q", ErrFailed, config.InstallGitlabRunner) + } + + if installGitlabRunnerScript != "" { log.Println("Installing GitLab Runner...") session, err := ssh.NewSession() diff --git a/internal/tart/config.go b/internal/tart/config.go index 3058eed..631d34d 100644 --- a/internal/tart/config.go +++ b/internal/tart/config.go @@ -38,7 +38,7 @@ type Config struct { AlwaysPull bool `env:"ALWAYS_PULL" envDefault:"true"` HostDir bool `env:"HOST_DIR"` Shell string `env:"SHELL"` - InstallGitlabRunner bool `env:"INSTALL_GITLAB_RUNNER"` + InstallGitlabRunner string `env:"INSTALL_GITLAB_RUNNER"` Timezone string `env:"TIMEZONE"` }