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
|
package events
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/hpcloud/tail"
"github.com/pkg/errors"
)
// ErrNoJournaldLogging indicates that there is no journald logging
// supported (requires libsystemd)
var ErrNoJournaldLogging = errors.New("No support for journald logging")
// String returns a string representation of EventerType
func (et EventerType) String() string {
if et == LogFile {
return "file"
}
return "journald"
}
// NewEvent creates a event struct and populates with
// the given status and time.
func NewEvent(status Status) Event {
return Event{
Status: status,
Time: time.Now(),
}
}
// Recycle checks if the event log has reach a limit and if so
// renames the current log and starts a new one. The remove bool
// indicates the old log file should be deleted.
func (e *Event) Recycle(path string, remove bool) error {
return errors.New("not implemented")
}
// ToJSONString returns the event as a json'ified string
func (e *Event) ToJSONString() (string, error) {
b, err := json.Marshal(e)
return string(b), err
}
// ToHumanReadable returns human readable event as a formatted string
func (e *Event) ToHumanReadable() string {
var humanFormat string
switch e.Type {
case Container, Pod:
humanFormat = fmt.Sprintf("%s %s %s %s (image=%s, name=%s)", e.Time, e.Type, e.Status, e.ID, e.Image, e.Name)
case Image:
humanFormat = fmt.Sprintf("%s %s %s %s %s", e.Time, e.Type, e.Status, e.ID, e.Name)
case System:
humanFormat = fmt.Sprintf("%s %s %s", e.Time, e.Type, e.Status)
case Volume:
humanFormat = fmt.Sprintf("%s %s %s %s", e.Time, e.Type, e.Status, e.Name)
}
return humanFormat
}
// NewEventFromString takes stringified json and converts
// it to an event
func newEventFromJSONString(event string) (*Event, error) {
e := Event{}
if err := json.Unmarshal([]byte(event), &e); err != nil {
return nil, err
}
return &e, nil
}
// ToString converts a Type to a string
func (t Type) String() string {
return string(t)
}
// ToString converts a status to a string
func (s Status) String() string {
return string(s)
}
// StringToType converts string to an EventType
func StringToType(name string) (Type, error) {
switch name {
case Container.String():
return Container, nil
case Image.String():
return Image, nil
case Pod.String():
return Pod, nil
case System.String():
return System, nil
case Volume.String():
return Volume, nil
case "":
return "", ErrEventTypeBlank
}
return "", errors.Errorf("unknown event type %q", name)
}
// StringToStatus converts a string to an Event Status
// TODO if we add more events, we might consider a go-generator to
// create the switch statement
func StringToStatus(name string) (Status, error) {
switch name {
case Attach.String():
return Attach, nil
case Checkpoint.String():
return Checkpoint, nil
case Restore.String():
return Restore, nil
case Cleanup.String():
return Cleanup, nil
case Commit.String():
return Commit, nil
case Create.String():
return Create, nil
case Exec.String():
return Exec, nil
case Exited.String():
return Exited, nil
case Export.String():
return Export, nil
case History.String():
return History, nil
case Import.String():
return Import, nil
case Init.String():
return Init, nil
case Kill.String():
return Kill, nil
case LoadFromArchive.String():
return LoadFromArchive, nil
case Mount.String():
return Mount, nil
case Pause.String():
return Pause, nil
case Prune.String():
return Prune, nil
case Pull.String():
return Pull, nil
case Push.String():
return Push, nil
case Refresh.String():
return Refresh, nil
case Remove.String():
return Remove, nil
case Renumber.String():
return Renumber, nil
case Restart.String():
return Restart, nil
case Restore.String():
return Restore, nil
case Save.String():
return Save, nil
case Start.String():
return Start, nil
case Stop.String():
return Stop, nil
case Sync.String():
return Sync, nil
case Tag.String():
return Tag, nil
case Unmount.String():
return Unmount, nil
case Unpause.String():
return Unpause, nil
case Untag.String():
return Untag, nil
}
return "", errors.Errorf("unknown event status %q", name)
}
func (e EventLogFile) getTail(options ReadOptions) (*tail.Tail, error) {
reopen := true
seek := tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}
if options.FromStart || !options.Stream {
seek.Whence = 0
reopen = false
}
stream := options.Stream
if len(options.Until) > 0 {
stream = false
}
return tail.TailFile(e.options.LogFilePath, tail.Config{ReOpen: reopen, Follow: stream, Location: &seek, Logger: tail.DiscardingLogger})
}
|