forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/openshift-install: Add 'destroy bootstrap' command
Using Terraform to remove all resources created by the bootstrap modules. For this to work, all platforms must define a bootstrap module (and they all currently do). This command moves the previous destroy-cluster into a new 'destroy cluster' subcommand, because grouping different destroy flavors into sub-commands makes the base command easier to understand. We expect both destroy flavors to be long-running, because it's hard to write generic logic for "is the cluster sufficiently live for us to remove the bootstrap". We don't want to hang forever if the cluster dies before coming up, but there's no solid rules for how long to wait before deciding that it's never going to come up. When we start destroying the bootstrap resources automatically in the future, will pick reasonable timeouts, but will want to still provide callers with the ability to manually remove the bootstrap resources if we happen to fall out of that timeout on a cluster that does eventually come up. I've also created a LoadMetadata helper to share the "retrieve the metadata from the asset directory" logic between the destroy-cluster and destroy-bootstrap logic. The new helper lives in the cluster asset plackage close to the code that determines that file's location. I've pushed the Terraform module unpacking and 'terraform init' call down into a helper used by the Apply and Destroy functions to make life easier on the callers. I've also fixed a path.Join -> filepath.Join typo in Apply, which dates back to ff5a57b (pkg/terraform: Modify some helper functions for the new binary layout, 2018-09-19, openshift#289). These aren't network paths ;).
- Loading branch information
Showing
9 changed files
with
183 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Package bootstrap uses Terraform to remove bootstrap resources. | ||
package bootstrap | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/openshift/installer/pkg/asset/cluster" | ||
"github.com/openshift/installer/pkg/terraform" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Destroy uses Terraform to remove bootstrap resources. | ||
func Destroy(dir string) (err error) { | ||
metadata, err := cluster.LoadMetadata(dir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
platform := metadata.Platform() | ||
if platform == "" { | ||
return errors.New("no platform configured in metadata") | ||
} | ||
|
||
tempDir, err := ioutil.TempDir("", "openshift-install-") | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create temporary directory for Terraform execution") | ||
} | ||
defer os.RemoveAll(tempDir) | ||
|
||
for _, filename := range []string{terraform.StateFileName, cluster.TfVarsFileName} { | ||
err = copy(filepath.Join(dir, filename), filepath.Join(tempDir, filename)) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to copy %s to the temporary directory", filename) | ||
} | ||
} | ||
|
||
logrus.Infof("Using Terraform to destroy bootstrap resources...") | ||
err = terraform.Destroy(tempDir, platform, "-target=module.bootstrap") | ||
if err != nil { | ||
err = errors.Wrap(err, "failed to run terraform") | ||
} | ||
|
||
return os.Rename(filepath.Join(dir, terraform.StateFileName), filepath.Join(tempDir, terraform.StateFileName)) | ||
} | ||
|
||
func copy(from string, to string) error { | ||
data, err := ioutil.ReadFile(from) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ioutil.WriteFile(to, data, 0666) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters