all repos — memnarch @ 3d31dd24b24de57ae31e0ac33c2446af9acf31cc

featherweight orchestrator

hosts/hosts.go (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package hosts

import (
  "errors"
  "fmt"
  "os"
  "os/exec"
  "path/filepath"
)

// 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
}

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
}