From e2a299f4bd707ab4601d68ba046e100e4e2b0fd1 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Thu, 14 Jul 2022 17:35:49 -0400 Subject: [PATCH 1/2] feat: add `coder server postgres-builtin-serve` to run the built-in DB In cases where `coder server` fails to start, you still need to be able to access your database! --- cli/server.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/cli/server.go b/cli/server.go index 58df27a4d528d..a6525f82e2b70 100644 --- a/cli/server.go +++ b/cli/server.go @@ -22,10 +22,6 @@ import ( "strings" "time" - "github.com/coder/coder/buildinfo" - "github.com/coder/coder/cryptorand" - "github.com/coder/coder/provisioner/echo" - "github.com/coreos/go-systemd/daemon" embeddedpostgres "github.com/fergusstrange/embedded-postgres" "github.com/google/go-github/v43/github" @@ -45,6 +41,7 @@ import ( "cdr.dev/slog" "cdr.dev/slog/sloggers/sloghuman" + "github.com/coder/coder/buildinfo" "github.com/coder/coder/cli/cliflag" "github.com/coder/coder/cli/cliui" "github.com/coder/coder/cli/config" @@ -58,6 +55,8 @@ import ( "github.com/coder/coder/coderd/tracing" "github.com/coder/coder/coderd/turnconn" "github.com/coder/coder/codersdk" + "github.com/coder/coder/cryptorand" + "github.com/coder/coder/provisioner/echo" "github.com/coder/coder/provisioner/terraform" "github.com/coder/coder/provisionerd" "github.com/coder/coder/provisionersdk" @@ -512,6 +511,29 @@ func server() *cobra.Command { }, }) + root.AddCommand(&cobra.Command{ + Use: "postgres-builtin-serve", + Short: "Run the built-in PostgreSQL deployment.", + RunE: func(cmd *cobra.Command, args []string) error { + cfg := createConfig(cmd) + logger := slog.Make(sloghuman.Sink(os.Stderr)) + if verbose { + logger = logger.Leveled(slog.LevelDebug) + } + + url, closePg, err := startBuiltinPostgres(cmd.Context(), cfg, logger) + if err != nil { + return err + } + defer func() { _ = closePg() }() + + cmd.Println(cliui.Styles.Code.Render("psql \"" + url + "\"")) + + <-cmd.Context().Done() + return nil + }, + }) + cliflag.DurationVarP(root.Flags(), &autobuildPollInterval, "autobuild-poll-interval", "", "CODER_AUTOBUILD_POLL_INTERVAL", time.Minute, "Specifies the interval at which to poll for and execute automated workspace build operations.") cliflag.StringVarP(root.Flags(), &accessURL, "access-url", "", "CODER_ACCESS_URL", "", "Specifies the external URL to access Coder.") cliflag.StringVarP(root.Flags(), &address, "address", "a", "CODER_ADDRESS", "127.0.0.1:3000", "The address to serve the API and dashboard.") From b40428b06e1af13f2ccd8cb6a723d4b94a2b667e Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Thu, 14 Jul 2022 17:40:57 -0400 Subject: [PATCH 2/2] correctly shutdown postgres --- cli/server.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/server.go b/cli/server.go index a6525f82e2b70..8978d7b80a0a3 100644 --- a/cli/server.go +++ b/cli/server.go @@ -529,7 +529,11 @@ func server() *cobra.Command { cmd.Println(cliui.Styles.Code.Render("psql \"" + url + "\"")) - <-cmd.Context().Done() + stopChan := make(chan os.Signal, 1) + defer signal.Stop(stopChan) + signal.Notify(stopChan, os.Interrupt) + + <-stopChan return nil }, })