github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/libpod/events/events.go (about)

     1  package events
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/containers/storage/pkg/stringid"
     9  	"github.com/pkg/errors"
    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  		// check if the container has labels and add it to the output
    81  		if len(e.Attributes) > 0 {
    82  			for k, v := range e.Attributes {
    83  				humanFormat += fmt.Sprintf(", %s=%s", k, v)
    84  			}
    85  		}
    86  		humanFormat += ")"
    87  	case Network:
    88  		humanFormat = fmt.Sprintf("%s %s %s %s (container=%s, name=%s)", e.Time, e.Type, e.Status, id, id, e.Network)
    89  	case Image:
    90  		humanFormat = fmt.Sprintf("%s %s %s %s %s", e.Time, e.Type, e.Status, id, e.Name)
    91  	case System:
    92  		if e.Name != "" {
    93  			humanFormat = fmt.Sprintf("%s %s %s %s", e.Time, e.Type, e.Status, e.Name)
    94  		} else {
    95  			humanFormat = fmt.Sprintf("%s %s %s", e.Time, e.Type, e.Status)
    96  		}
    97  	case Volume, Machine:
    98  		humanFormat = fmt.Sprintf("%s %s %s %s", e.Time, e.Type, e.Status, e.Name)
    99  	}
   100  	return humanFormat
   101  }
   102  
   103  // NewEventFromString takes stringified json and converts
   104  // it to an event
   105  func newEventFromJSONString(event string) (*Event, error) {
   106  	e := new(Event)
   107  	if err := json.Unmarshal([]byte(event), e); err != nil {
   108  		return nil, err
   109  	}
   110  	return e, nil
   111  }
   112  
   113  // String converts a Type to a string
   114  func (t Type) String() string {
   115  	return string(t)
   116  }
   117  
   118  // String converts a status to a string
   119  func (s Status) String() string {
   120  	return string(s)
   121  }
   122  
   123  // StringToType converts string to an EventType
   124  func StringToType(name string) (Type, error) {
   125  	switch name {
   126  	case Container.String():
   127  		return Container, nil
   128  	case Image.String():
   129  		return Image, nil
   130  	case Machine.String():
   131  		return Machine, nil
   132  	case Network.String():
   133  		return Network, nil
   134  	case Pod.String():
   135  		return Pod, nil
   136  	case System.String():
   137  		return System, nil
   138  	case Volume.String():
   139  		return Volume, nil
   140  	case "":
   141  		return "", ErrEventTypeBlank
   142  	}
   143  	return "", errors.Errorf("unknown event type %q", name)
   144  }
   145  
   146  // StringToStatus converts a string to an Event Status
   147  func StringToStatus(name string) (Status, error) {
   148  	switch name {
   149  	case Attach.String():
   150  		return Attach, nil
   151  	case AutoUpdate.String():
   152  		return AutoUpdate, nil
   153  	case Build.String():
   154  		return Build, nil
   155  	case Checkpoint.String():
   156  		return Checkpoint, nil
   157  	case Cleanup.String():
   158  		return Cleanup, nil
   159  	case Commit.String():
   160  		return Commit, nil
   161  	case Create.String():
   162  		return Create, nil
   163  	case Exec.String():
   164  		return Exec, nil
   165  	case ExecDied.String():
   166  		return ExecDied, nil
   167  	case Exited.String():
   168  		return Exited, nil
   169  	case Export.String():
   170  		return Export, nil
   171  	case History.String():
   172  		return History, nil
   173  	case Import.String():
   174  		return Import, nil
   175  	case Init.String():
   176  		return Init, nil
   177  	case Kill.String():
   178  		return Kill, nil
   179  	case LoadFromArchive.String():
   180  		return LoadFromArchive, nil
   181  	case Mount.String():
   182  		return Mount, nil
   183  	case NetworkConnect.String():
   184  		return NetworkConnect, nil
   185  	case NetworkDisconnect.String():
   186  		return NetworkDisconnect, nil
   187  	case Pause.String():
   188  		return Pause, nil
   189  	case Prune.String():
   190  		return Prune, nil
   191  	case Pull.String():
   192  		return Pull, nil
   193  	case Push.String():
   194  		return Push, nil
   195  	case Refresh.String():
   196  		return Refresh, nil
   197  	case Remove.String():
   198  		return Remove, nil
   199  	case Rename.String():
   200  		return Rename, nil
   201  	case Renumber.String():
   202  		return Renumber, nil
   203  	case Restart.String():
   204  		return Restart, nil
   205  	case Restore.String():
   206  		return Restore, nil
   207  	case Rotate.String():
   208  		return Rotate, nil
   209  	case Save.String():
   210  		return Save, nil
   211  	case Start.String():
   212  		return Start, nil
   213  	case Stop.String():
   214  		return Stop, nil
   215  	case Sync.String():
   216  		return Sync, nil
   217  	case Tag.String():
   218  		return Tag, nil
   219  	case Unmount.String():
   220  		return Unmount, nil
   221  	case Unpause.String():
   222  		return Unpause, nil
   223  	case Untag.String():
   224  		return Untag, nil
   225  	}
   226  	return "", errors.Errorf("unknown event status %q", name)
   227  }