Skip to content

Commit

Permalink
feat: Allow stop & delete bunch of services (#207)
Browse files Browse the repository at this point in the history
* feat: Be able to stop bunch of services

* feat: Be able to delete bunch of services

* wording: Use correct "use either ... or" expression
  • Loading branch information
mzottola authored Oct 5, 2023
1 parent e29ddae commit 4d2899c
Show file tree
Hide file tree
Showing 16 changed files with 859 additions and 69 deletions.
66 changes: 63 additions & 3 deletions cmd/application_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/pterm/pterm"
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
)

var applicationDeleteCmd = &cobra.Command{
Expand All @@ -23,6 +26,18 @@ var applicationDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if applicationName == "" && applicationNames == "" {
utils.PrintlnError(fmt.Errorf("use either --application \"<app name>\" or --applications \"<app1 name>, <app2 name>\" but not both at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if applicationName != "" && applicationNames != "" {
utils.PrintlnError(fmt.Errorf("you can't use --application and --applications 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 @@ -32,6 +47,48 @@ var applicationDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if applicationNames != "" {
// 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)
}

applications, _, err := client.ApplicationsApi.ListApplication(context.Background(), envId).Execute()

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

var serviceIds []string
for _, applicationName := range strings.Split(applicationNames, ",") {
trimmedApplicationName := strings.TrimSpace(applicationName)
serviceIds = append(serviceIds, utils.FindByApplicationName(applications.GetResults(), trimmedApplicationName).Id)
}

// stop multiple services
_, err = utils.DeleteServices(client, envId, serviceIds, utils.ApplicationType)
if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
utils.Println(fmt.Sprintf("Deleting applications %s in progress..", pterm.FgBlue.Sprintf(applicationNames)))
}

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

return
}

applications, _, err := client.ApplicationsApi.ListApplication(context.Background(), envId).Execute()

if err != nil {
Expand All @@ -51,6 +108,10 @@ var applicationDeleteCmd = &cobra.Command{

msg, err := utils.DeleteService(client, envId, application.Id, utils.ApplicationType, watchFlag)

if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
}

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
Expand All @@ -76,7 +137,6 @@ func init() {
applicationDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
applicationDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
applicationDeleteCmd.Flags().StringVarP(&applicationName, "application", "n", "", "Application Name")
applicationDeleteCmd.Flags().StringVarP(&applicationNames, "applications", "", "", "Application Names (comma separated) Example: --applications \"app1,app2,app3\"")
applicationDeleteCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch application status until it's ready or an error occurs")

_ = applicationDeleteCmd.MarkFlagRequired("application")
}
10 changes: 6 additions & 4 deletions cmd/application_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package cmd
import (
"context"
"fmt"
"os"
"time"

"github.com/pterm/pterm"
"github.com/qovery/qovery-cli/utils"
"github.com/qovery/qovery-client-go"
"github.com/spf13/cobra"
"os"
"time"

"github.com/qovery/qovery-cli/utils"
)

var applicationDeployCmd = &cobra.Command{
Expand All @@ -25,7 +27,7 @@ var applicationDeployCmd = &cobra.Command{
}

if applicationName == "" && applicationNames == "" {
utils.PrintlnError(fmt.Errorf("use neither --application \"<app name>\" nor --applications \"<app1 name>, <app2 name>\""))
utils.PrintlnError(fmt.Errorf("use either --application \"<app name>\" or --applications \"<app1 name>, <app2 name>\" but not both at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
Expand Down
63 changes: 60 additions & 3 deletions cmd/application_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/pterm/pterm"
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
)

var applicationStopCmd = &cobra.Command{
Expand All @@ -23,6 +26,18 @@ var applicationStopCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if applicationName == "" && applicationNames == "" {
utils.PrintlnError(fmt.Errorf("use either --application \"<app name>\" or --applications \"<app1 name>, <app2 name>\" but not both at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if applicationName != "" && applicationNames != "" {
utils.PrintlnError(fmt.Errorf("you can't use --application and --applications 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 @@ -32,6 +47,49 @@ var applicationStopCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if applicationNames != "" {
// 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)
}

applications, _, err := client.ApplicationsApi.ListApplication(context.Background(), envId).Execute()

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

var serviceIds []string
for _, applicationName := range strings.Split(applicationNames, ",") {
trimmedApplicationName := strings.TrimSpace(applicationName)
serviceIds = append(serviceIds, utils.FindByApplicationName(applications.GetResults(), trimmedApplicationName).Id)
}

// stop multiple services
_, err = utils.StopServices(client, envId, serviceIds, utils.ApplicationType)

if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
utils.Println(fmt.Sprintf("Stopping applications %s in progress..", pterm.FgBlue.Sprintf(applicationNames)))
}

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

return
}

applications, _, err := client.ApplicationsApi.ListApplication(context.Background(), envId).Execute()

if err != nil {
Expand Down Expand Up @@ -76,8 +134,7 @@ func init() {
applicationStopCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
applicationStopCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
applicationStopCmd.Flags().StringVarP(&applicationName, "application", "n", "", "Application Name")
applicationStopCmd.Flags().StringVarP(&applicationNames, "applications", "", "", "Application Names (comma separated) Example: --applications \"app1,app2,app3\"")
applicationStopCmd.Flags().StringVarP(&applicationCommitId, "commit-id", "c", "", "Application Commit ID")
applicationStopCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch application status until it's ready or an error occurs")

_ = applicationStopCmd.MarkFlagRequired("application")
}
62 changes: 59 additions & 3 deletions cmd/container_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/pterm/pterm"
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
)

var containerDeleteCmd = &cobra.Command{
Expand All @@ -23,6 +26,18 @@ var containerDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if containerName == "" && containerNames == "" {
utils.PrintlnError(fmt.Errorf("use either --container \"<container name>\" or --containers \"<container1 name>, <container2 name>\" but not both at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if containerName != "" && containerNames != "" {
utils.PrintlnError(fmt.Errorf("you can't use --container and --containers 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 @@ -32,6 +47,48 @@ var containerDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if containerNames != "" {
// 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)
}

containers, _, err := client.ContainersApi.ListContainer(context.Background(), envId).Execute()

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

var serviceIds []string
for _, containerName := range strings.Split(containerNames, ",") {
trimmedContainerName := strings.TrimSpace(containerName)
serviceIds = append(serviceIds, utils.FindByContainerName(containers.GetResults(), trimmedContainerName).Id)
}

_, err = utils.DeleteServices(client, envId, serviceIds, utils.ContainerType)

if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
utils.Println(fmt.Sprintf("Deleting containers %s in progress..", pterm.FgBlue.Sprintf(containerNames)))
}

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

return
}

containers, _, err := client.ContainersApi.ListContainer(context.Background(), envId).Execute()

if err != nil {
Expand Down Expand Up @@ -76,7 +133,6 @@ func init() {
containerDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
containerDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
containerDeleteCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name")
containerDeleteCmd.Flags().StringVarP(&containerNames, "containers", "", "", "Container Names (comma separated) (ex: --containers \"container1,container2\")")
containerDeleteCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch container status until it's ready or an error occurs")

_ = containerDeleteCmd.MarkFlagRequired("container")
}
5 changes: 3 additions & 2 deletions cmd/container_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"time"

"github.com/pterm/pterm"
"github.com/qovery/qovery-cli/utils"
"github.com/qovery/qovery-client-go"
"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
)

var containerDeployCmd = &cobra.Command{
Expand All @@ -26,7 +27,7 @@ var containerDeployCmd = &cobra.Command{
}

if containerName == "" && containerNames == "" {
utils.PrintlnError(fmt.Errorf("use neither --cronjob \"<container name>\" nor --cronjobs \"<container1 name>, <container2 name>\""))
utils.PrintlnError(fmt.Errorf("use either --cronjob \"<container name>\" or --cronjobs \"<container1 name>, <container2 name>\" but not both at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
Expand Down
Loading

0 comments on commit 4d2899c

Please sign in to comment.