diff options
Diffstat (limited to 'pkg/bindings/system/system.go')
-rw-r--r-- | pkg/bindings/system/system.go | 86 |
1 files changed, 74 insertions, 12 deletions
diff --git a/pkg/bindings/system/system.go b/pkg/bindings/system/system.go index df6b529de..010762bef 100644 --- a/pkg/bindings/system/system.go +++ b/pkg/bindings/system/system.go @@ -3,11 +3,14 @@ package system import ( "context" "encoding/json" + "fmt" "io" "net/http" "net/url" "strconv" + "time" + "github.com/containers/libpod/libpod/define" "github.com/containers/libpod/pkg/bindings" "github.com/containers/libpod/pkg/domain/entities" "github.com/pkg/errors" @@ -17,7 +20,7 @@ import ( // Events allows you to monitor libdpod related events like container creation and // removal. The events are then passed to the eventChan provided. The optional cancelChan // can be used to cancel the read of events and close down the HTTP connection. -func Events(ctx context.Context, eventChan chan (entities.Event), cancelChan chan bool, since, until *string, filters map[string][]string) error { +func Events(ctx context.Context, eventChan chan entities.Event, cancelChan chan bool, since, until *string, filters map[string][]string, stream *bool) error { conn, err := bindings.GetClient(ctx) if err != nil { return err @@ -29,6 +32,9 @@ func Events(ctx context.Context, eventChan chan (entities.Event), cancelChan cha if until != nil { params.Set("until", *until) } + if stream != nil { + params.Set("stream", strconv.FormatBool(*stream)) + } if filters != nil { filterString, err := bindings.FiltersToString(filters) if err != nil { @@ -36,7 +42,7 @@ func Events(ctx context.Context, eventChan chan (entities.Event), cancelChan cha } params.Set("filters", filterString) } - response, err := conn.DoRequest(nil, http.MethodGet, "/events", params) + response, err := conn.DoRequest(nil, http.MethodGet, "/events", params, nil) if err != nil { return err } @@ -47,18 +53,24 @@ func Events(ctx context.Context, eventChan chan (entities.Event), cancelChan cha logrus.Error(errors.Wrap(err, "unable to close event response body")) }() } + dec := json.NewDecoder(response.Body) - for { - e := entities.Event{} - if err := dec.Decode(&e); err != nil { - if err == io.EOF { - break - } - return errors.Wrap(err, "unable to decode event response") + for err = (error)(nil); err == nil; { + var e = entities.Event{} + err = dec.Decode(&e) + if err == nil { + eventChan <- e } - eventChan <- e } - return nil + close(eventChan) + switch { + case err == nil: + return nil + case errors.Is(err, io.EOF): + return nil + default: + return errors.Wrap(err, "unable to decode event response") + } } // Prune removes all unused system data. @@ -77,7 +89,57 @@ func Prune(ctx context.Context, all, volumes *bool) (*entities.SystemPruneReport if volumes != nil { params.Set("Volumes", strconv.FormatBool(*volumes)) } - response, err := conn.DoRequest(nil, http.MethodPost, "/system/prune", params) + response, err := conn.DoRequest(nil, http.MethodPost, "/system/prune", params, nil) + if err != nil { + return nil, err + } + return &report, response.Process(&report) +} + +func Version(ctx context.Context) (*entities.SystemVersionReport, error) { + var report entities.SystemVersionReport + var component entities.ComponentVersion + + version, err := define.GetVersion() + if err != nil { + return nil, err + } + report.Client = &version + + conn, err := bindings.GetClient(ctx) + if err != nil { + return nil, err + } + response, err := conn.DoRequest(nil, http.MethodGet, "/version", nil, nil) + if err != nil { + return nil, err + } + + if err = response.Process(&component); err != nil { + return nil, err + } + f, _ := strconv.ParseFloat(component.APIVersion, 64) + b, _ := time.Parse(time.RFC3339, component.BuildTime) + report.Server = &define.Version{ + APIVersion: int64(f), + Version: component.Version.Version, + GoVersion: component.GoVersion, + GitCommit: component.GitCommit, + Built: b.Unix(), + OsArch: fmt.Sprintf("%s/%s", component.Os, component.Arch), + } + return &report, err +} + +// DiskUsage returns information about image, container, and volume disk +// consumption +func DiskUsage(ctx context.Context) (*entities.SystemDfReport, error) { + var report entities.SystemDfReport + conn, err := bindings.GetClient(ctx) + if err != nil { + return nil, err + } + response, err := conn.DoRequest(nil, http.MethodGet, "/system/df", nil, nil) if err != nil { return nil, err } |