2021-08-02 01:10:41 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
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/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 {
|
2021-09-05 21:17:35 +00:00
|
|
|
app := internal.ValidateApp(c)
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2021-08-05 10:02:13 +00:00
|
|
|
if !internal.Force {
|
|
|
|
response := false
|
|
|
|
prompt := &survey.Confirm{
|
2022-01-04 10:37:04 +00:00
|
|
|
Message: fmt.Sprintf("about to remove %s, are you sure?", app.Name),
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
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-04 10:38:07 +00:00
|
|
|
isDeployed, _, err := stack.IsDeployed(c.Context, cl, app.StackName())
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
if isDeployed {
|
|
|
|
logrus.Fatalf("%s is still deployed. Run \"abra app undeploy %s \" or pass --force", app.Name, app.Name)
|
2021-09-05 19:54:52 +00:00
|
|
|
}
|
2021-08-06 10:09:35 +00:00
|
|
|
|
2021-08-05 10:02:13 +00:00
|
|
|
fs := filters.NewArgs()
|
2022-01-04 10:37:45 +00:00
|
|
|
fs.Add("name", app.StackName())
|
2021-09-16 10:17:56 +00:00
|
|
|
secretList, err := cl.SecretList(c.Context, 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 {
|
2021-09-02 15:52:42 +00:00
|
|
|
var secretNamesToRemove []string
|
2022-01-04 10:38:07 +00:00
|
|
|
|
2021-09-02 15:52:42 +00:00
|
|
|
if !internal.Force {
|
|
|
|
secretsPrompt := &survey.MultiSelect{
|
2021-09-10 22:54:02 +00:00
|
|
|
Message: "which secrets do you want to remove?",
|
2021-12-21 23:07:49 +00:00
|
|
|
Help: "'x' indicates selected, enter / return to confirm, ctrl-c to exit, vim mode is enabled",
|
|
|
|
VimMode: true,
|
2021-09-02 15:52:42 +00:00
|
|
|
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 {
|
2021-09-16 10:17:56 +00:00
|
|
|
err := cl.SecretRemove(c.Context, 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
|
|
|
}
|
|
|
|
|
2021-09-16 10:17:56 +00:00
|
|
|
volumeListOKBody, err := cl.VolumeList(c.Context, 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 {
|
2021-09-02 15:52:42 +00:00
|
|
|
if Volumes {
|
|
|
|
var removeVols []string
|
|
|
|
if !internal.Force {
|
|
|
|
volumesPrompt := &survey.MultiSelect{
|
2021-09-10 22:54:02 +00:00
|
|
|
Message: "which volumes do you want to remove?",
|
2021-12-21 23:07:49 +00:00
|
|
|
Help: "'x' indicates selected, enter / return to confirm, ctrl-c to exit, vim mode is enabled",
|
|
|
|
VimMode: true,
|
2021-09-02 15:52:42 +00:00
|
|
|
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 {
|
2021-09-16 10:17:56 +00:00
|
|
|
err := cl.VolumeRemove(c.Context, vol, internal.Force) // last argument is for force removing
|
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("volume %s removed", vol))
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
2021-09-02 15:52:42 +00:00
|
|
|
} else {
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Info("no volumes were removed")
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Info("no volumes to remove")
|
2021-08-05 10:02:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 21:17:35 +00:00
|
|
|
err = os.Remove(app.Path)
|
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("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-12-11 23:17:39 +00:00
|
|
|
BashComplete: autocomplete.AppNameComplete,
|
2021-08-02 01:10:41 +00:00
|
|
|
}
|