-
Notifications
You must be signed in to change notification settings - Fork 3
/
mesoslog.go
196 lines (171 loc) · 5.05 KB
/
mesoslog.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
ml "github.com/ContainX/go-mesoslog/mesoslog"
"github.com/spf13/cobra"
"io"
"os"
"strconv"
"strings"
"text/tabwriter"
)
const (
// StdErrFlag is a flag to output stderr logs vs stdout if true
StdErrFlag string = "stderr"
// CompletedFlag is a flag to output completed tasks if true
CompletedFlag string = "completed"
// LatestFlag is a flag to only capture the latest instance.
// This applies to completed and non-completed tasks
LatestFlag string = "latest"
// MasterFlag is the mesos master host:port flag
MasterFlag string = "master"
// DurationFlag is how often to poll in seconds
DurationFlag string = "duration"
// EnvMesosMaster is the MESOS_MASTER env variable
EnvMesosMaster string = "MESOS_MASTER"
)
var rootCmd = &cobra.Command{
Use: "mesoslog",
Short: "Download container logs from a mesos cluster",
Long: `
Mesos Log is a quick utility to quickly pull stdout or stderr container logs from a Mesos cluster.
`,
}
var printCmd = &cobra.Command{
Use: "print [appID]",
Short: "Outputs the log for the given [appId] to StdOut. Each running instances log will be outputed",
Run: printLog,
}
var tailCmd = &cobra.Command{
Use: "tail [appID]",
Short: "Tails logs [appId] to StdOut. Each running instance/task log will be outputed",
Run: tailLog,
}
var fileCmd = &cobra.Command{
Use: "file [appID] [output_dir]",
Short: "Outputs the log for the given [appId] to a file. Multiple files will be created (1 per running instance)",
Run: fileLog,
}
var appsCmd = &cobra.Command{
Use: "list",
Short: "List current application id's and task count (instances running)",
Run: listApps,
}
func main() {
rootCmd.PersistentFlags().Bool(StdErrFlag, false, "Output stderr log instead of default stdout")
printCmd.PersistentFlags().Bool(CompletedFlag, false, "Use completed tasks (default: running tasks)")
printCmd.PersistentFlags().Bool(LatestFlag, false, "Use the latest instance only")
rootCmd.PersistentFlags().StringP(MasterFlag, "m", "", "Mesos Master host:port (eg. 192.168.2.1:5050) or ENV [MESOS_MASTER]")
tailCmd.Flags().IntP(DurationFlag, "d", 5, "Log poll time (duration) in seconds")
rootCmd.AddCommand(appsCmd, printCmd, tailCmd, fileCmd)
rootCmd.Execute()
}
func printLog(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Usage()
fmt.Println("ERROR: An [appId] must be specified")
return
}
completed, _ := cmd.Flags().GetBool(CompletedFlag)
latest, _ := cmd.Flags().GetBool(LatestFlag)
logs, err := client(&ml.MesosClientOptions{SearchCompletedTasks: completed, ShowLatestOnly: latest}).GetLog(args[0], getLogType(), "")
if err != nil {
fmt.Printf("%s", err.Error())
return
}
for _, log := range logs {
fmt.Printf("\n=========================[ %s - Log For Task: %s ]============================\n", log.AppID, log.TaskID)
fmt.Printf("%s\n", log.Log)
}
}
func tailLog(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Usage()
fmt.Println("ERROR: An [appId] must be specified")
return
}
duration, _ := cmd.Flags().GetInt(DurationFlag)
err := client(nil).TailLog(args[0], getLogType(), duration)
if err != nil {
fmt.Printf("%s", err.Error())
return
}
}
func fileLog(cmd *cobra.Command, args []string) {
if len(args) < 2 {
cmd.Usage()
fmt.Println("ERROR: An [appId] and [output_dir] must be specified")
return
}
completed, _ := cmd.Flags().GetBool(CompletedFlag)
logs, err := client(&ml.MesosClientOptions{SearchCompletedTasks: completed}).GetLog(args[0], getLogType(), args[1])
if err != nil {
fmt.Printf("%s", err.Error())
return
}
for _, log := range logs {
fmt.Printf("Log written to %s\n", log.Log)
}
}
func listApps(cmd *cobra.Command, args []string) {
apps, err := client(nil).GetAppNames()
if err != nil {
fmt.Printf("%s", err.Error())
return
}
w := newTabWriter(os.Stdout)
fmt.Fprintf(w, "\nAPP_ID\tINSTANCES\n")
for k, v := range apps {
fmt.Fprintf(w, "%s\t%v\n", k, v)
}
flushWriter(w)
}
func getLogType() ml.LogType {
if rootCmd.PersistentFlags().Changed(StdErrFlag) {
if b, err := rootCmd.PersistentFlags().GetBool(StdErrFlag); err == nil && b {
return ml.STDERR
}
}
return ml.STDOUT
}
func client(options *ml.MesosClientOptions) *ml.MesosClient {
var host string
var port = 5050
master, err := rootCmd.PersistentFlags().GetString(MasterFlag)
if master == "" {
if os.Getenv(EnvMesosMaster) == "" {
printErr(fmt.Errorf("Must define a Master host and optional port"))
os.Exit(1)
}
master = os.Getenv(EnvMesosMaster)
}
if strings.Contains(master, ":") {
hp := strings.Split(master, ":")
host = hp[0]
port, err = strconv.Atoi(hp[1])
if err != nil {
printErr(err)
os.Exit(1)
}
} else {
host = master
}
c, err := ml.NewMesosClientWithOptions(host, port, options)
if err != nil {
printErr(err)
os.Exit(1)
}
return c
}
func printErr(err error) {
fmt.Printf("\nError: %s\n", err.Error())
}
func flushWriter(w *tabwriter.Writer) {
fmt.Fprintln(w, "")
w.Flush()
}
func newTabWriter(output io.Writer) *tabwriter.Writer {
w := new(tabwriter.Writer)
w.Init(output, 0, 8, 2, '\t', 0)
return w
}