@@ -6,15 +6,12 @@ import (
66 "fmt"
77 "io"
88 "net/http"
9- "os"
109 "sort"
1110 "strings"
1211
1312 "github.com/docker/docker/api/types/container"
14- "github.com/docker/docker/api/types/image"
1513 "github.com/docker/docker/api/types/network"
1614 "github.com/docker/docker/client"
17- "github.com/rs/zerolog"
1815
1916 "github.com/Masterminds/semver/v3"
2017)
@@ -26,11 +23,11 @@ import (
2623
2724// UpgradeContainer stops a container, removes it, and creates a new one with the specified image
2825func UpgradeContainer (ctx context.Context , containerName , newImage string ) error {
29- L = L .With ().
26+ l : = L .With ().
3027 Str ("Container" , containerName ).
3128 Str ("Image" , newImage ).
3229 Logger ()
33- L .Info ().Msg ("Upgrading container" )
30+ l .Info ().Msg ("Upgrading container" )
3431 cli , err := client .NewClientWithOpts (
3532 client .FromEnv ,
3633 client .WithAPIVersionNegotiation (),
@@ -43,31 +40,22 @@ func UpgradeContainer(ctx context.Context, containerName, newImage string) error
4340 if err != nil {
4441 return fmt .Errorf ("failed to inspect container %s: %w" , containerName , err )
4542 }
46- L .Debug ().Msg ("Stopping container" )
43+ l .Debug ().Msg ("Stopping container" )
4744 stopOpts := container.StopOptions {}
4845 if err := cli .ContainerStop (ctx , containerName , stopOpts ); err != nil {
4946 return fmt .Errorf ("failed to stop container %s: %w" , containerName , err )
5047 }
51- L .Debug ().Msg ("Removing container" )
48+ l .Debug ().Msg ("Removing container" )
5249 // keep the volumes
5350 removeOpts := container.RemoveOptions {RemoveVolumes : false }
5451 if err := cli .ContainerRemove (ctx , containerName , removeOpts ); err != nil {
5552 return fmt .Errorf ("failed to remove container %s: %w" , containerName , err )
5653 }
57- L .Debug ().Msg ("Pulling new image" )
58- pullReader , err := cli .ImagePull (ctx , newImage , image.PullOptions {})
59- if err != nil {
54+ l .Debug ().Msg ("Pulling new image" )
55+ if _ , err := ExecCmdWithContext (ctx , fmt .Sprintf ("docker pull %s" , newImage )); err != nil {
6056 return fmt .Errorf ("failed to pull image %s: %w" , newImage , err )
6157 }
62- defer pullReader .Close ()
63-
64- // log pull process for debug
65- if L .GetLevel () <= zerolog .DebugLevel {
66- _ , _ = io .Copy (os .Stdout , pullReader )
67- } else {
68- _ , _ = io .Copy (io .Discard , pullReader )
69- }
70- L .Debug ().Msg ("Image pulled successfully" )
58+ l .Debug ().Msg ("Image pulled successfully" )
7159
7260 inspect .Config .Image = newImage
7361
@@ -89,15 +77,15 @@ func UpgradeContainer(ctx context.Context, containerName, newImage string) error
8977 if err != nil {
9078 return fmt .Errorf ("failed to create container with image %s: %w" , newImage , err )
9179 }
92- L .Debug ().
80+ l .Debug ().
9381 Str ("ContainerID" , createResp .ID ).
9482 Msg ("Container created" )
95- L .Debug ().Msg ("Starting new container" )
83+ l .Debug ().Msg ("Starting new container" )
9684 startOpts := container.StartOptions {}
9785 if err := cli .ContainerStart (ctx , createResp .ID , startOpts ); err != nil {
9886 return fmt .Errorf ("failed to start container %s: %w" , containerName , err )
9987 }
100- L .Info ().
88+ l .Info ().
10189 Str ("ContainerID" , createResp .ID [:12 ]).
10290 Msg ("Container successfully rebooted with new image" )
10391 return nil
@@ -290,4 +278,4 @@ func matchesFilter(version string, include, exclude []string) bool {
290278 }
291279 }
292280 return false
293- }
281+ }
0 commit comments