-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcloud.go
349 lines (332 loc) · 9.41 KB
/
cloud.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright (c) 2021 ysicing <i@ysicing.me>
*/
package cmd
import (
"context"
"fmt"
"os"
"strings"
"github.com/ergoapi/util/zos"
"github.com/ysicing/ergo/cmd/flags"
"github.com/ysicing/ergo/pkg/config"
"github.com/ergoapi/log"
"github.com/ergoapi/util/file"
"github.com/gosuri/uitable"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/ysicing/ergo/common"
"github.com/ysicing/ergo/pkg/ergo/cloud"
"github.com/ysicing/ergo/pkg/ergo/cloud/aliyun"
"github.com/ysicing/ergo/pkg/ergo/cloud/qcloud"
"github.com/ysicing/ergo/pkg/util/factory"
"helm.sh/helm/v3/pkg/cli/output"
"sigs.k8s.io/yaml"
)
// NewCloudCommand 云服务商支持
func newCloudCommand(f factory.Factory) *cobra.Command {
cloud := &cobra.Command{
Use: "cloud [flags]",
Short: "cloud tools",
}
cloud.AddCommand(newCvmCmd(f))
cloud.AddCommand(newCloudDomain(f))
cloud.AddCommand(newCloudCR(f))
return cloud
}
func newCloudDomain(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "domain [flags]",
Short: "domain 域名服务",
}
cmd.AddCommand(newCloudDomainList(f))
return cmd
}
func newCloudDomainList(f factory.Factory) *cobra.Command {
l := f.GetLog()
cmd := &cobra.Command{
Use: "list",
Short: "域名列表",
RunE: func(cobraCmd *cobra.Command, args []string) error {
ergocfg, err := config.LoadYaml(common.GetDefaultErgoCfg())
if err != nil {
return err
}
templates := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "\U0001F449 {{ .Provider | cyan }}",
Inactive: " {{ .Provider | cyan }}",
Selected: "\U0001F389 {{ .Provider | red | cyan }}",
}
cloudprompt := promptui.Select{
Label: "选择云服务商",
Items: ergocfg.Cloud,
Size: 4,
Templates: templates,
}
selectid, _, _ := cloudprompt.Run()
ct := ergocfg.Cloud[selectid]
var domainlist cloud.DomainList
if ct.Provider == cloud.ProviderQcloud.Value() {
l.Debugf("load qcloud domain")
// 腾讯云
p, err := qcloud.NewDNS(qcloud.WithLog(l), qcloud.WithAPI(ct.Secrets.AID, ct.Secrets.AKey))
if err != nil {
l.Errorf("create qcloud api client err: %v", err)
} else {
pd, err := p.DomainList(context.Background())
if err == nil {
domainlist = append(domainlist, pd...)
} else {
l.Errorf("do qcloud dns api err: %v", err)
}
}
}
if ct.Provider == cloud.ProviderAliyun.Value() {
l.Debugf("load aliyun domain")
// 阿里云
p, err := aliyun.NewDNS(aliyun.WithLog(l), aliyun.WithAPI(ct.Secrets.AID, ct.Secrets.AKey))
if err != nil {
l.Errorf("create aliyun api client err: %v", err)
} else {
pd, err := p.DomainList(context.Background())
if err == nil {
domainlist = append(domainlist, pd...)
} else {
l.Errorf("do aliyun dns api err: %v", err)
}
}
}
table := uitable.New()
table.AddRow("服务商", "域名")
for _, re := range domainlist {
table.AddRow(re.Provider, re.Name)
}
_ = output.EncodeTable(os.Stdout, table)
domainfile := fmt.Sprintf("%v/.domain", common.GetDefaultCacheDir())
if file.CheckFileExists(domainfile) {
file.RemoveFiles(domainfile)
}
resp, _ := yaml.Marshal(domainlist)
_ = file.Writefile(domainfile, string(resp))
l.Donef("域名缓存成功: %v", domainfile)
return nil
},
}
return cmd
}
func newCloudCR(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "cr [flags]",
Short: "cr 容器镜像服务",
}
cmd.AddCommand(newCRList(f))
return cmd
}
func newCRList(f factory.Factory) *cobra.Command {
l := f.GetLog()
cmd := &cobra.Command{
Use: "list",
Short: "镜像列表",
RunE: func(cobraCmd *cobra.Command, args []string) error {
ergocfg, err := config.LoadYaml(common.GetDefaultErgoCfg())
if err != nil {
return err
}
templates := &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "\U0001F449 {{ .Provider | cyan }}",
Inactive: " {{ .Provider | cyan }}",
Selected: "\U0001F389 {{ .Provider | red | cyan }}",
}
cloudprompt := promptui.Select{
Label: "选择云服务商",
Items: ergocfg.Cloud,
Size: 4,
Templates: templates,
}
selectid, _, _ := cloudprompt.Run()
ct := ergocfg.Cloud[selectid]
var crlist cloud.CRList
if ct.Provider == cloud.ProviderQcloud.Value() {
l.StartWait("开始加载腾讯云镜像")
// 腾讯云
p, err := qcloud.NewTCR(qcloud.WithLog(l), qcloud.WithRegion("ap-beijing"), qcloud.WithAPI(ct.Secrets.AID, ct.Secrets.AKey))
if err != nil {
l.Errorf("create qcloud api client err: %v", err)
} else {
pcr, err := p.ListRepo(context.Background())
l.StopWait()
if err == nil {
crlist = append(crlist, pcr...)
l.Done("加载腾讯云镜像完成")
} else {
l.Errorf("do qcloud tcr api err: %v", err)
}
}
}
if ct.Provider == cloud.ProviderAliyun.Value() {
// 阿里云
l.StartWait("开始加载阿里云镜像")
p, err := aliyun.NewACR(aliyun.WithLog(l), aliyun.WithRegion("cn-beijing"), aliyun.WithAPI(ct.Secrets.AID, ct.Secrets.AKey))
if err != nil {
l.Errorf("create aliyun api client err: %v", err)
} else {
pcr, err := p.ListRepo(context.Background())
l.StopWait()
if err == nil {
crlist = append(crlist, pcr...)
l.Done("加载阿里云镜像完成")
} else {
l.Errorf("do aliyun acr api err: %v", err)
}
}
}
table := uitable.New()
table.AddRow("服务商", "Name", "命名空间", "镜像地址", "版本数", "更新时间")
for _, re := range crlist {
table.AddRow(re.Provider, re.Name, re.Namespace, fmt.Sprintf("%v/%v", re.Server, re.RepoName), len(re.Tags), re.UpdateTime)
}
l.Done("镜像获取成功")
output.EncodeTable(os.Stdout, table)
return nil
},
}
return cmd
}
type CvmOption struct {
*flags.GlobalFlags
log log.Logger
action string
}
// newCvmCmd ergo cvm
func newCvmCmd(f factory.Factory) *cobra.Command {
opt := &CvmOption{
GlobalFlags: globalFlags,
log: f.GetLog(),
}
cvm := &cobra.Command{
Use: "cvm [flags]",
Short: "开通竞价机器",
Version: "2.0.7",
RunE: func(cobraCmd *cobra.Command, args []string) error {
return opt.Run()
},
}
cvm.PersistentFlags().StringVarP(&opt.action, "action", "a", "", "操作 \"create\",\"new\",\"add\",\"destroy\",\"del\",\"rm\",\"list\" ")
return cvm
}
func (c *CvmOption) Run() error {
var aid, akey, provider, region string
ergocfg, err := config.LoadYaml(common.GetDefaultErgoCfg())
if err != nil {
return err
}
if len(ergocfg.Cloud) == 0 {
// 不存在
c.log.Debug("not found cloud provider, will gen one")
newprovider := addProvider()
ergocfg.Cloud = append(ergocfg.Cloud, newprovider)
ergocfg.Dump()
provider = newprovider.Provider
aid = newprovider.Secrets.AID
akey = newprovider.Secrets.AKey
if len(newprovider.Regions) > 0 {
region = newprovider.Regions[0]
}
} else {
// 存在
selectitem := append(ergocfg.Cloud, config.Provider{
Provider: "new",
})
c.log.Debugf("加载配置成功: %v", selectitem)
ps := promptui.Select{
Label: "选择凭证",
Items: selectitem,
Size: 4,
Templates: &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "\U0001F449 {{ .Provider | cyan }}",
Inactive: " {{ .Provider | cyan }}",
Selected: "\U0001F389 {{ .Provider | red | cyan }}",
},
}
psid, _, _ := ps.Run()
if selectitem[psid].Provider == "new" {
newprovider := addProvider()
ergocfg.Cloud = append(ergocfg.Cloud, newprovider)
ergocfg.Dump()
provider = newprovider.Provider
aid = newprovider.Secrets.AID
akey = newprovider.Secrets.AKey
if len(newprovider.Regions) > 0 {
region = newprovider.Regions[0]
}
} else {
provider = selectitem[psid].Provider
aid = selectitem[psid].Secrets.AID
akey = selectitem[psid].Secrets.AKey
if len(selectitem[psid].Regions) > 0 {
region = selectitem[psid].Regions[0]
}
}
}
if provider == cloud.ProviderQcloud.Value() {
// 腾讯云默认南京地域
if !strings.HasPrefix(region, "ap") {
region = "ap-nanjing"
}
p, err := qcloud.NewCvm(qcloud.WithLog(c.log), qcloud.WithAPI(aid, akey), qcloud.WithRegion(region))
if err != nil {
return err
}
switch c.action {
case "status":
return p.Status(context.TODO(), cloud.StatusOption{})
case "create", "new", "add":
return p.Create(context.TODO(), cloud.CreateOption{})
case "destroy", "del", "rm":
return p.Destroy(context.TODO(), cloud.DestroyOption{})
case "halt":
return p.Halt(context.TODO(), cloud.HaltOption{})
case "up":
return p.Up(context.TODO(), cloud.UpOption{})
case "snapshot":
return p.Snapshot(context.TODO(), cloud.SnapshotOption{})
default:
return p.List(context.TODO(), cloud.ListOption{})
}
}
return nil
}
func addProvider() config.Provider {
var provider, aid, akey, region string
pprompt := promptui.Prompt{
Label: "provider",
}
provider, _ = pprompt.Run()
aidprompt := promptui.Prompt{
Label: "aid",
}
aid, _ = aidprompt.Run()
akeyprompt := promptui.Prompt{
Label: "akey",
}
akey, _ = akeyprompt.Run()
regionprompt := promptui.Prompt{
Label: "region",
}
cfg := config.Provider{
UUID: zos.GenUUID(),
Provider: strings.Trim(provider, " "),
Secrets: config.Secrets{
AID: strings.Trim(aid, " "),
AKey: strings.Trim(akey, ""),
},
}
region, _ = regionprompt.Run()
if len(region) != 0 {
cfg.Regions = []string{strings.Trim(region, " ")}
}
return cfg
}