-
Notifications
You must be signed in to change notification settings - Fork 221
/
podman.go
334 lines (264 loc) · 8.99 KB
/
podman.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Copyright © 2019 – 2021 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package podman
import (
"bytes"
"encoding/json"
"fmt"
"io"
"github.com/HarryMichal/go-version"
"github.com/containers/toolbox/pkg/shell"
"github.com/sirupsen/logrus"
)
var (
podmanVersion string
)
var (
LogLevel = logrus.ErrorLevel
)
// CheckVersion compares provided version with the version of Podman.
//
// Takes in one string parameter that should be in the format that is used for versioning (eg. 1.0.0, 2.5.1-dev).
//
// Returns true if the current version is equal to or higher than the required version.
func CheckVersion(requiredVersion string) bool {
currentVersion, _ := GetVersion()
currentVersion = version.Normalize(currentVersion)
requiredVersion = version.Normalize(requiredVersion)
return version.CompareSimple(currentVersion, requiredVersion) >= 0
}
// ContainerExists checks using Podman if a container with given ID/name exists.
//
// Parameter container is a name or an id of a container.
func ContainerExists(container string) (bool, error) {
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "container", "exists", container}
exitCode, err := shell.RunWithExitCode("podman", nil, nil, nil, args...)
if exitCode != 0 && err == nil {
err = fmt.Errorf("failed to find container %s", container)
}
if err != nil {
return false, err
}
return true, nil
}
// GetContainers is a wrapper function around `podman ps --format json` command.
//
// Parameter args accepts an array of strings to be passed to the wrapped command (eg. ["-a", "--filter", "123"]).
//
// Returned value is a slice of dynamically unmarshalled json, so it needs to be treated properly.
//
// If a problem happens during execution, first argument is nil and second argument holds the error message.
func GetContainers(args ...string) ([]map[string]interface{}, error) {
var stdout bytes.Buffer
logLevelString := LogLevel.String()
args = append([]string{"--log-level", logLevelString, "ps", "--format", "json"}, args...)
if err := shell.Run("podman", nil, &stdout, nil, args...); err != nil {
return nil, err
}
output := stdout.Bytes()
var containers []map[string]interface{}
if err := json.Unmarshal(output, &containers); err != nil {
return nil, err
}
return containers, nil
}
// GetImages is a wrapper function around `podman images --format json` command.
//
// Parameter args accepts an array of strings to be passed to the wrapped command (eg. ["-a", "--filter", "123"]).
//
// Returned value is a slice of dynamically unmarshalled json, so it needs to be treated properly.
//
// If a problem happens during execution, first argument is nil and second argument holds the error message.
func GetImages(args ...string) ([]map[string]interface{}, error) {
var stdout bytes.Buffer
logLevelString := LogLevel.String()
args = append([]string{"--log-level", logLevelString, "images", "--format", "json"}, args...)
if err := shell.Run("podman", nil, &stdout, nil, args...); err != nil {
return nil, err
}
output := stdout.Bytes()
var images []map[string]interface{}
if err := json.Unmarshal(output, &images); err != nil {
return nil, err
}
return images, nil
}
// GetVersion returns version of Podman in a string
func GetVersion() (string, error) {
if podmanVersion != "" {
return podmanVersion, nil
}
var stdout bytes.Buffer
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "version", "--format", "json"}
if err := shell.Run("podman", nil, &stdout, nil, args...); err != nil {
return "", err
}
output := stdout.Bytes()
var jsonoutput map[string]interface{}
if err := json.Unmarshal(output, &jsonoutput); err != nil {
return "", err
}
podmanClientInfoInterface := jsonoutput["Client"]
switch podmanClientInfo := podmanClientInfoInterface.(type) {
case nil:
podmanVersion = jsonoutput["Version"].(string)
case map[string]interface{}:
podmanVersion = podmanClientInfo["Version"].(string)
}
return podmanVersion, nil
}
// ImageExists checks using Podman if an image with given ID/name exists.
//
// Parameter image is a name or an id of an image.
func ImageExists(image string) (bool, error) {
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "image", "exists", image}
exitCode, err := shell.RunWithExitCode("podman", nil, nil, nil, args...)
if exitCode != 0 && err == nil {
err = fmt.Errorf("failed to find image %s", image)
}
if err != nil {
return false, err
}
return true, nil
}
// Inspect is a wrapper around 'podman inspect' command
//
// Parameter 'typearg' takes in values 'container' or 'image' that is passed to the --type flag
func Inspect(typearg string, target string) (map[string]interface{}, error) {
var stdout bytes.Buffer
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "inspect", "--format", "json", "--type", typearg, target}
if err := shell.Run("podman", nil, &stdout, nil, args...); err != nil {
return nil, err
}
output := stdout.Bytes()
var info []map[string]interface{}
if err := json.Unmarshal(output, &info); err != nil {
return nil, err
}
return info[0], nil
}
func IsToolboxContainer(container string) (bool, error) {
info, err := Inspect("container", container)
if err != nil {
return false, fmt.Errorf("failed to inspect container %s", container)
}
labels, _ := info["Config"].(map[string]interface{})["Labels"].(map[string]interface{})
if labels["com.github.containers.toolbox"] != "true" && labels["com.github.debarshiray.toolbox"] != "true" {
return false, fmt.Errorf("%s is not a toolbox container", container)
}
return true, nil
}
func IsToolboxImage(image string) (bool, error) {
info, err := Inspect("image", image)
if err != nil {
return false, fmt.Errorf("failed to inspect image %s", image)
}
if info["Labels"] == nil {
return false, fmt.Errorf("%s is not a toolbox image", image)
}
labels := info["Labels"].(map[string]interface{})
if labels["com.github.containers.toolbox"] != "true" && labels["com.github.debarshiray.toolbox"] != "true" {
return false, fmt.Errorf("%s is not a toolbox image", image)
}
return true, nil
}
// Pull pulls an image
func Pull(imageName string) error {
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "pull", imageName}
if err := shell.Run("podman", nil, nil, nil, args...); err != nil {
return err
}
return nil
}
func RemoveContainer(container string, forceDelete bool) error {
logrus.Debugf("Removing container %s", container)
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "rm"}
if forceDelete {
args = append(args, "--force")
}
args = append(args, container)
exitCode, err := shell.RunWithExitCode("podman", nil, nil, nil, args...)
switch exitCode {
case 0:
if err != nil {
panic("unexpected error: 'podman rm' finished successfully")
}
case 1:
err = fmt.Errorf("container %s does not exist", container)
case 2:
err = fmt.Errorf("container %s is running", container)
default:
err = fmt.Errorf("failed to remove container %s", container)
}
if err != nil {
return err
}
return nil
}
func RemoveImage(image string, forceDelete bool) error {
logrus.Debugf("Removing image %s", image)
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "rmi"}
if forceDelete {
args = append(args, "--force")
}
args = append(args, image)
exitCode, err := shell.RunWithExitCode("podman", nil, nil, nil, args...)
switch exitCode {
case 0:
if err != nil {
panic("unexpected error: 'podman rmi' finished successfully")
}
case 1:
err = fmt.Errorf("image %s does not exist", image)
case 2:
err = fmt.Errorf("image %s has dependent children", image)
default:
err = fmt.Errorf("failed to remove image %s", image)
}
if err != nil {
return err
}
return nil
}
func SetLogLevel(logLevel logrus.Level) {
LogLevel = logLevel
}
func Start(container string, stderr io.Writer) error {
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "start", container}
if err := shell.Run("podman", nil, nil, stderr, args...); err != nil {
return err
}
return nil
}
func SystemMigrate(ociRuntimeRequired string) error {
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "system", "migrate"}
if ociRuntimeRequired != "" {
args = append(args, []string{"--new-runtime", ociRuntimeRequired}...)
}
if err := shell.Run("podman", nil, nil, nil, args...); err != nil {
return err
}
return nil
}