-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
246 lines (197 loc) · 7.18 KB
/
main.go
File metadata and controls
246 lines (197 loc) · 7.18 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)
var version = "dev"
var errHelp = errors.New("help requested")
type dockerClient interface {
ContainerInspect(ctx context.Context, containerID string) (container.InspectResponse, error)
ImageInspect(ctx context.Context, imageID string, inspectOpts ...client.ImageInspectOption) (image.InspectResponse, error)
ImagePull(ctx context.Context, ref string, options image.PullOptions) (io.ReadCloser, error)
ContainerStop(ctx context.Context, containerID string, options container.StopOptions) error
ContainerRemove(ctx context.Context, containerID string, options container.RemoveOptions) error
ContainerWait(ctx context.Context, containerID string, condition container.WaitCondition) (<-chan container.WaitResponse, <-chan error)
ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string) (container.CreateResponse, error)
ContainerStart(ctx context.Context, containerID string, options container.StartOptions) error
}
func main() {
os.Exit(run(os.Args[1:], os.Stdout, os.Stderr, newDockerClient))
}
func run(args []string, stdout io.Writer, stderr io.Writer, newClient func() (dockerClient, error)) int {
if err := runCommand(args, stdout, newClient); err != nil {
if errors.Is(err, errHelp) {
return 0
}
fmt.Fprintln(stderr, err)
return 1
}
return 0
}
func runCommand(args []string, stdout io.Writer, newClient func() (dockerClient, error)) error {
if len(args) == 0 {
return errors.New("no container id or name specified")
}
pluginMetadata := map[string]interface{}{
"SchemaVersion": "0.1.0",
"Vendor": "github.com/andreccosta",
"Version": strings.TrimPrefix(version, "v"),
"ShortDescription": "Recreate containers",
"Experimental": true,
}
if args[0] == "docker-cli-plugin-metadata" {
return json.NewEncoder(stdout).Encode(pluginMetadata)
}
targetArgs := args
if args[0] == "recreate" {
targetArgs = args[1:]
}
recreateCmd := flag.NewFlagSet("recreate", flag.ContinueOnError)
recreateCmd.SetOutput(io.Discard)
pullFlag := recreateCmd.Bool("pull", false, "Pull image before recreating the container")
if err := recreateCmd.Parse(targetArgs); err != nil {
if errors.Is(err, flag.ErrHelp) {
recreateCmd.SetOutput(stdout)
recreateCmd.Usage()
return errHelp
}
return fmt.Errorf("parse arguments: %w", err)
}
tail := recreateCmd.Args()
if len(tail) < 1 {
return errors.New("no container id or name specified")
}
cli, err := newClient()
if err != nil {
return fmt.Errorf("create docker client: %w", err)
}
if closer, ok := cli.(interface{ Close() error }); ok {
defer closer.Close()
}
return recreateContainer(context.Background(), cli, stdout, tail[0], *pullFlag)
}
func newDockerClient() (dockerClient, error) {
return client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
}
func recreateContainer(ctx context.Context, cli dockerClient, stdout io.Writer, containerID string, pullFlag bool) error {
originalContainer, err := cli.ContainerInspect(ctx, containerID)
if err != nil {
return fmt.Errorf("inspect container %s: %w", containerID, err)
}
platform, err := resolvePlatform(ctx, cli, originalContainer)
if err != nil {
return err
}
if pullFlag {
img := originalContainer.Config.Image
fmt.Fprintf(stdout, "Pulling image %s ...\n", img)
out, err := cli.ImagePull(ctx, img, image.PullOptions{Platform: formatPlatform(platform)})
if err != nil {
return fmt.Errorf("pull image %s: %w", img, err)
}
defer out.Close()
if _, err := io.Copy(io.Discard, out); err != nil {
return fmt.Errorf("read image pull response: %w", err)
}
}
wasRunning := originalContainer.State != nil && (originalContainer.State.Running || originalContainer.State.Paused)
if wasRunning {
fmt.Fprintf(stdout, "Stopping container %s ...\n", containerID)
if err := cli.ContainerStop(ctx, containerID, container.StopOptions{}); err != nil {
return fmt.Errorf("stop container %s: %w", containerID, err)
}
}
if originalContainer.HostConfig.AutoRemove && wasRunning {
waitCh, errCh := cli.ContainerWait(ctx, containerID, container.WaitConditionRemoved)
select {
case waitResp := <-waitCh:
if waitResp.Error != nil {
return fmt.Errorf("wait for container %s removal: %s", containerID, waitResp.Error.Message)
}
case err := <-errCh:
if err != nil {
return fmt.Errorf("wait for container %s removal: %w", containerID, err)
}
}
} else {
fmt.Fprintf(stdout, "Removing container %s ...\n", containerID)
if err := cli.ContainerRemove(ctx, containerID, container.RemoveOptions{}); err != nil {
return fmt.Errorf("remove container %s: %w", containerID, err)
}
}
name := normalizeContainerName(originalContainer.Name)
networkingConfig := recreateNetworkingConfig(originalContainer.NetworkSettings)
fmt.Fprintln(stdout, "Recreating container ...")
createdContainer, err := cli.ContainerCreate(
ctx,
originalContainer.Config,
originalContainer.HostConfig,
networkingConfig,
platform,
name,
)
if err != nil {
return fmt.Errorf("create replacement container for %s: %w", containerID, err)
}
fmt.Fprintf(stdout, "Starting container %s ...\n", shortContainerID(createdContainer.ID))
if err := cli.ContainerStart(ctx, createdContainer.ID, container.StartOptions{}); err != nil {
return fmt.Errorf("start container %s: %w", createdContainer.ID, err)
}
return nil
}
func resolvePlatform(ctx context.Context, cli dockerClient, inspected container.InspectResponse) (*v1.Platform, error) {
if inspected.ImageManifestDescriptor != nil && inspected.ImageManifestDescriptor.Platform != nil {
platform := *inspected.ImageManifestDescriptor.Platform
return &platform, nil
}
imageDetails, err := cli.ImageInspect(ctx, inspected.Image)
if err != nil {
return nil, fmt.Errorf("inspect image %s: %w", inspected.Image, err)
}
platform := &v1.Platform{
Architecture: imageDetails.Architecture,
OS: imageDetails.Os,
OSVersion: imageDetails.OsVersion,
Variant: imageDetails.Variant,
}
if platform.OS == "" && platform.Architecture == "" && platform.Variant == "" && platform.OSVersion == "" {
return nil, nil
}
return platform, nil
}
func formatPlatform(platform *v1.Platform) string {
if platform == nil || platform.OS == "" || platform.Architecture == "" {
return ""
}
value := platform.OS + "/" + platform.Architecture
if platform.Variant != "" {
value += "/" + platform.Variant
}
return value
}
func normalizeContainerName(name string) string {
return strings.TrimPrefix(name, "/")
}
func shortContainerID(id string) string {
if len(id) <= 10 {
return id
}
return id[:10]
}
func recreateNetworkingConfig(settings *container.NetworkSettings) *network.NetworkingConfig {
if settings == nil || len(settings.Networks) == 0 {
return nil
}
return &network.NetworkingConfig{EndpointsConfig: settings.Networks}
}