forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
journal.go
63 lines (52 loc) · 1.58 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
package main
import (
"fmt"
"github.com/coreos/fleet/job"
)
var (
flagLines int
flagFollow bool
cmdJournal = &Command{
Name: "journal",
Summary: "Print the journal of a unit in the cluster to stdout",
Usage: "[--lines=N] [-f|--follow] <unit>",
Run: runJournal,
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
This command does not work with global units.`,
}
)
func init() {
cmdJournal.Flags.IntVar(&flagLines, "lines", 10, "Number of recent log lines to return")
cmdJournal.Flags.BoolVar(&flagFollow, "follow", false, "Continuously print new entries as they are appended to the journal.")
cmdJournal.Flags.BoolVar(&flagFollow, "f", false, "Shorthand for --follow")
}
func runJournal(args []string) (exit int) {
if len(args) != 1 {
stderr("One unit file must be provided.")
return 1
}
name := unitNameMangle(args[0])
u, err := cAPI.Unit(name)
if err != nil {
stderr("Error retrieving unit %s: %v", name, err)
return 1
} else if u == nil {
stderr("Unit %s does not exist.", name)
return 1
} else if suToGlobal(*u) {
stderr("Unable to retrieve journal of global unit %s.", name)
return 1
} else if job.JobState(u.CurrentState) == job.JobStateInactive {
stderr("Unit %s does not appear to be running.", name)
return 1
}
command := fmt.Sprintf("journalctl --unit %s --no-pager -n %d", name, flagLines)
if flagFollow {
command += " -f"
}
return runCommand(command, u.MachineID)
}