github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/libpod/events/events.go (about) 1 package events 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "time" 8 9 "github.com/hpcloud/tail" 10 "github.com/pkg/errors" 11 ) 12 13 // ErrNoJournaldLogging indicates that there is no journald logging 14 // supported (requires libsystemd) 15 var ErrNoJournaldLogging = errors.New("No support for journald logging") 16 17 // String returns a string representation of EventerType 18 func (et EventerType) String() string { 19 switch et { 20 case LogFile: 21 return "file" 22 case Journald: 23 return "journald" 24 case Null: 25 return "none" 26 default: 27 return "invalid" 28 } 29 } 30 31 // IsValidEventer checks if the given string is a valid eventer type. 32 func IsValidEventer(eventer string) bool { 33 switch eventer { 34 case LogFile.String(): 35 return true 36 case Journald.String(): 37 return true 38 case Null.String(): 39 return true 40 default: 41 return false 42 } 43 } 44 45 // NewEvent creates a event struct and populates with 46 // the given status and time. 47 func NewEvent(status Status) Event { 48 return Event{ 49 Status: status, 50 Time: time.Now(), 51 } 52 } 53 54 // Recycle checks if the event log has reach a limit and if so 55 // renames the current log and starts a new one. The remove bool 56 // indicates the old log file should be deleted. 57 func (e *Event) Recycle(path string, remove bool) error { 58 return errors.New("not implemented") 59 } 60 61 // ToJSONString returns the event as a json'ified string 62 func (e *Event) ToJSONString() (string, error) { 63 b, err := json.Marshal(e) 64 return string(b), err 65 } 66 67 // ToHumanReadable returns human readable event as a formatted string 68 func (e *Event) ToHumanReadable() string { 69 var humanFormat string 70 switch e.Type { 71 case Container, Pod: 72 humanFormat = fmt.Sprintf("%s %s %s %s (image=%s, name=%s", e.Time, e.Type, e.Status, e.ID, e.Image, e.Name) 73 // check if the container has labels and add it to the output 74 if len(e.Attributes) > 0 { 75 for k, v := range e.Attributes { 76 humanFormat += fmt.Sprintf(", %s=%s", k, v) 77 } 78 } 79 humanFormat += ")" 80 case Network: 81 humanFormat = fmt.Sprintf("%s %s %s %s (container=%s, name=%s)", e.Time, e.Type, e.Status, e.ID, e.ID, e.Network) 82 case Image: 83 humanFormat = fmt.Sprintf("%s %s %s %s %s", e.Time, e.Type, e.Status, e.ID, e.Name) 84 case System: 85 humanFormat = fmt.Sprintf("%s %s %s", e.Time, e.Type, e.Status) 86 case Volume: 87 humanFormat = fmt.Sprintf("%s %s %s %s", e.Time, e.Type, e.Status, e.Name) 88 } 89 return humanFormat 90 } 91 92 // NewEventFromString takes stringified json and converts 93 // it to an event 94 func newEventFromJSONString(event string) (*Event, error) { 95 e := Event{} 96 if err := json.Unmarshal([]byte(event), &e); err != nil { 97 return nil, err 98 } 99 return &e, nil 100 101 } 102 103 // ToString converts a Type to a string 104 func (t Type) String() string { 105 return string(t) 106 } 107 108 // ToString converts a status to a string 109 func (s Status) String() string { 110 return string(s) 111 } 112 113 // StringToType converts string to an EventType 114 func StringToType(name string) (Type, error) { 115 switch name { 116 case Container.String(): 117 return Container, nil 118 case Image.String(): 119 return Image, nil 120 case Network.String(): 121 return Network, nil 122 case Pod.String(): 123 return Pod, nil 124 case System.String(): 125 return System, nil 126 case Volume.String(): 127 return Volume, nil 128 case "": 129 return "", ErrEventTypeBlank 130 } 131 return "", errors.Errorf("unknown event type %q", name) 132 } 133 134 // StringToStatus converts a string to an Event Status 135 // TODO if we add more events, we might consider a go-generator to 136 // create the switch statement 137 func StringToStatus(name string) (Status, error) { 138 switch name { 139 case Attach.String(): 140 return Attach, nil 141 case Build.String(): 142 return Build, nil 143 case Checkpoint.String(): 144 return Checkpoint, nil 145 case Cleanup.String(): 146 return Cleanup, nil 147 case Commit.String(): 148 return Commit, nil 149 case Create.String(): 150 return Create, nil 151 case Exec.String(): 152 return Exec, nil 153 case Exited.String(): 154 return Exited, nil 155 case Export.String(): 156 return Export, nil 157 case History.String(): 158 return History, nil 159 case Import.String(): 160 return Import, nil 161 case Init.String(): 162 return Init, nil 163 case Kill.String(): 164 return Kill, nil 165 case LoadFromArchive.String(): 166 return LoadFromArchive, nil 167 case Mount.String(): 168 return Mount, nil 169 case NetworkConnect.String(): 170 return NetworkConnect, nil 171 case NetworkDisconnect.String(): 172 return NetworkDisconnect, nil 173 case Pause.String(): 174 return Pause, nil 175 case Prune.String(): 176 return Prune, nil 177 case Pull.String(): 178 return Pull, nil 179 case Push.String(): 180 return Push, nil 181 case Refresh.String(): 182 return Refresh, nil 183 case Remove.String(): 184 return Remove, nil 185 case Renumber.String(): 186 return Renumber, nil 187 case Restart.String(): 188 return Restart, nil 189 case Restore.String(): 190 return Restore, nil 191 case Save.String(): 192 return Save, nil 193 case Start.String(): 194 return Start, nil 195 case Stop.String(): 196 return Stop, nil 197 case Sync.String(): 198 return Sync, nil 199 case Tag.String(): 200 return Tag, nil 201 case Unmount.String(): 202 return Unmount, nil 203 case Unpause.String(): 204 return Unpause, nil 205 case Untag.String(): 206 return Untag, nil 207 } 208 return "", errors.Errorf("unknown event status %q", name) 209 } 210 211 func (e EventLogFile) getTail(options ReadOptions) (*tail.Tail, error) { 212 reopen := true 213 seek := tail.SeekInfo{Offset: 0, Whence: os.SEEK_END} 214 if options.FromStart || !options.Stream { 215 seek.Whence = 0 216 reopen = false 217 } 218 stream := options.Stream 219 if len(options.Until) > 0 { 220 stream = false 221 } 222 return tail.TailFile(e.options.LogFilePath, tail.Config{ReOpen: reopen, Follow: stream, Location: &seek, Logger: tail.DiscardingLogger, Poll: true}) 223 }