diff --git a/.github/workflows/app-release.yml b/.github/workflows/app-release.yml index 83e0333..106e6ca 100644 --- a/.github/workflows/app-release.yml +++ b/.github/workflows/app-release.yml @@ -99,24 +99,27 @@ jobs: if: steps.target.outputs.base == '' && fromJSON(steps.target.outputs.major) <= fromJSON(steps.target.outputs.current) run: echo "::notice::No installer line follows AnsibleForms ${{ steps.target.outputs.major }} any more, nothing to bump." - # The RTE images are published by ansibleforms/rte, a few minutes after the app's own - # image. The installer of this version installs ansibleforms-rte-full of the same - # version, so the bump waits until it is published : a release never points at an image - # that is not there yet - - name: Wait for the RTE of the version + # The RTE images are published by ansibleforms/rte, and the RTE Runner by + # ansibleforms/runner, a few minutes after the app's own image. The installer of this + # version installs the Runner of the same version, its jobs running ansibleforms-rte-full, + # so the bump waits until both are published : a release never points at an image that is + # not there yet + - name: Wait for the RTE and the Runner of the version if: steps.target.outputs.base != '' env: VERSION: ${{ steps.target.outputs.version }} run: | set -euo pipefail - for i in $(seq 1 60); do - if docker manifest inspect "ghcr.io/ansibleforms/ansibleforms-rte-full:${VERSION}" >/dev/null 2>&1; then - echo "ghcr.io/ansibleforms/ansibleforms-rte-full:${VERSION} is published."; exit 0 - fi - sleep 30 + for image in ansibleforms-rte-full ansibleforms-rte-runner; do + for i in $(seq 1 60); do + if docker manifest inspect "ghcr.io/ansibleforms/${image}:${VERSION}" >/dev/null 2>&1; then + echo "ghcr.io/ansibleforms/${image}:${VERSION} is published."; continue 2 + fi + sleep 30 + done + echo "::error::ghcr.io/ansibleforms/${image}:${VERSION} was not published within 30 minutes. Once it is, run this workflow again by hand with ${VERSION}." + exit 1 done - echo "::error::ghcr.io/ansibleforms/ansibleforms-rte-full:${VERSION} was not published within 30 minutes. Once ansibleforms/rte published it, run this workflow again by hand with ${VERSION}." - exit 1 - uses: actions/create-github-app-token@v3 id: app diff --git a/CHANGELOG.md b/CHANGELOG.md index 708b065..46b5829 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## Unreleased +### Changed + +- **The RTE is the Runner written in Go, on Docker and Podman**: it starts each playbook in a + container of its own from `ansibleforms-rte-full`, through the engine's socket, which gives it + that engine's rights on the server. An upgrade switches an existing RTE in place, under the same + name, token and runner; `data/ansible`, the SSH key and the pip libraries still reach every job. + ## 7.8.0 AnsibleForms 7.8.0. diff --git a/internal/compose/compose.go b/internal/compose/compose.go index ea3e55e..09ffff4 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -386,6 +386,9 @@ func writeController(ctx context.Context, o Options) (*Result, error) { return nil, err } } + if err := prepareEngine(ctx, o.Runtime, o.Dir); err != nil { + return nil, err + } return &Result{ URL: fmt.Sprintf("%s://localhost:%d", scheme, o.Port), AdminUser: orDefault(GetEnv(string(env), "ADMIN_USERNAME"), "admin"), @@ -430,20 +433,27 @@ func pinCompose(dir, version, name string) error { return os.WriteFile(p, []byte(PrepareCompose(string(data), version, name)), 0o644) } -// runnerCompose is a runner's compose file: the RTE alone, talking to the app over one -// WebSocket (RTE_MODE in .env), with no database credentials. An inbound runner serves HTTPS -// (HTTPS=1 in .env) with ./data/certificates, where the app's image reads its certificate. -const runnerCompose = `# AnsibleForms RTE runner, written by the AnsibleForms installer. +// runnerCompose is a runner's compose file: the RTE Runner alone, talking to the app over one +// WebSocket (RTE_MODE in .env), with no database credentials, starting each job in a container +// of its own through the engine's socket (engine.go). An inbound runner serves HTTPS (HTTPS=1 in +// .env) with ./data/certificates. +const runnerCompose = `# AnsibleForms RTE Runner, written by the AnsibleForms installer. # It runs the playbooks of the app at APP_URL (see .env) over one WebSocket : it dials the app # (RTE_MODE=outbound) or the app dials it (RTE_MODE=inbound). It holds no database credentials. -# The job's files come from the app with each job ; ./data keeps what is not acked yet -# (rte-spool), the files received (rte-files), and its ansible.cfg, roles and collections. +# It starts each job in a container of its own from RTE_IMAGE, through the engine's socket +# (ENGINE_SOCKET in .env), which gives it that engine's full rights on this server. The job's +# files come from the app with each job ; ./data keeps what is not acked yet (rte-spool), the +# files received (rte-files), and the ansible.cfg, roles, collections, SSH key and python +# libraries every job gets (RTE_JOB_MOUNTS). # Inbound, it serves HTTPS (HTTPS=1 in .env) with the certificate in ./data/certificates. services: rte: image: {{IMAGE}} container_name: {{NAME}}_rte restart: unless-stopped + user: "${RTE_USER:-1000}" + group_add: + - "${ENGINE_SOCKET_GID:-0}" ports: - ${RTE_LOCAL_PORT}:8000 env_file: @@ -451,22 +461,45 @@ services: environment: - PORT=8000 - HTTPS=${HTTPS:-0} + - RTE_ENGINE_SOCKET=/run/engine.sock + - RTE_IMAGE={{JOB_IMAGE}} + - RTE_JOB_MOUNTS=` + jobMounts + ` - PYTHONPATH=/app/dist/persistent/python/lib healthcheck: - test: ["CMD", "node", "./healthcheck.js"] + test: ["CMD", "ansibleforms-rte-runner", "health"] interval: 10s timeout: 5s retries: 30 start_period: 30s volumes: - ./data:/app/dist/persistent - - ./data/ssh:/home/node/.ssh - - ./data/git/.gitconfig:/home/node/.gitconfig - - ./data/ansible/ansible.cfg:/etc/ansible/ansible.cfg - - ./data/ansible/roles:/etc/ansible/roles - - ./data/ansible/collections:/etc/ansible/collections + - ${ENGINE_SOCKET:-/var/run/docker.sock}:/run/engine.sock ` +// jobMounts is what every job gets from ./data, as the playbooks had it in the RTE's own +// container before 7.9.0 : the ansible.cfg, roles and collections, the SSH key and the git +// settings, and the libraries the pip install playbook adds. +const jobMounts = "/app/dist/persistent/ansible/ansible.cfg:/etc/ansible/ansible.cfg," + + "/app/dist/persistent/ansible/roles:/etc/ansible/roles," + + "/app/dist/persistent/ansible/collections:/etc/ansible/collections," + + "/app/dist/persistent/ssh:/home/node/.ssh:rw," + + "/app/dist/persistent/git/.gitconfig:/home/node/.gitconfig," + + "/app/dist/persistent/python:/app/dist/persistent/python:rw" + +// runnerComposeFor is a runner's compose file for a version and an instance. +// +// Args: +// +// version: the AnsibleForms version. +// name: the instance's name. +// +// Returns: +// +// The compose file. +func runnerComposeFor(version, name string) string { + return strings.NewReplacer("{{IMAGE}}", RunnerImage(version), "{{JOB_IMAGE}}", RteImage(version), "{{NAME}}", name).Replace(runnerCompose) +} + // runnerData are the docker repository's folders a runner keeps: the ansible configuration, // roles and collections, and the SSH and git settings (its playbooks come from the app). var runnerData = []string{"data", "data/ansible", "data/ssh", "data/git"} @@ -500,7 +533,7 @@ func writeRunner(ctx context.Context, o Options, carry map[string]string) (*Resu if _, _, err := Fetch(ctx, o.Version, o.Dir, only, o.KeepData); err != nil { return nil, err } - content := strings.NewReplacer("{{IMAGE}}", RteImage(o.Version), "{{NAME}}", o.Name).Replace(runnerCompose) + content := runnerComposeFor(o.Version, o.Name) r := o.Runner token := r.Token if token == "" { @@ -513,8 +546,6 @@ func writeRunner(ctx context.Context, o Options, carry map[string]string) (*Resu } lines := []string{ "# AnsibleForms RTE runner, written by the AnsibleForms installer", - "AF_ROLE=rte", - "NODE_ENV=production", "# outbound : it dials the app ; inbound : the app dials it at RTE_URL", "RTE_MODE=" + r.Mode, "# the app's address", @@ -561,6 +592,9 @@ func writeRunner(ctx context.Context, o Options, carry map[string]string) (*Resu // installer's own, just written) _ = os.Chmod(p, f.mode) } + if err := prepareEngine(ctx, o.Runtime, o.Dir); err != nil { + return nil, err + } return res, nil } diff --git a/internal/compose/compose_test.go b/internal/compose/compose_test.go index d4ac9e1..c1d0765 100644 --- a/internal/compose/compose_test.go +++ b/internal/compose/compose_test.go @@ -88,8 +88,9 @@ func newHost(t *testing.T) *host { "data/seed.yaml": read(t, "seed-7.3.yaml"), "data/mysql/conf.d/my.cnf": "[mysqld]\n", }) - wasSource, wasEuid, wasWait, wasPoll, wasSystemd := Source, geteuid, waitApp, pollEvery, systemdDir + wasSource, wasEuid, wasWait, wasPoll, wasSystemd, wasGID := Source, geteuid, waitApp, pollEvery, systemdDir, socketGID Source, pollEvery, systemdDir = h.repo, 10*time.Millisecond, t.TempDir()+"/" + socketGID = func(string) (int, error) { return 998, nil } geteuid = func() int { return 1001 } t.Setenv("ANSWERS", "7.3.0") waitApp = func(_ context.Context, url string, _ time.Duration) (string, error) { @@ -99,7 +100,7 @@ func newHost(t *testing.T) *host { return "", errors.New("no answer at " + url) } t.Cleanup(func() { - Source, geteuid, waitApp, pollEvery, systemdDir = wasSource, wasEuid, wasWait, wasPoll, wasSystemd + Source, geteuid, waitApp, pollEvery, systemdDir, socketGID = wasSource, wasEuid, wasWait, wasPoll, wasSystemd, wasGID }) return h } @@ -239,6 +240,13 @@ func TestInstallFails(t *testing.T) { "up": func(t *testing.T, h *host, o *Options) { t.Setenv("FAIL", "compose -p af up") }, "state": func(t *testing.T, h *host, o *Options) { t.Setenv("XDG_CONFIG_HOME", filepath.Join(h.repo, ".env")) }, "no answer": func(t *testing.T, h *host, o *Options) { t.Setenv("ANSWERS", "") }, + "engine": func(t *testing.T, h *host, o *Options) { + socketGID = func(string) (int, error) { return 0, errors.New("no socket") } + }, + "runner engine": func(t *testing.T, h *host, o *Options) { + o.Role, o.Runner = RoleRunner, RunnerOptions{Mode: "outbound"} + socketGID = func(string) (int, error) { return 0, errors.New("no socket") } + }, "runner fetch": func(t *testing.T, h *host, o *Options) { o.Role, o.Runner = RoleRunner, RunnerOptions{Mode: "outbound"} Source = filepath.Join(t.TempDir(), "none") diff --git a/internal/compose/engine.go b/internal/compose/engine.go new file mode 100644 index 0000000..334ec3f --- /dev/null +++ b/internal/compose/engine.go @@ -0,0 +1,121 @@ +package compose + +// ─── the engine the Runner starts the jobs on ───────────────────────────────── +// +// From 7.9.0 the RTE of a compose installation is the RTE Runner written in Go: it starts each +// job in a container of its own, through the engine's socket mounted into its container. The +// compose files (the docker repository's, and a Runner's own below) read where that socket is, +// and how the Runner's container may use it, from the .env : RTE_ENGINE, ENGINE_SOCKET, +// ENGINE_SOCKET_GID and RTE_USER. + +import ( + "context" + "fmt" + "os" + "path/filepath" + "strconv" + "strings" + "syscall" + + "github.com/ansibleforms/installer/internal/system" +) + +// socketGID reads the group a socket belongs to ; the tests give their own. +var socketGID = func(path string) (int, error) { + info, err := os.Stat(path) + if err != nil { + return 0, err + } + return int(info.Sys().(*syscall.Stat_t).Gid), nil +} + +// engineAccess is how a Runner's container reaches the engine that starts its jobs. +type engineAccess struct { + // Engine is docker or podman. + Engine string + // Socket is the engine's socket on this server. + Socket string + // GID is the socket's group, which the Runner's user joins. + GID int + // User is the Runner's user in its container: 1000, or 0 with rootless Podman, whose + // container root is the user running Podman. + User string +} + +// findEngine finds the engine's socket on this server: Docker's (DOCKER_HOST when it names a +// unix socket, else /var/run/docker.sock), rootful Podman's (/run/podman/podman.sock) or +// rootless Podman's (/run/user//podman/podman.sock), Podman's systemd socket started for +// it first (a server without systemd may run it otherwise: the socket itself decides). +// +// Args: +// +// ctx: the context, to cancel. +// r: the runtime. +// +// Returns: +// +// How the Runner's container reaches it. +// +// Errors: +// +// When the socket cannot be read. +func findEngine(ctx context.Context, r system.Runtime) (engineAccess, error) { + a := engineAccess{Engine: string(r), User: "1000"} + switch { + case r == system.Podman && geteuid() == 0: + a.Socket = "/run/podman/podman.sock" + _ = system.Run(ctx, "", "systemctl", "enable", "--now", "podman.socket") + case r == system.Podman: + a.Socket, a.User = fmt.Sprintf("/run/user/%d/podman/podman.sock", geteuid()), "0" + _ = system.Run(ctx, "", "systemctl", "--user", "enable", "--now", "podman.socket") + default: + a.Engine, a.Socket = string(system.Docker), "/var/run/docker.sock" + if v := os.Getenv("DOCKER_HOST"); strings.HasPrefix(v, "unix://") { + a.Socket = strings.TrimPrefix(v, "unix://") + } + } + gid, err := socketGID(a.Socket) + if err != nil { + return a, fmt.Errorf("the %s socket %s cannot be read: %w", a.Engine, a.Socket, err) + } + a.GID = gid + return a, nil +} + +// env is what the compose files read of it, for the .env. +// +// Returns: +// +// RTE_ENGINE, ENGINE_SOCKET, ENGINE_SOCKET_GID and RTE_USER. +func (a engineAccess) env() map[string]string { + return map[string]string{"RTE_ENGINE": a.Engine, "ENGINE_SOCKET": a.Socket, "ENGINE_SOCKET_GID": strconv.Itoa(a.GID), "RTE_USER": a.User} +} + +// prepareEngine readies an installation folder for the Runner: the engine's settings in its +// .env, and data/python/lib, the folder every job gets for the libraries the pip install +// playbook adds (made here, so the engine does not make it as root). +// +// Args: +// +// ctx: the context, to cancel. +// r: the runtime. +// dir: the installation folder. +// +// Errors: +// +// When the engine cannot be found, or a file cannot be written. +func prepareEngine(ctx context.Context, r system.Runtime, dir string) error { + a, err := findEngine(ctx, r) + if err != nil { + return err + } + envPath := filepath.Join(dir, ".env") + env, err := os.ReadFile(envPath) + if err != nil { + return err + } + if err := os.WriteFile(envPath, []byte(SetEnv(string(env), a.env())), 0o600); err != nil { + return err + } + return os.MkdirAll(filepath.Join(dir, "data", "python", "lib"), 0o755) +} diff --git a/internal/compose/engine_test.go b/internal/compose/engine_test.go new file mode 100644 index 0000000..2d9c2bb --- /dev/null +++ b/internal/compose/engine_test.go @@ -0,0 +1,86 @@ +package compose + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/ansibleforms/installer/internal/system" +) + +// TestFindEngine checks the engine's socket and how the Runner's container uses it: Docker's, +// or DOCKER_HOST's ; rootful Podman's, its systemd socket started ; rootless Podman's, run as +// the container's root ; and a socket that cannot be read. +func TestFindEngine(t *testing.T) { + h := newHost(t) + ctx := context.Background() + t.Setenv("DOCKER_HOST", "") + if a, err := findEngine(ctx, system.Docker); err != nil || a != (engineAccess{"docker", "/var/run/docker.sock", 998, "1000"}) { + t.Errorf("docker: %+v %v", a, err) + } + t.Setenv("DOCKER_HOST", "unix:///run/user/1001/docker.sock") + if a, _ := findEngine(ctx, system.Docker); a.Socket != "/run/user/1001/docker.sock" { + t.Errorf("DOCKER_HOST: %+v", a) + } + geteuid = func() int { return 0 } + if a, err := findEngine(ctx, system.Podman); err != nil || a != (engineAccess{"podman", "/run/podman/podman.sock", 998, "1000"}) || !h.bin.Called("systemctl enable --now podman.socket") { + t.Errorf("rootful podman: %+v %v", a, err) + } + geteuid = func() int { return 1001 } + if a, err := findEngine(ctx, system.Podman); err != nil || a != (engineAccess{"podman", "/run/user/1001/podman/podman.sock", 998, "0"}) || !h.bin.Called("systemctl --user enable --now podman.socket") { + t.Errorf("rootless podman: %+v %v", a, err) + } + socketGID = func(string) (int, error) { return 0, errors.New("no such file") } + if _, err := findEngine(ctx, system.Podman); err == nil || !strings.Contains(err.Error(), "podman socket /run/user/1001/podman/podman.sock cannot be read") { + t.Errorf("no socket: %v", err) + } + if err := prepareEngine(ctx, system.Podman, t.TempDir()); err == nil { + t.Error("prepared without a socket") + } +} + +// TestSocketGID checks a file's group is read. +func TestSocketGID(t *testing.T) { + f := filepath.Join(t.TempDir(), "sock") + _ = os.WriteFile(f, nil, 0o600) + if gid, err := socketGID(f); err != nil || gid != os.Getgid() { + t.Errorf("gid %d %v", gid, err) + } + if _, err := socketGID(f + ".missing"); err == nil { + t.Error("a missing file") + } +} + +// TestPrepareEngine checks the .env gets the engine's settings and data/python/lib is made, and +// that a folder without a .env, or whose .env cannot be written, stops it. +func TestPrepareEngine(t *testing.T) { + newHost(t) + ctx := context.Background() + dir := t.TempDir() + _ = os.WriteFile(filepath.Join(dir, ".env"), []byte("RTE_TOKEN=x\nRTE_USER=1000\n"), 0o600) + if err := prepareEngine(ctx, system.Docker, dir); err != nil { + t.Fatal(err) + } + env, _ := os.ReadFile(filepath.Join(dir, ".env")) + for _, want := range []string{"RTE_ENGINE=docker", "ENGINE_SOCKET=/var/run/docker.sock", "ENGINE_SOCKET_GID=998", "RTE_USER=1000", "RTE_TOKEN=x"} { + if !strings.Contains(string(env), want) { + t.Errorf(".env lacks %s: %s", want, env) + } + } + if info, err := os.Stat(filepath.Join(dir, "data", "python", "lib")); err != nil || !info.IsDir() { + t.Error("no data/python/lib") + } + if err := prepareEngine(ctx, system.Docker, t.TempDir()); err == nil { + t.Error("no .env") + } + ro := t.TempDir() + _ = os.WriteFile(filepath.Join(ro, ".env"), nil, 0o400) + _ = os.Chmod(ro, 0o500) + defer os.Chmod(ro, 0o700) + if err := prepareEngine(ctx, system.Docker, ro); err == nil && os.Geteuid() != 0 { + t.Error("a .env that cannot be written") + } +} diff --git a/internal/compose/files.go b/internal/compose/files.go index de580ac..bf49d44 100644 --- a/internal/compose/files.go +++ b/internal/compose/files.go @@ -264,11 +264,14 @@ var ( func PrepareCompose(content, version, name string) string { content = imageRe.ReplaceAllStringFunc(content, func(m string) string { repo := imageRe.FindStringSubmatch(m)[1] - if strings.HasSuffix(repo, "-rte-full") || strings.Contains(repo, "-rte-") { - if img := os.Getenv("AF_RTE_IMAGE"); img != "" { - return img - } - } else if img := os.Getenv("AF_APP_IMAGE"); img != "" { + override := "AF_APP_IMAGE" + switch { + case strings.HasSuffix(repo, "-rte-runner"): + override = "AF_RUNNER_IMAGE" + case strings.Contains(repo, "-rte-"): + override = "AF_RTE_IMAGE" + } + if img := os.Getenv(override); img != "" { return img } return repo + ":" + version @@ -276,7 +279,7 @@ func PrepareCompose(content, version, name string) string { return containerRe.ReplaceAllString(content, "${1}"+name+"_${2}") } -// RteImage is the RTE image a runner runs : AF_RTE_IMAGE (a mirror, a test build), else the +// RunnerImage is the RTE Runner's image : AF_RUNNER_IMAGE (a mirror, a test build), else the // published one at the version. // // Args: @@ -286,6 +289,23 @@ func PrepareCompose(content, version, name string) string { // Returns: // // The image. +func RunnerImage(version string) string { + if img := os.Getenv("AF_RUNNER_IMAGE"); img != "" { + return img + } + return "ghcr.io/ansibleforms/ansibleforms-rte-runner:" + version +} + +// RteImage is the image a runner's jobs run (RTE_IMAGE) : AF_RTE_IMAGE (a mirror, a test +// build), else the published ansibleforms-rte-full at the version. +// +// Args: +// +// version: the AnsibleForms version. +// +// Returns: +// +// The image. func RteImage(version string) string { if img := os.Getenv("AF_RTE_IMAGE"); img != "" { return img diff --git a/internal/compose/files_test.go b/internal/compose/files_test.go index e6c0792..fb4aa2b 100644 --- a/internal/compose/files_test.go +++ b/internal/compose/files_test.go @@ -200,17 +200,22 @@ func TestCopyLocal(t *testing.T) { // TestPrepareCompose checks the images pinned to the version (or to AF_APP_IMAGE and // AF_RTE_IMAGE) and the containers named after the instance. func TestPrepareCompose(t *testing.T) { - in := " image: ghcr.io/ansibleforms/ansibleforms:7\n image: ghcr.io/ansibleforms/ansibleforms-rte-full:7\n container_name: af_app\n" + in := " image: ghcr.io/ansibleforms/ansibleforms:7\n image: ghcr.io/ansibleforms/ansibleforms-rte-runner:7\n" + + " - RTE_IMAGE=ghcr.io/ansibleforms/ansibleforms-rte-full:7\n container_name: af_app\n" t.Setenv("AF_APP_IMAGE", "") t.Setenv("AF_RTE_IMAGE", "") + t.Setenv("AF_RUNNER_IMAGE", "") got := PrepareCompose(in, "7.3.0", "lab") - if !strings.Contains(got, "ansibleforms:7.3.0") || !strings.Contains(got, "rte-full:7.3.0") || !strings.Contains(got, "container_name: lab_app") { + if !strings.Contains(got, "ansibleforms:7.3.0") || !strings.Contains(got, "rte-full:7.3.0") || !strings.Contains(got, "rte-runner:7.3.0") || + !strings.Contains(got, "container_name: lab_app") || RunnerImage("7.3.0") != "ghcr.io/ansibleforms/ansibleforms-rte-runner:7.3.0" { t.Errorf("pinned %s", got) } t.Setenv("AF_APP_IMAGE", "local/app:dev") t.Setenv("AF_RTE_IMAGE", "local/rte:dev") + t.Setenv("AF_RUNNER_IMAGE", "local/runner:dev") got = PrepareCompose(in, "7.3.0", "af") - if !strings.Contains(got, "local/app:dev") || !strings.Contains(got, "local/rte:dev") || RteImage("7.3.0") != "local/rte:dev" { + if !strings.Contains(got, "local/app:dev") || !strings.Contains(got, "RTE_IMAGE=local/rte:dev") || !strings.Contains(got, "image: local/runner:dev") || + RteImage("7.3.0") != "local/rte:dev" || RunnerImage("7.3.0") != "local/runner:dev" { t.Errorf("overridden %s", got) } } diff --git a/internal/compose/migrate_test.go b/internal/compose/migrate_test.go index 30a5af0..43b6a2c 100644 --- a/internal/compose/migrate_test.go +++ b/internal/compose/migrate_test.go @@ -49,6 +49,9 @@ func put(t *testing.T, dir string, files map[string]string) { // ansible.cfg of its own, and a local copy of the docker repository to take the files from. func runner72(t *testing.T) (*state.State, Migration) { t.Helper() + wasGID := socketGID + socketGID = func(string) (int, error) { return 998, nil } + t.Cleanup(func() { socketGID = wasGID }) repo := t.TempDir() put(t, repo, map[string]string{ "docker-compose.yml": "services: {}\n", @@ -113,7 +116,7 @@ func TestPrepareMigrationOutbound(t *testing.T) { } // the .env : the socket mode, the runner kept, the settings carried, nothing of the database env := envLines(readFile(t, filepath.Join(st.Dir, ".env"))) - want := map[string]string{"AF_ROLE": "rte", "RTE_MODE": "outbound", "APP_URL": "https://forms.example.com", "RTE_NAME": "rte72.example.com", + want := map[string]string{"RTE_ENGINE": "docker", "ENGINE_SOCKET_GID": "998", "RTE_MODE": "outbound", "APP_URL": "https://forms.example.com", "RTE_NAME": "rte72.example.com", "RTE_TOKEN": token72, "RTE_LOCAL_PORT": "127.0.0.1:8001", "RTE_MAX_JOBS": "4", "HTTPS_PROXY": "http://proxy.example.com:3128"} for k, v := range want { if env[k] != v { diff --git a/internal/compose/ops.go b/internal/compose/ops.go index 87540d6..4cb96a2 100644 --- a/internal/compose/ops.go +++ b/internal/compose/ops.go @@ -194,8 +194,13 @@ func Upgrade(ctx context.Context, st *state.State, to string, prep *Prep) (*Undo undo.Compose = string(old) if st.Role == RoleRunner { - content := strings.NewReplacer("{{IMAGE}}", RteImage(to), "{{NAME}}", st.Name).Replace(runnerCompose) - if err := os.WriteFile(composeFile, []byte(content), 0o644); err != nil { + // the .env as it was: the engine's settings go into it below + envFile, err := os.ReadFile(filepath.Join(st.Dir, ".env")) + if err != nil { + return nil, err + } + undo.Env = string(envFile) + if err := os.WriteFile(composeFile, []byte(runnerComposeFor(to, st.Name)), 0o644); err != nil { return undo, err } } else { @@ -254,6 +259,14 @@ func Upgrade(ctx context.Context, st *state.State, to string, prep *Prep) (*Undo return undo, err } } + // from 7.9.0 the RTE is the Go Runner: the engine's socket in the .env, data/python/lib + // made and given to the containers' user + if err := prepareEngine(ctx, system.Runtime(st.Runtime), st.Dir); err != nil { + return undo, err + } + if err := ownData(ctx, system.Runtime(st.Runtime), filepath.Join(st.Dir, "data", "python")); err != nil { + return undo, err + } ui.Step(i18n.T("compose.pulling")) // an image that is only on this server (loaded from a file, a mirror) is not an error : up diff --git a/internal/compose/ops_test.go b/internal/compose/ops_test.go index bef2e38..a513111 100644 --- a/internal/compose/ops_test.go +++ b/internal/compose/ops_test.go @@ -130,7 +130,7 @@ func TestUpgradeRollbackRunner(t *testing.T) { if err != nil { t.Fatal(err) } - if c, _ := os.ReadFile(filepath.Join(h.dir, "docker-compose.yml")); !strings.Contains(string(c), "rte-full:7.4.0") { + if c, _ := os.ReadFile(filepath.Join(h.dir, "docker-compose.yml")); !strings.Contains(string(c), "rte-full:7.4.0") || !strings.Contains(string(c), "rte-runner:7.4.0") { t.Errorf("compose %s", c) } if err := Rollback(context.Background(), st, undo); err != nil { @@ -138,6 +138,17 @@ func TestUpgradeRollbackRunner(t *testing.T) { } } +// TestUpgradeRunnerNoEnv checks a Runner's upgrade stops when its .env cannot be read, before +// anything changes. +func TestUpgradeRunnerNoEnv(t *testing.T) { + h := newHost(t) + st := installedOn(t, h, RoleRunner) + _ = os.Remove(filepath.Join(h.dir, ".env")) + if _, err := Upgrade(context.Background(), st, "7.4.0", nil); err == nil { + t.Error("upgraded without a .env") + } +} + // TestUpgradeRollbackMySQL checks an upgrade that moves MySQL to a new major: the users that // are not the installer's saved, the data's folder remembered ; its rollback puts the new // data aside, restores the dump and replays those users. @@ -230,6 +241,14 @@ func TestUpgradeFails(t *testing.T) { t.Setenv("FAIL", "docker run") return nil }, + "engine": func(t *testing.T, h *host, st *state.State) *Prep { + socketGID = func(string) (int, error) { return 0, errors.New("no socket") } + return nil + }, + "own python": func(t *testing.T, h *host, st *state.State) *Prep { + t.Setenv("FAIL", "docker run") + return nil + }, "apply": func(t *testing.T, h *host, st *state.State) *Prep { return &Prep{SeedAction: "custom", Seed: "s"} },