forked from ovhlabs/sail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redeploy.go
189 lines (162 loc) · 6.74 KB
/
redeploy.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package service
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/runabove/sail/internal"
)
var (
redeployBody Redeploy
redeployPublished []string
redeployLink []string
redeployNetwork []string
redeployNetworkAllow string
redeployGateway []string
redeployVolume []string
redeployBatch bool
redeployPool string
)
func redeployCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "redeploy",
Short: "Redeploy a docker service: sail service redeploy <applicationName>/<serviceId>",
Long: `Redeploy a docker service: sail service redeploy <applicationName>/<serviceId>`,
Aliases: []string{"restart"},
Run: cmdRedeploy,
}
cmd.Flags().StringVarP(&redeployBody.ContainerModel, "model", "", "", "Container model")
cmd.Flags().IntVarP(&redeployBody.ContainerNumber, "number", "", 0, "Number of container to run")
cmd.Flags().StringSliceVarP(&redeployBody.ContainerEnvironment, "env", "e", nil, "override docker environment")
cmd.Flags().StringVarP(&redeployBody.RestartPolicy, "restart", "", "", "{no|always[:<max>]|on-failure[:<max>]}")
cmd.Flags().StringSliceVarP(&redeployBody.ContainerCommand, "command", "", nil, "override docker run command")
cmd.Flags().StringVarP(&redeployBody.RepositoryTag, "tag", "", "", "deploy from new image version")
cmd.Flags().StringVarP(&redeployBody.ContainerWorkdir, "workdir", "", "", "override docker workdir")
cmd.Flags().StringVarP(&redeployBody.ContainerEntrypoint, "entrypoint", "", "", "override docker entrypoint")
cmd.Flags().StringVarP(&redeployBody.ContainerUser, "user", "", "", "override docker user")
cmd.Flags().StringSliceVar(&redeployNetwork, "network", nil, "public|private|<namespace name>")
cmd.Flags().StringVarP(&redeployNetworkAllow, "network-allow", "", "", "[network:]ip[/mask] Use IPs whitelist")
cmd.Flags().StringSliceVarP(&redeployPublished, "publish", "", nil, "Publish a container's port to the host")
cmd.Flags().StringSliceVarP(&redeployGateway, "gateway", "", nil, "network-input:network-output")
cmd.Flags().StringSliceVarP(&redeployVolume, "volume", "", nil, "/path:size] (Size in GB)")
cmd.Flags().BoolVarP(&redeployBatch, "batch", "", false, "do not attach console on start")
cmd.Flags().StringSliceVarP(&redeployLink, "link", "", nil, "name:alias")
cmd.Flags().StringVarP(&redeployPool, "pool", "", "", "Dedicated host pool")
return cmd
}
// Redeploy struct holds all parameters sent to /applications/%s/services/%s/redeploy
type Redeploy struct {
Service string `json:"-"`
Volumes map[string]VolumeConfig `json:"volumes,omitempty"`
Repository string `json:"repository,omitempty"`
ContainerUser string `json:"container_user,omitempty"`
RestartPolicy string `json:"restart_policy,omitempty"`
ContainerCommand []string `json:"container_command,omitempty"`
ContainerNetwork map[string]map[string][]string `json:"container_network,omitempty"`
ContainerEntrypoint string `json:"container_user,omitempty"`
ContainerNumber int `json:"container_number,omitempty"`
RepositoryTag string `json:"repository_tag,omitempty"`
Links map[string]string `json:"links,omitempty"`
Application string `json:"namespace,omitempty"`
ContainerWorkdir string `json:"container_workdir,omitempty"`
ContainerEnvironment []string `json:"container_environment,omitempty"`
ContainerModel string `json:"container_model,omitempty"`
ContainerPorts map[string][]PortConfig `json:"container_ports,omitempty"`
Pool string `json:"pool,omitempty"`
}
func cmdRedeploy(cmd *cobra.Command, args []string) {
usage := "Invalid usage. sail service redeploy <applicationName>/<serviceId>. Please see sail service redeploy --help\n"
if len(args) != 1 {
fmt.Fprintf(os.Stderr, usage)
return
}
// Split namespace and repository
host, app, service, _, err := internal.ParseResourceName(args[0])
internal.Check(err)
redeployBody.Application = app
redeployBody.Service = service
if !internal.CheckHostConsistent(host) {
fmt.Fprintf(os.Stderr, "Error: Invalid Host %s for endpoint %s\n", host, internal.Host)
os.Exit(1)
}
// Redeploy
serviceRedeploy(redeployBody)
}
func serviceRedeploy(args Redeploy) {
// Parse volumes
if len(redeployVolume) > 0 {
args.Volumes = make(map[string]VolumeConfig)
}
for _, vol := range redeployVolume {
t := strings.Split(vol, ":")
if len(t) == 2 {
args.Volumes[t[0]] = VolumeConfig{Size: t[1]}
} else if len(t) == 1 {
args.Volumes[t[0]] = VolumeConfig{Size: "10"}
} else {
fmt.Fprintf(os.Stderr, "Error: Volume parameter '%s' not formated correctly\n", vol)
os.Exit(1)
}
}
// Parse links
if len(redeployLink) > 0 {
args.Links = make(map[string]string)
}
for _, link := range redeployLink {
t := strings.Split(link, ":")
if len(t) == 1 {
args.Links[t[0]] = t[0]
} else {
args.Links[t[0]] = t[1]
}
}
// Parse ContainerNetworks arguments
if len(redeployNetwork) > 0 {
args.ContainerNetwork = make(map[string]map[string][]string)
}
for _, network := range redeployNetwork {
args.ContainerNetwork[network] = make(map[string][]string)
}
for _, gat := range redeployGateway {
t := strings.Split(gat, ":")
if len(t) != 2 {
fmt.Fprintf(os.Stderr, "Invalid gateway parameter, should be \"input:output\". Typically, output will be 'predictor' or 'public'")
os.Exit(1)
}
}
// Load Pool
args.Pool = redeployPool
// Parse ContainerPorts
args.ContainerPorts = parsePublishedPort(redeployPublished)
app := args.Application
service := args.Service
path := fmt.Sprintf("/applications/%s/services/%s/redeploy", app, service)
body, err := json.MarshalIndent(args, " ", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal: %s\n", err)
return
}
// Attach console
if !redeployBatch {
internal.StreamPrint("GET", fmt.Sprintf("/applications/%s/services/%s/attach", app, service), nil)
}
// Redeploy
buffer, _, err := internal.Stream("POST", path+"?stream", body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
line, err := internal.DisplayStream(buffer)
internal.Check(err)
if line != nil {
var data map[string]interface{}
err = json.Unmarshal(line, &data)
internal.Check(err)
fmt.Printf("Hostname: %v\n", data["hostname"])
fmt.Printf("Running containers: %v/%v\n", data["container_number"], data["container_target"])
}
if !redeployBatch {
internal.ExitAfterCtrlC()
}
}