Skip to content

Commit

Permalink
Try scaling down services
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Jan 25, 2024
1 parent 4b79a05 commit d9aabae
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions cmd/backup/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ func (s *script) stopContainersAndServices() (func() error, error) {
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"

matchLabel := fmt.Sprintf(
"docker-volume-backup.stop-during-backup=%s",
s.c.BackupStopContainerLabel,
Expand All @@ -345,18 +351,22 @@ func (s *script) stopContainersAndServices() (func() error, error) {
return noop, fmt.Errorf("stopContainers: error querying for containers to stop: %w", err)
}

allServices, err := s.cli.ServiceList(context.Background(), types.ServiceListOptions{})
if err != nil {
return noop, fmt.Errorf("stopContainers: error querying for services: %w", err)
}
servicesToScaleDown, err := s.cli.ServiceList(context.Background(), types.ServiceListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{
Key: "label",
Value: matchLabel,
}),
})
if err != nil {
return noop, fmt.Errorf("stopContainers: error querying for services to scale down: %w", err)
var allServices []swarm.Service
var servicesToScaleDown []swarm.Service
if isDockerSwarm {
allServices, err = s.cli.ServiceList(context.Background(), types.ServiceListOptions{})
if err != nil {
return noop, fmt.Errorf("stopContainers: error querying for services: %w", err)
}
servicesToScaleDown, err = s.cli.ServiceList(context.Background(), types.ServiceListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{
Key: "label",
Value: matchLabel,
}),
})
if err != nil {
return noop, fmt.Errorf("stopContainers: error querying for services to scale down: %w", err)
}
}

if len(containersToStop) == 0 && len(servicesToScaleDown) == 0 {
Expand Down Expand Up @@ -393,6 +403,19 @@ func (s *script) stopContainersAndServices() (func() error, error) {
)
}

var scaledDownServices []swarm.Service
if isDockerSwarm {
for _, service := range servicesToScaleDown {
var zero uint64
service.Spec.Mode.Replicated.Replicas = &zero
if _, err := s.cli.ServiceUpdate(context.Background(), service.ID, service.Version, service.Spec, types.ServiceUpdateOptions{}); err != nil {

} else {
scaledDownServices = append(scaledDownServices, service)

Check failure on line 414 in cmd/backup/script.go

View workflow job for this annotation

GitHub Actions / lint

SA4010: this result of append is never used, except maybe in other appends (staticcheck)
}
}
}

s.stats.Containers = ContainersStats{
All: uint(len(allContainers)),
ToStop: uint(len(containersToStop)),
Expand Down

0 comments on commit d9aabae

Please sign in to comment.