From 3cdc86e20cbd311fb9c85cef63876a2f39e5e74d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 26 Feb 2020 11:11:07 +0000 Subject: can list remote jobs and can also pass outer args to scripts --- internal/clients/args.go | 1 + internal/clients/runclient.go | 57 ++++++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 14 deletions(-) (limited to 'internal/clients') diff --git a/internal/clients/args.go b/internal/clients/args.go index 4f3c86a..b4852d4 100644 --- a/internal/clients/args.go +++ b/internal/clients/args.go @@ -12,6 +12,7 @@ type Args struct { ServersStr string UserName string What string + Arguments []string Regex string TrustAllHosts bool Discovery string 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)) } -- cgit v1.2.3