-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add helm redeploy and stop cmd
- Loading branch information
Showing
3 changed files
with
294 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/pterm/pterm" | ||
"github.com/qovery/qovery-cli/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var helmRedeployCmd = &cobra.Command{ | ||
Use: "redeploy", | ||
Short: "Redeploy a helm", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
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 | ||
} | ||
|
||
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 | ||
} | ||
|
||
helms, _, err := client.HelmsAPI.ListHelms(context.Background(), envId).Execute() | ||
|
||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
|
||
helm := utils.FindByHelmName(helms.GetResults(), helmName) | ||
|
||
if helm == nil { | ||
utils.PrintlnError(fmt.Errorf("helm %s not found", helmName)) | ||
utils.PrintlnInfo("You can list all helms with: qovery helm list") | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
|
||
msg, err := utils.RedeployService(client, envId, helm.Id, utils.HelmType, watchFlag) | ||
|
||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
|
||
if msg != "" { | ||
utils.PrintlnInfo(msg) | ||
return | ||
} | ||
|
||
if watchFlag { | ||
utils.Println(fmt.Sprintf("Helm %s redeployed!", pterm.FgBlue.Sprintf(helmName))) | ||
} else { | ||
utils.Println(fmt.Sprintf("Redeploying helm %s in progress..", pterm.FgBlue.Sprintf(helmName))) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
helmCmd.AddCommand(helmRedeployCmd) | ||
helmRedeployCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") | ||
helmRedeployCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") | ||
helmRedeployCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") | ||
helmRedeployCmd.Flags().StringVarP(&helmName, "helm", "n", "", "Helm Name") | ||
helmRedeployCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch helm status until it's ready or an error occurs") | ||
|
||
_ = helmRedeployCmd.MarkFlagRequired("helm") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/qovery/qovery-client-go" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pterm/pterm" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/qovery/qovery-cli/utils" | ||
) | ||
|
||
var helmStopCmd = &cobra.Command{ | ||
Use: "stop", | ||
Short: "Stop a helm", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
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 helmName == "" && helmNames == "" { | ||
utils.PrintlnError(fmt.Errorf("use either --helm \"<helm name>\" or --helms \"<helm1 name>, <helm2 name>\" but not both at the same time")) | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
|
||
if helmName != "" && helmNames != "" { | ||
utils.PrintlnError(fmt.Errorf("you can't use --helm and --helms 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) | ||
|
||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
|
||
if helmNames != "" { | ||
// 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) | ||
} | ||
|
||
helms, _, err := client.HelmsAPI.ListHelms(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 _, helmName := range strings.Split(helmNames, ",") { | ||
trimmedHelmName := strings.TrimSpace(helmName) | ||
|
||
helm := utils.FindByHelmName(helms.GetResults(), trimmedHelmName) | ||
if helm == nil { | ||
utils.PrintlnError(fmt.Errorf("helm %s not found", trimmedHelmName)) | ||
utils.PrintlnInfo("You can list all helms with: qovery helm list") | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
serviceIds = append(serviceIds, helm.Id) | ||
} | ||
|
||
// stop multiple services | ||
_, err = utils.StopServices(client, envId, serviceIds, utils.HelmType) | ||
|
||
if watchFlag { | ||
utils.WatchEnvironment(envId, qovery.STATEENUM_STOPPED, client) | ||
} else { | ||
utils.Println(fmt.Sprintf("Stopping helms %s in progress..", pterm.FgBlue.Sprintf(helmNames))) | ||
} | ||
|
||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
|
||
return | ||
} | ||
|
||
helms, _, err := client.HelmsAPI.ListHelms(context.Background(), envId).Execute() | ||
|
||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
|
||
helm := utils.FindByHelmName(helms.GetResults(), helmName) | ||
|
||
if helm == nil { | ||
utils.PrintlnError(fmt.Errorf("helm %s not found", helmName)) | ||
utils.PrintlnInfo("You can list all helms with: qovery helm list") | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
|
||
msg, err := utils.StopService(client, envId, helm.Id, utils.HelmType, watchFlag) | ||
|
||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
|
||
if msg != "" { | ||
utils.PrintlnInfo(msg) | ||
return | ||
} | ||
|
||
if watchFlag { | ||
utils.Println(fmt.Sprintf("Helm %s stopped!", pterm.FgBlue.Sprintf(helmName))) | ||
} else { | ||
utils.Println(fmt.Sprintf("Stopping helm %s in progress..", pterm.FgBlue.Sprintf(helmName))) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
helmCmd.AddCommand(helmStopCmd) | ||
helmStopCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") | ||
helmStopCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") | ||
helmStopCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") | ||
helmStopCmd.Flags().StringVarP(&helmName, "helm", "n", "", "Helm Name") | ||
helmStopCmd.Flags().StringVarP(&helmNames, "helms", "", "", "Helm Names (comma separated) (ex: --helms \"helm1,helm2\")") | ||
helmStopCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch helm status until it's ready or an error occurs") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters