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