-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodman.go
106 lines (78 loc) · 2.58 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
package main
import (
"github.com/samber/lo"
)
type Podman struct {
Name string
Engine string
WorkDir string
}
func (p *Podman) GetImages(args ...string) ([]string, error) {
cmdArgs := []string{p.Engine, "image", "ls"}
cmdArgs = append(cmdArgs, "--filter", "reference="+p.Name)
cmdArgs = append(cmdArgs, "--format", "{{.ID}}")
cmdArgs = append(cmdArgs, args...)
out, _ := ExecCmd(cmdArgs, true)
return GetResults(out)
}
func (p *Podman) GetContainers(args ...string) ([]string, error) {
cmdArgs := []string{p.Engine, "container", "ls"}
cmdArgs = append(cmdArgs, "--latest")
cmdArgs = append(cmdArgs, "--filter", "ancestor="+p.Name)
cmdArgs = append(cmdArgs, "--format", "{{.ID}}")
cmdArgs = append(cmdArgs, args...)
out, _ := ExecCmd(cmdArgs, true)
return GetResults(out)
}
func (p *Podman) IsBuilt() (bool, error) {
images, err := p.GetImages()
_, exists := lo.First(images)
return exists, err
}
func (p *Podman) IsCreated() (bool, error) {
containers, err := p.GetContainers()
_, exists := lo.First(containers)
return exists, err
}
func (p *Podman) IsRunning() (bool, error) {
containers, err := p.GetContainers("--filter", "status=running")
_, exists := lo.First(containers)
return exists, err
}
func (p *Podman) Build(imageConfig string) (string, error) {
cmdArgs := []string{p.Engine, "image", "build"}
cmdArgs = append(cmdArgs, "--tag", p.Name)
cmdArgs = append(cmdArgs, "--file", "-")
return ExecCmdWithBuffer(cmdArgs, imageConfig, false)
}
func (p *Podman) Create() (string, error) {
cmdArgs := []string{p.Engine, "container", "create"}
cmdArgs = append(cmdArgs, "--net", "host")
cmdArgs = append(cmdArgs, p.Name)
return ExecCmd(cmdArgs, false)
}
func (p *Podman) Start() (string, error) {
containers, _ := p.GetContainers()
container, _ := lo.First(containers)
cmdArgs := []string{p.Engine, "container", "start"}
cmdArgs = append(cmdArgs, container)
return ExecCmd(cmdArgs, false)
}
func (p *Podman) PreExec(args ...string) (string, error) {
containers, _ := p.GetContainers()
container, _ := lo.First(containers)
cmdArgs := []string{p.Engine, "container", "exec"}
cmdArgs = append(cmdArgs, container)
cmdArgs = append(cmdArgs, args...)
return ExecCmd(cmdArgs, true)
}
func (p *Podman) Exec(args ...string) (string, error) {
containers, _ := p.GetContainers()
container, _ := lo.First(containers)
cmdArgs := []string{p.Engine, "container", "exec"}
cmdArgs = append(cmdArgs, "--interactive", "--tty")
cmdArgs = append(cmdArgs, "--workdir", p.WorkDir)
cmdArgs = append(cmdArgs, container)
cmdArgs = append(cmdArgs, args...)
return ExecCmd(cmdArgs, false)
}