-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcvm.go
174 lines (168 loc) · 4.48 KB
/
cvm.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
/*
* Copyright (c) 2021 ysicing <i@ysicing.me>
*/
package cmd
import (
"context"
"github.com/ergoapi/util/file"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/ysicing/ergo/cmd/flags"
"github.com/ysicing/ergo/common"
"github.com/ysicing/ergo/pkg/ergo/cloud"
"github.com/ysicing/ergo/pkg/ergo/cloud/qcloud"
"github.com/ysicing/ergo/pkg/util/factory"
"github.com/ysicing/ergo/pkg/util/log"
"github.com/ysicing/ergo/pkg/util/ssh"
"os"
"strings"
)
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.Init()
},
}
cvm.PersistentFlags().StringVarP(&opt.action, "action", "a", "", "操作 \"create\",\"new\",\"add\",\"destroy\",\"del\",\"rm\",\"list\" ")
return cvm
}
func (c *CvmOption) Init() error {
var aid, akey, provider, region string
cvmfile := common.GetDefaultCfgPathByName("cloud")
c.log.Debugf("load cloud cfg: %v", cvmfile)
if !file.CheckFileExists(cvmfile) {
c.log.Debugf("not found %v, will gen one", cvmfile)
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",
}
region, _ = regionprompt.Run()
c.log.Debugf("%v, %v, %v, %v", aid, akey, provider, region)
configs := cloud.Configs{}
configs.Add(cloud.Config{
Provider: provider,
Secrets: cloud.Secrets{
AID: aid,
AKey: akey,
},
Regions: []string{region},
})
if err := configs.Save(cvmfile); err != nil {
return err
}
return nil
} else {
configs, err := cloud.LoadCloudConfigs(cvmfile)
if err != nil {
return err
}
selectitem := append(configs.Configs, cloud.Config{
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" {
c.log.Debugf("found %v, will add one", cvmfile)
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",
}
region, _ = regionprompt.Run()
c.log.Debugf("%v, %v, %v, %v", aid, akey, provider, region)
configs.Add(cloud.Config{
Provider: provider,
Secrets: cloud.Secrets{
AID: aid,
AKey: akey,
},
Regions: []string{region},
})
if err := configs.Save(cvmfile); err != nil {
return err
}
args := os.Args
return ssh.RunCmd(args[0], "cvm")
}
c.log.Debugf("select %v", selectitem[psid])
if len(selectitem[psid].Regions) != 0 {
region = selectitem[psid].Regions[0]
}
provider = selectitem[psid].Provider
aid = selectitem[psid].Secrets.AID
akey = selectitem[psid].Secrets.AKey
}
// c.log.Debugf("%v %v , %v %v", c.action, provider, cloud.ProviderQcloud.Value(), provider == cloud.ProviderQcloud.Value())
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
}