Skip to content

Commit

Permalink
Merge pull request #19 from ysicing/feat-207
Browse files Browse the repository at this point in the history
Feat 207
  • Loading branch information
ysicing authored Oct 18, 2021
2 parents a38e3a8 + 34698eb commit 7895b07
Show file tree
Hide file tree
Showing 21 changed files with 598 additions and 55 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Ergo CHANGELOG

- v2.0.7
- 新增cvm命令创建竞价云服务器机器, 目前支持创建销毁2核4G的腾讯云Debian10(默认南京1区), 目前腾讯云最便宜

- v2.0.6
- ops添加ping子命令

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ ergo version

## 命令支持

- [x] cloud云服务商支持
- [ ] `dns`
- [ ] `domain`
- [ ] cvm 腾讯云开临时测试机器
- [x] `create` / `new` / `add`
- [x] `destroy` / `del` / `rm`
- [ ] `snapshot`
- [ ] `status`
- [ ] `halt`
- [ ] `up`
- [x] `ls` / `list`
- [x] code 初始化项目
- [x] completion
- [x] debian
Expand Down
174 changes: 174 additions & 0 deletions cmd/cvm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
3 changes: 1 addition & 2 deletions cmd/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,14 @@ func newOPSCmd(f factory.Factory) *cobra.Command {
Use: "ping",
Short: "ping",
Version: "2.0.6",
Args: require.MinimumNArgs(1),
Args: require.MinimumNArgs(1),
RunE: func(cobraCmd *cobra.Command, args []string) error {
return cmd.ping(args[0], pingcount)
},
}

pingcmd.PersistentFlags().IntVar(&pingcount, "c", 4, "ping count")


ops.AddCommand(pscmd)
ops.AddCommand(ncCmd(cmd))
ops.AddCommand(execCmd(cmd))
Expand Down
2 changes: 1 addition & 1 deletion cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewCmdPluginInstall(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "install [Repo] [Name]",
Short: "install plugin",
Long: `ergo install repo name or ergo install repo/name`,
Long: `ergo install repo name or ergo install repo/name`,
Aliases: []string{"i"},
Args: cobra.RangeArgs(1, 2),
RunE: func(cobraCmd *cobra.Command, args []string) error {
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func BuildRoot(f factory.Factory) *cobra.Command {
rootCmd.AddCommand(newCodeGenCmd(f))
rootCmd.AddCommand(newCloudCommand(f))
rootCmd.AddCommand(newSecCmd(f))
rootCmd.AddCommand(newCvmCmd(f))
// Add plugin commands

args := os.Args
Expand Down
6 changes: 3 additions & 3 deletions cmd/sec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func newSecCmd(f factory.Factory) *cobra.Command {

func newDeny(f factory.Factory) *cobra.Command {
deny := &cobra.Command{
Use: "deny [OPTIONS] [flags]",
Use: "deny [OPTIONS] [flags]",
Short: "deny sm",
Args: cobra.ExactValidArgs(1),
}
deny.AddCommand(denyIP(f))
return deny
}

func denyIP(f factory.Factory) *cobra.Command {
func denyIP(f factory.Factory) *cobra.Command {
denyPingCmd := &cobra.Command{
Use: "banip [flags]",
Short: "禁ip",
Expand All @@ -50,4 +50,4 @@ func denyIP(f factory.Factory) *cobra.Command {
},
}
return denyPingCmd
}
}
5 changes: 5 additions & 0 deletions common/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ func GetDefaultCfgDir() string {
func GetDefaultPluginRepoCfg() string {
return fmt.Sprintf("%v/plugin.repo.yaml", GetDefaultCfgDir())
}

// GetDefaultCfgPathByName 配置文件名
func GetDefaultCfgPathByName(name string) string {
return fmt.Sprintf("%v/%v.yml", GetDefaultCfgDir(), name)
}
6 changes: 3 additions & 3 deletions common/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ const (

const (
PluginRepoRemoteMode = "remote"
PluginRepoLocalMode = "local"
PluginGithubJiasu = "https://github.techoc.workers.dev"
)
PluginRepoLocalMode = "local"
PluginGithubJiasu = "https://github.techoc.workers.dev"
)
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.16

require (
github.com/ProtonMail/go-crypto v0.0.0-20210920160938-87db9fbc61c7 // indirect
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1016
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1290
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/blang/semver v3.5.1+incompatible
github.com/ergoapi/util v0.0.8
github.com/fatih/color v1.13.0 // indirect
github.com/go-git/go-git/v5 v5.4.2
github.com/go-logr/logr v1.1.0 // indirect
github.com/go-ping/ping v0.0.0-20211014180314-6e2b003bffdd // indirect
github.com/go-ping/ping v0.0.0-20211014180314-6e2b003bffdd
github.com/gofrs/flock v0.8.1
github.com/google/gofuzz v1.2.0 // indirect
github.com/gopasspw/gopass v1.12.8
Expand All @@ -34,10 +34,11 @@ require (
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.8.1
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.268
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.222
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.268
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.222
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.270
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.270
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.270
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.270
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.270
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/wangle201210/githubapi v0.0.0-20200804144924-cde7bbdc36ab
github.com/xanzy/ssh-agent v0.3.1 // indirect
Expand All @@ -48,7 +49,7 @@ require (
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
helm.sh/helm/v3 v3.7.0
helm.sh/helm/v3 v3.7.1
k8s.io/apimachinery v0.22.2
k8s.io/klog/v2 v2.20.0 // indirect
k8s.io/kubectl v0.22.2
Expand Down
Loading

0 comments on commit 7895b07

Please sign in to comment.