@@ -13,19 +13,17 @@ import (
1313 "time"
1414
1515 "github.com/docker/cli/cli/command/service/progress"
16- ctr "github.com/docker/docker/api/types/container"
17- "github.com/docker/docker/api/types/filters"
18- "github.com/docker/docker/api/types/swarm"
19- "github.com/docker/docker/api/types/system"
20- "github.com/docker/docker/client"
16+ "github.com/moby/moby/api/types/swarm"
17+ "github.com/moby/moby/client"
2118 "github.com/offen/docker-volume-backup/internal/errwrap"
2219)
2320
2421func scaleService (cli * client.Client , serviceID string , replicas uint64 ) ([]string , error ) {
25- service , _ , err := cli .ServiceInspectWithRaw (context .Background (), serviceID , swarm .ServiceInspectOptions {})
22+ result , err := cli .ServiceInspect (context .Background (), serviceID , client .ServiceInspectOptions {})
2623 if err != nil {
2724 return nil , errwrap .Wrap (err , fmt .Sprintf ("error inspecting service %s" , serviceID ))
2825 }
26+ service := result .Service
2927 serviceMode := & service .Spec .Mode
3028 switch {
3129 case serviceMode .Replicated != nil :
@@ -34,7 +32,7 @@ func scaleService(cli *client.Client, serviceID string, replicas uint64) ([]stri
3432 return nil , errwrap .Wrap (nil , fmt .Sprintf ("service to be scaled %s has to be in replicated mode" , service .Spec .Name ))
3533 }
3634
37- response , err := cli .ServiceUpdate (context .Background (), service .ID , service . Version , service .Spec , swarm. ServiceUpdateOptions { })
35+ response , err := cli .ServiceUpdate (context .Background (), service .ID , client. ServiceUpdateOptions { Version : service .Version , Spec : service . Spec })
3836 if err != nil {
3937 return nil , errwrap .Wrap (err , "error updating service" )
4038 }
@@ -65,30 +63,27 @@ func awaitContainerCountForService(cli *client.Client, serviceID string, count i
6563 ),
6664 )
6765 case <- poll .C :
68- containers , err := cli .ContainerList (context .Background (), ctr.ListOptions {
69- Filters : filters .NewArgs (filters.KeyValuePair {
70- Key : "label" ,
71- Value : fmt .Sprintf ("com.docker.swarm.service.id=%s" , serviceID ),
72- }),
66+ containers , err := cli .ContainerList (context .Background (), client.ContainerListOptions {
67+ Filters : client.Filters {}.Add ("label" , fmt .Sprintf ("com.docker.swarm.service.id=%s" , serviceID )),
7368 })
7469 if err != nil {
7570 return errwrap .Wrap (err , "error listing containers" )
7671 }
77- if len (containers ) == count {
72+ if len (containers . Items ) == count {
7873 return nil
7974 }
8075 }
8176 }
8277}
8378
8479func isSwarm (c interface {
85- Info (context.Context ) (system. Info , error )
80+ Info (context.Context , client. InfoOptions ) (client. SystemInfoResult , error )
8681}) (bool , error ) {
87- info , err := c .Info (context .Background ())
82+ result , err := c .Info (context .Background (), client. InfoOptions {} )
8883 if err != nil {
8984 return false , errwrap .Wrap (err , "error getting docker info" )
9085 }
91- return info . Swarm .LocalNodeState != "" && info . Swarm .LocalNodeState != swarm .LocalNodeStateInactive && info .Swarm .ControlAvailable , nil
86+ return result . Info . Swarm .LocalNodeState != "" && result . Info . Swarm .LocalNodeState != swarm .LocalNodeStateInactive && result . Info .Swarm .ControlAvailable , nil
9287}
9388
9489func hasLabel (labels map [string ]string , key , value string ) bool {
@@ -113,7 +108,6 @@ func (s *script) stopContainersAndServices() (func() error, error) {
113108 if s .cli == nil {
114109 return noop , nil
115110 }
116-
117111 isDockerSwarm , err := isSwarm (s .cli )
118112 if err != nil {
119113 return noop , errwrap .Wrap (err , "error determining swarm state" )
@@ -143,13 +137,13 @@ func (s *script) stopContainersAndServices() (func() error, error) {
143137 s .c .BackupStopDuringBackupNoRestartLabel ,
144138 )
145139
146- allContainers , err := s .cli .ContainerList (context .Background (), ctr. ListOptions {})
140+ allContainers , err := s .cli .ContainerList (context .Background (), client. ContainerListOptions {})
147141 if err != nil {
148142 return noop , errwrap .Wrap (err , "error querying for containers" )
149143 }
150144
151145 var containersToStop []handledContainer
152- for _ , c := range allContainers {
146+ for _ , c := range allContainers . Items {
153147 hasStopDuringBackupLabel , hasStopDuringBackupNoRestartLabel , err := checkStopLabels (c .Labels , labelValue , s .c .BackupStopDuringBackupNoRestartLabel )
154148 if err != nil {
155149 return noop , errwrap .Wrap (err , "error querying for containers to stop" )
@@ -168,7 +162,8 @@ func (s *script) stopContainersAndServices() (func() error, error) {
168162 var allServices []swarm.Service
169163 var servicesToScaleDown []handledSwarmService
170164 if isDockerSwarm {
171- allServices , err = s .cli .ServiceList (context .Background (), swarm.ServiceListOptions {Status : true })
165+ result , err := s .cli .ServiceList (context .Background (), client.ServiceListOptions {Status : true })
166+ allServices := result .Items
172167 if err != nil {
173168 return noop , errwrap .Wrap (err , "error querying for services" )
174169 }
@@ -205,18 +200,18 @@ func (s *script) stopContainersAndServices() (func() error, error) {
205200 if isDockerSwarm {
206201 for _ , container := range containersToStop {
207202 if swarmServiceID , ok := container .summary .Labels ["com.docker.swarm.service.id" ]; ok {
208- parentService , _ , err := s .cli .ServiceInspectWithRaw (context .Background (), swarmServiceID , swarm .ServiceInspectOptions {})
203+ parentService , err := s .cli .ServiceInspect (context .Background (), swarmServiceID , client .ServiceInspectOptions {})
209204 if err != nil {
210205 return noop , errwrap .Wrap (err , fmt .Sprintf ("error querying for parent service with ID %s" , swarmServiceID ))
211206 }
212- for label := range parentService .Spec .Labels {
207+ for label := range parentService .Service . Spec .Labels {
213208 if label == "docker-volume-backup.stop-during-backup" {
214209 return noop , errwrap .Wrap (
215210 nil ,
216211 fmt .Sprintf (
217212 "container %s is labeled to stop but has parent service %s which is also labeled, cannot continue" ,
218213 container .summary .Names [0 ],
219- parentService .Spec .Name ,
214+ parentService .Service . Spec .Name ,
220215 ),
221216 )
222217 }
@@ -229,7 +224,7 @@ func (s *script) stopContainersAndServices() (func() error, error) {
229224 fmt .Sprintf (
230225 "Stopping %d out of %d running container(s) as they were labeled %s or %s." ,
231226 len (containersToStop ),
232- len (allContainers ),
227+ len (allContainers . Items ),
233228 stopDuringBackupLabel ,
234229 stopDuringBackupNoRestartLabel ,
235230 ),
@@ -249,7 +244,7 @@ func (s *script) stopContainersAndServices() (func() error, error) {
249244 var stoppedContainers []handledContainer
250245 var stopErrors []error
251246 for _ , container := range containersToStop {
252- if err := s .cli .ContainerStop (context .Background (), container .summary .ID , ctr. StopOptions {}); err != nil {
247+ if _ , err := s .cli .ContainerStop (context .Background (), container .summary .ID , client. ContainerStopOptions {}); err != nil {
253248 stopErrors = append (stopErrors , err )
254249 } else {
255250 stoppedContainers = append (stoppedContainers , container )
@@ -286,7 +281,7 @@ func (s *script) stopContainersAndServices() (func() error, error) {
286281 }
287282
288283 s .stats .Containers = ContainersStats {
289- All : uint (len (allContainers )),
284+ All : uint (len (allContainers . Items )),
290285 ToStop : uint (len (containersToStop )),
291286 Stopped : uint (len (stoppedContainers )),
292287 StopErrors : uint (len (stopErrors )),
@@ -328,7 +323,8 @@ func (s *script) stopContainersAndServices() (func() error, error) {
328323 // in case a container was part of a swarm service, the service requires to
329324 // be force updated instead of restarting the container as it would otherwise
330325 // remain in a "completed" state
331- service , _ , err := s .cli .ServiceInspectWithRaw (context .Background (), swarmServiceID , swarm.ServiceInspectOptions {})
326+ result , err := s .cli .ServiceInspect (context .Background (), swarmServiceID , client.ServiceInspectOptions {})
327+ service := result .Service
332328 if err != nil {
333329 restartErrors = append (
334330 restartErrors ,
@@ -339,14 +335,14 @@ func (s *script) stopContainersAndServices() (func() error, error) {
339335 service .Spec .TaskTemplate .ForceUpdate += 1
340336 if _ , err := s .cli .ServiceUpdate (
341337 context .Background (), service .ID ,
342- service . Version , service .Spec , swarm. ServiceUpdateOptions { },
338+ client. ServiceUpdateOptions { Spec : service .Spec , Version : service . Version },
343339 ); err != nil {
344340 restartErrors = append (restartErrors , err )
345341 }
346342 continue
347343 }
348344
349- if err := s .cli .ContainerStart (context .Background (), container .summary .ID , ctr. StartOptions {}); err != nil {
345+ if _ , err := s .cli .ContainerStart (context .Background (), container .summary .ID , client. ContainerStartOptions {}); err != nil {
350346 restartErrors = append (restartErrors , err )
351347 } else {
352348 restartedContainers = append (restartedContainers , container )
0 commit comments