forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
journal.go
58 lines (49 loc) · 1.46 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
package main
import (
"fmt"
"os"
)
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] job",
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`,
}
)
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 {
fmt.Fprintln(os.Stderr, "One unit file must be provided.")
return 1
}
jobName := unitNameMangle(args[0])
j, err := cAPI.Job(jobName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error retrieving Job %s: %v", jobName, err)
return 1
}
if j == nil {
fmt.Fprintf(os.Stderr, "Job %s does not exist.\n", jobName)
return 1
} else if j.UnitState == nil {
fmt.Fprintf(os.Stderr, "Job %s does not appear to be running.\n", jobName)
return 1
}
command := fmt.Sprintf("journalctl --unit %s --no-pager -n %d", jobName, flagLines)
if flagFollow {
command += " -f"
}
return runCommand(command, j.UnitState.MachineID)
}