Skip to content

Commit 9e25686

Browse files
FEATURE: add an option for custom namespaces (#862)
Add an option to select a targeted namespace, env var DISCOURSE_NAMESPACE Configure: Add `source-tag` to select which tag to configure from. Rename old `tag` option to `target-tag` to differentiate from `source-tag` option Migrate: Add `tag` to select which tag to migrate with
1 parent 3186478 commit 9e25686

File tree

6 files changed

+95
-22
lines changed

6 files changed

+95
-22
lines changed

launcher_go/v2/cli_build.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ func (r *DockerBuildCmd) Run(cli *Cli, ctx *context.Context) error {
4040
return err
4141
}
4242

43+
namespace := cli.Namespace
44+
if namespace == "" {
45+
namespace = utils.DefaultNamespace
46+
}
4347
pupsArgs := "--skip-tags=precompile,migrate,db"
4448
builder := docker.DockerBuilder{
45-
Config: config,
46-
Ctx: ctx,
47-
Stdin: strings.NewReader(config.Dockerfile(pupsArgs, r.BakeEnv)),
48-
Dir: dir,
49-
ImageTag: r.Tag,
49+
Config: config,
50+
Ctx: ctx,
51+
Stdin: strings.NewReader(config.Dockerfile(pupsArgs, r.BakeEnv)),
52+
Dir: dir,
53+
Namespace: namespace,
54+
ImageTag: r.Tag,
5055
}
5156
if err := builder.Run(); err != nil {
5257
return err
@@ -58,8 +63,9 @@ func (r *DockerBuildCmd) Run(cli *Cli, ctx *context.Context) error {
5863
}
5964

6065
type DockerConfigureCmd struct {
61-
Tag string `default:"latest" help:"Resulting image tag."`
62-
Config string `arg:"" name:"config" help:"config" predictor:"config"`
66+
SourceTag string `help:"Source image tag to build from."`
67+
TargetTag string `help:"Target image tag to save as."`
68+
Config string `arg:"" name:"config" help:"config" predictor:"config"`
6369
}
6470

6571
func (r *DockerConfigureCmd) Run(cli *Cli, ctx *context.Context) error {
@@ -78,11 +84,24 @@ func (r *DockerConfigureCmd) Run(cli *Cli, ctx *context.Context) error {
7884
}
7985

8086
containerId := "discourse-build-" + uuidString
87+
namespace := cli.Namespace
88+
if namespace == "" {
89+
namespace = utils.DefaultNamespace
90+
}
91+
sourceTag := ""
92+
if len(r.SourceTag) > 0 {
93+
sourceTag = ":" + r.SourceTag
94+
}
95+
targetTag := ""
96+
if len(r.TargetTag) > 0 {
97+
targetTag = ":" + r.TargetTag
98+
}
8199

82100
pups := docker.DockerPupsRunner{
83101
Config: config,
84102
PupsArgs: "--tags=db,precompile",
85-
SavedImageName: utils.BaseImageName + r.Config + ":" + r.Tag,
103+
FromImageName: namespace + "/" + r.Config + sourceTag,
104+
SavedImageName: namespace + "/" + r.Config + targetTag,
86105
ExtraEnv: []string{"SKIP_EMBER_CLI_COMPILE=1"},
87106
Ctx: ctx,
88107
ContainerId: containerId,
@@ -93,6 +112,7 @@ func (r *DockerConfigureCmd) Run(cli *Cli, ctx *context.Context) error {
93112

94113
type DockerMigrateCmd struct {
95114
Config string `arg:"" name:"config" help:"config" predictor:"config"`
115+
Tag string `default:"latest" help:"Image tag to migrate."`
96116
SkipPostDeploymentMigrations bool `env:"SKIP_POST_DEPLOYMENT_MIGRATIONS" help:"Skip post-deployment migrations. Runs safe migrations only. Defers breaking-change migrations. Make sure you run post-deployment migrations after a full deploy is complete if you use this option."`
97117
}
98118

@@ -106,12 +126,22 @@ func (r *DockerMigrateCmd) Run(cli *Cli, ctx *context.Context) error {
106126
if r.SkipPostDeploymentMigrations {
107127
env = append(env, "SKIP_POST_DEPLOYMENT_MIGRATIONS=1")
108128
}
129+
130+
namespace := cli.Namespace
131+
if namespace == "" {
132+
namespace = utils.DefaultNamespace
133+
}
134+
tag := ""
135+
if len(r.Tag) > 0 {
136+
tag = ":" + r.Tag
137+
}
109138
pups := docker.DockerPupsRunner{
110-
Config: config,
111-
PupsArgs: "--tags=db,migrate",
112-
ExtraEnv: env,
113-
Ctx: ctx,
114-
ContainerId: containerId,
139+
Config: config,
140+
PupsArgs: "--tags=db,migrate",
141+
FromImageName: namespace + "/" + r.Config + tag,
142+
ExtraEnv: env,
143+
Ctx: ctx,
144+
ContainerId: containerId,
115145
}
116146
return pups.Run()
117147
}

launcher_go/v2/cli_build_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,45 @@ var _ = Describe("Build", func() {
173173
checkMigrateCmd(RanCmds[0])
174174
})
175175

176+
Context("With a custom namespace", func() {
177+
BeforeEach(func() {
178+
cli.Namespace = "testnamespace"
179+
})
180+
181+
It("Should run docker build with correct namespace and custom flags", func() {
182+
runner := ddocker.DockerBuildCmd{Config: "test", Tag: "testtag"}
183+
runner.Run(cli, &ctx)
184+
Expect(len(RanCmds)).To(Equal(1))
185+
checkBuildCmd(RanCmds[0])
186+
Expect(RanCmds[0].String()).To(ContainSubstring("testnamespace/test:testtag"))
187+
})
188+
189+
It("Should run docker configure with correct namespace and tags", func() {
190+
runner := ddocker.DockerConfigureCmd{Config: "test", SourceTag: "build", TargetTag: "configure"}
191+
runner.Run(cli, &ctx)
192+
Expect(len(RanCmds)).To(Equal(3))
193+
194+
Expect(RanCmds[0].String()).To(MatchRegexp(
195+
"--name discourse-build-test " +
196+
"testnamespace/test:build /bin/bash -c /usr/local/bin/pups --stdin --tags=db,precompile",
197+
))
198+
Expect(RanCmds[1].String()).To(MatchRegexp(
199+
"docker commit " +
200+
`--change LABEL org\.opencontainers\.image\.created="[\d\-T:Z]+" ` +
201+
`--change CMD \["/sbin/boot"\] ` +
202+
"discourse-build-test testnamespace/test:configure",
203+
))
204+
checkConfigureClean(RanCmds[2])
205+
})
206+
207+
It("Should run docker migrate with correct namespace", func() {
208+
runner := ddocker.DockerMigrateCmd{Config: "test"}
209+
runner.Run(cli, &ctx)
210+
Expect(len(RanCmds)).To(Equal(1))
211+
Expect(RanCmds[0].String()).To(ContainSubstring("testnamespace/test "))
212+
})
213+
})
214+
176215
It("Should allow skip post deployment migrations", func() {
177216
runner := ddocker.DockerMigrateCmd{Config: "test", SkipPostDeploymentMigrations: true}
178217
runner.Run(cli, &ctx)

launcher_go/v2/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const defaultBootCommand = "/sbin/boot"
18-
const defaultBaseImage = "discourse/base:2.0.20231121-0024"
18+
const defaultBaseImage = "discourse/base:2.0.20240825-0027"
1919

2020
type Config struct {
2121
Name string `yaml:-`
@@ -217,7 +217,7 @@ func (config *Config) RunImage() string {
217217
if len(config.Run_Image) > 0 {
218218
return config.Run_Image
219219
}
220-
return utils.BaseImageName + config.Name
220+
return "local_discourse/" + config.Name
221221
}
222222

223223
func (config *Config) DockerHostname(defaultHostname string) string {

launcher_go/v2/docker/commands.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import (
1919
)
2020

2121
type DockerBuilder struct {
22-
Config *config.Config
23-
Ctx *context.Context
24-
Stdin io.Reader
25-
Dir string
26-
ImageTag string
22+
Config *config.Config
23+
Ctx *context.Context
24+
Stdin io.Reader
25+
Dir string
26+
Namespace string
27+
ImageTag string
2728
}
2829

2930
func (r *DockerBuilder) Run() error {
@@ -46,7 +47,7 @@ func (r *DockerBuilder) Run() error {
4647
cmd.Args = append(cmd.Args, "--pull")
4748
cmd.Args = append(cmd.Args, "--force-rm")
4849
cmd.Args = append(cmd.Args, "-t")
49-
cmd.Args = append(cmd.Args, utils.BaseImageName+r.Config.Name+":"+r.ImageTag)
50+
cmd.Args = append(cmd.Args, r.Namespace+"/"+r.Config.Name+":"+r.ImageTag)
5051
cmd.Args = append(cmd.Args, "--shm-size=512m")
5152
cmd.Args = append(cmd.Args, "-f")
5253
cmd.Args = append(cmd.Args, "-")
@@ -219,6 +220,7 @@ func (r *DockerRunner) Run() error {
219220
type DockerPupsRunner struct {
220221
Config *config.Config
221222
PupsArgs string
223+
FromImageName string
222224
SavedImageName string
223225
ExtraEnv []string
224226
Ctx *context.Context
@@ -254,6 +256,7 @@ func (r *DockerPupsRunner) Run() error {
254256
Ctx: r.Ctx,
255257
ExtraEnv: r.ExtraEnv,
256258
Rm: rm,
259+
CustomImage: r.FromImageName,
257260
ContainerId: r.ContainerId,
258261
Cmd: commands,
259262
Stdin: strings.NewReader(r.Config.Yaml()),

launcher_go/v2/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Cli struct {
1818
ConfDir string `default:"./containers" hidden:"" help:"Discourse pups config directory." predictor:"dir"`
1919
TemplatesDir string `default:"." hidden:"" help:"Home project directory containing a templates/ directory which in turn contains pups yaml templates." predictor:"dir"`
2020
BuildDir string `default:"./tmp" hidden:"" help:"Temporary build folder for building images." predictor:"dir"`
21+
Namespace string `default:"local_discourse" env:"DISCOURSE_NAMESPACE" help:"image namespace."`
2122
BuildCmd DockerBuildCmd `cmd:"" name:"build" help:"Build a base image. This command does not need a running database. Saves resulting container."`
2223
ConfigureCmd DockerConfigureCmd `cmd:"" name:"configure" help:"Configure and save an image with all dependencies and environment baked in. Updates themes and precompiles all assets. Saves resulting container."`
2324
MigrateCmd DockerMigrateCmd `cmd:"" name:"migrate" help:"Run migration tasks for a site. Running container is temporary and is not saved."`

launcher_go/v2/utils/consts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
const Version = "v2.0.0"
1111

12-
const BaseImageName = "local_discourse/"
12+
const DefaultNamespace = "local_discourse"
1313

1414
// Known secrets, or otherwise not public info from config so we can build public images
1515
var KnownSecrets = []string{

0 commit comments

Comments
 (0)