/
container.go
195 lines (163 loc) · 4.41 KB
/
container.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
package container
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/opencontainers/go-digest"
"github.com/rs/zerolog"
"github.com/terrails/yacu/types/image"
"github.com/terrails/yacu/utils"
yacutypes "github.com/terrails/yacu/types"
)
type Container struct {
Raw *types.ContainerJSON
ID string
Name string
Labels map[string]string
Image *image.ImageData
Repository reference.NamedTagged
StopTimeout int
MinImageAge int
}
type Containers []*Container
func New(client *client.Client, data *types.ContainerJSON, stopTimeout, minImageAge int) (*Container, error) {
named, err := reference.ParseNormalizedNamed(data.Config.Image)
if err != nil {
return nil, err
}
named = reference.TagNameOnly(named)
namedTagged, ok := named.(reference.NamedTagged)
// only allow tagged images
if !ok {
return nil, yacutypes.ErrRepositoryNotTagged
}
imageRaw, _, err := client.ImageInspectWithRaw(context.Background(), data.Image)
if err != nil {
return nil, err
}
imageData, err := image.NewData(&imageRaw, namedTagged)
if err != nil {
return nil, err
}
if val, ok := data.Config.Labels[LABEL_STOP_TIMEOUT]; ok {
if ival, err := strconv.ParseInt(val, 10, 0); err == nil {
stopTimeout = int(ival)
}
}
if val, ok := data.Config.Labels[LABEL_IMAGE_AGE]; ok {
if ival, err := strconv.ParseInt(val, 10, 0); err == nil {
minImageAge = int(ival)
}
}
return &Container{
Raw: data,
ID: data.ID,
Name: data.Name,
Labels: data.Config.Labels,
Image: imageData,
Repository: namedTagged,
StopTimeout: stopTimeout,
MinImageAge: minImageAge,
}, nil
}
func (c *Container) IsRunning() bool {
return c.Raw.State.Status == "running"
}
func (c *Container) IsYacu() bool {
return strings.HasPrefix(reference.Path(c.Repository), "terrails/yacu")
}
func (c *Container) RepositoryFamiliarized() string {
return utils.FamiliarTagged(c.Repository)
}
func (c *Container) HasRepoDigest(digest digest.Digest) bool {
for _, str := range c.Image.Raw.RepoDigests {
if strings.Contains(str, digest.String()) {
return true
}
}
return false
}
func (c *Container) CleanImageId() string {
// image ID without hash (sha256:) portion
split := strings.SplitN(c.Image.ID, ":", 2)
if len(split) == 1 {
return c.Image.ID
} else {
return split[1]
}
}
func (c *Container) ShouldScan(all, stopped bool) bool {
// disable updating self for now
// TODO: implement
if c.IsYacu() {
return false
}
if !stopped && !c.IsRunning() {
return false
}
if val, ok := c.Labels[LABEL_ENABLE]; ok {
if bval, err := strconv.ParseBool(val); err == nil {
return bval
} else {
// do not scan if label value is invalid
return false
}
}
return all
}
func (c *Container) IsOutdated() (bool, error) {
// parse created time from container image
createdTime := c.Image.Created
if utils.DaysPassed(createdTime) < c.MinImageAge {
return false, nil
}
return true, nil
}
func (c *Container) Stop(ctx context.Context, client *client.Client) error {
logger := c.logger(ctx)
logger.Debug().Msg("Attempting to stop container")
if err := client.ContainerStop(
context.Background(),
c.ID,
container.StopOptions{
Timeout: &c.StopTimeout,
},
); err != nil {
logger.Err(err).Msg("Failed to stop container")
return fmt.Errorf("failed to stop container %s: %w", c.Name, err)
}
logger.Debug().Msg("Stopped container")
return nil
}
func (c *Container) Start(ctx context.Context, client *client.Client) error {
logger := c.logger(ctx)
logger.Debug().Msg("Attempting to start container")
// used to avoid odd `failed to create shim task` entrypoint errors when starting a newly created container
for i := 0; i < 3; i++ {
// do max 3 retries, if it fails after then its just broken
if err := client.ContainerStart(
context.Background(),
c.ID,
types.ContainerStartOptions{},
); err != nil {
if i == 2 {
logger.Err(err).Msg("Failed to start container")
return fmt.Errorf("failed to start container %s: %w", c.Name, err)
}
time.Sleep(time.Second)
continue
}
break
}
return nil
}
func (c *Container) logger(ctx context.Context) *zerolog.Logger {
logger := zerolog.Ctx(ctx).With().Str("container", c.Name).Str("repository", c.Repository.String()).Logger()
return &logger
}