/
lifecycle_stop.go
139 lines (111 loc) · 4.49 KB
/
lifecycle_stop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package cmd
import (
"fmt"
"os"
"strings"
"time"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/qovery/qovery-cli/utils"
)
var lifecycleStopCmd = &cobra.Command{
Use: "stop",
Short: "Stop a lifecycle job",
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 lifecycleName == "" && lifecycleNames == "" {
utils.PrintlnError(fmt.Errorf("use either --lifecycle \"<lifecycle name>\" or --lifecycles \"<lifecycle1 name>, <lifecycle2 name>\" but not both at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
if lifecycleName != "" && lifecycleNames != "" {
utils.PrintlnError(fmt.Errorf("you can't use --lifecycle and --lifecycles 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 lifecycleNames != "" {
// 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)
}
lifecycles, err := ListLifecycleJobs(envId, client)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
var serviceIds []string
for _, lifecycleName := range strings.Split(lifecycleNames, ",") {
trimmedLifecycleName := strings.TrimSpace(lifecycleName)
serviceIds = append(serviceIds, utils.GetJobId(utils.FindByJobName(lifecycles, trimmedLifecycleName)))
}
// stop multiple services
_, err = utils.StopServices(client, envId, serviceIds, utils.JobType)
if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
utils.Println(fmt.Sprintf("Stopping lifecycle jobs %s in progress..", pterm.FgBlue.Sprintf(lifecycleNames)))
}
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
return
}
lifecycles, err := ListLifecycleJobs(envId, client)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
lifecycle := utils.FindByJobName(lifecycles, lifecycleName)
if lifecycle == nil || lifecycle.LifecycleJobResponse == nil {
utils.PrintlnError(fmt.Errorf("lifecycle %s not found", lifecycleName))
utils.PrintlnInfo("You can list all lifecycle jobs with: qovery lifecycle list")
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
msg, err := utils.StopService(client, envId, lifecycle.LifecycleJobResponse.Id, utils.JobType, 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("Lifecycle %s stopped!", pterm.FgBlue.Sprintf(lifecycleName)))
} else {
utils.Println(fmt.Sprintf("Stopping lifecycle %s in progress..", pterm.FgBlue.Sprintf(lifecycleName)))
}
},
}
func init() {
lifecycleCmd.AddCommand(lifecycleStopCmd)
lifecycleStopCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
lifecycleStopCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
lifecycleStopCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
lifecycleStopCmd.Flags().StringVarP(&lifecycleName, "lifecycle", "n", "", "Lifecycle Name")
lifecycleStopCmd.Flags().StringVarP(&lifecycleNames, "lifecycles", "", "", "Lifecycle Job Names (comma separated) (ex: --lifecycles \"lifecycle1,lifecycle2\")")
lifecycleStopCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch lifecycle status until it's ready or an error occurs")
}