Skip to content

Commit

Permalink
command/agent/host: eliminate calling external programs
Browse files Browse the repository at this point in the history
  • Loading branch information
langmartin committed Jul 1, 2020
1 parent e224ca6 commit b564e61
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 71 deletions.
49 changes: 3 additions & 46 deletions command/agent/host/darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,10 @@

package host

import (
"bufio"
"strings"
)

func network() string {
return call("ifconfig")
}

func resolvConf() string {
return call("scutil", "--dns")
return ""
}

func mountedPaths() (paths []string) {
// TODO (langmartin) diskutil list -plist physical is a better source for disks to
// check, but needs a little xml parsing
out := call("mount")
rd := bufio.NewReader(strings.NewReader(out))

for {
str, err := rd.ReadString('\n')
if err != nil {
break
}

// find the last ( in the line, it's the beginning of the args
i := len(str) - 1
for ; ; i-- {
if str[i] == '(' {
break
}
}

// split the args part of the string
args := strings.Split(str[i+1:len(str)-2], ", ")

// split the device and mountpoint
mnt := strings.Split(str[:i-1], " on ")

switch args[0] {
case "devfs", "autofs":
continue
default:
}

paths = append(paths, mnt[1])
}

return paths
func mountedPaths() []string {
return []string{"/"}
}
41 changes: 23 additions & 18 deletions command/agent/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package host
import (
"fmt"
"os"
"os/exec"
"strings"

sockaddr "github.com/hashicorp/go-sockaddr"
)

type HostData struct {
OS string
Network string
ResolvConf string
Hosts string
Systemd string
Disk map[string]DiskUsage
OS string
Network string
ResolvConf string
Hosts string
Environment map[string]string
Disk map[string]DiskUsage
}

type DiskUsage struct {
Expand All @@ -36,12 +37,12 @@ func MakeHostData() (*HostData, error) {
}

return &HostData{
OS: uname,
Network: network(),
ResolvConf: resolvConf(),
Hosts: etcHosts(),
Systemd: slurp("/etc/systemd/nomad.conf"),
Disk: du,
OS: uname,
Network: network(),
ResolvConf: resolvConf(),
Hosts: etcHosts(),
Environment: environment(),
Disk: du,
}, nil
}

Expand All @@ -66,11 +67,15 @@ func diskUsage(path string) (du DiskUsage, err error) {
return du, nil
}

// call executes the command line and returns stdout, ignoring errors
func call(cmd string, args ...string) string {
// cmd = which(cmd)
out, _ := exec.Command(cmd, args...).Output()
return string(out)
// environment returns the process environment in a map
func environment() map[string]string {
env := make(map[string]string)

for _, e := range os.Environ() {
s := strings.SplitN(e, "=", 2)
env[s[0]] = s[1]
}
return env
}

// slurp returns the file contents as a string, ignoring errors
Expand Down
24 changes: 17 additions & 7 deletions command/agent/host/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,39 @@ package host

import (
"bufio"
"os"
"strings"
)

func network() string {
return call("ip", "address", "list")
}

func resolvConf() string {
return slurp("/etc/resolv.conf")
}

// mountedPaths produces a list of mounts
func mountedPaths() []string {
out := call("findmnt", "-D", "-tnotmpfs,nodevtmpfs", "-o", "TARGET")
rd := bufio.NewReader(strings.NewReader(out))
fh := os.Open("/proc/mounts")
rd := bufio.NewReader(strings.NewReader(fh))

var paths []string
for {
str, err := rd.ReadString('\n')
if err != nil {
break
}
paths = append(paths, str)

ln := strings.Split(str, " ")
switch ln[2] {
case "autofs", "binfmt_misc", "cgroup", "debugfs",
"devpts", "devtmpfs",
"fusectl", "fuse.lxcfs",
"hugetlbfs", "mqueue",
"proc", "pstore", "rpc_pipefs", "securityfs",
"sysfs", "tmpfs", "vboxsf":
continue
default:
}

paths = append(paths, ln[1])
}

return paths
Expand Down
4 changes: 4 additions & 0 deletions command/agent/host/unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func etcHosts() string {
return slurp("/etc/hosts")
}

func resolvConf() string {
return slurp("/etc/resolv.conf")
}

func nullStr(bs []byte) string {
// find the null byte
var i int
Expand Down

0 comments on commit b564e61

Please sign in to comment.