aboutsummaryrefslogtreecommitdiff
path: root/pkg/bindings/system/system.go
blob: e567e7a86bc69d9def216cb35edddbc00d474c8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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"
	"github.com/sirupsen/logrus"
)

// 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 {
	conn, err := bindings.GetClient(ctx)
	if err != nil {
		return err
	}
	params := url.Values{}
	if since != nil {
		params.Set("since", *since)
	}
	if until != nil {
		params.Set("until", *until)
	}
	if filters != nil {
		filterString, err := bindings.FiltersToString(filters)
		if err != nil {
			return errors.Wrap(err, "invalid filters")
		}
		params.Set("filters", filterString)
	}
	response, err := conn.DoRequest(nil, http.MethodGet, "/events", params)
	if err != nil {
		return err
	}
	if cancelChan != nil {
		go func() {
			<-cancelChan
			err = response.Body.Close()
			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")
		}
		eventChan <- e
	}
	return nil
}

// Prune removes all unused system data.
func Prune(ctx context.Context, all, volumes *bool) (*entities.SystemPruneReport, error) {
	var (
		report entities.SystemPruneReport
	)
	conn, err := bindings.GetClient(ctx)
	if err != nil {
		return nil, err
	}
	params := url.Values{}
	if all != nil {
		params.Set("All", strconv.FormatBool(*all))
	}
	if volumes != nil {
		params.Set("Volumes", strconv.FormatBool(*volumes))
	}
	response, err := conn.DoRequest(nil, http.MethodPost, "/system/prune", params)
	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)
	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{
		RemoteAPIVersion: 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
}

// Reset removes all unused system data.
func Reset(ctx context.Context) error {
	conn, err := bindings.GetClient(ctx)
	if err != nil {
		return err
	}
	response, err := conn.DoRequest(nil, http.MethodPost, "/system/reset", nil)
	if err != nil {
		return err
	}
	return response.Process(response)
}

// 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)
	if err != nil {
		return nil, err
	}
	return &report, response.Process(&report)
}