From 2d8927a56514f7cd5a06c8dc9f244cbf79198d20 Mon Sep 17 00:00:00 2001 From: BenjaminCh Date: Thu, 26 Oct 2023 17:45:16 +0200 Subject: [PATCH] feat: support deploying several db at once Add `--databases` option to `database deploy` so you can deploy a list of databases Ticket: ENG-1617 --- cmd/database_deploy.go | 45 ++++++++++++++++++++++++++++++++++++++++-- utils/qovery.go | 34 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/cmd/database_deploy.go b/cmd/database_deploy.go index 11d23968..bf2c12bb 100644 --- a/cmd/database_deploy.go +++ b/cmd/database_deploy.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "time" "github.com/pterm/pterm" "github.com/qovery/qovery-cli/utils" @@ -23,6 +24,18 @@ var databaseDeployCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + if databaseName == "" && databaseNames == "" { + utils.PrintlnError(fmt.Errorf("use either --database \"\" or --databases \", \" but not both at the same time")) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + if databaseName != "" && databaseNames != "" { + utils.PrintlnError(fmt.Errorf("you can't use --database and --databases at the same time")) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + client := utils.GetQoveryClient(tokenType, token) _, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) @@ -40,6 +53,35 @@ var databaseDeployCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + if databaseNames != "" { + // wait until service is ready + for { + if utils.IsEnvironmentInATerminalState(envId, client) { + break + } + + utils.Println(fmt.Sprintf("Waiting for environment %s to be ready..", pterm.FgBlue.Sprintf(envId))) + time.Sleep(5 * time.Second) + } + + // deploy multiple services + err := utils.DeployDatabases(client, envId, databaseNames) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("Deploying databases %s in progress..", pterm.FgBlue.Sprintf(databaseNames))) + + if watchFlag { + utils.WatchEnvironment(envId, "unused", client) + } + + return + } + database := utils.FindByDatabaseName(databases.GetResults(), databaseName) if database == nil { @@ -76,7 +118,6 @@ func init() { databaseDeployCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") databaseDeployCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") databaseDeployCmd.Flags().StringVarP(&databaseName, "database", "n", "", "Database Name") + databaseDeployCmd.Flags().StringVarP(&databaseNames, "databases", "", "", "Database Names (comma separated) (ex: --databases \"database1,database2\")") databaseDeployCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch database status until it's ready or an error occurs") - - _ = databaseDeployCmd.MarkFlagRequired("database") } diff --git a/utils/qovery.go b/utils/qovery.go index 0ab9d993..29c5c9d7 100644 --- a/utils/qovery.go +++ b/utils/qovery.go @@ -1402,6 +1402,40 @@ func DeployJobs(client *qovery.APIClient, envId string, jobNames string, commitI return deployAllServices(client, envId, req) } +func DeployDatabases(client *qovery.APIClient, envId string, databaseNames string) error { + if databaseNames == "" { + return nil + } + + var databasesToDeploy []string + + databases, _, err := client.DatabasesAPI.ListDatabase(context.Background(), envId).Execute() + + if err != nil { + return err + } + + for _, databaseName := range strings.Split(databaseNames, ",") { + trimmedDatabaseName := strings.TrimSpace(databaseName) + database := FindByDatabaseName(databases.GetResults(), trimmedDatabaseName) + + if database == nil { + return fmt.Errorf("database %s not found", trimmedDatabaseName) + } + + databasesToDeploy = append(databasesToDeploy, database.Id) + } + + req := qovery.DeployAllRequest{ + Applications: nil, + Containers: nil, + Databases: databasesToDeploy, + Jobs: nil, + } + + return deployAllServices(client, envId, req) +} + func deployAllServices(client *qovery.APIClient, envId string, req qovery.DeployAllRequest) error { _, _, err := client.EnvironmentActionsAPI.DeployAllServices(context.Background(), envId).DeployAllRequest(req).Execute() if err != nil {