Skip to content

Commit

Permalink
feat(3.2.0): 优化跨云安装k3s
Browse files Browse the repository at this point in the history
优化跨云安装k3s

Signed-off-by: ysicing <i@ysicing.me>
  • Loading branch information
ysicing committed Mar 19, 2022
1 parent 441fe66 commit 868f58a
Show file tree
Hide file tree
Showing 70 changed files with 410 additions and 570 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ergo CHANGELOG

- v3.1.5
- v3.2.0
- [x] 支持批量执行shell
- [x] 优化k3s安装

- v3.1.4
Expand Down
1 change: 0 additions & 1 deletion cmd/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,5 @@ func newDebianCmd(f factory.Factory) *cobra.Command {
debian.PersistentFlags().StringVar(&opt.SSHCfg.PkFile, "pk", "", "私钥")
debian.PersistentFlags().StringVar(&opt.SSHCfg.PkPass, "pkpass", "", "私钥密码")
debian.PersistentFlags().StringSliceVar(&opt.IPs, "ip", nil, "机器IP")
debian.PersistentFlags().BoolVar(&opt.Local, "local", false, "本地安装")
return debian
}
86 changes: 40 additions & 46 deletions cmd/debian/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package debian

import (
"fmt"
"sync"

"github.com/ergoapi/util/exnet"
"github.com/ergoapi/util/file"
"github.com/ysicing/ergo/cmd/flags"
"github.com/ysicing/ergo/pkg/ergo/debian"
Expand All @@ -16,86 +16,80 @@ import (

type Option struct {
*flags.GlobalFlags
// log log.Logger
Local bool
SSHCfg sshutil.SSH
IPs []string
}

func (cmd *Option) prepare() {
func (cmd *Option) prepare(f factory.Factory) {
address, _ := exnet.IsLocalHostAddrs()
cmd.SSHCfg.LocalAddress = address
if len(cmd.IPs) == 0 {
cmd.Local = true
cmd.IPs = append(cmd.IPs, "127.0.0.1")
}
cmd.SSHCfg.Log = f.GetLog()
}

func (cmd *Option) Init(f factory.Factory) error {
cmd.prepare()
cmd.SSHCfg.Log = f.GetLog()
if cmd.Local {
debian.RunLocalShell("init", cmd.SSHCfg.Log)
return nil
}
cmd.prepare(f)
var wg sync.WaitGroup
for _, ip := range cmd.IPs {
wg.Add(1)
go debian.RunInit(cmd.SSHCfg, ip, &wg)
if exnet.IsLocalIP(ip, cmd.SSHCfg.LocalAddress) || cmd.IPs[0] == "127.0.0.1" {
debian.RunLocalShell("init", cmd.SSHCfg.Log)
} else {
wg.Add(1)
go debian.RunInit(cmd.SSHCfg, ip, &wg)
}
}
wg.Wait()
return nil
}

func (cmd *Option) UpCore(f factory.Factory) error {
cmd.prepare()
cmd.SSHCfg.Log = f.GetLog()
// 本地
if cmd.Local {
debian.RunLocalShell("upcore", cmd.SSHCfg.Log)
return nil
}
cmd.prepare(f)
var wg sync.WaitGroup
for _, ip := range cmd.IPs {
wg.Add(1)
go debian.RunUpgradeCore(cmd.SSHCfg, ip, &wg)
if exnet.IsLocalIP(ip, cmd.SSHCfg.LocalAddress) || cmd.IPs[0] == "127.0.0.1" {
debian.RunLocalShell("upcore", cmd.SSHCfg.Log)
} else {
wg.Add(1)
go debian.RunUpgradeCore(cmd.SSHCfg, ip, &wg)
}
}
wg.Wait()
return nil
}

func (cmd *Option) Apt(f factory.Factory) error {
cmd.prepare()
cmd.SSHCfg.Log = f.GetLog()
// 本地
if cmd.Local {
if file.CheckFileExists("/etc/apt/sources.list") {
debian.RunLocalShell("apt", cmd.SSHCfg.Log)
return nil
}
return fmt.Errorf("仅支持Debian系")
}
cmd.prepare(f)
var wg sync.WaitGroup
for _, ip := range cmd.IPs {
wg.Add(1)
go debian.RunAddDebSource(cmd.SSHCfg, ip, &wg)
if exnet.IsLocalIP(ip, cmd.SSHCfg.LocalAddress) || cmd.IPs[0] == "127.0.0.1" {
if file.CheckFileExists("/etc/apt/sources.list") {
debian.RunLocalShell("apt", cmd.SSHCfg.Log)
}
cmd.SSHCfg.Log.Warn("仅支持Debian系")
} else {
wg.Add(1)
go debian.RunAddDebSource(cmd.SSHCfg, ip, &wg)
}
}
wg.Wait()
return nil
}

func (cmd *Option) Swap(f factory.Factory) error {
cmd.prepare()
cmd.SSHCfg.Log = f.GetLog()
// 本地
if cmd.Local {
if file.CheckFileExists("/etc/apt/sources.list") {
debian.RunLocalShell("swap", cmd.SSHCfg.Log)
return nil
}
return fmt.Errorf("仅支持Debian系")
}
cmd.prepare(f)
var wg sync.WaitGroup
for _, ip := range cmd.IPs {
wg.Add(1)
go debian.RunAddDebSwap(cmd.SSHCfg, ip, &wg)
if exnet.IsLocalIP(ip, cmd.SSHCfg.LocalAddress) || cmd.IPs[0] == "127.0.0.1" {
if file.CheckFileExists("/etc/apt/sources.list") {
debian.RunLocalShell("swap", cmd.SSHCfg.Log)
}
cmd.SSHCfg.Log.Warn("仅支持Debian系")
} else {
wg.Add(1)
go debian.RunAddDebSwap(cmd.SSHCfg, ip, &wg)
}
}
wg.Wait()
return nil
Expand Down
10 changes: 5 additions & 5 deletions cmd/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/ergoapi/log"
"github.com/ergoapi/util/environ"
"github.com/ergoapi/util/excmd"
"github.com/ergoapi/util/file"
"github.com/ergoapi/util/zos"
"github.com/gosuri/uitable"
Expand All @@ -24,7 +25,6 @@ import (
"github.com/ysicing/ergo/common"
"github.com/ysicing/ergo/pkg/ergo/git/github"
"github.com/ysicing/ergo/pkg/util/factory"
"github.com/ysicing/ergo/pkg/util/ssh"
"helm.sh/helm/v3/pkg/cli/output"
)

Expand Down Expand Up @@ -157,11 +157,11 @@ func (ext *ExtOptions) limaPre(cobraCmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("请先安装brew")
}
err = ssh.RunCmd(brewbin, "update")
err = excmd.RunCmd(brewbin, "update")
if err != nil {
return fmt.Errorf("run: brew update ,err: %v", err)
}
err = ssh.RunCmd(brewbin, "install", "lima")
err = excmd.RunCmd(brewbin, "install", "lima")
if err != nil {
return fmt.Errorf("run: brew install lima ,err: %v", err)
}
Expand Down Expand Up @@ -212,13 +212,13 @@ func (ext *ExtOptions) lima(cobraCmd *cobra.Command, args []string) error {
ext.Log.Warnf("instance lima-ergo already exists")
return nil
}
err := ssh.RunCmd(limabin, "start", limacfg)
err := excmd.RunCmd(limabin, "start", limacfg)
if err != nil {
return fmt.Errorf("limactl start %v ,err: %v", limacfg, err)
}
return nil
}
err = ssh.RunCmd(limabin, args...)
err = excmd.RunCmd(limabin, args...)
if err != nil {
return fmt.Errorf("limactl %v ,err: %v", args[0], err)
}
Expand Down
15 changes: 10 additions & 5 deletions cmd/op/op.go → cmd/op/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"sync"

"github.com/ergoapi/log"
"github.com/ergoapi/util/exnet"
"github.com/spf13/cobra"
"github.com/ysicing/ergo/pkg/ergo/ops/exec"
sshutil "github.com/ysicing/ergo/pkg/util/ssh"
)

type execOption struct {
local bool
sshcfg sshutil.SSH
ips []string
}
Expand All @@ -23,8 +23,10 @@ func ExecCmd() *cobra.Command {
exec := &cobra.Command{
Use: "exec",
Short: "执行命令",
Version: "2.0.0",
Version: "3.2.0",
RunE: func(cobraCmd *cobra.Command, args []string) error {
address, _ := exnet.IsLocalHostAddrs()
cmd.sshcfg.LocalAddress = address
return cmd.Exec(args)
},
}
Expand All @@ -33,19 +35,22 @@ func ExecCmd() *cobra.Command {
exec.PersistentFlags().StringVar(&cmd.sshcfg.PkFile, "pk", "", "私钥")
exec.PersistentFlags().StringVar(&cmd.sshcfg.PkPass, "pkpass", "", "私钥密码")
exec.PersistentFlags().StringSliceVar(&cmd.ips, "ip", nil, "机器IP")
exec.PersistentFlags().BoolVar(&cmd.local, "local", false, "本地安装")
return exec
}

func (cmd *execOption) Exec(args []string) error {
cmd.sshcfg.Log = log.GetInstance()
if cmd.local {
if len(cmd.ips) == 0 {
return exec.LocalRun(args...)
}
var wg sync.WaitGroup
for _, ip := range cmd.ips {
wg.Add(1)
exec.RunSH(cmd.sshcfg, ip, &wg, args...)
if exnet.IsLocalIP(ip, cmd.sshcfg.LocalAddress) {
exec.LocalRun(args...)
} else {
exec.RunSH(cmd.sshcfg, ip, &wg, args...)
}
}
wg.Wait()
return nil
Expand Down
6 changes: 3 additions & 3 deletions cmd/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"os"
"strings"

"github.com/ergoapi/util/excmd"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"github.com/ysicing/ergo/common"
"github.com/ysicing/ergo/pkg/ergo/repo"
"github.com/ysicing/ergo/pkg/util/factory"
"github.com/ysicing/ergo/pkg/util/ssh"
"helm.sh/helm/v3/cmd/helm/require"
"helm.sh/helm/v3/pkg/cli/output"
)
Expand Down Expand Up @@ -113,11 +113,11 @@ func InitCmd(f factory.Factory) *cobra.Command {
Short: "add default repo",
RunE: func(cobraCmd *cobra.Command, args []string) error {
cmdArgs := os.Args
if err := ssh.RunCmd(cmdArgs[0], "repo", "add", common.ErgoOwner, common.DefaultRepoURL); err != nil {
if err := excmd.RunCmd(cmdArgs[0], "repo", "add", common.ErgoOwner, common.DefaultRepoURL); err != nil {
log.Debugf("添加默认库失败: %v", err)
return fmt.Errorf("添加默认库失败")
}
return ssh.RunCmd(cmdArgs[0], "repo", "update")
return excmd.RunCmd(cmdArgs[0], "repo", "update")
},
}
return cmd
Expand Down
2 changes: 1 addition & 1 deletion docs/ergo.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ A simple command line client for devops
* [ergo upgrade](ergo_upgrade.md) - upgrade ergo to the newest version
* [ergo version](ergo_version.md) - show ergo version

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_addons.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Ergo add-ons are components, services, or pieces of infrastructure that are full
* [ergo addons search](ergo_addons_search.md) - search add-ons
* [ergo addons uninstall](ergo_addons_uninstall.md) - uninstall add-ons

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_addons_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ ergo addons install [repo] [name] [flags]

* [ergo addons](ergo_addons.md) - Ergo add-ons are components, services, or pieces of infrastructure that are fully maintained for you, either by a third-party provider or by Ergo

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_addons_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ ergo addons list [flags]

* [ergo addons](ergo_addons.md) - Ergo add-ons are components, services, or pieces of infrastructure that are fully maintained for you, either by a third-party provider or by Ergo

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_addons_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ ergo addons search [flags]

* [ergo addons](ergo_addons.md) - Ergo add-ons are components, services, or pieces of infrastructure that are fully maintained for you, either by a third-party provider or by Ergo

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_addons_uninstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ ergo addons uninstall [repo] [name] [flags]

* [ergo addons](ergo_addons.md) - Ergo add-ons are components, services, or pieces of infrastructure that are fully maintained for you, either by a third-party provider or by Ergo

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_cloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ cloud tools
* [ergo cloud cvm](ergo_cloud_cvm.md) - 开通竞价机器
* [ergo cloud domain](ergo_cloud_domain.md) - domain 域名服务

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_cloud_cr.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ cr 容器镜像服务
* [ergo cloud](ergo_cloud.md) - cloud tools
* [ergo cloud cr list](ergo_cloud_cr_list.md) - 镜像列表

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_cloud_cr_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ ergo cloud cr list [flags]

* [ergo cloud cr](ergo_cloud_cr.md) - cr 容器镜像服务

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_cloud_cvm.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ ergo cloud cvm [flags]

* [ergo cloud](ergo_cloud.md) - cloud tools

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_cloud_domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ domain 域名服务
* [ergo cloud](ergo_cloud.md) - cloud tools
* [ergo cloud domain list](ergo_cloud_domain_list.md) - 域名列表

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
2 changes: 1 addition & 1 deletion docs/ergo_cloud_domain_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ ergo cloud domain list [flags]

* [ergo cloud domain](ergo_cloud_domain.md) - domain 域名服务

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
3 changes: 1 addition & 2 deletions docs/ergo_debian.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ debian tools
```
-h, --help help for debian
--ip strings 机器IP
--local 本地安装
--pass string 密码
--pk string 私钥
--pkpass string 私钥密码
Expand All @@ -30,4 +29,4 @@ debian tools
* [ergo debian swap](ergo_debian_swap.md) - 添加swap
* [ergo debian upcore](ergo_debian_upcore.md) - upgrade debian linux core

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
3 changes: 1 addition & 2 deletions docs/ergo_debian_apt.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ ergo debian apt [flags]
--config string The ergo config file to use (default "/Users/ysicing/.ergo/config/ergo.yml")
--debug Prints the stack trace if an error occurs
--ip strings 机器IP
--local 本地安装
--pass string 密码
--pk string 私钥
--pkpass string 私钥密码
Expand All @@ -30,4 +29,4 @@ ergo debian apt [flags]

* [ergo debian](ergo_debian.md) - debian tools

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
3 changes: 1 addition & 2 deletions docs/ergo_debian_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ ergo debian init [flags]
--config string The ergo config file to use (default "/Users/ysicing/.ergo/config/ergo.yml")
--debug Prints the stack trace if an error occurs
--ip strings 机器IP
--local 本地安装
--pass string 密码
--pk string 私钥
--pkpass string 私钥密码
Expand All @@ -30,4 +29,4 @@ ergo debian init [flags]

* [ergo debian](ergo_debian.md) - debian tools

###### Auto generated by spf13/cobra on 14-Mar-2022
###### Auto generated by spf13/cobra on 19-Mar-2022
Loading

0 comments on commit 868f58a

Please sign in to comment.