Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9bc7b28

Browse files
committed
feat: support postgresql connection via individual flags
1 parent 886dcbe commit 9bc7b28

File tree

9 files changed

+210
-10
lines changed

9 files changed

+210
-10
lines changed

cli/server.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,22 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
395395

396396
config := r.createConfig()
397397

398+
if vals.PostgresURL == "" && vals.PostgresHost != "" && vals.PostgresUsername != "" && vals.PostgresPassword != "" && vals.PostgresDatabase != "" {
399+
pgURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?%s",
400+
vals.PostgresUsername.String(),
401+
vals.PostgresPassword.String(),
402+
vals.PostgresHost.String(),
403+
vals.PostgresPort.String(),
404+
vals.PostgresDatabase.String(),
405+
vals.PostgresOptions.String(),
406+
)
407+
err = vals.PostgresURL.Set(pgURL)
408+
if err != nil {
409+
return err
410+
}
411+
cliui.Infof(inv.Stdout, "Created PostgreSQL URL from provided flags")
412+
}
413+
398414
builtinPostgres := false
399415
// Only use built-in if PostgreSQL URL isn't specified!
400416
if !vals.InMemoryDatabase && vals.PostgresURL == "" {

cli/server_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,31 @@ func TestReadGitAuthProvidersFromEnv(t *testing.T) {
154154
func TestServer(t *testing.T) {
155155
t.Parallel()
156156

157+
t.Run("PostgresURLFromFlags", func(t *testing.T) {
158+
t.Parallel()
159+
160+
inv, cfg := clitest.New(t,
161+
"server",
162+
"--http-address", ":0",
163+
"--access-url", "https://foobarbaz.mydomain",
164+
"--cache-dir", t.TempDir(),
165+
"--postgres-host", "localhost",
166+
"--postgres-port", "5432",
167+
"--postgres-username", "coder",
168+
"--postgres-password", "password",
169+
"--postgres-database", "coder",
170+
"--postgres-options", "sslmode=disable",
171+
)
172+
173+
pty := ptytest.New(t).Attach(inv)
174+
175+
clitest.Start(t, inv)
176+
177+
// Just wait for startup
178+
_ = waitAccessURL(t, cfg)
179+
180+
pty.ExpectMatch("Created PostgreSQL URL from provided flags")
181+
})
157182
t.Run("BuiltinPostgres", func(t *testing.T) {
158183
t.Parallel()
159184
if testing.Short() {

cli/testdata/coder_server_--help.golden

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,29 @@ OPTIONS:
5454
Type of auth to use when connecting to postgres.
5555

5656
--postgres-url string, $CODER_PG_CONNECTION_URL
57-
URL of a PostgreSQL database. If empty, PostgreSQL binaries will be
58-
downloaded from Maven (https://repo1.maven.org/maven2) and store all
59-
data in the config root. Access the built-in database with "coder
60-
server postgres-builtin-url".
57+
URL of a PostgreSQL database. If empty and credentials are not
58+
provided via other flags, PostgreSQL binaries will be downloaded from
59+
Maven (https://repo1.maven.org/maven2) and store all data in the
60+
config root. Access the built-in database with "coder server
61+
postgres-builtin-url".
62+
63+
--postgres-database string, $CODER_PG_DATABASE
64+
PostgreSQL database name.
65+
66+
--postgres-host string, $CODER_PG_HOST
67+
PostgreSQL server hostname.
68+
69+
--postgres-options string, $CODER_PG_OPTIONS
70+
PostgreSQL connection options (e.g. 'sslmode=require').
71+
72+
--postgres-password string, $CODER_PG_PASSWORD
73+
PostgreSQL password.
74+
75+
--postgres-port string, $CODER_PG_PORT (default: 5432)
76+
PostgreSQL server port.
77+
78+
--postgres-username string, $CODER_PG_USERNAME
79+
PostgreSQL username.
6180

6281
--ssh-keygen-algorithm string, $CODER_SSH_KEYGEN_ALGORITHM (default: ed25519)
6382
The algorithm to use for generating ssh keys. Accepted values are

coderd/deployment_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestDeploymentValues(t *testing.T) {
2525
cfg.OIDC.AuthURLParams.Set(`{"foo":"bar"}`)
2626
cfg.OIDC.EmailField.Set("some_random_field_you_never_expected")
2727
cfg.PostgresURL.Set(hi)
28+
cfg.PostgresPassword.Set(hi)
2829
cfg.SCIMAPIKey.Set(hi)
2930
cfg.ExternalTokenEncryptionKeys.Set("the_random_key_we_never_expected,an_other_key_we_never_unexpected")
3031
cfg.Provisioner.DaemonPSK = "provisionersftw"
@@ -45,6 +46,7 @@ func TestDeploymentValues(t *testing.T) {
4546
require.Empty(t, scrubbed.Values.OAuth2.Github.ClientSecret.Value())
4647
require.Empty(t, scrubbed.Values.OIDC.ClientSecret.Value())
4748
require.Empty(t, scrubbed.Values.PostgresURL.Value())
49+
require.Empty(t, scrubbed.Values.PostgresPassword.Value())
4850
require.Empty(t, scrubbed.Values.SCIMAPIKey.Value())
4951
require.Empty(t, scrubbed.Values.ExternalTokenEncryptionKeys.Value())
5052
require.Empty(t, scrubbed.Values.Provisioner.DaemonPSK.Value())

codersdk/deployment.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ type DeploymentValues struct {
352352
InMemoryDatabase serpent.Bool `json:"in_memory_database,omitempty" typescript:",notnull"`
353353
PostgresURL serpent.String `json:"pg_connection_url,omitempty" typescript:",notnull"`
354354
PostgresAuth string `json:"pg_auth,omitempty" typescript:",notnull"`
355+
PostgresHost serpent.String `json:"pg_host,omitempty" typescript:",notnull"`
356+
PostgresPort serpent.String `json:"pg_port,omitempty" typescript:",notnull"`
357+
PostgresUsername serpent.String `json:"pg_username,omitempty" typescript:",notnull"`
358+
PostgresPassword serpent.String `json:"pg_password,omitempty" typescript:",notnull"`
359+
PostgresDatabase serpent.String `json:"pg_database,omitempty" typescript:",notnull"`
360+
PostgresOptions serpent.String `json:"pg_options,omitempty" typescript:",notnull"`
355361
OAuth2 OAuth2Config `json:"oauth2,omitempty" typescript:",notnull"`
356362
OIDC OIDCConfig `json:"oidc,omitempty" typescript:",notnull"`
357363
Telemetry TelemetryConfig `json:"telemetry,omitempty" typescript:",notnull"`
@@ -2251,7 +2257,7 @@ when required by your organization's security policy.`,
22512257
},
22522258
{
22532259
Name: "Postgres Connection URL",
2254-
Description: "URL of a PostgreSQL database. If empty, PostgreSQL binaries will be downloaded from Maven (https://repo1.maven.org/maven2) and store all data in the config root. Access the built-in database with \"coder server postgres-builtin-url\".",
2260+
Description: "URL of a PostgreSQL database. If empty and credentials are not provided via other flags, PostgreSQL binaries will be downloaded from Maven (https://repo1.maven.org/maven2) and store all data in the config root. Access the built-in database with \"coder server postgres-builtin-url\".",
22552261
Flag: "postgres-url",
22562262
Env: "CODER_PG_CONNECTION_URL",
22572263
Annotations: serpent.Annotations{}.Mark(annotationSecretKey, "true"),
@@ -2266,6 +2272,55 @@ when required by your organization's security policy.`,
22662272
Value: serpent.EnumOf(&c.PostgresAuth, PostgresAuthDrivers...),
22672273
YAML: "pgAuth",
22682274
},
2275+
{
2276+
Name: "Postgres Host",
2277+
Description: "PostgreSQL server hostname.",
2278+
Flag: "postgres-host",
2279+
Env: "CODER_PG_HOST",
2280+
Value: &c.PostgresHost,
2281+
YAML: "pgHost",
2282+
},
2283+
{
2284+
Name: "Postgres Port",
2285+
Description: "PostgreSQL server port.",
2286+
Flag: "postgres-port",
2287+
Env: "CODER_PG_PORT",
2288+
Default: "5432",
2289+
Value: &c.PostgresPort,
2290+
YAML: "pgPort",
2291+
},
2292+
{
2293+
Name: "Postgres Username",
2294+
Description: "PostgreSQL username.",
2295+
Flag: "postgres-username",
2296+
Env: "CODER_PG_USERNAME",
2297+
Value: &c.PostgresUsername,
2298+
YAML: "pgUsername",
2299+
},
2300+
{
2301+
Name: "Postgres Password",
2302+
Description: "PostgreSQL password.",
2303+
Flag: "postgres-password",
2304+
Env: "CODER_PG_PASSWORD",
2305+
Annotations: serpent.Annotations{}.Mark(annotationSecretKey, "true"),
2306+
Value: &c.PostgresPassword,
2307+
},
2308+
{
2309+
Name: "Postgres Database",
2310+
Description: "PostgreSQL database name.",
2311+
Flag: "postgres-database",
2312+
Env: "CODER_PG_DATABASE",
2313+
Value: &c.PostgresDatabase,
2314+
YAML: "pgDatabase",
2315+
},
2316+
{
2317+
Name: "Postgres Options",
2318+
Description: "PostgreSQL connection options (e.g. 'sslmode=require').",
2319+
Flag: "postgres-options",
2320+
Env: "CODER_PG_OPTIONS",
2321+
Value: &c.PostgresOptions,
2322+
YAML: "pgOptions",
2323+
},
22692324
{
22702325
Name: "Secure Auth Cookie",
22712326
Description: "Controls if the 'Secure' property is set on browser session cookies.",

codersdk/deployment_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func TestDeploymentValues_HighlyConfigurable(t *testing.T) {
6363
"Postgres Connection URL": {
6464
yaml: true,
6565
},
66+
"Postgres Password": {
67+
yaml: true,
68+
},
6669
"SCIM API Key": {
6770
yaml: true,
6871
},

docs/reference/cli/server.md

Lines changed: 56 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/cli/testdata/coder_server_--help.golden

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,29 @@ OPTIONS:
5555
Type of auth to use when connecting to postgres.
5656

5757
--postgres-url string, $CODER_PG_CONNECTION_URL
58-
URL of a PostgreSQL database. If empty, PostgreSQL binaries will be
59-
downloaded from Maven (https://repo1.maven.org/maven2) and store all
60-
data in the config root. Access the built-in database with "coder
61-
server postgres-builtin-url".
58+
URL of a PostgreSQL database. If empty and credentials are not
59+
provided via other flags, PostgreSQL binaries will be downloaded from
60+
Maven (https://repo1.maven.org/maven2) and store all data in the
61+
config root. Access the built-in database with "coder server
62+
postgres-builtin-url".
63+
64+
--postgres-database string, $CODER_PG_DATABASE
65+
PostgreSQL database name.
66+
67+
--postgres-host string, $CODER_PG_HOST
68+
PostgreSQL server hostname.
69+
70+
--postgres-options string, $CODER_PG_OPTIONS
71+
PostgreSQL connection options (e.g. 'sslmode=require').
72+
73+
--postgres-password string, $CODER_PG_PASSWORD
74+
PostgreSQL password.
75+
76+
--postgres-port string, $CODER_PG_PORT (default: 5432)
77+
PostgreSQL server port.
78+
79+
--postgres-username string, $CODER_PG_USERNAME
80+
PostgreSQL username.
6281

6382
--ssh-keygen-algorithm string, $CODER_SSH_KEYGEN_ALGORITHM (default: ed25519)
6483
The algorithm to use for generating ssh keys. Accepted values are

site/src/api/typesGenerated.ts

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)