Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support deploying several db at once #215

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading