2020-10-12 10:18:45 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package aci
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-15 12:49:43 +02:00
|
|
|
"fmt"
|
2020-10-12 10:18:45 +02:00
|
|
|
|
2020-10-14 16:06:45 +02:00
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
|
|
|
|
"github.com/docker/compose-cli/aci/convert"
|
2020-10-12 10:18:45 +02:00
|
|
|
"github.com/docker/compose-cli/api/resources"
|
|
|
|
"github.com/docker/compose-cli/context/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type aciResourceService struct {
|
|
|
|
aciContext store.AciContext
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:49:43 +02:00
|
|
|
func (cs *aciResourceService) Prune(ctx context.Context, request resources.PruneRequest) (resources.PruneResult, error) {
|
2020-10-14 16:06:45 +02:00
|
|
|
res, err := getACIContainerGroups(ctx, cs.aciContext.SubscriptionID, cs.aciContext.ResourceGroup)
|
2020-10-15 12:49:43 +02:00
|
|
|
result := resources.PruneResult{}
|
2020-10-14 16:06:45 +02:00
|
|
|
if err != nil {
|
2020-10-15 12:49:43 +02:00
|
|
|
return result, err
|
2020-10-14 16:06:45 +02:00
|
|
|
}
|
|
|
|
multierr := &multierror.Error{}
|
|
|
|
deleted := []string{}
|
2020-10-15 12:49:43 +02:00
|
|
|
cpus := 0.
|
|
|
|
mem := 0.
|
|
|
|
|
2020-10-14 16:06:45 +02:00
|
|
|
for _, containerGroup := range res {
|
|
|
|
if !request.Force && convert.GetGroupStatus(containerGroup) == "Node "+convert.StatusRunning {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:49:43 +02:00
|
|
|
for _, container := range *containerGroup.Containers {
|
|
|
|
hostConfig := convert.ToHostConfig(container, containerGroup)
|
|
|
|
cpus += hostConfig.CPUReservation
|
|
|
|
mem += convert.BytesToGB(float64(hostConfig.MemoryReservation))
|
|
|
|
}
|
|
|
|
|
2020-10-14 16:06:45 +02:00
|
|
|
if !request.DryRun {
|
|
|
|
_, err := deleteACIContainerGroup(ctx, cs.aciContext, *containerGroup.Name)
|
|
|
|
multierr = multierror.Append(multierr, err)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
deleted = append(deleted, *containerGroup.Name)
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 12:49:43 +02:00
|
|
|
result.DeletedIDs = deleted
|
|
|
|
result.Summary = fmt.Sprintf("Total CPUs reclaimed: %.2f, total memory reclaimed: %.2f GB", cpus, mem)
|
|
|
|
return result, multierr.ErrorOrNil()
|
2020-10-12 10:18:45 +02:00
|
|
|
}
|