2021-08-02 01:10:41 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2022-01-18 13:13:20 +00:00
|
|
|
"context"
|
2021-08-05 10:02:13 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2021-08-02 01:10:41 +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"
|
2021-11-26 21:24:41 +00:00
|
|
|
stack "coopcloud.tech/abra/pkg/upstream/stack"
|
2021-08-05 10:02:13 +00:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-01-18 13:13:20 +00:00
|
|
|
"github.com/urfave/cli"
|
2021-08-02 01:10:41 +00:00
|
|
|
)
|
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
var appRemoveCommand = cli.Command{
|
2022-01-25 12:48:04 +00:00
|
|
|
Name: "remove",
|
|
|
|
Aliases: []string{"rm"},
|
|
|
|
ArgsUsage: "<domain>",
|
2023-01-23 12:29:46 +00:00
|
|
|
Usage: "Remove all app data, locally and remotely",
|
|
|
|
Description: `
|
|
|
|
This command removes everything related to an app which is already undeployed.
|
|
|
|
|
|
|
|
By default, it will prompt for confirmation before proceeding. All secrets,
|
|
|
|
volumes and the local app env file will be deleted.
|
|
|
|
|
|
|
|
Only run this command when you are sure you want to completely remove the app
|
|
|
|
and all associated app data. This is a destructive action, Be Careful!
|
|
|
|
|
|
|
|
If you would like to delete specific volumes or secrets, please use removal
|
|
|
|
sub-commands under "app volume" and "app secret" instead.
|
|
|
|
|
|
|
|
Please note, if you delete the local app env file without removing volumes and
|
|
|
|
secrets first, Abra will *not* be able to help you remove them afterwards.
|
|
|
|
|
|
|
|
To delete everything without prompt, use the "--force/-f" or the "--no-input/n"
|
|
|
|
flag.
|
|
|
|
`,
|
2021-08-02 06:36:35 +00:00
|
|
|
Flags: []cli.Flag{
|
2021-08-05 10:02:13 +00:00
|
|
|
internal.ForceFlag,
|
2022-01-18 13:13:20 +00:00
|
|
|
internal.DebugFlag,
|
|
|
|
internal.NoInputFlag,
|
2021-08-05 10:02:13 +00:00
|
|
|
},
|
2023-01-23 12:29:46 +00:00
|
|
|
BashComplete: autocomplete.AppNameComplete,
|
|
|
|
Before: internal.SubCommandBefore,
|
2021-08-05 10:02:13 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2021-09-05 21:17:35 +00:00
|
|
|
app := internal.ValidateApp(c)
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2022-03-11 15:39:20 +00:00
|
|
|
if !internal.Force && !internal.NoInput {
|
2021-08-05 10:02:13 +00:00
|
|
|
response := false
|
2023-01-23 12:29:46 +00:00
|
|
|
msg := "ALERTA ALERTA: this will completely remove %s data and configurations locally and remotely, are you sure?"
|
|
|
|
prompt := &survey.Confirm{Message: fmt.Sprintf(msg, app.Name)}
|
2021-08-09 14:17:31 +00:00
|
|
|
if err := survey.AskOne(prompt, &response); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-08-05 10:02:13 +00:00
|
|
|
if !response {
|
2022-01-04 10:37:22 +00:00
|
|
|
logrus.Fatal("aborting as requested")
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2021-09-05 21:17:35 +00:00
|
|
|
cl, err := client.New(app.Server)
|
2021-08-05 10:02:13 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-11-26 21:24:41 +00:00
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
isDeployed, _, err := stack.IsDeployed(context.Background(), cl, app.StackName())
|
2022-01-04 10:38:07 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
if isDeployed {
|
2022-03-12 15:08:26 +00:00
|
|
|
logrus.Fatalf("%s is still deployed. Run \"abra app undeploy %s\"", app.Name, app.Name)
|
2021-09-05 19:54:52 +00:00
|
|
|
}
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2022-03-30 14:11:52 +00:00
|
|
|
fs, err := app.Filters(false, false)
|
2022-03-27 10:40:05 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
secretList, err := cl.SecretList(context.Background(), types.SecretListOptions{Filters: fs})
|
2021-08-05 10:02:13 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2021-08-05 10:02:13 +00:00
|
|
|
secrets := make(map[string]string)
|
2021-09-05 19:54:52 +00:00
|
|
|
var secretNames []string
|
2021-08-05 10:02:13 +00:00
|
|
|
|
2021-09-05 19:54:52 +00:00
|
|
|
for _, cont := range secretList {
|
|
|
|
secrets[cont.Spec.Annotations.Name] = cont.ID // we have to map the names to ID's
|
|
|
|
secretNames = append(secretNames, cont.Spec.Annotations.Name)
|
|
|
|
}
|
2021-08-05 10:02:13 +00:00
|
|
|
|
2021-09-05 19:54:52 +00:00
|
|
|
if len(secrets) > 0 {
|
2023-01-23 12:29:46 +00:00
|
|
|
for _, name := range secretNames {
|
2022-01-18 13:13:20 +00:00
|
|
|
err := cl.SecretRemove(context.Background(), secrets[name])
|
2021-09-02 15:52:42 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Info(fmt.Sprintf("secret: %s removed", name))
|
2021-09-02 15:52:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Info("no secrets to remove")
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 14:11:52 +00:00
|
|
|
fs, err = app.Filters(false, true)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:13:20 +00:00
|
|
|
volumeListOKBody, err := cl.VolumeList(context.Background(), fs)
|
2021-08-05 10:02:13 +00:00
|
|
|
volumeList := volumeListOKBody.Volumes
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2021-08-11 10:49:53 +00:00
|
|
|
var vols []string
|
2021-09-05 19:54:52 +00:00
|
|
|
for _, vol := range volumeList {
|
|
|
|
vols = append(vols, vol.Name)
|
|
|
|
}
|
2021-08-05 10:02:13 +00:00
|
|
|
|
2021-09-05 19:54:52 +00:00
|
|
|
if len(vols) > 0 {
|
2023-01-23 12:29:46 +00:00
|
|
|
var removeVols []string
|
|
|
|
for _, vol := range removeVols {
|
|
|
|
err := cl.VolumeRemove(context.Background(), vol, internal.Force) // last argument is for force removing
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
2023-01-23 12:29:46 +00:00
|
|
|
logrus.Info(fmt.Sprintf("volume %s removed", vol))
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-23 12:29:46 +00:00
|
|
|
logrus.Info("no volumes to remove")
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 12:29:46 +00:00
|
|
|
if err = os.Remove(app.Path); err != nil {
|
2021-09-02 15:52:42 +00:00
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2023-01-23 12:29:46 +00:00
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Info(fmt.Sprintf("file: %s removed", app.Path))
|
2021-09-02 15:52:42 +00:00
|
|
|
|
2021-08-05 10:02:13 +00:00
|
|
|
return nil
|
2021-08-02 06:36:35 +00:00
|
|
|
},
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|