diff --git a/cmd/backup/main.go b/cmd/backup/main.go index eda6019c..0dfdbb25 100644 --- a/cmd/backup/main.go +++ b/cmd/backup/main.go @@ -44,12 +44,16 @@ func main() { }() s.must(s.withLabeledCommands(lifecyclePhaseArchive, func() error { - restartContainers, err := s.stopContainers() + if s.cli == nil { + return nil + } + + restartContainersOrServices, err := s.stopContainersOrServices() // The mechanism for restarting containers is not using hooks as it // should happen as soon as possible (i.e. before uploading backups or // similar). defer func() { - s.must(restartContainers()) + s.must(restartContainersOrServices()) }() if err != nil { return err diff --git a/cmd/backup/script.go b/cmd/backup/script.go index 29d8d730..b148679a 100644 --- a/cmd/backup/script.go +++ b/cmd/backup/script.go @@ -33,7 +33,6 @@ import ( "github.com/docker/docker/api/types" ctr "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" - "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" "github.com/leekchan/timeutil" "github.com/offen/envconfig" @@ -318,14 +317,63 @@ func newScript() (*script, error) { return s, nil } -// stopContainers stops all Docker containers that are marked as to being -// stopped during the backup and returns a function that can be called to -// restart everything that has been stopped. -func (s *script) stopContainers() (func() error, error) { +func (s *script) stopContainersOrServices() (func() error, error) { if s.cli == nil { return noop, nil } + dockerInfo, err := s.cli.Info(context.Background()) + if err != nil { + return noop, fmt.Errorf("stopContainers: error getting docker info: %w", err) + } + isDockerSwarm := dockerInfo.Swarm.LocalNodeState != "inactive" + if isDockerSwarm { + return s.stopServices() + } + return s.stopContainers() + +} + +func (s *script) stopServices() (func() error, error) { + serviceLabel := fmt.Sprintf( + "docker-volume-backup.stop-during-backup=%s", + s.c.BackupStopContainerLabel, + ) + matchingServices, err := s.cli.ServiceList(context.Background(), types.ServiceListOptions{ + Filters: filters.NewArgs(filters.KeyValuePair{ + Key: "label", + Value: serviceLabel, + }), + }) + if err != nil { + return noop, fmt.Errorf("stopServices: error querying services: %w", err) + } + + for _, service := range matchingServices { + var zero uint64 + service.Spec.Mode.Replicated.Replicas = &zero + _, err := s.cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + if err != nil { + return noop, fmt.Errorf("stopServices: error scaling down services: %w", err) + } + } + + return func() error { + for _, service := range matchingServices { + service.Spec.Mode.Replicated.Replicas = service.PreviousSpec.Mode.Replicated.Replicas + _, err := s.cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{}) + if err != nil { + return fmt.Errorf("stopServices: error scaling up services: %w", err) + } + } + return nil + }, nil +} + +// stopContainers stops all Docker containers or services that are marked as to being +// stopped during the backup and returns a function that can be called to +// restart everything that has been stopped. +func (s *script) stopContainers() (func() error, error) { allContainers, err := s.cli.ContainerList(context.Background(), types.ContainerListOptions{}) if err != nil { return noop, fmt.Errorf("stopContainers: error querying for containers: %w", err) @@ -385,42 +433,13 @@ func (s *script) stopContainers() (func() error, error) { } return func() error { - servicesRequiringUpdate := map[string]struct{}{} - var restartErrors []error for _, container := range stoppedContainers { - if swarmServiceName, ok := container.Labels["com.docker.swarm.service.name"]; ok { - servicesRequiringUpdate[swarmServiceName] = struct{}{} - continue - } if err := s.cli.ContainerStart(context.Background(), container.ID, types.ContainerStartOptions{}); err != nil { restartErrors = append(restartErrors, err) } } - if len(servicesRequiringUpdate) != 0 { - services, _ := s.cli.ServiceList(context.Background(), types.ServiceListOptions{}) - for serviceName := range servicesRequiringUpdate { - var serviceMatch swarm.Service - for _, service := range services { - if service.Spec.Name == serviceName { - serviceMatch = service - break - } - } - if serviceMatch.ID == "" { - return fmt.Errorf("stopContainers: couldn't find service with name %s", serviceName) - } - serviceMatch.Spec.TaskTemplate.ForceUpdate += 1 - if _, err := s.cli.ServiceUpdate( - context.Background(), serviceMatch.ID, - serviceMatch.Version, serviceMatch.Spec, types.ServiceUpdateOptions{}, - ); err != nil { - restartErrors = append(restartErrors, err) - } - } - } - if len(restartErrors) != 0 { return fmt.Errorf( "stopContainers: %d error(s) restarting containers and services: %w",