Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
or later. On Kubernetes its namespace is created if needed and labelled to admit privileged pods
(`pod-security.kubernetes.io/enforce=privileged`), which its rootless BuildKit needs; when the
installer may not, it stops and says what a cluster admin runs.
- **A native Builder**: `--mode native --role builder` installs the builder directly on the server,
as root, without Docker or Podman. Its static binary comes from the builder's release at the
controller's version and BuildKit from moby/buildkit's release that the installer pins, each
checked against its SHA-256. BuildKit runs as the root service `ansibleforms-buildkitd`, its
socket open to the builder's group only, and the builder as the service of its own system user.
An upgrade moves the builder to the new version and BuildKit to the installer's, an uninstall
removes both services, and a native Builder is found and taken over. Without root it is refused,
saying to run the installer with sudo or to use Docker, Podman or Kubernetes.
- **`AF_CHART` names the chart to install from on Kubernetes**: a chart folder or `.tgz`, such as a
test build or a copy for an air-gapped cluster, or a mirror's address, in place of the published
chart. Installs and upgrades both use it.
Expand Down
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The application itself lives in [ansibleforms/ansibleforms](https://github.com/a
- [What lives where](#what-lives-where)
- [Building and testing](#building-and-testing)
- [Texts and languages](#texts-and-languages)
- [BuildKit's version](#buildkits-version)
- [Pull requests](#pull-requests)
- [Releasing](#releasing)
- [Following app releases](#following-app-releases)
Expand Down Expand Up @@ -58,6 +59,21 @@ A new or changed text goes into all eleven catalogs, with the app's own words fo

---

## BuildKit's version

A native Builder runs the BuildKit release that `BuildkitVersion` in `internal/native/builder.go` pins, and an upgrade moves it there.
Dependabot does not see that constant, so it is bumped by hand, best together with the `moby/buildkit` image of the builder's Dockerfile.

moby/buildkit publishes no checksum files: the SHA-256 of both archives is pinned next to the version, in `buildkitSums`, and checked on download.
Take them from GitHub's digests of the release's assets, then run the tests:

```bash
gh api repos/moby/buildkit/releases/tags/v0.35.1 \
--jq '.assets[] | select(.name | test("linux-(amd|arm)64.tar.gz$")) | "\(.digest) \(.name)"'
```

---

## Pull requests

Open one pull request per change, against `main`, with a title such as `fix: short summary of the change`.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The first question is what to install: the whole application, or a part that joi
|---|---|---|
| Controller | the web interface, its database and its own Runner | Docker, Podman, Kubernetes or native |
| Runner | the component that runs the playbooks of a controller elsewhere, outbound or inbound | Docker, Podman, Kubernetes or native |
| Builder | the component that builds the RTE images a controller defines and pushes them to a registry | Docker, Podman or Kubernetes |
| Builder | the component that builds the RTE images a controller defines and pushes them to a registry | Docker, Podman, Kubernetes or native |

A Runner and a Builder ask for the controller's address, then join it with a join token or wait for an admin's approval.
On Docker and Podman an upgrade writes their compose file again for the new version; on Kubernetes they are the Helm chart's runner and builder modes,
Expand All @@ -41,7 +41,7 @@ installed and upgraded through `helm` with the values the installer keeps.
A Builder has a few needs of its own:

- **Controller:** AnsibleForms 7.9.0 or later; it is approved under Connections > Builders.
- **No native install:** its BuildKit runs in its container, on Docker, Podman or Kubernetes.
- **Native:** as root, the builder runs as a systemd service of its own user, BuildKit as a root service.
- **Kubernetes:** the installer labels its namespace privileged.
- **Rootless Podman:** it runs as the container's root, privileged in your user namespace only.

Expand Down
48 changes: 27 additions & 21 deletions internal/cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ func install(ctx context.Context, f Flags) error {
return nil
}
if f.Mode == string(state.Native) {
// a builder runs BuildKit in a container : it needs an engine or a cluster
if builder() {
return errors.New(i18n.T("native.cannot_builder"))
}
if why := nativeSupported(); why != "" {
// a builder's BuildKit runs as a root service : root, or containers
if builder() && why == "root" {
why = "builder"
}
return errors.New(i18n.T("native.cannot_" + why))
}
p.mode = f.Mode
Expand Down Expand Up @@ -280,9 +280,12 @@ func install(ctx context.Context, f Flags) error {
home, _ := os.UserHomeDir()
def = filepath.Join(home, filepath.Base(def))
}
if nativeMode() {
switch {
case nativeMode() && builder():
ui.Explain(i18n.T("install.dirHelpNativeBuilder"))
case nativeMode():
ui.Explain(i18n.T("install.dirHelpNative"))
} else {
default:
ui.Explain(i18n.T("install.dirHelp"))
}
d, err := textAgain(f.Dir, i18n.T("install.dir"), orStr(p.dir, def), notEmpty)
Expand All @@ -304,7 +307,10 @@ func install(ctx context.Context, f Flags) error {
}
if nativeMode() {
// the system user and the services are named after it
def = map[bool]string{true: "ansibleforms-runner", false: "ansibleforms"}[runner()]
def = map[string]string{compose.RoleRunner: "ansibleforms-runner", compose.RoleBuilder: "ansibleforms-builder"}[p.role]
if def == "" {
def = "ansibleforms"
}
help, title = i18n.T("install.nameHelpNative"), i18n.T("install.nameNative")
}
ui.Explain(help)
Expand Down Expand Up @@ -855,8 +861,8 @@ func doKubeRunner(ctx context.Context, f Flags, p *plan) error {
return nil
}

// doBuilder installs an RTE builder on compose : it dials the app, approved at once with a
// join token, else by an admin under Connections > Builders.
// doBuilder installs an RTE builder on compose or directly on this server : it dials the app,
// approved at once with a join token, else by an admin under Connections > Builders.
//
// Args:
//
Expand All @@ -876,7 +882,11 @@ func doBuilder(ctx context.Context, p *plan) error {
r.JoinToken = token
}
o := compose.Options{Role: compose.RoleBuilder, Runtime: p.runtime, Version: p.version.String(), Dir: p.dir, Name: p.name, Runner: r}
st, res, err := composeInstall(ctx, o)
install := composeInstall
if p.mode == string(state.Native) {
install = nativeInstall
}
st, res, err := install(ctx, o)
if err != nil {
return err
}
Expand Down Expand Up @@ -944,8 +954,8 @@ func builderDone(name string, r compose.RunnerOptions, token string) []string {

// chooseEngine picks Docker or Podman, Kubernetes or this server directly, in one list : the
// engines the server has as they are, Docker or Podman to install when missing, Kubernetes
// greyed out when no cluster is reached, and this server greyed out for a builder (its
// BuildKit runs in a container) or where it is not supported.
// greyed out when no cluster is reached, and this server greyed out where it is not supported
// (without root : a builder's BuildKit runs as a root service, as a controller's MariaDB does).
//
// Args:
//
Expand All @@ -955,7 +965,7 @@ func builderDone(name string, r compose.RunnerOptions, token string) []string {
// prev: the answer of before (Esc), the default.
// withKube: Kubernetes is listed (no --mode).
// role: controller, runner (Kubernetes installs the chart's runner mode, an RTE alone) or
// builder (the chart's builder mode ; never natively).
// builder (the chart's builder mode ; natively with BuildKit as a service).
//
// Returns:
//
Expand Down Expand Up @@ -987,15 +997,11 @@ func chooseEngine(ctx context.Context, f Flags, tools system.Tools, prev string,
}
kube := withKube && tools.Kubectl && tools.Helm && tools.Cluster
nativeOption := func() ui.Option {
hint := "install.modeNativeHint"
if role == compose.RoleRunner {
hint = "install.modeNativeRunnerHint"
}
why := nativeSupported()
if role == compose.RoleBuilder {
why = "builder"
hint := map[string]string{compose.RoleRunner: "install.modeNativeRunnerHint", compose.RoleBuilder: "install.modeNativeBuilderHint"}[role]
if hint == "" {
hint = "install.modeNativeHint"
}
if why != "" {
if why := nativeSupported(); why != "" {
return ui.Option{Label: i18n.T("install.modeNative_" + why), Value: string(state.Native), Disabled: true}
}
return ui.Option{Label: i18n.T("install.modeNative"), Value: string(state.Native), Hint: i18n.T(hint)}
Expand Down
37 changes: 32 additions & 5 deletions internal/cli/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,8 @@ func TestInstallBuilderWizard(t *testing.T) {
}

// TestInstallBuilderKubeAndNative checks a builder on Kubernetes (the chart's builder mode)
// with a join token and without, a failed helm, and a builder refused directly on this server.
// with a join token and without, a failed helm, and a builder directly on this server: given
// by its flag or picked, as root, and refused without root or on a Linux it does not support.
func TestInstallBuilderKubeAndNative(t *testing.T) {
for _, join := range []string{"afj_x", ""} {
s := fakeServer(t)
Expand All @@ -493,18 +494,44 @@ func TestInstallBuilderKubeAndNative(t *testing.T) {
if err := install(context.Background(), Flags{InstallerVersion: "7.9.0", Lang: "en", Role: "builder", Mode: "kubernetes", AppURL: "https://app"}); err == nil {
t.Error("kube builder failed")
}
// directly on this server, as root : its folder and its name under /opt, installed natively
s = fakeServer(t)
if err := install(context.Background(), Flags{InstallerVersion: "7.9.0", Lang: "en", Role: "builder", Mode: "native"}); err == nil || !strings.Contains(err.Error(), "BuildKit") {
t.Errorf("native builder: %v", err)
s.app = "7.9.0"
t.Setenv("AS_ROOT", "1")
answering(t, map[string][]tui.Answer{})
if err := install(context.Background(), Flags{InstallerVersion: "7.9.0", Lang: "en", Role: "builder", Mode: "native", AppURL: "https://app", JoinToken: "afj_x"}); err != nil || !s.called("nativeInstall") {
t.Fatalf("native builder: %v, %v", err, s.calls)
}
if st, _ := state.Load(); st.Mode != state.Native || st.Role != "builder" || st.Name != "ansibleforms-builder" || st.Dir != "/opt/ansibleforms-builder" {
t.Errorf("native builder state %+v", st)
}
// picked from the list, then a failed installation
s = fakeServer(t)
s.app = "7.9.0"
s.fail["nativeInstall"] = errors.New("systemd failed")
answering(t, map[string][]tui.Answer{title("install.runtime"): {pick("native")}})
if err := install(context.Background(), Flags{InstallerVersion: "7.9.0", Lang: "en", Role: "builder", AppURL: "https://app"}); err == nil || !s.called("nativeInstall") {
t.Errorf("native builder failed: %v", err)
}
// without root : refused, saying to run it as root or to use containers
s = fakeServer(t)
t.Setenv("NATIVE_WHY", "root")
if err := install(context.Background(), Flags{InstallerVersion: "7.9.0", Lang: "en", Role: "builder", Mode: "native"}); err == nil ||
!strings.Contains(err.Error(), "needs root") || !strings.Contains(err.Error(), "sudo") || !strings.Contains(err.Error(), "Docker, Podman or Kubernetes") {
t.Errorf("native builder without root: %v", err)
}
t.Setenv("NATIVE_WHY", "unsupported")
if err := install(context.Background(), Flags{InstallerVersion: "7.9.0", Lang: "en", Role: "builder", Mode: "native"}); err == nil || !strings.Contains(err.Error(), "not supported") {
t.Errorf("native builder unsupported: %v", err)
}
if s.called("nativeInstall") {
t.Error("installed natively")
}
}

// TestBuilderChecks checks the builder's own texts and checks : a controller older than
// 7.9.0 refused, no address unattended, a join token that is none, the native choice greyed
// out and the Kubernetes one with the builder mode's hint, the review, and the role names.
// 7.9.0 refused, no address unattended, a join token that is none, the Kubernetes choice
// with the builder mode's hint, the review, and the role names.
func TestBuilderChecks(t *testing.T) {
ctx := context.Background()
s := fakeServer(t)
Expand Down
46 changes: 34 additions & 12 deletions internal/detect/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type unit struct {
const unitProperties = "Id,Description,User,WorkingDirectory,EnvironmentFiles,ExecStart,ActiveState"

// services finds AnsibleForms run as systemd services: the units whose description, folder,
// settings or command name AnsibleForms and that run Node.js themselves (a unit that brings a
// compose project up is the containers', found with them).
// settings or command name AnsibleForms and that run Node.js or the RTE builder themselves (a
// unit that brings a compose project up is the containers', found with them).
//
// Args:
//
Expand Down Expand Up @@ -129,7 +129,7 @@ func parseShow(out string) []unit {
//
// Returns:
//
// True when it names AnsibleForms and runs Node.js, not a container engine.
// True when it names AnsibleForms and runs Node.js or the RTE builder, not a container engine.
func isAnsibleForms(u unit) bool {
all := strings.ToLower(u.ID + " " + u.Description + " " + u.WorkingDirectory + " " + u.EnvironmentFiles + " " + u.ExecStart)
if !strings.Contains(all, "ansibleforms") && !strings.Contains(all, "ansible forms") {
Expand All @@ -139,7 +139,13 @@ func isAnsibleForms(u unit) bool {
if strings.Contains(cmd, "docker") || strings.Contains(cmd, "podman") || strings.Contains(cmd, " compose ") {
return false
}
return strings.Contains(cmd, "node") || strings.Contains(cmd, "npm")
return strings.Contains(cmd, "node") || strings.Contains(cmd, "npm") || isBuilder(u)
}

// isBuilder tells whether a unit runs the RTE builder's binary (a native Builder: its
// BuildKit's unit runs buildkitd, and is not one of AnsibleForms' own).
func isBuilder(u unit) bool {
return strings.Contains(strings.ToLower(u.ExecStart), "ansibleforms-rte-builder")
}

// envFile is the first EnvironmentFile of a unit (systemctl shows "path (ignore_errors=no)").
Expand Down Expand Up @@ -211,7 +217,11 @@ func nativeInstall(ctx context.Context, env Env, dir string, units []unit) Insta
}
sort.Strings(i.Units)
main := rte
if app != nil {
if app != nil && isBuilder(*app) {
// a native Builder : the builder alone, named as its instance
i.Role, main = RoleBuilder, app
i.Name = strings.TrimSuffix(app.ID, ".service")
} else if app != nil {
i.Role, main = RoleController, app
i.Name = strings.TrimSuffix(app.ID, ".service")
} else {
Expand Down Expand Up @@ -252,29 +262,41 @@ func nativeInstall(ctx context.Context, env Env, dir string, units []unit) Insta

// nativeProblem tells why native services cannot be taken over, if they cannot: the upgrade
// puts a release in releases/, moves the current link and restarts <name> and <name>-rte,
// run by the system user <name> with app.env and rte.env, as the installer writes them.
// run by the system user <name> with app.env and rte.env, as the installer writes them (a
// Builder: <name> alone, with builder.env, in its folder).
//
// Args:
//
// i: the installation found.
// app: the app's unit (nil for a Runner).
// app: the app's unit (nil for a Runner ; a Builder's builder).
// rte: the RTE's unit (nil when there is none).
//
// Returns:
//
// The problem's code, "" when there is none.
func nativeProblem(i Install, app, rte *unit) string {
layout := func(u *unit, service string) bool {
want := i.Name
if service == "rte" {
want, wd := i.Name, filepath.Join(i.Dir, "current", "server")
switch service {
case "rte":
want += "-rte"
case "builder":
// the builder runs in its folder, its binary in the current release
wd = i.Dir
}
return u != nil && u.ID == want+".service" && u.User == i.Name &&
filepath.Clean(u.WorkingDirectory) == filepath.Join(i.Dir, "current", "server") &&
filepath.Clean(u.WorkingDirectory) == wd &&
envFile(*u) == filepath.Join(i.Dir, service+".env")
}
if !layout(rte, "rte") || (i.Role == RoleController && !layout(app, "app")) {
return NativeLayout
switch i.Role {
case RoleBuilder:
if !layout(app, "builder") {
return NativeLayout
}
default:
if !layout(rte, "rte") || (i.Role == RoleController && !layout(app, "app")) {
return NativeLayout
}
}
target, err := os.Readlink(filepath.Join(i.Dir, "current"))
if err != nil || filepath.Dir(filepath.Clean(target)) != "releases" {
Expand Down
42 changes: 42 additions & 0 deletions internal/detect/native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,45 @@ func TestNativeInstallRunnerAndProbe(t *testing.T) {
t.Errorf("controller %+v", c)
}
}

// TestNativeBuilder checks a native Builder's unit found as a Builder named after it, with
// its port and the version of its current release, taken over in the installer's layout ;
// and each way its layout is not the installer's. BuildKit's own unit is not AnsibleForms'.
func TestNativeBuilder(t *testing.T) {
dir := t.TempDir()
if err := os.MkdirAll(filepath.Join(dir, "releases", "7.9.0"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join("releases", "7.9.0"), filepath.Join(dir, "current")); err != nil {
t.Fatal(err)
}
write(t, dir, map[string]string{"builder.env": "PORT=8011\n"})
u := unit{ID: "bx.service", User: "bx", WorkingDirectory: dir, EnvironmentFiles: filepath.Join(dir, "builder.env") + " (ignore_errors=no)",
ExecStart: "{ path=" + dir + "/current/ansibleforms-rte-builder ; argv[]=" + dir + "/current/ansibleforms-rte-builder }", ActiveState: "active"}
if !isAnsibleForms(u) || !isBuilder(u) || baseDir(u) != dir {
t.Fatalf("the builder's unit %+v", u)
}
bk := unit{ID: "ansibleforms-buildkitd.service", Description: "BuildKit of the AnsibleForms RTE builder (bx)", ExecStart: dir + "/current/buildkit/buildkitd --addr unix:///run/buildkit/buildkitd.sock"}
if isAnsibleForms(bk) {
t.Error("BuildKit's unit taken for AnsibleForms")
}
f := &fake{t: t, has: map[string]bool{}, out: map[string]string{}}
b := nativeInstall(context.Background(), f.env(), dir, []unit{u})
if b.Role != RoleBuilder || b.Name != "bx" || b.Port != 8011 || b.Version != "7.9.0" || !b.Running || !b.Adoptable() {
t.Fatalf("builder %+v", b)
}
if st := b.State(); st.Mode != state.Native || st.Role != RoleBuilder || st.Dir != dir || st.Name != "bx" {
t.Errorf("state %+v", st)
}
for name, change := range map[string]func(u *unit){
"another user": func(u *unit) { u.User = "root" },
"another folder": func(u *unit) { u.WorkingDirectory = "/srv" },
"another file": func(u *unit) { u.EnvironmentFiles = "/etc/builder.env" },
} {
other := u
change(&other)
if p := nativeProblem(b, &other, nil); p != NativeLayout {
t.Errorf("%s: %q", name, p)
}
}
}
Loading