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
|
package clients
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"runtime"
"strings"
"github.com/mimecast/dtail/internal/clients/handlers"
"github.com/mimecast/dtail/internal/io/logger"
"github.com/mimecast/dtail/internal/omode"
)
// RunClient is a client to run various commands on the server.
type RunClient struct {
baseClient
jobName string
background string
}
// NewRunClient returns a new run client to execute commands on the remote server.
func NewRunClient(args Args, background, jobName string) (*RunClient, error) {
args.Mode = omode.RunClient
if jobName == "" {
jobName = hash(strings.Join(args.Arguments, " "))
}
c := RunClient{
baseClient: baseClient{
Args: args,
throttleCh: make(chan struct{}, args.ConnectionsPerCPU*runtime.NumCPU()),
retry: false,
},
jobName: jobName,
background: background,
}
c.init(c)
return &c, nil
}
func (c RunClient) makeHandler(server string) handlers.Handler {
return handlers.NewClientHandler(server)
}
func (c RunClient) makeCommands() (commands []string) {
if c.Timeout > 0 {
commands = append(commands, fmt.Sprintf("timeout %d run%s %s", c.Timeout, c.options(), c.What))
return
}
commands = append(commands, fmt.Sprintf("run%s %s", c.options(), c.What))
return
}
func (c RunClient) options() string {
var sb strings.Builder
logger.Debug("options", fmt.Sprintf(":background=%s", c.background))
sb.WriteString(fmt.Sprintf(":background=%s", c.background))
logger.Debug("options", fmt.Sprintf(":jobName=%s", c.jobName))
sb.WriteString(fmt.Sprintf(":jobName=%s", c.jobName))
if len(c.Arguments) > 0 {
logger.Debug("options", fmt.Sprintf(":outerArgs=base64%%%s", strings.Join(c.Arguments, " ")))
sb.WriteString(fmt.Sprintf(":outerArgs=base64%%%s", encode64(strings.Join(c.Arguments, " "))))
}
return sb.String()
}
func encode64(str string) string {
return base64.StdEncoding.EncodeToString([]byte(str))
}
func hash(str string) string {
h := sha256.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
|