From 662ad4281807813fb0dde8edd2dadf11454f02bc Mon Sep 17 00:00:00 2001 From: Teg79 Date: Thu, 19 Nov 2026 18:12:37 +0100 Subject: [PATCH] fix(bootstrap): refuse a platform still on Gitea, which Forgejo can't open, before changing anything Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0182yxsfwRowwnxCfgrQwjnA --- CHANGELOG.md | 7 ++++ internal/bootstrap/forgeguard.go | 55 +++++++++++++++++++++++++ internal/bootstrap/orchestrator.go | 6 +++ internal/bootstrap/orchestrator_test.go | 51 +++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 internal/bootstrap/forgeguard.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 31292a1..6a85cc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,13 @@ workflow refuses a tag whose version has no entry here. ## [Unreleased] +### Fixed + +- `urbex bootstrap` refuses a platform created by urbex 0.1.0, whose forge + is still Gitea: Forgejo can't open Gitea 1.24's database, and starting it + there left the forge down. Nothing is changed; the message says how to + start the platform again on Forgejo (see Upgrading). + ## [0.1.0] - 2026-11-17 The first preview release: urbex as binaries for Linux and macOS (amd64, diff --git a/internal/bootstrap/forgeguard.go b/internal/bootstrap/forgeguard.go new file mode 100644 index 0000000..1d764ba --- /dev/null +++ b/internal/bootstrap/forgeguard.go @@ -0,0 +1,55 @@ +package bootstrap + +import ( + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "regexp" + "strings" +) + +// forgeComposeCopy is the forge's compose file as its LXC runs it, copied +// into the GitOps repo by every bootstrap (the gitea role), 0.1.0's +// included. +var forgeComposeCopy = filepath.Join(PlatformStacksDir, "gitea", "compose.yaml") + +// giteaImage matches the forge image of a platform created by urbex +// 0.1.0: Gitea, which Forgejo replaced (ADR-0034). +var giteaImage = regexp.MustCompile(`(?m)^\s*image:\s*(docker\.io/)?gitea/gitea[:@]\S*`) + +// ErrPlatformRunsGitea is returned by Run on a platform whose forge is +// still Gitea: Forgejo can't open the database of Gitea 1.23 and later. +var ErrPlatformRunsGitea = errors.New("the platform's forge is Gitea") + +// refuseGiteaPlatform stops a bootstrap that would start Forgejo on +// Gitea's data (urbforge/urbex-cli#116): Forgejo refuses a database +// migrated by Gitea past 1.22 and exits, and the forge stays down. +// It reads only the GitOps repo; nothing has been changed yet. +func refuseGiteaPlatform(gitopsRepoPath string) error { + data, err := os.ReadFile(filepath.Join(gitopsRepoPath, forgeComposeCopy)) + if errors.Is(err, fs.ErrNotExist) { + return nil + } + if err != nil { + return err + } + m := giteaImage.Find(data) + if m == nil { + return nil + } + return fmt.Errorf(`%w (%s, in %s): this platform was created by urbex 0.1.0. +This urbex runs Forgejo, which can't read the database of Gitea 1.24, so it +won't touch the forge or anything else on this platform. To move to this +release, start the platform again: + + urbex destroy # in each project, for each environment (teardown requires it); + # the applications' named volumes go with their LXCs: back them up first + urbex teardown # the platform and its LXCs, Gitea's repositories with them + urbex bootstrap # a new platform, on Forgejo + +then push your projects' repositories again from your clones and 'urbex apply' +their environments. Or keep using urbex 0.1.0 on this platform. +See "Upgrading" in the CHANGELOG (0.2.0)`, ErrPlatformRunsGitea, strings.TrimSpace(regexp.MustCompile(`\s+`).ReplaceAllString(string(m), " ")), forgeComposeCopy) +} diff --git a/internal/bootstrap/orchestrator.go b/internal/bootstrap/orchestrator.go index 353183e..3a1a70d 100644 --- a/internal/bootstrap/orchestrator.go +++ b/internal/bootstrap/orchestrator.go @@ -20,6 +20,7 @@ import ( "net/http" "os" "path/filepath" + "slices" "time" "github.com/urbforge/urbex-cli/internal/credentials" @@ -102,6 +103,11 @@ func (o *Orchestrator) Run(ctx context.Context, opts Options) error { } fmt.Fprintf(o.Out, "Base services already present on Proxmox: %v\n", alreadyPresent) fmt.Fprintf(o.Out, "Base services to provision: %v\n", toCreate) + if slices.Contains(alreadyPresent, Hostname("gitea")) { + if err := refuseGiteaPlatform(opts.GitOpsRepoPath); err != nil { + return err + } + } ips, prefixLen, err := allocateIPs(o.Config) if err != nil { diff --git a/internal/bootstrap/orchestrator_test.go b/internal/bootstrap/orchestrator_test.go index 420ae35..b1f244d 100644 --- a/internal/bootstrap/orchestrator_test.go +++ b/internal/bootstrap/orchestrator_test.go @@ -588,3 +588,54 @@ func (k *fakeKomodo) TagResource(ctx context.Context, kind, name string, tags .. k.f.tags[kind+" "+name] = tags return nil } + +// A platform created by urbex 0.1.0 runs Gitea 1.24, whose database +// Forgejo can't open (urbforge/urbex-cli#116): bootstrap refuses before +// it changes anything, and says how to move on. +func TestRun_RefusesAPlatformStillOnGitea(t *testing.T) { + dir := t.TempDir() + for _, f := range []string{"terraform/base/terraform.tfstate", "platform/gitea/compose.yaml"} { + if err := os.MkdirAll(filepath.Dir(filepath.Join(dir, f)), 0o755); err != nil { + t.Fatal(err) + } + } + os.WriteFile(filepath.Join(dir, "terraform/base/terraform.tfstate"), []byte("{}"), 0o644) + compose := "services:\n gitea:\n image: gitea/gitea:1.24\n restart: unless-stopped\n" + os.WriteFile(filepath.Join(dir, "platform/gitea/compose.yaml"), []byte(compose), 0o644) + + o, runner, _ := newRunOrchestrator(t, &fakeProxmox{hostnames: []string{"urbex-gitea", "urbex-komodo"}}) + err := o.Run(context.Background(), Options{GitOpsRepoPath: dir}) + if !errors.Is(err, ErrPlatformRunsGitea) { + t.Fatalf("Run: %v, want ErrPlatformRunsGitea", err) + } + for _, want := range []string{"gitea/gitea:1.24", "urbex 0.1.0", "urbex teardown", "urbex destroy", "back them up", "Upgrading"} { + if !strings.Contains(err.Error(), want) { + t.Errorf("the error doesn't say %q:\n%v", want, err) + } + } + if len(runner.calls) != 0 { + t.Errorf("ran %v before refusing", runner.calls) + } + if _, err := os.Stat(filepath.Join(dir, "ansible")); err == nil { + t.Error("materialized the assets before refusing") + } + + // The same platform on Forgejo goes on. + os.WriteFile(filepath.Join(dir, "platform/gitea/compose.yaml"), []byte("services:\n gitea:\n image: codeberg.org/forgejo/forgejo:15.0.11@sha256:5d05\n"), 0o644) + if err := refuseGiteaPlatform(dir); err != nil { + t.Errorf("a Forgejo platform refused: %v", err) + } + // So does a platform without the copy (a new one). + if err := refuseGiteaPlatform(t.TempDir()); err != nil { + t.Errorf("no compose copy refused: %v", err) + } + if err := refuseGiteaPlatform(dir + "/nope"); err != nil { + t.Error(err) + } + for _, img := range []string{"docker.io/gitea/gitea:1.24", "gitea/gitea@sha256:abcd"} { + os.WriteFile(filepath.Join(dir, "platform/gitea/compose.yaml"), []byte("services:\n gitea:\n image: "+img+"\n"), 0o644) + if !errors.Is(refuseGiteaPlatform(dir), ErrPlatformRunsGitea) { + t.Errorf("%s not refused", img) + } + } +}