Skip to content

Commit

Permalink
feat: support deploying several db at once (#215)
Browse files Browse the repository at this point in the history
Add `--databases` option to `database deploy` so you can deploy a list of databases

Ticket: ENG-1617
  • Loading branch information
benjaminch authored Oct 26, 2023
1 parent bc4b782 commit 191062e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
45 changes: 43 additions & 2 deletions cmd/database_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"time"

"github.com/pterm/pterm"
"github.com/qovery/qovery-cli/utils"
Expand All @@ -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 \"<database name>\" or --databases \"<database1 name>, <database2 name>\" 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)

Expand All @@ -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 {
Expand Down Expand Up @@ -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")
}
34 changes: 34 additions & 0 deletions utils/qovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 191062e

Please sign in to comment.