-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.go
65 lines (54 loc) · 1.74 KB
/
dev.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
package podutil
import (
"context"
"fmt"
"github.com/rebuy-de/rebuy-go-sdk/v8/pkg/cmdutil"
"github.com/rebuy-de/rebuy-go-sdk/v8/pkg/logutil"
)
func StartDevcontainer(ctx context.Context, conn *Connection, name string, image string, opts ...CreateContainerOption) (*InspectContainerResult, error) {
go func() {
<-ctx.Done()
logutil.Get(ctx).Infof("stopping %q container", name)
err := conn.StopContainer(name)
if err != nil {
logutil.Get(ctx).Errorf("failed stop container %q: %s", name, err.Error())
}
}()
// API is low level, so we must take care about image pulling ourselves.
err := conn.PullImage(PullImageInput{
Reference: image,
Policy: ImagePullPolicyMissing,
})
if err != nil {
return nil, fmt.Errorf("pulling image %q: %w", image, err)
}
container, err := conn.InspectContainer(name)
if err != nil {
return nil, fmt.Errorf("inspecting existing container: %w", err)
}
if container != nil {
logutil.Get(ctx).Warning("container already exists, hence reusing it")
} else {
_, err := conn.CreateContainer(CreateContainerInput{
Name: name,
Image: image,
PublishImagePorts: true,
Remove: true, // always remove after stop
// Make sure we do not have orphan pods when the go app gets killed
// hard. Ideally we would make the timeout shorter and reset it
// once in a while, but this does not seem possible with the podman
// API.
TimeoutSeconds: 3600 * 8,
}, opts...)
cmdutil.Must(err)
container, err = conn.InspectContainer(name)
if err != nil {
return nil, fmt.Errorf("inspecting new container: %w", err)
}
}
err = conn.StartContainer(name)
if err != nil {
return nil, fmt.Errorf("starting container: %w", err)
}
return container, nil
}