Skip to content

Commit

Permalink
feat: 新增命令行执行远程运维功能
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Jul 31, 2023
1 parent 23fa4eb commit 3051cdd
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
3. 对批量远端节点执行shell命令
- BatchRunRemoteNodeFromConfig
- BatchRunRemoteNodeFromConfigWithTimeout
4. 支持命令行执行远程运维动作
- 可以将项目二进制编译更方便使用
```yaml
remoteNodes:
- host: 127.0.0.1
Expand Down Expand Up @@ -81,4 +83,35 @@ func main() {
fmt.Println("===============================")

}
```

### 示例2
**使用方法**
```bash
➜ shell_extender git:(main) ✗ go run main.go remoteExec --user=root --password=<password> --host=<host> --port=22 --script=./script.sh
host ip exec result: 1.14.120.233
NAME READY STATUS RESTARTS AGE
ddd-55c668c8ff-v45lw 1/1 Running 0 197d
example-deployment-658789c5cd-l4kwp 1/1 Running 0 56d
example-deployment-658789c5cd-qkn8d 1/1 Running 1 104d
example-pod 1/1 Running 3 104d
jiangjiang-76fb44d88-pdzqv 1/1 Running 1 261d
k8splay1-59c7f5b4cb-mbw5v 2/2 Running 6 263d
k8splay1-59c7f5b4cb-x964b 2/2 Running 6 263d
kkkk-58cb7984db-mmffd 1/1 Running 1 197d
my-deployment-5966cb4d75-cgm8f 1/1 Running 0 56d
my-deployment-5966cb4d75-df58x 1/1 Running 1 113d
my-deployment-5966cb4d75-gcg5w 1/1 Running 1 113d
myapp-rs-ftv7x 1/1 Running 0 56d
myapp-rs-kr487 1/1 Running 1 289d
mycrd-controller-78b98dcd7-hls78 1/1 Running 1 199d
mycrd-controller-78b98dcd7-n7ctv 1/1 Running 1 197d
mycsi-nginx-64c7d9cb77-mj7q7 1/1 Running 0 61d
mypod 1/1 Running 0 113d
myredis-0 1/1 Running 0 56d
myredis-1 1/1 Running 2 233d
nfscsi-nginx-768ff5bf55-jjfwx 1/1 Running 0 56d
ss-web-0 1/1 Running 0 56d
ss-web-1 1/1 Running 1 289d

```
37 changes: 37 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"
)

var runCmd = &cobra.Command{
Use: "run",
Short: "opentelemetry-test-server",
Long: "",
}

var (
host string
user string
password string
port int
script string
)

func init() {
runCmd.PersistentFlags().StringVarP(&host, "host", "i", "", "host ip")
runCmd.PersistentFlags().StringVarP(&user, "user", "u", "root", "remote user name")
runCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "remote user password")
runCmd.PersistentFlags().IntVarP(&port, "port", "P", 22, "remote user password")
runCmd.PersistentFlags().StringVarP(&script, "script", "s", "", "bash shell script")
runCmd.AddCommand(remoteExecShellCmd())
}

func Execute() {
if err := runCmd.Execute(); err != nil {
fmt.Printf("cmd err: %s\n", err)
os.Exit(1)
}
}
8 changes: 8 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cmd

type config struct {
host string
user string
password string
port int
}
38 changes: 38 additions & 0 deletions cmd/remote_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"fmt"
"github.com/practice/shell_extender/pkg/remote_command"
"github.com/spf13/cobra"
"io/ioutil"
)

func remoteExecShellCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "remoteExec",
Short: "exec shell script for remote server",
Long: "exec shell script for remote server",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := &config{
host: host,
user: user,
password: password,
port: port,
}
err := remote_command.RunRemoteNode(cfg.user, cfg.password, cfg.host, cfg.port, readFile(script))
return err
},
}
return cmd
}

func readFile(path string) string {
// 读取文件内容
data, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("read file error:", err)
return ""
}

return string(data)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ go 1.18

require (
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/spf13/cobra v1.7.0
golang.org/x/crypto v0.11.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.10.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
Expand All @@ -15,3 +23,4 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/practice/shell_extender/cmd"

func main() {
cmd.Execute()
}
1 change: 1 addition & 0 deletions script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kubectl get pods

0 comments on commit 3051cdd

Please sign in to comment.