Skip to content

Commit

Permalink
添加跨主机执行shell指令
Browse files Browse the repository at this point in the history
  • Loading branch information
weiliang-ms committed Dec 22, 2021
1 parent 04cf3b9 commit 2e5c64b
Show file tree
Hide file tree
Showing 12 changed files with 279 additions and 41 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
[![codecov](https://codecov.io/gh/weiliang-ms/easyctl/branch/master/graph/badge.svg?token=7RGD5V5L9Y)](https://codecov.io/gh/weiliang-ms/easyctl)
![GitHub](https://img.shields.io/github/license/weiliang-ms/easyctl?style=flat-square)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fweiliang-ms%2Feasyctl.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fweiliang-ms%2Feasyctl?ref=badge_shield)
# easyctl

# easyctl

`easyctl`是一款基于`golang`轻量级运维工具集,详情使用请移步[使用文档](https://weiliang-ms.github.io/easyctl/)

[CHANGELOG](changelog/CHANGELOG.md)

兼容性:

- [x] 全部功能兼容`CentOS7`
Expand Down Expand Up @@ -41,8 +44,6 @@ sudo tar zxvf easyctl-latest-linux-amd64.tar.gz
sudo cp easyctl /usr/local/bin
```

- [Windows](https://github.com/weiliang-ms/easyctl/releases/download/latest/easyctl-latest-windows-amd64.zip)

> 下载[tag版本](https://github.com/weiliang-ms/easyctl/tags)
## 迭代计划
Expand All @@ -68,6 +69,8 @@ sudo cp easyctl /usr/local/bin
- [x] [防火墙](https://weiliang-ms.github.io/easyctl/%E7%A6%81%E7%94%A8%E6%8C%87%E4%BB%A4%E9%9B%86/03%E4%B8%BB%E6%9C%BA%E7%A6%81%E7%94%A8%E9%98%B2%E7%81%AB%E5%A2%99.html)
- [x] [ping](https://weiliang-ms.github.io/easyctl/%E7%A6%81%E7%94%A8%E6%8C%87%E4%BB%A4%E9%9B%86/01%E4%B8%BB%E6%9C%BA%E7%A6%81Ping.html)
- [x] [selinux](https://weiliang-ms.github.io/easyctl/%E7%A6%81%E7%94%A8%E6%8C%87%E4%BB%A4%E9%9B%86/02%E4%B8%BB%E6%9C%BA%E7%A6%81%E7%94%A8selinux.html)
- `exec`
- [x] [跨主机并行执行shell]()
- `export`
- [x] [从harbor批量下载chart](https://weiliang-ms.github.io/easyctl/%E5%AF%BC%E5%87%BA%E6%8C%87%E4%BB%A4%E9%9B%86/01%E4%BB%8Eharbor%E5%BA%93%E4%B8%AD%E5%AF%BC%E5%87%BAchart.html)
- [x] [从harbor导出镜像tag列表](https://weiliang-ms.github.io/easyctl/%E5%AF%BC%E5%87%BA%E6%8C%87%E4%BB%A4%E9%9B%86/02%E4%BB%8Eharbor%E5%BA%93%E4%B8%AD%E5%AF%BC%E5%87%BA%E9%95%9C%E5%83%8Ftag%E5%88%97%E8%A1%A8.html)
Expand Down
199 changes: 199 additions & 0 deletions book/执行指令集/01跨主机并行执行shell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
## 跨主机并行执行shell

### 版本&兼容性

> 版本支持
- [v0.7.16-alpha以上](https://github.com/weiliang-ms/easyctl/releases/)

> 兼容性
- [x] `CentOS6`
- [x] `CentOS7`

### 使用方式

> 参考以下链接进行安装
- [安装说明文档](../-安装文档/README.md)

> 生成默认配置文件
```shell
$ easyctl exec shell
INFO[0000] 生成配置文件样例, 请携带 -c 参数重新执行 -> config.yaml
```

> 修改配置文件
`config.yaml`, 修改主机列表。`easyctl`根据主机列表`ssh`远程至目标主机执行`shell`

```yaml
server:
# - host: 10.79.165.[11:13]
# username: root
# password: neusoft
# port: 22
# - host: 10.79.164.[61:63]
# username: root
# password: neusoft
# port: 22
- host: 10.10.10.[1:3]
username: root
privateKeyPath: ~/.ssh/id_rsa
password: ""
port: 22
excludes:
- 192.168.235.132
script: "1.sh"
```
> 执行
添加`--debug`可以输出详细内容。

```shell
$ easyctl exec shell -c config.yaml --debug
```

### 配置项说明

- 主机配置段:该段配置远程执行`shell`的主机信息,字段说明如下
- `host: 10.10.10.[1:3]` 主机地址段,适用于`ip`连续场景。分隔符可以为`[1:3]`、`1-2`、`[1-2]`、`1:2`
- `username`: 远程主机`ssh`用户名称,缺省值为`root`
- `password`: 对应`username`的密码
- `privateKeyPath`: `ssh`私钥路径
- `port`: `ssh`端口,默认`22`
- `excludes`: 排除`host`地址段内的`ip`地址列表

`privateKeyPath`优先级高于`password`:

1. `privateKeyPath`为空,取`password`值,`ssh`使用密码登录方式
2. `privateKeyPath`非空,取`privateKeyPath`值,`ssh`使用密钥登录方式

```yaml
server:
# - host: 10.79.165.[11:13]
# username: root
# password: neusoft
# port: 22
# - host: 10.79.164.[61:63]
# username: root
# password: neusoft
# port: 22
- host: 10.10.10.[1:3]
username: root
privateKeyPath: ~/.ssh/id_rsa
password: ""
port: 22
excludes:
- 192.168.235.132
```

- 脚本配置:
- `script: "date"`: 远程执行的`shell`指令,适用于运行单个`shell`指令场景
- `script: "1.sh"`: 远程执行的`shell`脚本,适用于运行多个`shell`指令场景

### 配置样例

> 1.主机: `10.10.10.1-10.10.10.10`执行`date`指令,使用密钥登录方式

```yaml
server:
- host: 10.10.10.[1:10]
username: root
privateKeyPath: ~/.ssh/id_rsa
password: ""
port: 22
excludes:
- 192.168.235.132
script: "date"
```

> 2.主机: `10.10.10.1-10.10.10.10`执行`date`指令,使用密码登录方式

```yaml
server:
- host: 10.10.10.[1:10]
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123456"
port: 22
excludes:
- 192.168.235.132
script: "date"
```

> 3.主机: `10.10.10.1`、`10.10.10.3`、`10.10.10.4`执行`date`指令,使用密码登录方式

```yaml
server:
- host: 10.10.10.[1:4]
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123456"
port: 22
excludes:
- 10.10.10.2
script: "date"
```

> 4.主机: `10.10.10.1`、`10.10.10.3`、`10.10.10.4`执行`shell`脚本,使用密码登录方式

```yaml
server:
- host: 10.10.10.[1:4]
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123456"
port: 22
excludes:
- 10.10.10.2
script: "/root/modify-sysctl.sh"
```

`/root/modify-sysctl.sh`脚本内容如下

```shell
#!/bin/bash
sed -i '/vm.dirty_background_ratio/d' /etc/sysctl.conf
sed -i '/vm.dirty_ratio/d' /etc/sysctl.conf
echo "vm.dirty_ratio=10" >> /etc/sysctl.conf
echo "vm.dirty_background_ratio=5" >> /etc/sysctl.conf
sysctl -p
```

> 5.主机: `10.10.10.1`、`10.10.10.3`、`10.10.10.4`执行`shell`脚本,使用密码登录方式,且`ssh`端口及密码均不一致

```yaml
server:
- host: 10.10.10.1
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123456"
port: 22
- host: 10.10.10.3
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "123"
port: 22122
- host: 10.10.10.4
username: root
# privateKeyPath: ~/.ssh/id_rsa
password: "456"
port: 22222
excludes:
- 10.10.10.2
script: "/root/modify-sysctl.sh"
```

`/root/modify-sysctl.sh`脚本内容如下

```shell
#!/bin/bash
sed -i '/vm.dirty_background_ratio/d' /etc/sysctl.conf
sed -i '/vm.dirty_ratio/d' /etc/sysctl.conf
echo "vm.dirty_ratio=10" >> /etc/sysctl.conf
echo "vm.dirty_background_ratio=5" >> /etc/sysctl.conf
sysctl -p
```
2 changes: 1 addition & 1 deletion book/清理指令集/01多主机redis清理.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

> 参考以下链接进行安装
- [安装说明文档](../安装文档/README.md)
- [安装说明文档](../-安装文档/README.md)

> 生成默认配置文件
Expand Down
2 changes: 1 addition & 1 deletion book/清理指令集/02多主机dns配置清理.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

> 参考以下链接进行安装
- [安装说明文档](../安装文档/README.md)
- [安装说明文档](../-安装文档/README.md)

> 生成默认配置文件
Expand Down
2 changes: 2 additions & 0 deletions changelog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- `v0.7.16-alpha` `2021-12-22 08:28:00`
- 添加`exec`指令集子指令`shell`
- `v0.7.15-alpha` `2021-11-05 11:28:00`
- 添加`install`指令集子指令`docker-ce`
- `v0.7.14-alpha` `2021-10-26 17:00:00`
Expand Down
13 changes: 6 additions & 7 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ var (
)

//go:embed asset/executor.yaml
var config []byte

func init() {
RootCmd.Flags().StringVarP(&configFile, "config", "c", "", "配置文件")
}
var configByte []byte

// RootCmd close命令
var RootCmd = &cobra.Command{
Use: "exec [flags]",
Short: "执行命令指令集",
Example: "\neasyctl exec -c config.yaml",
Run: func(cmd *cobra.Command, args []string) {
Args: cobra.ExactValidArgs(1),
}

},
func init() {
RootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "配置文件")
RootCmd.AddCommand(shellCmd)
}
21 changes: 21 additions & 0 deletions cmd/exec/shell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package exec

import (
"github.com/spf13/cobra"
"github.com/weiliang-ms/easyctl/pkg/exec"
"github.com/weiliang-ms/easyctl/pkg/util/command"
)

// RootCmd close命令
var shellCmd = &cobra.Command{
Use: "shell [flags]",
Short: "执行命令指令集",
Example: "\neasyctl exec -c config.yaml",
Run: func(cmd *cobra.Command, args []string) {
if runErr := command.SetExecutorDefault(
command.Item{Cmd: cmd, Fnc: exec.Run, DefaultConfig: configByte, ConfigFilePath: configFile}); runErr.Err != nil {
command.DefaultLogger.Errorln(runErr.Msg)
panic(runErr.Err)
}
},
}
11 changes: 2 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/weiliang-ms/easyctl/cmd/boot"
"github.com/weiliang-ms/easyctl/cmd/clean"
"github.com/weiliang-ms/easyctl/cmd/deny"
"github.com/weiliang-ms/easyctl/cmd/exec"
"github.com/weiliang-ms/easyctl/cmd/export"
"github.com/weiliang-ms/easyctl/cmd/harden"
"github.com/weiliang-ms/easyctl/cmd/install"
Expand Down Expand Up @@ -50,6 +51,7 @@ func init() {
boot.RootCmd,
clean.RootCmd,
deny.RootCmd,
exec.RootCmd,
set.RootCmd,
install.RootCmd,
upgrade.Cmd,
Expand All @@ -65,15 +67,6 @@ func init() {
RootCmd.AddCommand(cmd)
}

//header := &doc.GenManHeader{
// Title: "MINE",
// Section: "3",
//}
//err := doc.GenManTree(RootCmd, header, "/tmp")
//if err != nil {
// log.Fatal(err)
//}

}

func main() {
Expand Down
15 changes: 15 additions & 0 deletions pkg/exec/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package exec

import (
"github.com/weiliang-ms/easyctl/pkg/runner"
"github.com/weiliang-ms/easyctl/pkg/util/command"
)

// Run 执行指令
func Run(item command.OperationItem) command.RunErr {
executor, err := runner.ParseExecutor(item.B, item.Logger)
if err != nil {
return command.RunErr{Err: err}
}
return runner.RemoteRun(item.B, item.Logger, executor.Script)
}
Loading

0 comments on commit 2e5c64b

Please sign in to comment.