/
update.go
196 lines (153 loc) · 4.25 KB
/
update.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
package update
import (
"context"
"errors"
"fmt"
"time"
"github.com/rs/zerolog/log"
"github.com/dyrector-io/dyrectorio/golang/internal/helper/docker"
"github.com/dyrector-io/dyrectorio/golang/internal/helper/image"
"github.com/dyrector-io/dyrectorio/golang/pkg/dagent/utils"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
containerbuilder "github.com/dyrector-io/dyrectorio/golang/pkg/builder/container"
)
var _selfUpdateDeadline *int64
func getUniqueContainerName(ctx context.Context, base string) (string, error) {
name := base
count := 0
for {
container, err := docker.GetContainerByName(ctx, name)
if err != nil {
return base, err
}
if container == nil {
return name, nil
}
count++
name = base + fmt.Sprint(count)
}
}
// TODO(robot9706): move to utils
func findImageAndPull(ctx context.Context, imageName string) (string, error) {
exists, err := image.Exists(ctx, nil, imageName)
if err != nil {
return "", err
}
if !exists {
err = image.Pull(ctx, nil, imageName, "")
if err != nil {
return "", err
}
}
findImage, err := image.GetImageByReference(ctx, imageName)
if err != nil {
return "", err
}
return findImage.ID, nil
}
func createNewDAgentContainer(ctx context.Context, cli *client.Client, oldContainerID, name, imageWithTag string) error {
inspect, err := cli.ContainerInspect(ctx, oldContainerID)
if err != nil {
return err
}
mounts := []mount.Mount{}
for _, elem := range inspect.Mounts {
mounts = append(mounts, mount.Mount{
Type: elem.Type,
Source: elem.Source,
Target: elem.Destination,
})
}
log.Debug().Msg("Creating new DAgent")
builder := containerbuilder.NewDockerBuilder(ctx).
WithClient(cli).
WithImage(imageWithTag).
WithRestartPolicy(containerbuilder.RestartPolicyName(inspect.HostConfig.RestartPolicy.Name)).
WithName(name).
WithEnv(inspect.Config.Env).
WithMountPoints(mounts)
cont, err := builder.CreateAndStart()
if err != nil {
return err
}
log.Debug().Str("containerID", *cont.GetContainerID()).Msg("Created new dagent")
return nil
}
func SelfUpdate(ctx context.Context, tag string, timeoutSeconds int32) error {
if _selfUpdateDeadline != nil && time.Now().Unix() > *_selfUpdateDeadline {
return errors.New("update already in progress")
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
container, err := utils.GetOwnContainer(ctx)
if err != nil {
return err
}
newImage, err := image.ExpandImageNameWithTag(container.Image, tag)
if err != nil {
return err
}
log.Info().Str("newImage", newImage).Msg("Finding new image")
newImageID, err := findImageAndPull(ctx, newImage)
if err != nil {
return err
}
ownImage, err := utils.GetOwnContainerImage()
if err != nil {
return err
}
if newImageID == ownImage.ID {
return errors.New("already using desired image")
}
originalName := container.Names[0]
rename, err := getUniqueContainerName(ctx, originalName+"-update")
if err != nil {
return err
}
log.Debug().Str("oldName", originalName).Str("newName", rename).Msg("Renaming DAgent container")
err = cli.ContainerRename(ctx, container.ID, rename)
if err != nil {
return err
}
err = createNewDAgentContainer(ctx, cli, container.ID, originalName, newImage)
if err != nil {
renameErr := cli.ContainerRename(ctx, container.ID, originalName)
if renameErr != nil {
return fmt.Errorf("%s (%s)", err.Error(), renameErr.Error())
}
return err
}
unixTime := time.Now().Unix() + int64(timeoutSeconds)
_selfUpdateDeadline = &unixTime
return err
}
func RemoveSelf(ctx context.Context) error {
if _selfUpdateDeadline == nil {
return nil
}
if time.Now().Unix() > *_selfUpdateDeadline {
log.Warn().Int64("startedAtUnix", *_selfUpdateDeadline).Msg("Update timed out")
_selfUpdateDeadline = nil
return nil
}
log.Info().Msg("Update finished, shutting down")
self, err := utils.GetOwnContainer(ctx)
if err != nil {
return err
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
err = cli.ContainerRemove(ctx, self.ID, types.ContainerRemoveOptions{
Force: true,
})
if err != nil {
return err
}
return nil
}