From fd97e8019e2ab166d9475ed59782c86247d8430b Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Tue, 18 May 2021 11:03:05 +0200 Subject: [PATCH] Move embedded remote helpers to cmd/safcm/ To permit using safcm as an actual library the helpers must be passed to rpc.DialSSH() and not directly embedded. --- .gitignore | 2 +- Makefile | 2 +- cmd/safcm-remote/build.sh | 2 +- {remote => cmd/safcm}/remote.go | 10 +++++----- cmd/safcm/sync.go | 16 ++++++++++++---- rpc/conn.go | 3 +++ rpc/dial.go | 15 +++++++++++---- 7 files changed, 34 insertions(+), 16 deletions(-) rename {remote => cmd/safcm}/remote.go (82%) diff --git a/.gitignore b/.gitignore index 139f3d5..8e87b2f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/cmd/safcm/remote/ /cmd/safcm/testdata/ssh/project/no-changes.example.org/ /cmd/safcm/testdata/ssh/ssh/authorized_keys /cmd/safcm/testdata/ssh/ssh/id_ed25519 @@ -5,6 +6,5 @@ /cmd/safcm/testdata/ssh/ssh/known_hosts /cmd/safcm/testdata/ssh/sshd/ssh_host_key /cmd/safcm/testdata/ssh/sshd/ssh_host_key.pub -/remote/helpers/ /safcm /tags diff --git a/Makefile b/Makefile index fa2db0b..4bf77c2 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ lint: shellcheck cmd/safcm/testdata/ssh/prepare.sh clean: - rm -rf remote/helpers/ + rm -rf cmd/safcm/remote/ rm -f safcm rm -f cmd/safcm/testdata/ssh/ssh/authorized_keys rm -f cmd/safcm/testdata/ssh/ssh/id_ed25519 diff --git a/cmd/safcm-remote/build.sh b/cmd/safcm-remote/build.sh index 3ee7ccd..73957ed 100755 --- a/cmd/safcm-remote/build.sh +++ b/cmd/safcm-remote/build.sh @@ -30,7 +30,7 @@ build_arm() { } -dest=../../remote/helpers +dest=../safcm/remote mkdir -p "$dest" diff --git a/remote/remote.go b/cmd/safcm/remote.go similarity index 82% rename from remote/remote.go rename to cmd/safcm/remote.go index 3a41987..4fbb7d4 100644 --- a/remote/remote.go +++ b/cmd/safcm/remote.go @@ -15,13 +15,13 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package remote +package main import ( "embed" ) -// Helpers contains remote helper binaries for different operating systems and -// architectures. -//go:embed helpers/* -var Helpers embed.FS +// RemoteHelpers contains remote helper binaries for different operating +// systems and architectures. +//go:embed remote/* +var RemoteHelpers embed.FS diff --git a/cmd/safcm/sync.go b/cmd/safcm/sync.go index 1a482b3..bee0133 100644 --- a/cmd/safcm/sync.go +++ b/cmd/safcm/sync.go @@ -20,6 +20,7 @@ package main import ( "flag" "fmt" + "io/fs" "log" "os" "os/signal" @@ -369,15 +370,22 @@ func (s *Sync) Host(wg *sync.WaitGroup) error { wg.Done() }() + helpers, err := fs.Sub(RemoteHelpers, "remote") + if err != nil { + conn.Kill() + return err + } + // Connect to remote host user := s.host.SshUser if user == "" { user = s.config.SshUser } - err := conn.DialSSH(rpc.SSHConfig{ - Host: s.host.Name, - User: user, - SshConfig: s.config.SshConfig, + err = conn.DialSSH(rpc.SSHConfig{ + Host: s.host.Name, + User: user, + SshConfig: s.config.SshConfig, + RemoteHelpers: helpers, }) if err != nil { conn.Kill() diff --git a/rpc/conn.go b/rpc/conn.go index 03b7485..3f7ade1 100644 --- a/rpc/conn.go +++ b/rpc/conn.go @@ -20,6 +20,7 @@ package rpc import ( "bufio" "fmt" + "io/fs" "os/exec" "strings" "sync" @@ -36,6 +37,8 @@ type Conn struct { sshRemote string sshOpts []string + remoteHelpers fs.FS + cmd *exec.Cmd conn *safcm.GobConn } diff --git a/rpc/dial.go b/rpc/dial.go index c0c6cd4..c098714 100644 --- a/rpc/dial.go +++ b/rpc/dial.go @@ -24,18 +24,20 @@ import ( "encoding/hex" "fmt" "io" + "io/fs" "os/exec" "strconv" "strings" "ruderich.org/simon/safcm" - "ruderich.org/simon/safcm/remote" ) type SSHConfig struct { Host string User string // optional SshConfig string // optional + + RemoteHelpers fs.FS } func (c *Conn) DialSSH(cfg SSHConfig) error { @@ -43,6 +45,11 @@ func (c *Conn) DialSSH(cfg SSHConfig) error { return fmt.Errorf("cannot reuse Conn") } + if cfg.RemoteHelpers == nil { + return fmt.Errorf("SSHConfig.RemoteHelpers not set") + } + c.remoteHelpers = cfg.RemoteHelpers + remote := cfg.Host if cfg.User != "" { remote = cfg.User + "@" + cfg.Host @@ -208,9 +215,9 @@ f return err } - // Get embedded helper binary - helper, err := remote.Helpers.ReadFile( - fmt.Sprintf("helpers/%s-%s", goos, goarch)) + // Get remote helper binary + helper, err := fs.ReadFile(c.remoteHelpers, + fmt.Sprintf("%s-%s", goos, goarch)) if err != nil { return fmt.Errorf("remote not built for GOOS/GOARCH %s/%s", goos, goarch) -- 2.45.2