From 01c1575c8f40741bc31bb25a0913be58fadbd0cc Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Mon, 15 Jul 2024 15:45:32 -0500 Subject: [PATCH 01/16] Stub out commands for config management. --- cmd/flexctl/config.go | 45 +++++++++++++++++++++++++++++++++++++++++++ cmd/flexctl/main.go | 2 ++ 2 files changed, 47 insertions(+) create mode 100644 cmd/flexctl/config.go diff --git a/cmd/flexctl/config.go b/cmd/flexctl/config.go new file mode 100644 index 0000000..1df65d4 --- /dev/null +++ b/cmd/flexctl/config.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func newConfigCmd() *cobra.Command { + var configCmd = &cobra.Command{ + Use: "config", + Short: "Manage Postgres configuration", + } + + configCmd.AddCommand(newConfigShowCmd()) + configCmd.AddCommand(newConfigUpdateCmd()) + + return configCmd +} + +func newConfigShowCmd() *cobra.Command { + var configShowCmd = &cobra.Command{ + Use: "show", + Short: "Show current configuration", + Run: func(cmd *cobra.Command, args []string) { + // Add your logic here for showing the configuration + fmt.Println("Showing configuration...") + }, + } + + return configShowCmd +} + +func newConfigUpdateCmd() *cobra.Command { + var configUpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update configuration", + Run: func(cmd *cobra.Command, args []string) { + // Add your logic here for updating the configuration + fmt.Println("Updating configuration...") + }, + } + + return configUpdateCmd +} diff --git a/cmd/flexctl/main.go b/cmd/flexctl/main.go index 98405f6..73c4a13 100644 --- a/cmd/flexctl/main.go +++ b/cmd/flexctl/main.go @@ -22,6 +22,8 @@ func main() { backupCmd.AddCommand(backupShowCmd) backupCmd.AddCommand(backupCreateCmd) + rootCmd.AddCommand(newConfigCmd()) + if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) From 09341481adfde64a4fa8aa28694a9cd2148739ae Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 16 Jul 2024 09:51:57 -0500 Subject: [PATCH 02/16] Get `flexctl backup config show` working. --- cmd/flexctl/backups.go | 60 ++++++++++++++++++++++++ cmd/flexctl/config.go | 45 ------------------ cmd/flexctl/main.go | 3 +- internal/flypg/barman_config.go | 8 ++-- internal/supervisor/ensure_kill_linux.go | 1 + 5 files changed, 66 insertions(+), 51 deletions(-) delete mode 100644 cmd/flexctl/config.go diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index c68107c..c9ec972 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -2,7 +2,9 @@ package main import ( "context" + "encoding/json" "fmt" + "net/http" "os" "time" @@ -224,3 +226,61 @@ func listBackups(cmd *cobra.Command) error { func backupsEnabled() bool { return os.Getenv("S3_ARCHIVE_CONFIG") != "" } + +func newBackupConfigCmd() *cobra.Command { + var configCmd = &cobra.Command{ + Use: "config", + Short: "Manage backup configuration", + } + + configCmd.AddCommand(newConfigShowCmd()) + configCmd.AddCommand(newConfigUpdateCmd()) + + return configCmd +} + +type configShowResult struct { + Result flypg.BarmanSettings `json:"result"` +} + +func newConfigShowCmd() *cobra.Command { + var configShowCmd = &cobra.Command{ + Use: "show", + Short: "Show current configuration", + RunE: func(cmd *cobra.Command, args []string) error { + resp, err := http.Get("http://localhost:5500/commands/admin/settings/view/barman") + if err != nil { + return err + } + defer resp.Body.Close() + + var rv configShowResult + err = json.NewDecoder(resp.Body).Decode(&rv) + if err != nil { + return err + } + + fmt.Printf(" ArchiveTimeout = %s\n", rv.Result.ArchiveTimeout) + fmt.Printf(" RecoveryWindow = %s\n", rv.Result.RecoveryWindow) + fmt.Printf(" FullBackupFrequency = %s\n", rv.Result.FullBackupFrequency) + fmt.Printf(" MinimumRedundancy = %s\n", rv.Result.MinimumRedundancy) + + return nil + }, + } + + return configShowCmd +} + +func newConfigUpdateCmd() *cobra.Command { + var configUpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update configuration", + Run: func(cmd *cobra.Command, args []string) { + // Add your logic here for updating the configuration + fmt.Println("Updating configuration...") + }, + } + + return configUpdateCmd +} diff --git a/cmd/flexctl/config.go b/cmd/flexctl/config.go deleted file mode 100644 index 1df65d4..0000000 --- a/cmd/flexctl/config.go +++ /dev/null @@ -1,45 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -func newConfigCmd() *cobra.Command { - var configCmd = &cobra.Command{ - Use: "config", - Short: "Manage Postgres configuration", - } - - configCmd.AddCommand(newConfigShowCmd()) - configCmd.AddCommand(newConfigUpdateCmd()) - - return configCmd -} - -func newConfigShowCmd() *cobra.Command { - var configShowCmd = &cobra.Command{ - Use: "show", - Short: "Show current configuration", - Run: func(cmd *cobra.Command, args []string) { - // Add your logic here for showing the configuration - fmt.Println("Showing configuration...") - }, - } - - return configShowCmd -} - -func newConfigUpdateCmd() *cobra.Command { - var configUpdateCmd = &cobra.Command{ - Use: "update", - Short: "Update configuration", - Run: func(cmd *cobra.Command, args []string) { - // Add your logic here for updating the configuration - fmt.Println("Updating configuration...") - }, - } - - return configUpdateCmd -} diff --git a/cmd/flexctl/main.go b/cmd/flexctl/main.go index 73c4a13..58f7db7 100644 --- a/cmd/flexctl/main.go +++ b/cmd/flexctl/main.go @@ -21,8 +21,7 @@ func main() { backupCmd.AddCommand(backupListCmd) backupCmd.AddCommand(backupShowCmd) backupCmd.AddCommand(backupCreateCmd) - - rootCmd.AddCommand(newConfigCmd()) + backupCmd.AddCommand(newBackupConfigCmd()) if err := rootCmd.Execute(); err != nil { fmt.Println(err) diff --git a/internal/flypg/barman_config.go b/internal/flypg/barman_config.go index 9b219e0..f23cde1 100644 --- a/internal/flypg/barman_config.go +++ b/internal/flypg/barman_config.go @@ -13,10 +13,10 @@ import ( ) type BarmanSettings struct { - ArchiveTimeout string - RecoveryWindow string - FullBackupFrequency string - MinimumRedundancy string + ArchiveTimeout string `json:"archive_timeout"` + RecoveryWindow string `json:"recovery_window"` + FullBackupFrequency string `json:"full_backup_frequency"` + MinimumRedundancy string `json:"minimum_redundancy"` } type BarmanConfig struct { diff --git a/internal/supervisor/ensure_kill_linux.go b/internal/supervisor/ensure_kill_linux.go index 0946c5d..df95c56 100644 --- a/internal/supervisor/ensure_kill_linux.go +++ b/internal/supervisor/ensure_kill_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package supervisor From 77ed090c6953b38f6b4cb6252287ea1137ca1fd3 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 16 Jul 2024 10:35:49 -0500 Subject: [PATCH 03/16] Get `flexctl backup config update` working. --- cmd/flexctl/backups.go | 74 +++++++++++++++++++++++++++++++-- internal/flypg/barman_config.go | 8 ++-- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index c9ec972..ba7669c 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "encoding/json" "fmt" @@ -272,15 +273,82 @@ func newConfigShowCmd() *cobra.Command { return configShowCmd } +type successfulUpdateResult struct { + Message string `json:"message,omitempty"` + RestartRequired bool `json:"restart_required"` +} + +type configUpdateResult struct { + Result successfulUpdateResult `json:"result,omitempty"` + Error string `json:"error,omitempty"` +} + func newConfigUpdateCmd() *cobra.Command { var configUpdateCmd = &cobra.Command{ Use: "update", Short: "Update configuration", - Run: func(cmd *cobra.Command, args []string) { - // Add your logic here for updating the configuration - fmt.Println("Updating configuration...") + RunE: func(cmd *cobra.Command, args []string) error { + update := flypg.BarmanSettings{ + ArchiveTimeout: cmd.Flag("archive-timeout").Value.String(), + RecoveryWindow: cmd.Flag("recovery-window").Value.String(), + FullBackupFrequency: cmd.Flag("full-backup-frequency").Value.String(), + MinimumRedundancy: cmd.Flag("minimum-redundancy").Value.String(), + } + + jsonBody, err := json.Marshal(update) + if err != nil { + return err + } + + resp, err := http.Post("http://localhost:5500/commands/admin/settings/update/barman", "application/json", bytes.NewBuffer(jsonBody)) + if err != nil { + return err + } + defer resp.Body.Close() + + var rv configUpdateResult + err = json.NewDecoder(resp.Body).Decode(&rv) + if err != nil { + return err + } + + if rv.Error != "" { + return fmt.Errorf("error updating configuration: %s", rv.Error) + } + + if rv.Result.Message != "" { + fmt.Println(rv.Result.Message) + } + + if rv.Result.RestartRequired { + fmt.Println("A restart is required for these changes to take effect.") + } + + return nil }, } + configUpdateCmd.Flags().StringP("archive-timeout", "", "", "Archive timeout") + configUpdateCmd.Flags().StringP("recovery-window", "", "", "Recovery window") + configUpdateCmd.Flags().StringP("full-backup-frequency", "", "", "Full backup frequency") + configUpdateCmd.Flags().StringP("minimum-redundancy", "", "", "Minimum redundancy") + + configUpdateCmd.PreRunE = func(cmd *cobra.Command, args []string) error { + requiredFlags := []string{"archive-timeout", "recovery-window", "full-backup-frequency", "minimum-redundancy"} + providedFlags := 0 + + for _, flag := range requiredFlags { + if cmd.Flag(flag).Changed { + providedFlags++ + } + } + + if providedFlags < 1 { + return fmt.Errorf("At least one flag must be specified.") + } + + return nil + } + return configUpdateCmd } diff --git a/internal/flypg/barman_config.go b/internal/flypg/barman_config.go index f23cde1..1bf230d 100644 --- a/internal/flypg/barman_config.go +++ b/internal/flypg/barman_config.go @@ -13,10 +13,10 @@ import ( ) type BarmanSettings struct { - ArchiveTimeout string `json:"archive_timeout"` - RecoveryWindow string `json:"recovery_window"` - FullBackupFrequency string `json:"full_backup_frequency"` - MinimumRedundancy string `json:"minimum_redundancy"` + ArchiveTimeout string `json:"archive_timeout,omitempty"` + RecoveryWindow string `json:"recovery_window,omitempty"` + FullBackupFrequency string `json:"full_backup_frequency,omitempty"` + MinimumRedundancy string `json:"minimum_redundancy,omitempty"` } type BarmanConfig struct { From 5cbf8dc0eb9821f9d0b1dab2d1153a03c5885cb8 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 16 Jul 2024 10:43:34 -0500 Subject: [PATCH 04/16] gofmt --- cmd/flexctl/backups.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index ba7669c..aac431f 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -319,7 +319,7 @@ func newConfigUpdateCmd() *cobra.Command { if rv.Result.Message != "" { fmt.Println(rv.Result.Message) } - + if rv.Result.RestartRequired { fmt.Println("A restart is required for these changes to take effect.") } From 3c8935415a581fc0188a550d4d5414b1faf410c4 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 16 Jul 2024 10:49:38 -0500 Subject: [PATCH 05/16] Appease our linter overlords. --- cmd/flexctl/backups.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index aac431f..06a64ff 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -344,7 +344,7 @@ func newConfigUpdateCmd() *cobra.Command { } if providedFlags < 1 { - return fmt.Errorf("At least one flag must be specified.") + return fmt.Errorf("at least one flag must be specified") } return nil From 9c2352993c18be185754db7b7db875906a3537b4 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 16 Jul 2024 10:54:25 -0500 Subject: [PATCH 06/16] Probably not necessary to close these as they're short-lived. --- cmd/flexctl/backups.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index 06a64ff..cfac979 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -253,7 +253,6 @@ func newConfigShowCmd() *cobra.Command { if err != nil { return err } - defer resp.Body.Close() var rv configShowResult err = json.NewDecoder(resp.Body).Decode(&rv) @@ -304,7 +303,6 @@ func newConfigUpdateCmd() *cobra.Command { if err != nil { return err } - defer resp.Body.Close() var rv configUpdateResult err = json.NewDecoder(resp.Body).Decode(&rv) From 74d382d54aa3ea5ff4bcf9e7fc2c5dc7038a53e0 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 16 Jul 2024 11:41:01 -0500 Subject: [PATCH 07/16] Retrieve flag values separately. --- cmd/flexctl/backups.go | 98 ++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 38 deletions(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index cfac979..f5ee72a 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -283,55 +283,77 @@ type configUpdateResult struct { } func newConfigUpdateCmd() *cobra.Command { - var configUpdateCmd = &cobra.Command{ + var cmd = &cobra.Command{ Use: "update", Short: "Update configuration", - RunE: func(cmd *cobra.Command, args []string) error { - update := flypg.BarmanSettings{ - ArchiveTimeout: cmd.Flag("archive-timeout").Value.String(), - RecoveryWindow: cmd.Flag("recovery-window").Value.String(), - FullBackupFrequency: cmd.Flag("full-backup-frequency").Value.String(), - MinimumRedundancy: cmd.Flag("minimum-redundancy").Value.String(), - } + } - jsonBody, err := json.Marshal(update) - if err != nil { - return err - } + cmd.RunE = func(cmd *cobra.Command, args []string) error { - resp, err := http.Post("http://localhost:5500/commands/admin/settings/update/barman", "application/json", bytes.NewBuffer(jsonBody)) - if err != nil { - return err - } + archiveTimeout, err := cmd.Flags().GetString("archive-timeout") + if err != nil { + return err + } - var rv configUpdateResult - err = json.NewDecoder(resp.Body).Decode(&rv) - if err != nil { - return err - } + recoveryWindow, err := cmd.Flags().GetString("recovery-window") + if err != nil { + return err + } - if rv.Error != "" { - return fmt.Errorf("error updating configuration: %s", rv.Error) - } + fullBackupFrequency, err := cmd.Flags().GetString("full-backup-frequency") + if err != nil { + return err + } - if rv.Result.Message != "" { - fmt.Println(rv.Result.Message) - } + minimumRedundancy, err := cmd.Flags().GetString("minimum-redundancy") + if err != nil { + return err + } - if rv.Result.RestartRequired { - fmt.Println("A restart is required for these changes to take effect.") - } + update := flypg.BarmanSettings{ + ArchiveTimeout: archiveTimeout, + RecoveryWindow: recoveryWindow, + FullBackupFrequency: fullBackupFrequency, + MinimumRedundancy: minimumRedundancy, + } - return nil - }, + jsonBody, err := json.Marshal(update) + if err != nil { + return err + } + + resp, err := http.Post("http://localhost:5500/commands/admin/settings/update/barman", "application/json", bytes.NewBuffer(jsonBody)) + if err != nil { + return err + } + + var rv configUpdateResult + err = json.NewDecoder(resp.Body).Decode(&rv) + if err != nil { + return err + } + + if rv.Error != "" { + return fmt.Errorf("error updating configuration: %s", rv.Error) + } + + if rv.Result.Message != "" { + fmt.Println(rv.Result.Message) + } + + if rv.Result.RestartRequired { + fmt.Println("A restart is required for these changes to take effect.") + } + + return nil } - configUpdateCmd.Flags().StringP("archive-timeout", "", "", "Archive timeout") - configUpdateCmd.Flags().StringP("recovery-window", "", "", "Recovery window") - configUpdateCmd.Flags().StringP("full-backup-frequency", "", "", "Full backup frequency") - configUpdateCmd.Flags().StringP("minimum-redundancy", "", "", "Minimum redundancy") + cmd.Flags().StringP("archive-timeout", "", "", "Archive timeout") + cmd.Flags().StringP("recovery-window", "", "", "Recovery window") + cmd.Flags().StringP("full-backup-frequency", "", "", "Full backup frequency") + cmd.Flags().StringP("minimum-redundancy", "", "", "Minimum redundancy") - configUpdateCmd.PreRunE = func(cmd *cobra.Command, args []string) error { + cmd.PreRunE = func(cmd *cobra.Command, args []string) error { requiredFlags := []string{"archive-timeout", "recovery-window", "full-backup-frequency", "minimum-redundancy"} providedFlags := 0 @@ -348,5 +370,5 @@ func newConfigUpdateCmd() *cobra.Command { return nil } - return configUpdateCmd + return cmd } From 1bf8a12719814463c0865503da56ace8a312d8d5 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 16 Jul 2024 13:54:14 -0500 Subject: [PATCH 08/16] Style fixes. --- cmd/flexctl/backups.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index f5ee72a..6214187 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -255,8 +255,7 @@ func newConfigShowCmd() *cobra.Command { } var rv configShowResult - err = json.NewDecoder(resp.Body).Decode(&rv) - if err != nil { + if err := json.NewDecoder(resp.Body).Decode(&rv); err != nil { return err } @@ -328,8 +327,7 @@ func newConfigUpdateCmd() *cobra.Command { } var rv configUpdateResult - err = json.NewDecoder(resp.Body).Decode(&rv) - if err != nil { + if err := json.NewDecoder(resp.Body).Decode(&rv); err != nil { return err } From cfb4655c8f55d24dc2106e508d41efe443994dcd Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 17 Jul 2024 09:49:58 -0500 Subject: [PATCH 09/16] Construct URL from app environment. --- cmd/flexctl/backups.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index 6214187..c059f45 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -244,12 +244,27 @@ type configShowResult struct { Result flypg.BarmanSettings `json:"result"` } +func getApiUrl() (string, error) { + hostname := os.Getenv("FLY_APP_NAME") + if hostname == "" { + return "", fmt.Errorf("FLY_APP_NAME is not set") + } + url := fmt.Sprintf("http://%s.internal:5500", hostname) + return url, nil +} + func newConfigShowCmd() *cobra.Command { var configShowCmd = &cobra.Command{ Use: "show", Short: "Show current configuration", RunE: func(cmd *cobra.Command, args []string) error { - resp, err := http.Get("http://localhost:5500/commands/admin/settings/view/barman") + url, err := getApiUrl() + if err != nil { + return err + } + + url = fmt.Sprintf("%s/commands/admin/settings/view/barman", url) + resp, err := http.Get(url) if err != nil { return err } @@ -321,7 +336,13 @@ func newConfigUpdateCmd() *cobra.Command { return err } - resp, err := http.Post("http://localhost:5500/commands/admin/settings/update/barman", "application/json", bytes.NewBuffer(jsonBody)) + url, err := getApiUrl() + if err != nil { + return err + } + + url = fmt.Sprintf("%s/commands/admin/settings/update/barman", url) + resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonBody)) if err != nil { return err } From c40d8e8d3f59222bd14b44aa39da9f3985e53d5b Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 17 Jul 2024 11:52:00 -0500 Subject: [PATCH 10/16] Cosmetic changes. --- cmd/flexctl/backups.go | 9 ++++----- cmd/flexctl/main.go | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index c059f45..2875c84 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -228,14 +228,13 @@ func backupsEnabled() bool { return os.Getenv("S3_ARCHIVE_CONFIG") != "" } -func newBackupConfigCmd() *cobra.Command { +func newBackupConfig() *cobra.Command { var configCmd = &cobra.Command{ Use: "config", Short: "Manage backup configuration", } - configCmd.AddCommand(newConfigShowCmd()) - configCmd.AddCommand(newConfigUpdateCmd()) + configCmd.AddCommand(newConfigShow(), newConfigUpdate()) return configCmd } @@ -253,7 +252,7 @@ func getApiUrl() (string, error) { return url, nil } -func newConfigShowCmd() *cobra.Command { +func newConfigShow() *cobra.Command { var configShowCmd = &cobra.Command{ Use: "show", Short: "Show current configuration", @@ -296,7 +295,7 @@ type configUpdateResult struct { Error string `json:"error,omitempty"` } -func newConfigUpdateCmd() *cobra.Command { +func newConfigUpdate() *cobra.Command { var cmd = &cobra.Command{ Use: "update", Short: "Update configuration", diff --git a/cmd/flexctl/main.go b/cmd/flexctl/main.go index 58f7db7..2bdd744 100644 --- a/cmd/flexctl/main.go +++ b/cmd/flexctl/main.go @@ -21,7 +21,7 @@ func main() { backupCmd.AddCommand(backupListCmd) backupCmd.AddCommand(backupShowCmd) backupCmd.AddCommand(backupCreateCmd) - backupCmd.AddCommand(newBackupConfigCmd()) + backupCmd.AddCommand(newBackupConfig()) if err := rootCmd.Execute(); err != nil { fmt.Println(err) From 7987ec794293671a35881329ddc1e55a74718531 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 17 Jul 2024 13:52:57 -0500 Subject: [PATCH 11/16] Print restart instructions if required. --- cmd/flexctl/backups.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index 2875c84..6689302 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -243,11 +243,19 @@ type configShowResult struct { Result flypg.BarmanSettings `json:"result"` } -func getApiUrl() (string, error) { - hostname := os.Getenv("FLY_APP_NAME") - if hostname == "" { +func getAppName() (string, error) { + name := os.Getenv("FLY_APP_NAME") + if name == "" { return "", fmt.Errorf("FLY_APP_NAME is not set") } + return name, nil +} + +func getApiUrl() (string, error) { + hostname, err := getAppName() + if err != nil { + return "", err + } url := fmt.Sprintf("http://%s.internal:5500", hostname) return url, nil } @@ -360,7 +368,11 @@ func newConfigUpdate() *cobra.Command { } if rv.Result.RestartRequired { - fmt.Println("A restart is required for these changes to take effect.") + appName, err := getAppName() + if err != nil { + return err + } + fmt.Printf("A restart is required for these changes to take effect. Run `fly app restart %s` to restart.)", appName) } return nil From 4a90876c694759f86a77e71e3b1dc6a4ef2e9dfa Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 17 Jul 2024 14:11:05 -0500 Subject: [PATCH 12/16] Fix restart command. --- cmd/flexctl/backups.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index 6689302..a5aa19d 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -372,7 +372,7 @@ func newConfigUpdate() *cobra.Command { if err != nil { return err } - fmt.Printf("A restart is required for these changes to take effect. Run `fly app restart %s` to restart.)", appName) + fmt.Printf("A restart is required for these changes to take effect. Run `fly app restart -a %s` to restart.)", appName) } return nil From c77ff26db4e289f04c00f816572bae44562eb310 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 17 Jul 2024 14:37:12 -0500 Subject: [PATCH 13/16] Switch from `fly app restart` to `fly pg restart`. --- cmd/flexctl/backups.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index a5aa19d..89c1585 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -372,7 +372,7 @@ func newConfigUpdate() *cobra.Command { if err != nil { return err } - fmt.Printf("A restart is required for these changes to take effect. Run `fly app restart -a %s` to restart.)", appName) + fmt.Printf("A restart is required for these changes to take effect. Run `fly pg restart -a %s` to restart.)", appName) } return nil From ffcc75fe7eef1f8820a735126d76a50d202ec7c3 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 17 Jul 2024 14:56:56 -0500 Subject: [PATCH 14/16] Cosmetic updates. --- cmd/flexctl/backups.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index 89c1585..390f62c 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -229,14 +229,14 @@ func backupsEnabled() bool { } func newBackupConfig() *cobra.Command { - var configCmd = &cobra.Command{ + var cmd = &cobra.Command{ Use: "config", Short: "Manage backup configuration", } - configCmd.AddCommand(newConfigShow(), newConfigUpdate()) + cmd.AddCommand(newConfigShow(), newConfigUpdate()) - return configCmd + return cmd } type configShowResult struct { @@ -261,7 +261,7 @@ func getApiUrl() (string, error) { } func newConfigShow() *cobra.Command { - var configShowCmd = &cobra.Command{ + var cmd = &cobra.Command{ Use: "show", Short: "Show current configuration", RunE: func(cmd *cobra.Command, args []string) error { @@ -290,7 +290,7 @@ func newConfigShow() *cobra.Command { }, } - return configShowCmd + return cmd } type successfulUpdateResult struct { From 7bbc4000635a2fe99dde0afd360a3eb0795333c4 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 17 Jul 2024 14:59:00 -0500 Subject: [PATCH 15/16] Check that backups are enabled before updating backup configs. --- cmd/flexctl/backups.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index 390f62c..02558ee 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -265,6 +265,10 @@ func newConfigShow() *cobra.Command { Use: "show", Short: "Show current configuration", RunE: func(cmd *cobra.Command, args []string) error { + if !backupsEnabled() { + return fmt.Errorf("backups are not enabled") + } + url, err := getApiUrl() if err != nil { return err @@ -310,6 +314,9 @@ func newConfigUpdate() *cobra.Command { } cmd.RunE = func(cmd *cobra.Command, args []string) error { + if !backupsEnabled() { + return fmt.Errorf("backups are not enabled") + } archiveTimeout, err := cmd.Flags().GetString("archive-timeout") if err != nil { From 06d9e36f76a26dd4a71be9e1fce75af2ed5d820f Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Thu, 18 Jul 2024 09:14:48 -0500 Subject: [PATCH 16/16] Add missing newline. --- cmd/flexctl/backups.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/flexctl/backups.go b/cmd/flexctl/backups.go index 02558ee..346f497 100644 --- a/cmd/flexctl/backups.go +++ b/cmd/flexctl/backups.go @@ -379,7 +379,7 @@ func newConfigUpdate() *cobra.Command { if err != nil { return err } - fmt.Printf("A restart is required for these changes to take effect. Run `fly pg restart -a %s` to restart.)", appName) + fmt.Printf("A restart is required for these changes to take effect. Run `fly pg restart -a %s` to restart.)\n", appName) } return nil