Skip to content

Commit 0808c17

Browse files
[WIP] Launcher2 (#791)
Partial merge of Launcher2's CLI * FEATURE: merge launcher2 - build command merge launcher2, only build commands
1 parent ceb9264 commit 0808c17

24 files changed

+1697
-0
lines changed

launcher_go/README.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Launcher2
2+
3+
Build and run discourse images. Drop in replacement for launcher the shell script.
4+
5+
## Changes from launcher
6+
7+
No software prerequisites are checked here. It assumes you have docker set up and whatever minimum requirements setup for Discourse: namely a recent enough version of docker, git.
8+
9+
Some things are not implemented from launcher1.
10+
11+
* `DOCKER_HOST_IP` - container can use `host.docker.internal` in most cases. Supported on mac and windows... can also be [added on linux via docker args](https://stackoverflow.com/questions/72827527/what-is-running-on-host-docker-internal-host).
12+
* debug containers - not implemented. No debug containers saved on build. Under the hood, launcher2 uses docker build which does not allow images to be saved along the way.
13+
* stable `mac-address` - not implemented.
14+
15+
## New features
16+
17+
In a nutshell: split bootstrap/rebuild process up into distinct parts to allow for greater flexibility in how we build and deploy Discourse containers.
18+
19+
### Separates bootstrap process into distinct build, configure, and migrate steps.
20+
21+
Separating the larger bootstrap process into separate steps allows us to break up the work.
22+
23+
`bootstrap` becomes an alias for: `build`, `migrate`, `configure`. There are multiple benefits to this.
24+
25+
#### Build: Easier creation for prebuilt docker images
26+
27+
Share built docker images by only running a `build` step - this build step does not need to connect to a database.
28+
It does not need postgres or redis running. This makes for a simple way to install custom plugins to your Discourse image.
29+
30+
The resulting image is able to be used in Kubernetes and other docker environments.
31+
32+
This is done by deferring finishing the build step, to a later configure step -- which boostraps the db, and precompiles assets.
33+
34+
The `configure` and `migrate` steps can now be done on boot through use of env vars set in the `app.yml` config: `CREATE_DB_ON_BOOT`, `MIGRATE_ON_BOOT`, and `PRECOMPILE_ON_BOOT`, which allows for more portable containers able to drop in and bootstrap themselves and the database as they come into service.
35+
36+
#### Build: Better environment management
37+
38+
The resulting image from a build is a container with no environment (unless `--bake-env` is specified). Additionally, well-known secrets are excluded from the build environment, resulting in a clean history of the prebuilt image that may be more easily shared.
39+
40+
Environment is only bound to a container either with `--bake-env` on build, or on a subsequent `configure` step.
41+
42+
#### Migrate: Adds support to *when* migrations are run
43+
44+
`Build` and `Configure` steps do not run migrations, allowing for external tooling to specify exactly when migrations are run.
45+
46+
`Migrate`, (and`bootstrap`, and `rebuild`) steps are the only ones that run migrations.
47+
48+
#### Migrate: Adds support for *how* migrations are run: `SKIP_POST_DEPLOYMENT_MIGRATIONS` support
49+
50+
the `migrate` step exposes env vars that turn on separate post deploy migration steps.
51+
52+
Allows the ability to turn on and skip post migration steps from launcher when running a stand-alone migrate step.
53+
54+
#### Rebuild: Minimize downtime
55+
56+
Both standalone and multi-container setups' downtime have been minimized for rebuilds
57+
58+
##### Standalone
59+
On standalone builds, only stop the running container after the base build is done.
60+
Standalone sites will only need to be offline during migration and configure steps.
61+
62+
For standalone, `rebuild` runs `build`, `stop`, `migrate`, `configure`, `destroy`, `start`.
63+
64+
##### Multiple container, web only
65+
On multi-container setups or setups with a configured external database using web only containers, rebuilds attempt to run migrations without stopping the container.
66+
A multi-container stays up as migration (skipping post deployment migrations) and as any necessary configuration steps are run. After deploy, post deployment migrations are run to clean up any destructive migrations.
67+
68+
For web-only, `rebuild` runs `build`, `migrate (skip post migrations)`, `configure`, `destroy`, `start`, `migrate`.
69+
70+
#### Rebuild: Serve offline page during downtime
71+
72+
Adds the ability to build and run an image that finishes a build on boot, allowing the server to display an offline page.
73+
For standalone builds above, this allows for the accrued downtime from migration and configure steps to happen more gracefully.
74+
75+
Additional container env vars get turned on by adding the `offline-page.template.yml` template:
76+
* `CREATE_DB_ON_BOOT`
77+
* `MIGRATE_ON_BOOT`
78+
* `PRECOMPILE_ON_BOOT`
79+
80+
These allow containers to boot cleanly from a cold state, and complete db creation, migration, and precompile steps on boot.
81+
82+
During this time, nginx can be up which allows standalone builds to display an offline page.
83+
84+
These variables may also be used for other applications where more flexible bootstrapping is desired.
85+
86+
##### Standalone
87+
On rebuild, a standalone site will skip migration if it detects the presence of `MIGRATE_ON_BOOT` in the app config, and will skip configure steps if it detects the presence of `PRECOMPILE_ON_BOOT` in the app config.
88+
89+
For standalone, `rebuild` runs `build`, `destroy`, `start`, skipping `migrate` and `configure`. The started container then serves an offline page, and runs migrate and precompiles assets before fully entering service.
90+
91+
##### Multiple container, web only
92+
On rebuild, a web only container will act in the same way as a standalone container. This may result in the same downtime as standalone services, as the containers are swapped, and the new container is still responsible for migration and precompiling before serving traffic.
93+
94+
For web-only containers, it may be desired to either ensure that `MIGRATE_ON_BOOT` and `PRECOMPILE_ON_BOOT` are false. Alternatively, you may run with `--full-build` which will ensure that migration and precompile steps are not deferred for the 'live' deploy.
95+
96+
### Multiline env support
97+
98+
Allows the use of multiline env vars so this is valid config, and is passed through to the container as expected:
99+
```
100+
env:
101+
SECRET_KEY: |
102+
---START OF SECRET KEY---
103+
123456
104+
78910
105+
---END OF SECRET KEY---
106+
```
107+
108+
### More dependable SIGINT/SIGTERM handling.
109+
110+
Launcher wraps docker run commands, which run as children in process trees. Launcher2 does the same, but attempts to kill or stop the underlying docker processes from interrupt signals.
111+
112+
Tools that extend or depend on launcher should be able to send SIGINT/SIGTERM signals to tell launcher to shut down, and launcher should clean up child processes appropriately.
113+
114+
### Docker compose generation.
115+
116+
Allows easier exporting of configuration from discourse's pups configuration to a docker compose configuration.
117+
118+
### Autocomplete support
119+
120+
Run `source <(./launcher2 sh)` to activate completions for the current shell, or add the results of `./launcher2 sh` to your dotfiles
121+
122+
Autocompletes commands, subcommands, and suggests `app` config files from your containers directory. Having a long site name should not feel like a pain to type.
123+
124+
## Maintainability
125+
126+
Golang is well suited as a drop in replacement as just like a shellscript, the deployed binary can still carry minimal assumptions about a particular platform to run. (IE, no dependency on ruby, python, etc)
127+
128+
Golang allows us to use a fully fleshed out programming language to run native yaml parsing: Calling out to ruby through a docker container worked well enough, but got complicated shuffling results through stdout into shell variables.
129+
130+
Launcher has outgrown being a simple wrapper script around Docker. Golang has good support for tests and breaking up code into separate modules to better support further growth around additional subcommands we may wish to add.
131+
132+
## Roadmap
133+
134+
Scaffolding out subcommands, possibly as a later rewrite for `discourse-setup` as having native YAML libraries should make config parsing and editing simpler to do.

launcher_go/v2/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.PHONY: default
2+
default: build
3+
4+
.PHONY: build
5+
build:
6+
go build -o bin/launcher2
7+
8+
.PHONY: test
9+
test:
10+
go test ./...

launcher_go/v2/bin/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
launcher2*

launcher_go/v2/cli_build.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"errors"
6+
"github.com/discourse/discourse_docker/launcher_go/v2/config"
7+
"github.com/discourse/discourse_docker/launcher_go/v2/docker"
8+
"os"
9+
"strings"
10+
)
11+
12+
/*
13+
* build
14+
* migrate
15+
* configure
16+
* bootstrap
17+
*/
18+
type DockerBuildCmd struct {
19+
BakeEnv bool `short:"e" help:"Bake in the configured environment to image after build."`
20+
Tag string `default:"latest" help:"Resulting image tag."`
21+
22+
Config string `arg:"" name:"config" help:"configuration" predictor:"config"`
23+
}
24+
25+
func (r *DockerBuildCmd) Run(cli *Cli, ctx *context.Context) error {
26+
config, err := config.LoadConfig(cli.ConfDir, r.Config, true, cli.TemplatesDir)
27+
if err != nil {
28+
return errors.New("YAML syntax error. Please check your containers/*.yml config files.")
29+
}
30+
31+
dir := cli.BuildDir + "/" + r.Config
32+
if err := os.MkdirAll(dir, 0755); err != nil && !os.IsExist(err) {
33+
return err
34+
}
35+
if err := config.WriteYamlConfig(dir); err != nil {
36+
return err
37+
}
38+
39+
pupsArgs := "--skip-tags=precompile,migrate,db"
40+
builder := docker.DockerBuilder{
41+
Config: config,
42+
Ctx: ctx,
43+
Stdin: strings.NewReader(config.Dockerfile(pupsArgs, r.BakeEnv)),
44+
Dir: dir,
45+
ImageTag: r.Tag,
46+
}
47+
if err := builder.Run(); err != nil {
48+
return err
49+
}
50+
cleaner := CleanCmd{Config: r.Config}
51+
cleaner.Run(cli)
52+
53+
return nil
54+
}
55+
56+
type CleanCmd struct {
57+
Config string `arg:"" name:"config" help:"config to clean" predictor:"config"`
58+
}
59+
60+
func (r *CleanCmd) Run(cli *Cli) error {
61+
dir := cli.BuildDir + "/" + r.Config
62+
os.Remove(dir + "/config.yaml")
63+
if err := os.Remove(dir); err != nil {
64+
return err
65+
}
66+
return nil
67+
}

launcher_go/v2/cli_build_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
7+
"bytes"
8+
"context"
9+
ddocker "github.com/discourse/discourse_docker/launcher_go/v2"
10+
. "github.com/discourse/discourse_docker/launcher_go/v2/test_utils"
11+
"github.com/discourse/discourse_docker/launcher_go/v2/utils"
12+
"io"
13+
"os"
14+
"os/exec"
15+
"strings"
16+
)
17+
18+
var _ = Describe("Build", func() {
19+
var testDir string
20+
var out *bytes.Buffer
21+
var cli *ddocker.Cli
22+
var ctx context.Context
23+
24+
BeforeEach(func() {
25+
utils.DockerPath = "docker"
26+
out = &bytes.Buffer{}
27+
utils.Out = out
28+
testDir, _ = os.MkdirTemp("", "ddocker-test")
29+
30+
ctx = context.Background()
31+
32+
cli = &ddocker.Cli{
33+
ConfDir: "./test/containers",
34+
TemplatesDir: "./test",
35+
BuildDir: testDir,
36+
}
37+
utils.CmdRunner = CreateNewFakeCmdRunner()
38+
})
39+
AfterEach(func() {
40+
os.RemoveAll(testDir)
41+
})
42+
43+
Context("When running build commands", func() {
44+
var checkBuildCmd = func(cmd exec.Cmd) {
45+
Expect(cmd.String()).To(ContainSubstring("docker build"))
46+
Expect(cmd.String()).To(ContainSubstring("--build-arg DISCOURSE_DEVELOPER_EMAILS"))
47+
Expect(cmd.Dir).To(Equal(testDir + "/test"))
48+
49+
//db password is ignored
50+
Expect(cmd.Env).ToNot(ContainElement("DISCOURSE_DB_PASSWORD=SOME_SECRET"))
51+
Expect(cmd.Env).ToNot(ContainElement("DISCOURSEDB_SOCKET="))
52+
buf := new(strings.Builder)
53+
io.Copy(buf, cmd.Stdin)
54+
// docker build's stdin is a dockerfile
55+
Expect(buf.String()).To(ContainSubstring("COPY config.yaml /temp-config.yaml"))
56+
Expect(buf.String()).To(ContainSubstring("--skip-tags=precompile,migrate,db"))
57+
Expect(buf.String()).ToNot(ContainSubstring("SKIP_EMBER_CLI_COMPILE=1"))
58+
}
59+
60+
It("Should run docker build with correct arguments", func() {
61+
runner := ddocker.DockerBuildCmd{Config: "test"}
62+
runner.Run(cli, &ctx)
63+
Expect(len(RanCmds)).To(Equal(1))
64+
checkBuildCmd(RanCmds[0])
65+
})
66+
})
67+
})

0 commit comments

Comments
 (0)