-
Notifications
You must be signed in to change notification settings - Fork 24
/
container_stop.go
139 lines (112 loc) · 4.57 KB
/
container_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 (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/qovery/qovery-cli/utils"
)
var containerStopCmd = &cobra.Command{
Use: "stop",
Short: "Stop a container",
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 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)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
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("%s", 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)
}
// stop multiple services
_, err = utils.StopServices(client, envId, serviceIds, utils.ContainerType)
if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
utils.Println(fmt.Sprintf("Stopping containers %s in progress..", pterm.FgBlue.Sprintf("%s", 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 {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
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
}
msg, err := utils.StopService(client, envId, container.Id, utils.ContainerType, 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("Container %s stopped!", pterm.FgBlue.Sprintf("%s", containerName)))
} else {
utils.Println(fmt.Sprintf("Stopping container %s in progress..", pterm.FgBlue.Sprintf("%s", containerName)))
}
},
}
func init() {
containerCmd.AddCommand(containerStopCmd)
containerStopCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
containerStopCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
containerStopCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
containerStopCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name")
containerStopCmd.Flags().StringVarP(&containerNames, "containers", "", "", "Container Names (comma separated) (ex: --containers \"container1,container2\")")
containerStopCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch container status until it's ready or an error occurs")
}