abra/cli/app/undeploy.go

128 lines
3.2 KiB
Go
Raw Normal View History

package app
2021-08-29 23:36:42 +00:00
import (
"context"
2023-02-15 02:25:37 +00:00
"fmt"
2023-02-17 08:24:04 +00:00
"time"
2021-08-29 23:36:42 +00:00
"coopcloud.tech/abra/cli/internal"
2021-12-11 23:17:39 +00:00
"coopcloud.tech/abra/pkg/autocomplete"
2021-09-05 19:37:03 +00:00
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/config"
stack "coopcloud.tech/abra/pkg/upstream/stack"
2023-02-15 02:25:37 +00:00
"github.com/docker/docker/api/types/filters"
dockerClient "github.com/docker/docker/client"
2021-08-29 23:36:42 +00:00
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
2021-08-29 23:36:42 +00:00
)
2023-02-15 02:25:37 +00:00
var prune bool
var pruneFlag = &cli.BoolFlag{
Name: "prune, p",
Destination: &prune,
2023-02-17 08:24:04 +00:00
Usage: "Prunes unused containers, networks, and dangling images for an app",
}
// pruneSystem runs the equivalent of a "docker system prune" after undeploying
// in order to clean up left over state related to the deployment. We must run
// this logic inside a loop as it may take some time for the undeployed to come
// down.
func pruneSystem(c *cli.Context, cl *dockerClient.Client, app config.App) error {
2023-02-17 08:24:04 +00:00
for {
if !prune {
return nil
}
2023-02-17 08:24:04 +00:00
stackName := app.StackName()
ctx := context.Background()
2023-02-17 08:24:04 +00:00
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)
2023-02-17 08:24:04 +00:00
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)
2023-02-17 08:24:04 +00:00
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)
2023-02-17 08:24:04 +00:00
break
}
2023-02-17 08:24:04 +00:00
return nil
2023-02-15 02:25:37 +00:00
}
var appUndeployCommand = cli.Command{
2022-01-25 12:48:04 +00:00
Name: "undeploy",
Aliases: []string{"un"},
ArgsUsage: "<domain>",
Flags: []cli.Flag{
internal.DebugFlag,
internal.NoInputFlag,
2023-02-15 02:25:37 +00:00
pruneFlag,
},
Before: internal.SubCommandBefore,
Usage: "Undeploy an app",
BashComplete: autocomplete.AppNameComplete,
2021-09-04 23:34:56 +00:00
Description: `
This does not destroy any of the application data. However, you should remain
vigilant, as your swarm installation will consider any previously attached
2023-02-14 13:59:35 +00:00
volumes as eligible for pruning once undeployed.
2021-09-04 23:34:56 +00:00
`,
2021-08-29 23:36:42 +00:00
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
2021-10-21 13:10:43 +00:00
stackName := app.StackName()
cl, err := client.New(app.Server)
2021-08-29 23:36:42 +00:00
if err != nil {
logrus.Fatal(err)
}
2021-12-25 01:03:09 +00:00
logrus.Debugf("checking whether %s is already deployed", stackName)
2021-10-21 13:10:43 +00:00
isDeployed, deployedVersion, err := stack.IsDeployed(context.Background(), cl, stackName)
2021-10-21 13:10:43 +00:00
if err != nil {
logrus.Fatal(err)
}
if !isDeployed {
2022-01-01 16:22:19 +00:00
logrus.Fatalf("%s is not deployed?", app.Name)
2021-10-21 13:10:43 +00:00
}
if err := internal.DeployOverview(app, deployedVersion, "continue with undeploy?"); err != nil {
2021-10-21 13:10:43 +00:00
logrus.Fatal(err)
}
rmOpts := stack.Remove{Namespaces: []string{app.StackName()}}
if err := stack.RunRemove(context.Background(), cl, rmOpts); err != nil {
2021-08-29 23:36:42 +00:00
logrus.Fatal(err)
}
if err := pruneSystem(c, cl, app); err != nil {
logrus.Fatal(err)
}
2023-02-15 02:25:37 +00:00
return nil
},
}