forked from toolshed/abra
		
	
		
			
				
	
	
		
			168 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			168 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package app
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"os"
 | |
| 	"strings"
 | |
| 
 | |
| 	"coopcloud.tech/abra/cli/internal"
 | |
| 	"coopcloud.tech/abra/pkg/autocomplete"
 | |
| 	"coopcloud.tech/abra/pkg/client"
 | |
| 	"coopcloud.tech/abra/pkg/i18n"
 | |
| 	"coopcloud.tech/abra/pkg/log"
 | |
| 	"coopcloud.tech/abra/pkg/upstream/stack"
 | |
| 	"github.com/AlecAivazis/survey/v2"
 | |
| 	"github.com/docker/docker/api/types"
 | |
| 	"github.com/spf13/cobra"
 | |
| )
 | |
| 
 | |
| // translators: `abra app remove` aliases. use a comma separated list of aliases with
 | |
| // no spaces in between
 | |
| var appRemoveAliases = i18n.G("rm")
 | |
| 
 | |
| var AppRemoveCommand = &cobra.Command{
 | |
| 	// translators: `app remove` command
 | |
| 	Use:     i18n.G("remove <domain> [flags]"),
 | |
| 	Aliases: strings.Split(appRemoveAliases, ","),
 | |
| 	// translators: Short description for `app remove` command
 | |
| 	Short: i18n.G("Remove all app data, locally and remotely"),
 | |
| 	Long: i18n.G(`Remove 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.`),
 | |
| 	Example: i18n.G("  abra app remove 1312.net"),
 | |
| 	Args:    cobra.ExactArgs(1),
 | |
| 	ValidArgsFunction: func(
 | |
| 		cmd *cobra.Command,
 | |
| 		args []string,
 | |
| 		toComplete string) ([]string, cobra.ShellCompDirective) {
 | |
| 		return autocomplete.AppNameComplete()
 | |
| 	},
 | |
| 	Run: func(cmd *cobra.Command, args []string) {
 | |
| 		app := internal.ValidateApp(args)
 | |
| 
 | |
| 		if !internal.Force && !internal.NoInput {
 | |
| 			log.Warn(i18n.G("ALERTA ALERTA: deleting %s data and config (local/remote)", app.Name))
 | |
| 
 | |
| 			response := false
 | |
| 			prompt := &survey.Confirm{Message: i18n.G("are you sure?")}
 | |
| 			if err := survey.AskOne(prompt, &response); err != nil {
 | |
| 				log.Fatal(err)
 | |
| 			}
 | |
| 
 | |
| 			if !response {
 | |
| 				log.Fatal(i18n.G("aborting as requested"))
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		cl, err := client.New(app.Server)
 | |
| 		if err != nil {
 | |
| 			log.Fatal(err)
 | |
| 		}
 | |
| 
 | |
| 		deployMeta, err := stack.IsDeployed(context.Background(), cl, app.StackName())
 | |
| 		if err != nil {
 | |
| 			log.Fatal(err)
 | |
| 		}
 | |
| 		if deployMeta.IsDeployed {
 | |
| 			log.Fatal(i18n.G("%s is still deployed. Run \"abra app undeploy %s\"", app.Name, app.Name))
 | |
| 		}
 | |
| 
 | |
| 		fs, err := app.Filters(false, false)
 | |
| 		if err != nil {
 | |
| 			log.Fatal(err)
 | |
| 		}
 | |
| 
 | |
| 		configs, err := client.GetConfigs(cl, context.Background(), app.Server, fs)
 | |
| 		if err != nil {
 | |
| 			log.Fatal(err)
 | |
| 		}
 | |
| 		configNames := client.GetConfigNames(configs)
 | |
| 
 | |
| 		if len(configNames) > 0 {
 | |
| 			if err := client.RemoveConfigs(cl, context.Background(), configNames, internal.Force); err != nil {
 | |
| 				log.Fatal(i18n.G("removing configs failed: %s", err))
 | |
| 			}
 | |
| 
 | |
| 			log.Info(i18n.G("%d config(s) removed successfully", len(configNames)))
 | |
| 		} else {
 | |
| 			log.Info(i18n.G("no configs to remove"))
 | |
| 		}
 | |
| 
 | |
| 		secretList, err := cl.SecretList(context.Background(), types.SecretListOptions{Filters: fs})
 | |
| 		if err != nil {
 | |
| 			log.Fatal(err)
 | |
| 		}
 | |
| 
 | |
| 		secrets := make(map[string]string)
 | |
| 		var secretNames []string
 | |
| 
 | |
| 		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)
 | |
| 		}
 | |
| 
 | |
| 		if len(secrets) > 0 {
 | |
| 			for _, name := range secretNames {
 | |
| 				err := cl.SecretRemove(context.Background(), secrets[name])
 | |
| 				if err != nil {
 | |
| 					log.Fatal(err)
 | |
| 				}
 | |
| 				log.Info(i18n.G("secret: %s removed", name))
 | |
| 			}
 | |
| 		} else {
 | |
| 			log.Info(i18n.G("no secrets to remove"))
 | |
| 		}
 | |
| 
 | |
| 		fs, err = app.Filters(false, true)
 | |
| 		if err != nil {
 | |
| 			log.Fatal(err)
 | |
| 		}
 | |
| 
 | |
| 		volumeList, err := client.GetVolumes(cl, context.Background(), app.Server, fs)
 | |
| 		if err != nil {
 | |
| 			log.Fatal(err)
 | |
| 		}
 | |
| 		volumeNames := client.GetVolumeNames(volumeList)
 | |
| 
 | |
| 		if len(volumeNames) > 0 {
 | |
| 			err := client.RemoveVolumes(cl, context.Background(), volumeNames, internal.Force, 5)
 | |
| 			if err != nil {
 | |
| 				log.Fatal(i18n.G("removing volumes failed: %s", err))
 | |
| 			}
 | |
| 
 | |
| 			log.Info(i18n.G("%d volume(s) removed successfully", len(volumeNames)))
 | |
| 		} else {
 | |
| 			log.Info(i18n.G("no volumes to remove"))
 | |
| 		}
 | |
| 
 | |
| 		if err = os.Remove(app.Path); err != nil {
 | |
| 			log.Fatal(err)
 | |
| 		}
 | |
| 
 | |
| 		log.Info(i18n.G("file: %s removed", app.Path))
 | |
| 	},
 | |
| }
 | |
| 
 | |
| func init() {
 | |
| 	AppRemoveCommand.Flags().BoolVarP(
 | |
| 		&internal.Force,
 | |
| 		i18n.G("force"),
 | |
| 		i18n.G("f"),
 | |
| 		false,
 | |
| 		i18n.G("perform action without further prompt"),
 | |
| 	)
 | |
| }
 |