/
container.go
322 lines (262 loc) · 8.1 KB
/
container.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package commands
import (
"os"
"io"
"fmt"
"text/tabwriter"
"github.com/codegangsta/cli"
)
type OUTContainer struct{
ID string `json:"id"`
SystemDomain string `json:"system_domain"`
CustomDomain string `json:"custom_domain"`
Port int64 `json:"port"`
AppID string `json:"appid"`
AppSecret string `json:"secret"`
Status string `json:"status"`
Storage string `json:"storage"`
Version string `json:"version"`
ExpireAt string `json:"expire_at"`
}
func highlight(status string) string {
switch status {
case "running":
return fmt.Sprintf("[\033[32m%s\033[0m]", status)
case "stopped":
return fmt.Sprintf("[\033[31m%s\033[0m]", status)
default:
return fmt.Sprintf("[\033[33m%s\033[0m]", status)
}
return fmt.Sprintf("[\033[33m%s\033[0m]", status)
}
func printOUTContainer(out io.Writer, container OUTContainer) {
fmt.Fprintf(out, "------------------------\t--------------------------\n")
fmt.Fprintf(out, "H2OBJECT Version:\t%s\n", container.Version)
fmt.Fprintf(out, "Container ID:\t%s\n", container.ID)
fmt.Fprintf(out, "Container System Domain:\t%s\n", container.SystemDomain)
fmt.Fprintf(out, "Container Custom Domain:\t%s\n", container.CustomDomain)
fmt.Fprintf(out, "Container Port:\t%d\n", container.Port)
fmt.Fprintf(out, "Container Status:\t%s\n", highlight(container.Status))
fmt.Fprintf(out, "Container AppID:\t%s\n", container.AppID)
fmt.Fprintf(out, "Container AppSecret:\t%s\n", container.AppSecret)
fmt.Fprintf(out, "Container Storage Max:\t%s\n", container.Storage)
fmt.Fprintf(out, "Container Expire Date:\t%s\n", container.ExpireAt)
fmt.Fprintf(out, "\n")
}
func containerGetCommand(ctx *cli.Context) {
workdir := ctx.GlobalString("workdir")
stdout := os.Stdout
stderr := os.Stderr
host := ctx.String("Host")
port := ctx.Int("Port")
var token string = ""
config, err := LoadConfigFile(workdir)
if err == nil {
token = config.Auth.Token
}
if token == "" {
fmt.Fprintln(stdout, "container command need login first. ")
os.Exit(1)
}
client := NewClient(workdir, host, port)
var containers []OUTContainer
if err := client.GetContainers(token, ctx.Args(), &containers); err != nil {
fmt.Fprintln(stderr, err.Error())
os.Exit(1)
}
w := tabwriter.NewWriter(stdout, 10, 0, 2, ' ', 0)
fmt.Fprintf(w, "Container ID\tSystem Domain\tCustom Domain\tPort\tStatus\tApplication ID\tApplciation Secret\t\n")
for _, c := range containers {
fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%s\t%s\t%s\n", c.ID, c.SystemDomain, c.CustomDomain, c.Port, highlight(c.Status), c.AppID, c.AppSecret)
}
fmt.Fprintf(w, "\n")
}
func containerCreateCommand(ctx *cli.Context) {
workdir := ctx.GlobalString("workdir")
stdout := os.Stdout
host := ctx.String("Host")
port := ctx.Int("Port")
var token string = ""
config, err := LoadConfigFile(workdir)
if err == nil {
token = config.Auth.Token
}
if token == "" {
fmt.Fprintln(stdout, "container command need login first. ")
os.Exit(1)
}
var invitation string = ""
if len(ctx.Args()) == 1 {
invitation = ctx.Args()[0]
}
client := NewClient(workdir, host, port)
var container OUTContainer
if err := client.CreateContainer(token, invitation, version, &container); err != nil {
fmt.Fprintln(stdout, err.Error())
os.Exit(1)
}
fmt.Fprintf(stdout, "Container Created OK.\n")
printOUTContainer(tabwriter.NewWriter(stdout, 10, 0, 2, ' ', 0), container)
}
func containerStartCommand(ctx *cli.Context) {
workdir := ctx.GlobalString("workdir")
stdout := os.Stdout
host := ctx.String("Host")
port := ctx.Int("Port")
var token string = ""
config, err := LoadConfigFile(workdir)
if err == nil {
token = config.Auth.Token
}
if token == "" {
fmt.Fprintln(stdout, "container command need login first. ")
os.Exit(1)
}
if len(ctx.Args()) != 1 {
fmt.Fprintln(stdout, "command need [container id] args. ")
os.Exit(1)
}
client := NewClient(workdir, host, port)
var container OUTContainer
if err := client.StartContainer(token, ctx.Args()[0], version, &container); err != nil {
fmt.Fprintln(stdout, err.Error())
os.Exit(1)
}
fmt.Fprintf(stdout, "Container Start OK.\n")
printOUTContainer(tabwriter.NewWriter(stdout, 10, 0, 2, ' ', 0), container)
}
func containerStopCommand(ctx *cli.Context) {
workdir := ctx.GlobalString("workdir")
stdout := os.Stdout
host := ctx.String("Host")
port := ctx.Int("Port")
var token string = ""
config, err := LoadConfigFile(workdir)
if err == nil {
token = config.Auth.Token
}
if token == "" {
fmt.Fprintln(stdout, "container command need login first. ")
os.Exit(1)
}
if len(ctx.Args()) != 1 {
fmt.Fprintln(stdout, "command need [container id] args. ")
os.Exit(1)
}
client := NewClient(workdir, host, port)
var container OUTContainer
if err := client.StopContainer(token, ctx.Args()[0], &container); err != nil {
fmt.Fprintln(stdout, err.Error())
os.Exit(1)
}
fmt.Fprintf(stdout, "Container Stop OK.\n")
printOUTContainer(tabwriter.NewWriter(stdout, 10, 0, 2, ' ', 0), container)
}
func containerRestartCommand(ctx *cli.Context) {
workdir := ctx.GlobalString("workdir")
stdout := os.Stdout
host := ctx.String("Host")
port := ctx.Int("Port")
var token string = ""
config, err := LoadConfigFile(workdir)
if err == nil {
token = config.Auth.Token
}
if token == "" {
fmt.Fprintln(stdout, "container command need login first. ")
os.Exit(1)
}
if len(ctx.Args()) != 1 {
fmt.Fprintln(stdout, "command need [container id] args. ")
os.Exit(1)
}
client := NewClient(workdir, host, port)
var container OUTContainer
if err := client.RestartContainer(token, ctx.Args()[0], &container); err != nil {
fmt.Fprintln(stdout, err.Error())
os.Exit(1)
}
fmt.Fprintf(stdout, "Container Restart OK.\n")
printOUTContainer(tabwriter.NewWriter(stdout, 10, 0, 2, ' ', 0), container)
}
func containerPauseCommand(ctx *cli.Context) {
workdir := ctx.GlobalString("workdir")
stdout := os.Stdout
host := ctx.String("Host")
port := ctx.Int("Port")
var token string = ""
config, err := LoadConfigFile(workdir)
if err == nil {
token = config.Auth.Token
}
if token == "" {
fmt.Fprintln(stdout, "container command need login first. ")
os.Exit(1)
}
if len(ctx.Args()) != 1 {
fmt.Fprintln(stdout, "command need [container id] args. ")
os.Exit(1)
}
client := NewClient(workdir, host, port)
var container OUTContainer
if err := client.PauseContainer(token, ctx.Args()[0], &container); err != nil {
fmt.Fprintln(stdout, err.Error())
os.Exit(1)
}
fmt.Fprintf(stdout, "Container Pause OK.\n")
printOUTContainer(tabwriter.NewWriter(stdout, 10, 0, 2, ' ', 0), container)
}
func containerUnpauseCommand(ctx *cli.Context) {
workdir := ctx.GlobalString("workdir")
stdout := os.Stdout
host := ctx.String("Host")
port := ctx.Int("Port")
var token string = ""
config, err := LoadConfigFile(workdir)
if err == nil {
token = config.Auth.Token
}
if token == "" {
fmt.Fprintln(stdout, "container command need login first. ")
os.Exit(1)
}
if len(ctx.Args()) != 1 {
fmt.Fprintln(stdout, "command need [container id] args. ")
os.Exit(1)
}
client := NewClient(workdir, host, port)
var container OUTContainer
if err := client.UnpauseContainer(token, ctx.Args()[0], &container); err != nil {
fmt.Fprintln(stdout, err.Error())
os.Exit(1)
}
fmt.Fprintf(stdout, "Container Unpause OK.\n")
printOUTContainer(tabwriter.NewWriter(stdout, 10, 0, 2, ' ', 0), container)
}
func containerDomainCommand(ctx *cli.Context) {
workdir := ctx.GlobalString("workdir")
stdout := os.Stdout
host := ctx.String("Host")
port := ctx.Int("Port")
var token string = ""
config, err := LoadConfigFile(workdir)
if err == nil {
token = config.Auth.Token
}
if token == "" {
fmt.Fprintln(stdout, "container command need login first. ")
os.Exit(1)
}
if len(ctx.Args()) != 2 {
fmt.Fprintln(stdout, "command need [container id] [domain] args. ")
os.Exit(1)
}
client := NewClient(workdir, host, port)
var container OUTContainer
if err := client.DomainContainer(token, ctx.Args()[0], ctx.Args()[1], &container); err != nil {
fmt.Fprintln(stdout, err.Error())
os.Exit(1)
}
fmt.Fprintf(stdout, "Container Domain Set OK.\n")
printOUTContainer(tabwriter.NewWriter(stdout, 10, 0, 2, ' ', 0), container)
}