Skip to content

Commit

Permalink
feat(ENG-1883): allow to queue stop deployment request for services a…
Browse files Browse the repository at this point in the history
…nd env
  • Loading branch information
fabienfleureau committed Jan 22, 2025
1 parent 2cc5a59 commit 0af3443
Show file tree
Hide file tree
Showing 7 changed files with 499 additions and 172 deletions.
146 changes: 96 additions & 50 deletions cmd/application_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/qovery/qovery-client-go"
"os"
"strings"
"time"
Expand All @@ -20,35 +21,41 @@ var applicationStopCmd = &cobra.Command{
utils.Capture(cmd)

tokenType, token, err := utils.GetAccessToken()
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
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
}
checkError(err)
validateApplicationArguments(applicationName, applicationNames)

client := utils.GetQoveryClient(tokenType, token)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
organizationId, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)
checkError(err)

if isDeploymentQueueEnabledForOrganization(organizationId) {
serviceIds := buildServiceIdsFromApplicationNames(client, envId, applicationName, applicationNames)
_, err := client.EnvironmentActionsAPI.
StopSelectedServices(context.Background(), envId).
EnvironmentServiceIdsAllRequest(qovery.EnvironmentServiceIdsAllRequest{
ApplicationIds: serviceIds,
}).
Execute()
checkError(err)
if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
if applicationName != "" {
utils.Println(fmt.Sprintf("Request to stop application %s has been queued...", pterm.FgBlue.Sprintf("%s", applicationName)))
} else {
utils.Println(fmt.Sprintf("Request to stop applications %s has been queued...", pterm.FgBlue.Sprintf("%s", applicationNames)))
}
}
return
}

// TODO once deployment queue is enabled for all organizations, remove the following code block
applications, _, err := client.ApplicationsAPI.ListApplication(context.Background(), envId).Execute()
checkError(err)

if applicationNames != "" {
// wait until service is ready
// TODO: this is not needed since we can put the deployment request in queue
for {
if utils.IsEnvironmentInATerminalState(envId, client) {
break
Expand All @@ -58,14 +65,6 @@ var applicationStopCmd = &cobra.Command{
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)
Expand All @@ -81,23 +80,10 @@ var applicationStopCmd = &cobra.Command{
utils.Println(fmt.Sprintf("Stopping applications %s in progress..", pterm.FgBlue.Sprintf("%s", applicationNames)))
}

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

checkError(err)
return
}

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
}

application := utils.FindByApplicationName(applications.GetResults(), applicationName)

if application == nil {
Expand All @@ -109,11 +95,7 @@ var applicationStopCmd = &cobra.Command{

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

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

if msg != "" {
utils.PrintlnInfo(msg)
Expand All @@ -128,6 +110,70 @@ var applicationStopCmd = &cobra.Command{
},
}

func buildServiceIdsFromApplicationNames(
client *qovery.APIClient,
environmentId string,
applicationName string,
applicationNames string,
) []string {
var serviceIds []string
applications, _, err := client.ApplicationsAPI.ListApplication(context.Background(), environmentId).Execute()
checkError(err)

if applicationName != "" {
application := utils.FindByApplicationName(applications.GetResults(), applicationName)
if application == nil {
utils.PrintlnError(fmt.Errorf("application %s not found", applicationName))
utils.PrintlnInfo("You can list all applications with: qovery application list")
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
serviceIds = append(serviceIds, application.Id)
}
if applicationNames != "" {
for _, applicationName := range strings.Split(applicationNames, ",") {
trimmedApplicationName := strings.TrimSpace(applicationName)
application := utils.FindByApplicationName(applications.GetResults(), trimmedApplicationName)
if application == nil {
utils.PrintlnError(fmt.Errorf("application %s not found", applicationName))
utils.PrintlnInfo("You can list all applications with: qovery application list")
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
serviceIds = append(serviceIds, application.Id)
}
}

return serviceIds
}

func isDeploymentQueueEnabledForOrganization(organizationId string) bool {
return organizationId == "3f421018-8edf-4a41-bb86-bec62791b6dc" || // backdev
organizationId == "3d542888-3d2c-474a-b1ad-712556db66da" // QSandbox
}

func validateApplicationArguments(applicationName string, applicationNames string) {
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
}
}

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

func init() {
applicationCmd.AddCommand(applicationStopCmd)
applicationStopCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
Expand Down
99 changes: 76 additions & 23 deletions cmd/container_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/qovery/qovery-client-go"
"os"
"strings"
"time"
Expand All @@ -20,31 +21,32 @@ var containerStopCmd = &cobra.Command{
utils.Capture(cmd)

tokenType, token, err := utils.GetAccessToken()
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
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
}
checkError(err)
validateContainerArguments(containerName, containerNames)

client := utils.GetQoveryClient(tokenType, token)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
organizationId, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)
checkError(err)

if isDeploymentQueueEnabledForOrganization(organizationId) {
serviceIds := buildServiceIdsFromContainerNames(client, envId, containerName, containerNames)
_, err := client.EnvironmentActionsAPI.
StopSelectedServices(context.Background(), envId).
EnvironmentServiceIdsAllRequest(qovery.EnvironmentServiceIdsAllRequest{
ContainerIds: serviceIds,
}).
Execute()
checkError(err)
if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
if containerName != "" {
utils.Println(fmt.Sprintf("Request to stop container %s has been queued...", pterm.FgBlue.Sprintf("%s", containerName)))
} else {
utils.Println(fmt.Sprintf("Request to stop containers %s has been queued...", pterm.FgBlue.Sprintf("%s", containerNames)))
}
}
return
}

if containerNames != "" {
Expand Down Expand Up @@ -128,6 +130,57 @@ var containerStopCmd = &cobra.Command{
},
}

func buildServiceIdsFromContainerNames(
client *qovery.APIClient,
environmentId string,
containerName string,
containerNames string,
) []string {
var serviceIds []string
containers, _, err := client.ContainersAPI.ListContainer(context.Background(), environmentId).Execute()
checkError(err)

if containerName != "" {
container := utils.FindByContainerName(containers.GetResults(), containerName)
if container == nil {
utils.PrintlnError(fmt.Errorf("container %s not found", containerName))
utils.PrintlnInfo("You can list all containers with: qovery container list")
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
serviceIds = append(serviceIds, container.Id)
}
if containerNames != "" {
for _, containerName := range strings.Split(containerNames, ",") {
trimmedContainerName := strings.TrimSpace(containerName)
container := utils.FindByContainerName(containers.GetResults(), trimmedContainerName)
if container == nil {
utils.PrintlnError(fmt.Errorf("container %s not found", containerName))
utils.PrintlnInfo("You can list all containers with: qovery container list")
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
serviceIds = append(serviceIds, container.Id)
}
}

return serviceIds
}

func validateContainerArguments(containerName string, containerNames string) {
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
}
}

func init() {
containerCmd.AddCommand(containerStopCmd)
containerStopCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
Expand Down
Loading

0 comments on commit 0af3443

Please sign in to comment.