/
container_deploy.go
134 lines (107 loc) · 4.28 KB
/
container_deploy.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
package cmd
import (
"context"
"fmt"
"os"
"time"
"github.com/pterm/pterm"
"github.com/qovery/qovery-client-go"
"github.com/spf13/cobra"
"github.com/qovery/qovery-cli/utils"
)
var containerDeployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy 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(envId)))
time.Sleep(5 * time.Second)
}
// deploy multiple services
err := utils.DeployContainers(client, envId, containerNames, containerTag)
if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}
utils.Println(fmt.Sprintf("Deploying containers %s in progress..", pterm.FgBlue.Sprintf(containerNames)))
if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
}
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
}
req := qovery.ContainerDeployRequest{
ImageTag: container.Tag,
}
if containerTag != "" {
req.ImageTag = containerTag
}
msg, err := utils.DeployService(client, envId, container.Id, utils.ContainerType, req, 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 deployed!", pterm.FgBlue.Sprintf(containerName)))
} else {
utils.Println(fmt.Sprintf("Deploying container %s in progress..", pterm.FgBlue.Sprintf(containerName)))
}
},
}
func init() {
containerCmd.AddCommand(containerDeployCmd)
containerDeployCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
containerDeployCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
containerDeployCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
containerDeployCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name")
containerDeployCmd.Flags().StringVarP(&containerNames, "containers", "", "", "Container Names (comma separated) (ex: --containers \"container1,container2\")")
containerDeployCmd.Flags().StringVarP(&containerTag, "tag", "t", "", "Container Tag")
containerDeployCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch container status until it's ready or an error occurs")
}