summaryrefslogtreecommitdiff
path: root/internal/clients/runclient.go
diff options
context:
space:
mode:
authorPaul Buetow <pbuetow@mimecast.com>2020-02-26 11:11:07 +0000
committerPaul Buetow <pbuetow@mimecast.com>2020-02-26 11:11:07 +0000
commit3cdc86e20cbd311fb9c85cef63876a2f39e5e74d (patch)
tree9cb50347900ff1ba4dc6a7b6e4766ebd951c2c58 /internal/clients/runclient.go
parent6e176034306026b922c1df4231a1807f36cbe460 (diff)
can list remote jobs and can also pass outer args to scripts
Diffstat (limited to 'internal/clients/runclient.go')
-rw-r--r--internal/clients/runclient.go57
1 files changed, 43 insertions, 14 deletions
diff --git a/internal/clients/runclient.go b/internal/clients/runclient.go
index c2f6f62..543df15 100644
--- a/internal/clients/runclient.go
+++ b/internal/clients/runclient.go
@@ -1,32 +1,41 @@
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
- background bool
- cancel bool
+ jobName string
+ background string
}
-// NewRunClient returns a new cat client.
-func NewRunClient(args Args, background, cancel bool) (*RunClient, error) {
+// 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,
- cancel: cancel,
}
c.init(c)
@@ -39,20 +48,40 @@ func (c RunClient) makeHandler(server string) handlers.Handler {
func (c RunClient) makeCommands() (commands []string) {
if c.Timeout > 0 {
- commands = append(commands, fmt.Sprintf("timeout %d run%s %s", c.Timeout, c.flags(), c.What))
+ 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.flags(), c.What))
+ commands = append(commands, fmt.Sprintf("run%s %s", c.options(), c.What))
+ logger.Debug(commands)
+
return
}
-func (c RunClient) flags() string {
- if c.background {
- return ":background.start"
- }
- if c.cancel {
- return ":background.cancel"
+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 ""
+
+ 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))
}