forked from coreos/fleet
/
journal.go
80 lines (65 loc) · 1.7 KB
/
journal.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
package main
import (
"fmt"
"log"
"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/ssh"
)
func newJournalCommand() cli.Command {
return cli.Command{
Name: "journal",
Usage: "Print the journal of a unit in the cluster to stdout",
Action: journalAction,
Description: `Outputs the journal of a unit by connecting to the machine that the unit
occupies.
Read the last 10 lines:
fleetctl journal foo.service
Read the last 100 lines:
fleetctl journal --lines 100 foo.service`,
Flags: []cli.Flag{
cli.IntFlag{"lines, n", 10, "Number of recent log lines to return."},
},
}
}
func journalAction(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Println("One unit file must be provided.")
syscall.Exit(1)
}
jobName := c.Args()[0]
r := getRegistry(c)
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("journalctl -u %s --no-pager -l -n %d", jobName, c.Int("lines"))
stdout, err := ssh.Execute(sshClient, cmd)
if err != nil {
log.Fatalf("Unable to run command over SSH: %s", err.Error())
}
for true {
bytes, prefix, err := stdout.ReadLine()
if err != nil {
break
}
print(string(bytes))
if !prefix {
print("\n")
}
}
}