github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/cmd/orbctl/teardown.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/caos/orbos/pkg/kubernetes/cli"
     9  
    10  	"github.com/caos/orbos/pkg/git"
    11  
    12  	"github.com/caos/orbos/pkg/labels"
    13  
    14  	"github.com/spf13/cobra"
    15  
    16  	"github.com/caos/orbos/internal/operator/orbiter"
    17  	orbadapter "github.com/caos/orbos/internal/operator/orbiter/kinds/orb"
    18  )
    19  
    20  func TeardownCommand(getRv GetRootValues) *cobra.Command {
    21  
    22  	var (
    23  		cmd = &cobra.Command{
    24  			Use:   "teardown",
    25  			Short: "Tear down an Orb",
    26  			Long:  "Destroys a whole Orb",
    27  			Aliases: []string{
    28  				"shoot",
    29  				"destroy",
    30  				"devastate",
    31  				"annihilate",
    32  				"crush",
    33  				"bulldoze",
    34  				"total",
    35  				"smash",
    36  				"decimate",
    37  				"kill",
    38  				"trash",
    39  				"wipe-off-the-map",
    40  				"pulverize",
    41  				"take-apart",
    42  				"destruct",
    43  				"obliterate",
    44  				"disassemble",
    45  				"explode",
    46  				"blow-up",
    47  			},
    48  		}
    49  	)
    50  
    51  	cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
    52  
    53  		rv := getRv("teardown", "", nil)
    54  		defer rv.ErrFunc(err)
    55  
    56  		if !rv.Gitops {
    57  			return errors.New("teardown command is only supported with the --gitops flag and a committed orbiter.yml")
    58  		}
    59  
    60  		if _, err := cli.Init(monitor, rv.OrbConfig, rv.GitClient, rv.Kubeconfig, rv.Gitops, true, false); err != nil {
    61  			return err
    62  		}
    63  
    64  		if rv.GitClient.Exists(git.OrbiterFile) {
    65  			monitor.WithFields(map[string]interface{}{
    66  				"version": version,
    67  				"commit":  gitCommit,
    68  				"repoURL": rv.OrbConfig.URL,
    69  			}).Info("Destroying Orb")
    70  
    71  			fmt.Println("Are you absolutely sure you want to destroy all clusters and providers in this Orb? [y/N]")
    72  			var response string
    73  			fmt.Scanln(&response)
    74  
    75  			if !contains([]string{"y", "yes"}, strings.ToLower(response)) {
    76  				monitor.Info("Not touching Orb")
    77  				return nil
    78  			}
    79  			finishedChan := make(chan struct{})
    80  			return orbiter.Destroy(
    81  				monitor,
    82  				rv.GitClient,
    83  				orbadapter.AdaptFunc(
    84  					labels.NoopOperator("ORBOS"),
    85  					rv.OrbConfig,
    86  					gitCommit,
    87  					true,
    88  					false,
    89  					rv.GitClient,
    90  				),
    91  				finishedChan,
    92  			)
    93  		}
    94  		return nil
    95  	}
    96  	return cmd
    97  }
    98  
    99  func contains(slice []string, elem string) bool {
   100  	for _, item := range slice {
   101  		if elem == item {
   102  			return true
   103  		}
   104  	}
   105  	return false
   106  }