]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
Move embedded remote helpers to cmd/safcm/
authorSimon Ruderich <simon@ruderich.org>
Tue, 18 May 2021 09:03:05 +0000 (11:03 +0200)
committerSimon Ruderich <simon@ruderich.org>
Tue, 18 May 2021 09:08:29 +0000 (11:08 +0200)
To permit using safcm as an actual library the helpers must be passed to
rpc.DialSSH() and not directly embedded.

.gitignore
Makefile
cmd/safcm-remote/build.sh
cmd/safcm/remote.go [moved from remote/remote.go with 82% similarity]
cmd/safcm/sync.go
rpc/conn.go
rpc/dial.go

index 139f3d5e8fb195a44c4005477567864350baeb08..8e87b2f84d9438df5b9930c690e6dd29c7ddf88e 100644 (file)
@@ -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
index fa2db0bbd326f4a4f3da2dfc9e3ef8b531460a41..4bf77c25414480744a3747898e44fa803c83dba0 100644 (file)
--- 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
index 3ee7ccd99607161c2890ebc0f82ecf8ae1334196..73957ed7562938b4a8376a364b962008311b0c4e 100755 (executable)
@@ -30,7 +30,7 @@ build_arm() {
 }
 
 
-dest=../../remote/helpers
+dest=../safcm/remote
 
 mkdir -p "$dest"
 
similarity index 82%
rename from remote/remote.go
rename to cmd/safcm/remote.go
index 3a41987f0adb757c1fd5f8f504ee18a9a565cd15..4fbb7d4834583b02ecc8aa35991c725a6b92cf4c 100644 (file)
 // You should have received a copy of the GNU General Public License
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-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
index 1a482b3dd958c274f5fad95a03114596df959d14..bee0133e4fd4326ef1610325fc6ae8649147da29 100644 (file)
@@ -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()
index 03b748545f12c1c588acb24bac2761e0b439b25c..3f7ade144c1c2905254da49eb7924552aa3c362d 100644 (file)
@@ -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
 }
index c0c6cd4088b2859d758aa0d06cf64405a2b70a16..c098714cc0759388dd32e0d723819c4b4c598665 100644 (file)
@@ -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)