2021-08-02 01:10:41 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2021-08-05 10:02:13 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
"coopcloud.tech/abra/cli/internal"
|
2021-09-05 19:37:03 +00:00
|
|
|
"coopcloud.tech/abra/pkg/client"
|
|
|
|
"coopcloud.tech/abra/pkg/config"
|
2021-08-05 10:02:13 +00:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-08-02 01:10:41 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-08-02 06:36:35 +00:00
|
|
|
// Volumes stores the variable from VolumesFlag
|
|
|
|
var Volumes bool
|
|
|
|
|
|
|
|
// VolumesFlag is used to specify if volumes should be deleted when deleting an app
|
|
|
|
var VolumesFlag = &cli.BoolFlag{
|
|
|
|
Name: "volumes",
|
|
|
|
Value: false,
|
|
|
|
Destination: &Volumes,
|
|
|
|
}
|
|
|
|
|
2021-08-02 01:10:41 +00:00
|
|
|
var appRemoveCommand = &cli.Command{
|
2021-08-05 10:02:13 +00:00
|
|
|
Name: "remove",
|
2021-09-03 12:22:40 +00:00
|
|
|
Usage: "Remove an already undeployed app",
|
2021-09-04 23:34:56 +00:00
|
|
|
Aliases: []string{"rm"},
|
2021-08-02 06:36:35 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
VolumesFlag,
|
2021-08-05 10:02:13 +00:00
|
|
|
internal.ForceFlag,
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
appName := c.Args().First()
|
|
|
|
if appName == "" {
|
2021-08-12 13:41:35 +00:00
|
|
|
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided"))
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2021-08-05 10:02:13 +00:00
|
|
|
if !internal.Force {
|
|
|
|
response := false
|
|
|
|
prompt := &survey.Confirm{
|
|
|
|
Message: fmt.Sprintf("About to delete %s, are you sure", appName),
|
|
|
|
}
|
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 {
|
|
|
|
return errors.New("User aborted app removal")
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2021-08-05 10:02:13 +00:00
|
|
|
appFiles, err := config.LoadAppFiles("")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2021-08-05 10:02:13 +00:00
|
|
|
appPath := appFiles[appName].Path
|
2021-09-04 22:41:31 +00:00
|
|
|
server := appFiles[appName].Server
|
2021-08-05 10:02:13 +00:00
|
|
|
ctx := context.Background()
|
2021-09-04 22:41:31 +00:00
|
|
|
cl, err := client.New(server)
|
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
|
|
|
fs := filters.NewArgs()
|
|
|
|
fs.Add("name", appName)
|
|
|
|
secretList, err := cl.SecretList(ctx, types.SecretListOptions{Filters: fs})
|
|
|
|
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-02 15:52:42 +00:00
|
|
|
if len(secrets) > 0 {
|
|
|
|
var secretNames []string
|
2021-08-05 10:02:13 +00:00
|
|
|
|
2021-09-02 15:52:42 +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-02 15:52:42 +00:00
|
|
|
var secretNamesToRemove []string
|
|
|
|
if !internal.Force {
|
|
|
|
secretsPrompt := &survey.MultiSelect{
|
|
|
|
Message: "Which secrets do you want to remove?",
|
|
|
|
Options: secretNames,
|
|
|
|
Default: secretNames,
|
|
|
|
}
|
|
|
|
if err := survey.AskOne(secretsPrompt, &secretNamesToRemove); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2021-09-02 15:52:42 +00:00
|
|
|
for _, name := range secretNamesToRemove {
|
|
|
|
err := cl.SecretRemove(ctx, secrets[name])
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
logrus.Info(fmt.Sprintf("Secret: %s removed", name))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Info("No secrets to remove")
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
volumeListOKBody, err := cl.VolumeList(ctx, fs)
|
|
|
|
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-02 15:52:42 +00:00
|
|
|
if len(vols) > 0 {
|
|
|
|
for _, vol := range volumeList {
|
|
|
|
vols = append(vols, vol.Name)
|
|
|
|
}
|
2021-08-05 10:02:13 +00:00
|
|
|
|
2021-09-02 15:52:42 +00:00
|
|
|
if Volumes {
|
|
|
|
var removeVols []string
|
|
|
|
if !internal.Force {
|
|
|
|
volumesPrompt := &survey.MultiSelect{
|
|
|
|
Message: "Which volumes do you want to remove?",
|
|
|
|
Options: vols,
|
|
|
|
Default: vols,
|
|
|
|
}
|
|
|
|
if err := survey.AskOne(volumesPrompt, &removeVols); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-08-09 14:17:31 +00:00
|
|
|
}
|
2021-09-02 15:52:42 +00:00
|
|
|
for _, vol := range removeVols {
|
|
|
|
err := cl.VolumeRemove(ctx, vol, internal.Force) // last argument is for force removing
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
logrus.Info(fmt.Sprintf("Volume %s removed", vol))
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
2021-09-02 15:52:42 +00:00
|
|
|
} else {
|
|
|
|
logrus.Info("No volumes were removed")
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-02 15:52:42 +00:00
|
|
|
logrus.Info("No volumes to remove")
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 15:52:42 +00:00
|
|
|
err = os.Remove(appPath)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
logrus.Info(fmt.Sprintf("File: %s removed", appPath))
|
|
|
|
|
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
|
|
|
}
|