forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
journal.go
85 lines (71 loc) · 2.44 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
81
82
83
84
85
// Copyright 2014 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"github.com/coreos/fleet/job"
)
var (
flagLines int
flagFollow bool
flagSudo bool
cmdJournal = &Command{
Name: "journal",
Summary: "Print the journal of a unit in the cluster to stdout",
Usage: "[--lines=N] [--ssh-port=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")
cmdJournal.Flags.IntVar(&sharedFlags.SSHPort, "ssh-port", 22, "Connect to remote hosts over SSH using this TCP port")
cmdJournal.Flags.BoolVar(&flagSudo, "sudo", false, "Execute journal command with sudo")
}
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 flagSudo {
command = "sudo " + command
}
if flagFollow {
command += " -f"
}
return runCommand(command, u.MachineID)
}