2023-09-17 05:23:59 +00:00
|
|
|
package hosts
|
|
|
|
|
2023-12-11 05:37:26 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2023-09-17 05:23:59 +00:00
|
|
|
// a host is a name and an address
|
|
|
|
// memnarch expects an SSH private key to connect to Addr to exist at MEMNARCH_HOSTS/Name
|
|
|
|
|
|
|
|
type Host struct {
|
|
|
|
Name string
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoteMachine interface {
|
|
|
|
Run(...string) error
|
|
|
|
}
|
|
|
|
|
2023-12-11 05:37:26 +00:00
|
|
|
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...
|
2023-09-17 05:23:59 +00:00
|
|
|
return nil
|
|
|
|
}
|