github.com/openshift/installer@v1.4.17/cmd/openshift-install/destroy.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/openshift/installer/cmd/openshift-install/command"
    13  	assetstore "github.com/openshift/installer/pkg/asset/store"
    14  	"github.com/openshift/installer/pkg/clusterapi"
    15  	"github.com/openshift/installer/pkg/destroy"
    16  	"github.com/openshift/installer/pkg/destroy/bootstrap"
    17  	quotaasset "github.com/openshift/installer/pkg/destroy/quota"
    18  	"github.com/openshift/installer/pkg/metrics/timer"
    19  
    20  	_ "github.com/openshift/installer/pkg/destroy/aws"
    21  	_ "github.com/openshift/installer/pkg/destroy/azure"
    22  	_ "github.com/openshift/installer/pkg/destroy/baremetal"
    23  	_ "github.com/openshift/installer/pkg/destroy/gcp"
    24  	_ "github.com/openshift/installer/pkg/destroy/ibmcloud"
    25  	_ "github.com/openshift/installer/pkg/destroy/nutanix"
    26  	_ "github.com/openshift/installer/pkg/destroy/openstack"
    27  	_ "github.com/openshift/installer/pkg/destroy/ovirt"
    28  	_ "github.com/openshift/installer/pkg/destroy/powervs"
    29  	_ "github.com/openshift/installer/pkg/destroy/vsphere"
    30  )
    31  
    32  func newDestroyCmd() *cobra.Command {
    33  	cmd := &cobra.Command{
    34  		Use:   "destroy",
    35  		Short: "Destroy part of an OpenShift cluster",
    36  		Long:  "",
    37  		RunE: func(cmd *cobra.Command, args []string) error {
    38  			return cmd.Help()
    39  		},
    40  	}
    41  	cmd.AddCommand(newDestroyBootstrapCmd())
    42  	cmd.AddCommand(newDestroyClusterCmd())
    43  	return cmd
    44  }
    45  
    46  func newDestroyClusterCmd() *cobra.Command {
    47  	return &cobra.Command{
    48  		Use:   "cluster",
    49  		Short: "Destroy an OpenShift cluster",
    50  		Args:  cobra.ExactArgs(0),
    51  		Run: func(_ *cobra.Command, _ []string) {
    52  			cleanup := command.SetupFileHook(command.RootOpts.Dir)
    53  			defer cleanup()
    54  
    55  			err := runDestroyCmd(command.RootOpts.Dir, os.Getenv("OPENSHIFT_INSTALL_REPORT_QUOTA_FOOTPRINT") == "true")
    56  			if err != nil {
    57  				logrus.Fatal(err)
    58  			}
    59  			logrus.Infof("Uninstallation complete!")
    60  		},
    61  	}
    62  }
    63  
    64  func runDestroyCmd(directory string, reportQuota bool) error {
    65  	timer.StartTimer(timer.TotalTimeElapsed)
    66  	destroyer, err := destroy.New(logrus.StandardLogger(), directory)
    67  	if err != nil {
    68  		return fmt.Errorf("failed while preparing to destroy cluster: %w", err)
    69  	}
    70  	quota, err := destroyer.Run()
    71  	if err != nil {
    72  		return fmt.Errorf("failed to destroy cluster: %w", err)
    73  	}
    74  
    75  	if reportQuota {
    76  		if err := quotaasset.WriteQuota(directory, quota); err != nil {
    77  			return fmt.Errorf("failed to record quota: %w", err)
    78  		}
    79  	}
    80  
    81  	store, err := assetstore.NewStore(directory)
    82  	if err != nil {
    83  		return fmt.Errorf("failed to create asset store: %w", err)
    84  	}
    85  	for _, asset := range clusterTarget.assets {
    86  		if err := store.Destroy(asset); err != nil {
    87  			return fmt.Errorf("failed to destroy asset %q: %w", asset.Name(), err)
    88  		}
    89  	}
    90  
    91  	// delete the state file as well
    92  	err = store.DestroyState()
    93  	if err != nil {
    94  		return fmt.Errorf("failed to remove state file: %w", err)
    95  	}
    96  
    97  	// delete terraform files
    98  	tfstateFiles, err := filepath.Glob(filepath.Join(directory, "*.tfstate"))
    99  	if err != nil {
   100  		return fmt.Errorf("failed to glob for tfstate files: %w", err)
   101  	}
   102  	tfvarsFiles, err := filepath.Glob(filepath.Join(directory, "*.tfvars.json"))
   103  	if err != nil {
   104  		return fmt.Errorf("failed to glob for tfvars files: %w", err)
   105  	}
   106  	for _, f := range append(tfstateFiles, tfvarsFiles...) {
   107  		if err := os.Remove(f); err != nil {
   108  			return fmt.Errorf("failed to remove terraform file %q: %w", f, err)
   109  		}
   110  	}
   111  
   112  	// ensure capi etcd data store and capi artifacts are cleaned up
   113  	capiArtifactsDir := filepath.Join(directory, clusterapi.ArtifactsDir)
   114  	if err := os.RemoveAll(capiArtifactsDir); err != nil {
   115  		logrus.Warnf("failed to remove %s: %v", capiArtifactsDir, err)
   116  	}
   117  
   118  	timer.StopTimer(timer.TotalTimeElapsed)
   119  	timer.LogSummary()
   120  
   121  	return nil
   122  }
   123  
   124  func newDestroyBootstrapCmd() *cobra.Command {
   125  	return &cobra.Command{
   126  		Use:   "bootstrap",
   127  		Short: "Destroy the bootstrap resources",
   128  		Args:  cobra.ExactArgs(0),
   129  		Run: func(cmd *cobra.Command, args []string) {
   130  			cleanup := command.SetupFileHook(command.RootOpts.Dir)
   131  			defer cleanup()
   132  
   133  			timer.StartTimer(timer.TotalTimeElapsed)
   134  			err := bootstrap.Destroy(context.TODO(), command.RootOpts.Dir)
   135  			if err != nil {
   136  				logrus.Fatal(err)
   137  			}
   138  			timer.StopTimer(timer.TotalTimeElapsed)
   139  			timer.LogSummary()
   140  		},
   141  	}
   142  }