117 lines
2.8 KiB
Go
117 lines
2.8 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
|
"coopcloud.tech/abra/pkg/autocomplete"
|
|
"coopcloud.tech/abra/pkg/client"
|
|
stack "coopcloud.tech/abra/pkg/upstream/stack"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
var prune bool
|
|
|
|
var pruneFlag = &cli.BoolFlag{
|
|
Name: "prune, p",
|
|
Destination: &prune,
|
|
Usage: "Prunes unused containers, networks, and dangling images for an app",
|
|
}
|
|
|
|
func cleanup(c *cli.Context) error {
|
|
for {
|
|
if !prune {
|
|
return nil
|
|
}
|
|
app := internal.ValidateApp(c)
|
|
stackName := app.StackName()
|
|
cl, err := client.New(app.Server)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
|
|
pruneFilters := filters.NewArgs()
|
|
stackSearch := fmt.Sprintf("%s*", stackName)
|
|
pruneFilters.Add("label", stackSearch)
|
|
cr, err := cl.ContainersPrune(ctx, pruneFilters)
|
|
if err != nil {
|
|
logrus.Errorf(err.Error())
|
|
time.Sleep(time.Second)
|
|
continue
|
|
}
|
|
logrus.Infof("Containers deleted: %s; Space reclaimed: %v", cr.ContainersDeleted, cr.SpaceReclaimed)
|
|
|
|
nr, err := cl.NetworksPrune(ctx, pruneFilters)
|
|
if err != nil {
|
|
logrus.Errorf(err.Error())
|
|
time.Sleep(time.Second)
|
|
continue
|
|
}
|
|
logrus.Infof("Networks deleted %s", nr.NetworksDeleted)
|
|
|
|
ir, err := cl.ImagesPrune(ctx, pruneFilters)
|
|
if err != nil {
|
|
logrus.Errorf(err.Error())
|
|
time.Sleep(time.Second)
|
|
continue
|
|
}
|
|
logrus.Infof("Images deleted: %s; Space reclaimed: %v", ir.ImagesDeleted, ir.SpaceReclaimed)
|
|
break
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var appUndeployCommand = cli.Command{
|
|
Name: "undeploy",
|
|
Aliases: []string{"un"},
|
|
ArgsUsage: "<domain>",
|
|
Flags: []cli.Flag{
|
|
internal.DebugFlag,
|
|
internal.NoInputFlag,
|
|
pruneFlag,
|
|
},
|
|
Before: internal.SubCommandBefore,
|
|
Usage: "Undeploy an app",
|
|
Description: `
|
|
This does not destroy any of the application data. However, you should remain
|
|
vigilant, as your swarm installation will consider any previously attached
|
|
volumes as eligible for pruning once undeployed.
|
|
`,
|
|
Action: func(c *cli.Context) error {
|
|
app := internal.ValidateApp(c)
|
|
stackName := app.StackName()
|
|
cl, err := client.New(app.Server)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
logrus.Debugf("checking whether %s is already deployed", stackName)
|
|
|
|
isDeployed, deployedVersion, err := stack.IsDeployed(context.Background(), cl, stackName)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
if !isDeployed {
|
|
logrus.Fatalf("%s is not deployed?", app.Name)
|
|
}
|
|
|
|
if err := internal.DeployOverview(app, deployedVersion, "continue with undeploy?"); err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
rmOpts := stack.Remove{Namespaces: []string{app.StackName()}}
|
|
if err := stack.RunRemove(context.Background(), cl, rmOpts); err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
cleanup(c)
|
|
return nil
|
|
},
|
|
BashComplete: autocomplete.AppNameComplete,
|
|
}
|