From 7d00f0e133ba54296273f8914e3bcb453f827f9a Mon Sep 17 00:00:00 2001 From: kalder <61064868+kalder@users.noreply.github.com> Date: Tue, 7 Feb 2023 02:29:21 -0800 Subject: vm/proxyapp: pass file data when running in tcp mode (#3648) Instead of just passing the path to the file (on the local machine) to the ProxyApp, we need to pass the file data. This applies for both calls to Copy() and CreateInstance(). --- vm/proxyapp/init.go | 3 ++ vm/proxyapp/proxyappclient.go | 80 ++++++++++++++++++++++++++++++---------- vm/proxyapp/proxyrpc/proxyrpc.go | 6 ++- 3 files changed, 68 insertions(+), 21 deletions(-) (limited to 'vm/proxyapp') diff --git a/vm/proxyapp/init.go b/vm/proxyapp/init.go index 8153cf031..467187f46 100644 --- a/vm/proxyapp/init.go +++ b/vm/proxyapp/init.go @@ -68,6 +68,9 @@ type Config struct { // server_tls_cert points a TLS certificate used to authenticate the server. // If not provided, the default system certificate pool will be used. ServerTLSCert string `json:"server_tls_cert"` + // transfer_file_content will send the file content as a byte array in + // addition to the filename. + TransferFileContent bool `json:"transfer_file_content"` // config is an optional remote plugin config ProxyAppConfig json.RawMessage `json:"config"` } diff --git a/vm/proxyapp/proxyappclient.go b/vm/proxyapp/proxyappclient.go index 1e6d679ac..946556f24 100644 --- a/vm/proxyapp/proxyappclient.go +++ b/vm/proxyapp/proxyappclient.go @@ -11,10 +11,13 @@ import ( "crypto/x509" "fmt" "io" + "io/fs" "net" "net/rpc" "net/rpc/jsonrpc" "os" + "path/filepath" + "strings" "sync" "time" @@ -96,7 +99,9 @@ func (p *pool) init(params *proxyAppParams, cfg *Config) error { if cfg.Command != "" { p.proxy, err = runProxyApp(params, cfg.Command, usePipedRPC) } else { - p.proxy = &ProxyApp{} + p.proxy = &ProxyApp{ + transferFileContent: cfg.TransferFileContent, + } } if err != nil { return fmt.Errorf("failed to run ProxyApp: %w", err) @@ -170,11 +175,12 @@ func (p *pool) Close() error { type ProxyApp struct { *rpc.Client - terminate context.CancelFunc - onTerminated chan bool - onLostConnection chan bool - stopLogPooling chan bool - logPoolingDone chan bool + transferFileContent bool + terminate context.CancelFunc + onTerminated chan bool + onLostConnection chan bool + stopLogPooling chan bool + logPoolingDone chan bool } func initPipedRPCClient(cmd subProcessCmd) (*rpc.Client, []io.Closer, error) { @@ -354,12 +360,40 @@ func (proxy *ProxyApp) CreatePool(config string, debug bool) (int, error) { func (proxy *ProxyApp) CreateInstance(workdir string, index int) (vmimpl.Instance, error) { var reply proxyrpc.CreateInstanceResult - err := proxy.Call( - "ProxyVM.CreateInstance", - proxyrpc.CreateInstanceParams{ - Workdir: workdir, - Index: index}, - &reply) + + params := proxyrpc.CreateInstanceParams{ + Workdir: workdir, + Index: index, + } + + if proxy.transferFileContent { + workdirData := make(map[string][]byte) + + err := filepath.WalkDir(workdir, func(path string, d fs.DirEntry, e error) error { + if d.IsDir() { + return nil + } + + name := strings.TrimPrefix(path, workdir) + + data, err := os.ReadFile(path) + if err != nil { + return fmt.Errorf("read file on host: %s", err) + } + + workdirData[name] = data + + return nil + }) + + if err != nil { + return nil, fmt.Errorf("failed to walk workdir: %v", err) + } + + params.WorkdirData = workdirData + } + + err := proxy.Call("ProxyVM.CreateInstance", params, &reply) if err != nil { return nil, fmt.Errorf("failed to proxy.Call(\"ProxyVM.CreateInstance\"): %w", err) } @@ -379,13 +413,21 @@ type instance struct { // nolint: dupl func (inst *instance) Copy(hostSrc string) (string, error) { var reply proxyrpc.CopyResult - err := inst.ProxyApp.Call( - "ProxyVM.Copy", - proxyrpc.CopyParams{ - ID: inst.ID, - HostSrc: hostSrc, - }, - &reply) + params := proxyrpc.CopyParams{ + ID: inst.ID, + HostSrc: hostSrc, + } + + if inst.ProxyApp.transferFileContent { + data, err := os.ReadFile(hostSrc) + if err != nil { + return "", fmt.Errorf("read file on host: %s", err) + } + + params.Data = data + } + + err := inst.ProxyApp.Call("ProxyVM.Copy", params, &reply) if err != nil { return "", err } diff --git a/vm/proxyapp/proxyrpc/proxyrpc.go b/vm/proxyapp/proxyrpc/proxyrpc.go index d3fd606a4..95bc77ce0 100644 --- a/vm/proxyapp/proxyrpc/proxyrpc.go +++ b/vm/proxyapp/proxyrpc/proxyrpc.go @@ -27,8 +27,9 @@ type CreatePoolResult struct { } type CreateInstanceParams struct { - Workdir string - Index int + Workdir string + Index int + WorkdirData map[string][]byte } type CreateInstanceResult struct { @@ -38,6 +39,7 @@ type CreateInstanceResult struct { type CopyParams struct { ID string HostSrc string + Data []byte } type CopyResult struct { -- cgit mrf-deployment