forked from coreos/fleet
/
status.go
87 lines (70 loc) · 1.88 KB
/
status.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"log"
"path"
"syscall"
gossh "github.com/coreos/fleet/third_party/code.google.com/p/go.crypto/ssh"
"github.com/coreos/fleet/third_party/github.com/codegangsta/cli"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/ssh"
)
func newStatusUnitsCommand() cli.Command {
return cli.Command{
Name: "status",
Usage: "Output the status of one or more units in the cluster",
Description: `Output the status of one or more units currently running in the cluster.
Supports glob matching of units in the current working directory or matches
previously started units.
Show status of a single unit:
fleetctl status foo.service
Show status of an entire directory with glob matching:
fleetctl status myservice/*`,
Action: statusUnitsAction,
}
}
func statusUnitsAction(c *cli.Context) {
r := getRegistry(c)
for i, v := range c.Args() {
// This extra newline here to match systemctl status output
if i != 0 {
fmt.Printf("\n")
}
name := path.Base(v)
printUnitStatus(c, r, name)
}
}
func printUnitStatus(c *cli.Context, r *registry.Registry, jobName string) {
js := r.GetJobState(jobName)
if js == nil {
fmt.Printf("%s does not appear to be running\n", jobName)
syscall.Exit(1)
}
addr := fmt.Sprintf("%s:22", js.MachineState.PublicIP)
var err error
var sshClient *gossh.ClientConn
if tun := getTunnelFlag(); tun != "" {
sshClient, err = ssh.NewTunnelledSSHClient("core", tun, addr)
} else {
sshClient, err = ssh.NewSSHClient("core", addr)
}
if err != nil {
log.Fatal(err.Error())
}
defer sshClient.Close()
cmd := fmt.Sprintf("systemctl status -l %s", jobName)
stdout, err := ssh.Execute(sshClient, cmd)
if err != nil {
log.Fatalf("Unable to execute command over SSH: %s", err.Error())
}
for true {
bytes, prefix, err := stdout.ReadLine()
if err != nil {
break
}
print(string(bytes))
if !prefix {
print("\n")
}
}
}