|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/apprenda/kismatic/pkg/install" |
| 10 | + "github.com/apprenda/kismatic/pkg/util" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +type volumeDeleteOptions struct { |
| 15 | + verbose bool |
| 16 | + outputFormat string |
| 17 | + generatedAssetsDir string |
| 18 | + force bool |
| 19 | +} |
| 20 | + |
| 21 | +// NewCmdVolumeDelete returns the command for deleting storage volumes |
| 22 | +func NewCmdVolumeDelete(in io.Reader, out io.Writer, planFile *string) *cobra.Command { |
| 23 | + opts := volumeDeleteOptions{} |
| 24 | + cmd := &cobra.Command{ |
| 25 | + Use: "delete volume-name", |
| 26 | + Short: "delete storage volumes", |
| 27 | + Long: `Delete storage volumes created by the 'volume add' command. |
| 28 | + |
| 29 | +WARNING all data in the volume will be lost.`, |
| 30 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 31 | + if opts.force == false { |
| 32 | + ans, err := util.PromptForString(in, out, "Are you sure you want to delete this volume? All data will be lost", "N", []string{"N", "y"}) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("error getting user response: %v", err) |
| 35 | + } |
| 36 | + if strings.ToLower(ans) != "y" { |
| 37 | + os.Exit(0) |
| 38 | + } |
| 39 | + } |
| 40 | + return doVolumeDelete(out, opts, *planFile, args) |
| 41 | + }, |
| 42 | + } |
| 43 | + cmd.Flags().BoolVar(&opts.verbose, "verbose", false, "enable verbose logging") |
| 44 | + cmd.Flags().StringVarP(&opts.outputFormat, "output", "o", "simple", `output format (options simple|raw)`) |
| 45 | + cmd.Flags().StringVar(&opts.generatedAssetsDir, "generated-assets-dir", "generated", "path to the directory where assets generated during the installation process will be stored") |
| 46 | + cmd.Flags().BoolVar(&opts.force, "force", false, `do not prompt`) |
| 47 | + return cmd |
| 48 | +} |
| 49 | + |
| 50 | +func doVolumeDelete(out io.Writer, opts volumeDeleteOptions, planFile string, args []string) error { |
| 51 | + // get volume name and size from arguments |
| 52 | + var volumeName string |
| 53 | + switch len(args) { |
| 54 | + case 1: |
| 55 | + volumeName = args[0] |
| 56 | + default: |
| 57 | + return fmt.Errorf("%d arguments were provided, but add does not support more than 1 arguments", len(args)) |
| 58 | + } |
| 59 | + |
| 60 | + // setup ansible for execution |
| 61 | + planner := &install.FilePlanner{File: planFile} |
| 62 | + if !planner.PlanExists() { |
| 63 | + return planFileNotFoundErr{filename: planFile} |
| 64 | + } |
| 65 | + execOpts := install.ExecutorOptions{ |
| 66 | + OutputFormat: opts.outputFormat, |
| 67 | + Verbose: opts.verbose, |
| 68 | + // Need to refactor executor code... this will do for now as we don't need the generated assets dir in this command |
| 69 | + GeneratedAssetsDirectory: opts.generatedAssetsDir, |
| 70 | + } |
| 71 | + exec, err := install.NewExecutor(out, out, execOpts) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + plan, err := planner.Read() |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + // Run validation |
| 81 | + vopts := &validateOpts{ |
| 82 | + outputFormat: opts.outputFormat, |
| 83 | + verbose: opts.verbose, |
| 84 | + planFile: planFile, |
| 85 | + skipPreFlight: true, |
| 86 | + generatedAssetsDir: opts.generatedAssetsDir, |
| 87 | + } |
| 88 | + if err := doValidate(out, planner, vopts); err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + if err := exec.DeleteVolume(plan, volumeName); err != nil { |
| 93 | + return fmt.Errorf("error deleting volume: %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + fmt.Fprintln(out) |
| 97 | + fmt.Fprintln(out, "Successfully deleted the persistent volume in the kubernetes cluster.") |
| 98 | + return nil |
| 99 | +} |
0 commit comments