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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
package events
import (
"encoding/json"
"fmt"
"time"
"github.com/containers/storage/pkg/stringid"
"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 {
switch et {
case LogFile:
return "file"
case Journald:
return "journald"
case Null:
return "none"
default:
return "invalid"
}
}
// IsValidEventer checks if the given string is a valid eventer type.
func IsValidEventer(eventer string) bool {
switch eventer {
case LogFile.String():
return true
case Journald.String():
return true
case Null.String():
return true
default:
return false
}
}
// 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(truncate bool) string {
var humanFormat string
id := e.ID
if truncate {
id = stringid.TruncateID(id)
}
switch e.Type {
case Container, Pod:
humanFormat = fmt.Sprintf("%s %s %s %s (image=%s, name=%s", e.Time, e.Type, e.Status, id, e.Image, e.Name)
// check if the container has labels and add it to the output
if len(e.Attributes) > 0 {
for k, v := range e.Attributes {
humanFormat += fmt.Sprintf(", %s=%s", k, v)
}
}
humanFormat += ")"
case Network:
humanFormat = fmt.Sprintf("%s %s %s %s (container=%s, name=%s)", e.Time, e.Type, e.Status, id, id, e.Network)
case Image:
humanFormat = fmt.Sprintf("%s %s %s %s %s", e.Time, e.Type, e.Status, id, e.Name)
case System:
if e.Name != "" {
humanFormat = fmt.Sprintf("%s %s %s %s", e.Time, e.Type, e.Status, e.Name)
} else {
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 Network.String():
return Network, 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 Build.String():
return Build, nil
case Checkpoint.String():
return Checkpoint, 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 ExecDied.String():
return ExecDied, 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 NetworkConnect.String():
return NetworkConnect, nil
case NetworkDisconnect.String():
return NetworkDisconnect, 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 Rename.String():
return Rename, nil
case Renumber.String():
return Renumber, nil
case Restart.String():
return Restart, nil
case Restore.String():
return Restore, nil
case Rotate.String():
return Rotate, 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)
}
|