-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli_plugin.go
69 lines (60 loc) · 1.79 KB
/
cli_plugin.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
package podman
import (
"fmt"
"io"
log "go.arcalot.io/log/v2"
"go.flow.arcalot.io/podmandeployer/internal/cliwrapper"
)
type CliPlugin struct {
wrapper cliwrapper.CliWrapper
containerImage string
containerName string
config *Config
logger log.Logger
stdin io.WriteCloser
stdout io.ReadCloser
}
func (p *CliPlugin) Write(b []byte) (n int, err error) {
return p.stdin.Write(b)
}
func (p *CliPlugin) Read(b []byte) (n int, err error) {
return p.stdout.Read(b)
}
func (p *CliPlugin) Close() error {
containerRunning, err := p.wrapper.ContainerRunning(p.containerImage)
if err != nil {
p.logger.Warningf("error while checking if container exists (%s);"+
" killing container in case it still exists", err.Error())
} else if containerRunning {
p.logger.Infof("container %s still exists; killing container", p.containerName)
}
var killErr error
if err != nil || containerRunning {
killErr = p.wrapper.Kill(p.containerName)
}
// Still clean up even if the kill fails. Clean() uses the --force parameter, so that
// will be another attempt at killing the container.
cleanErr := p.wrapper.Clean(p.containerName)
if err := p.stdin.Close(); err != nil {
p.logger.Warningf("failed to close stdin pipe")
} else {
p.logger.Debugf("stdin pipe successfully closed")
}
if err := p.stdout.Close(); err != nil {
p.logger.Warningf("failed to close stdout pipe")
} else {
p.logger.Debugf("stdout pipe successfully closed")
}
switch {
case killErr != nil && cleanErr != nil:
return fmt.Errorf("error while killing container (%s) and cleaning up container (%s)", killErr.Error(), cleanErr.Error())
case killErr != nil:
return killErr
case cleanErr != nil:
return cleanErr
}
return nil
}
func (p *CliPlugin) ID() string {
return p.containerName
}