hosts: mostly implement Host.Run()

This commit is contained in:
Iris Lightshard 2023-12-10 22:37:26 -07:00
parent c28267af04
commit 3d31dd24b2
Signed by: Iris Lightshard
GPG key ID: F54E0D40695271D4

View file

@ -1,5 +1,13 @@
package hosts package hosts
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
)
// a host is a name and an address // a host is a name and an address
// memnarch expects an SSH private key to connect to Addr to exist at MEMNARCH_HOSTS/Name // memnarch expects an SSH private key to connect to Addr to exist at MEMNARCH_HOSTS/Name
@ -12,6 +20,27 @@ type RemoteMachine interface {
Run(...string) error Run(...string) error
} }
func (*Host) Run(cmdArgs ...string) error { func (self *Host) Run(cmdArgs ...string) error {
// make sure keyfile exists
keysDir := os.Getenv("MEMNARCH_HOSTS")
keyfile := filepath.Join(keysDir, self.Name)
info, err := os.Stat(keyfile)
if (err != nil) {
return err
}
if (info.IsDir()) {
return errors.New("supposed keyfile is actually a directory")
}
// ssh in
completeArgs := append([]string{"-i", keyfile, "memnarch@"+self.Addr}, cmdArgs...)
sshCmd := exec.Command("ssh", completeArgs...)
output, err := sshCmd.CombinedOutput()
// TODO: log the metadata
fmt.Println(output)
// if error, return it
if err != nil {
return err
}
// otherwise...
return nil return nil
} }