github.com/AbhinandanKurakure/podman/v3@v3.4.10/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  // ToString converts a Type to a string
   103  func (t Type) String() string {
   104  	return string(t)
   105  }
   106  
   107  // ToString converts a status to a string
   108  func (s Status) String() string {
   109  	return string(s)
   110  }
   111  
   112  // StringToType converts string to an EventType
   113  func StringToType(name string) (Type, error) {
   114  	switch name {
   115  	case Container.String():
   116  		return Container, nil
   117  	case Image.String():
   118  		return Image, nil
   119  	case Network.String():
   120  		return Network, nil
   121  	case Pod.String():
   122  		return Pod, nil
   123  	case System.String():
   124  		return System, nil
   125  	case Volume.String():
   126  		return Volume, nil
   127  	case "":
   128  		return "", ErrEventTypeBlank
   129  	}
   130  	return "", errors.Errorf("unknown event type %q", name)
   131  }
   132  
   133  // StringToStatus converts a string to an Event Status
   134  // TODO if we add more events, we might consider a go-generator to
   135  // create the switch statement
   136  func StringToStatus(name string) (Status, error) {
   137  	switch name {
   138  	case Attach.String():
   139  		return Attach, nil
   140  	case Build.String():
   141  		return Build, nil
   142  	case Checkpoint.String():
   143  		return Checkpoint, nil
   144  	case Cleanup.String():
   145  		return Cleanup, nil
   146  	case Commit.String():
   147  		return Commit, nil
   148  	case Create.String():
   149  		return Create, nil
   150  	case Exec.String():
   151  		return Exec, nil
   152  	case ExecDied.String():
   153  		return ExecDied, nil
   154  	case Exited.String():
   155  		return Exited, nil
   156  	case Export.String():
   157  		return Export, nil
   158  	case History.String():
   159  		return History, nil
   160  	case Import.String():
   161  		return Import, nil
   162  	case Init.String():
   163  		return Init, nil
   164  	case Kill.String():
   165  		return Kill, nil
   166  	case LoadFromArchive.String():
   167  		return LoadFromArchive, nil
   168  	case Mount.String():
   169  		return Mount, nil
   170  	case NetworkConnect.String():
   171  		return NetworkConnect, nil
   172  	case NetworkDisconnect.String():
   173  		return NetworkDisconnect, nil
   174  	case Pause.String():
   175  		return Pause, nil
   176  	case Prune.String():
   177  		return Prune, nil
   178  	case Pull.String():
   179  		return Pull, nil
   180  	case Push.String():
   181  		return Push, nil
   182  	case Refresh.String():
   183  		return Refresh, nil
   184  	case Remove.String():
   185  		return Remove, nil
   186  	case Renumber.String():
   187  		return Renumber, nil
   188  	case Restart.String():
   189  		return Restart, nil
   190  	case Restore.String():
   191  		return Restore, nil
   192  	case Save.String():
   193  		return Save, nil
   194  	case Start.String():
   195  		return Start, nil
   196  	case Stop.String():
   197  		return Stop, nil
   198  	case Sync.String():
   199  		return Sync, nil
   200  	case Tag.String():
   201  		return Tag, nil
   202  	case Unmount.String():
   203  		return Unmount, nil
   204  	case Unpause.String():
   205  		return Unpause, nil
   206  	case Untag.String():
   207  		return Untag, nil
   208  	}
   209  	return "", errors.Errorf("unknown event status %q", name)
   210  }
   211  
   212  func (e EventLogFile) getTail(options ReadOptions) (*tail.Tail, error) {
   213  	reopen := true
   214  	seek := tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}
   215  	if options.FromStart || !options.Stream {
   216  		seek.Whence = 0
   217  		reopen = false
   218  	}
   219  	stream := options.Stream
   220  	return tail.TailFile(e.options.LogFilePath, tail.Config{ReOpen: reopen, Follow: stream, Location: &seek, Logger: tail.DiscardingLogger, Poll: true})
   221  }