/
exec.go
140 lines (119 loc) · 3.56 KB
/
exec.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
// Copyright 2023 Huawei Cloud Computing Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package operation
import (
"fmt"
"github.com/openGemini/gemix/pkg/cluster/config"
"github.com/openGemini/gemix/utils"
"golang.org/x/crypto/ssh"
)
type Executor interface {
ExecRunAction(action *RunAction, errChan chan error) string
ExecStopAction(action *StopAction) (string, error)
ExecCommand(ip string, command string) (string, error)
}
type GeminiExecutor struct {
sshClients map[string]*ssh.Client
}
func NewGeminiExecutor(clients map[string]*ssh.Client) Executor {
new := &GeminiExecutor{
sshClients: clients,
}
return new
}
func (e *GeminiExecutor) ExecCommand(ip string, command string) (string, error) {
sshClient := e.sshClients[ip]
if sshClient == nil {
fmt.Printf("no ssh client for %s\n", ip)
return "", utils.ErrNoSshClient
}
sshSession, err := utils.NewSshSession(sshClient)
if err != nil {
fmt.Printf("new ssh session failed! %v", err)
}
defer sshSession.Close()
combo, err := sshSession.CombinedOutput(command)
if err != nil {
fmt.Printf("exec: %s on %s failed! %v\n", command, ip, err)
return "", err
}
return string(combo), nil
}
type RunInfo struct {
ScriptPath string
Args []string
}
type RunAction struct {
User string
Info *RunInfo
Remote *config.RemoteHost
}
type RunActions struct {
MetaAction []*RunAction
SqlAction []*RunAction
StoreAction []*RunAction
}
func (e *GeminiExecutor) ExecRunAction(action *RunAction, errChan chan error) string {
ip := action.Remote.Ip
sshClient := e.sshClients[ip]
if sshClient == nil {
fmt.Printf("no ssh client for %s\n", ip)
errChan <- utils.ErrNoSshClient
return ""
}
sshSession, err := utils.NewSshSession(sshClient)
if err != nil {
fmt.Printf("new ssh session failed! %v", err)
}
defer sshSession.Close()
// generate command
command := fmt.Sprintf("chmod a+x %s; /bin/bash %s", action.Info.Args[2], action.Info.ScriptPath)
for _, arg := range action.Info.Args {
command = fmt.Sprintf("%s %s", command, arg)
}
combo, err := sshSession.CombinedOutput(command)
if err != nil {
fmt.Printf("exec: %s on %s failed! %v\n", command, ip, err)
errChan <- err
return ""
}
return string(combo)
}
type StopAction struct {
ProcessNames []string
Remote *config.RemoteHost
}
func (e *GeminiExecutor) ExecStopAction(action *StopAction) (string, error) {
ip := action.Remote.Ip
sshClient := e.sshClients[ip]
if sshClient == nil {
fmt.Printf("no ssh client for %s\n", ip)
return "", utils.ErrNoSshClient
}
command := ""
for _, bin := range action.ProcessNames {
command = fmt.Sprintf(`%s pgrep -x %s > /dev/null && killall %s && echo "Terminating process: %s" || echo "Process %s not found.";`, command, bin, bin, bin, bin)
}
sshSession, err := utils.NewSshSession(sshClient)
if err != nil {
fmt.Printf("new ssh session failed! %v", err)
}
defer sshSession.Close()
combo, err := sshSession.CombinedOutput(command)
if err != nil {
fmt.Printf("exec: %s on %s failed! %v\n", command, ip, err)
return "", err
}
return string(combo), nil
}