summaryrefslogtreecommitdiff
path: root/context.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-09-30 09:16:01 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2019-09-30 09:16:01 -0600
commit8eb2c3725199b17ae713dd0756a0e491e4829c12 (patch)
treed368a149518ed7247de4a2571e41aa7a6dbb38c7 /context.go
parent1e662262179d326586d2beb849f842b82b7324c4 (diff)
Clean up provisioned modules on error; refactor Run(); add Validate()
Modules that return an error during provisioning should still be cleaned up so that they don't leak any resources they may have allocated before the error occurred. Cleanup should be able to run even if Provision does not complete fully.
Diffstat (limited to 'context.go')
-rw-r--r--context.go9
1 files changed, 9 insertions, 0 deletions
diff --git a/context.go b/context.go
index 2fd84d5..c29aa1d 100644
--- a/context.go
+++ b/context.go
@@ -131,6 +131,14 @@ func (ctx Context) LoadModule(name string, rawMsg json.RawMessage) (interface{},
if prov, ok := val.(Provisioner); ok {
err := prov.Provision(ctx)
if err != nil {
+ // incomplete provisioning could have left state
+ // dangling, so make sure it gets cleaned up
+ if cleanerUpper, ok := val.(CleanerUpper); ok {
+ err2 := cleanerUpper.Cleanup()
+ if err2 != nil {
+ err = fmt.Errorf("%v; additionally, cleanup: %v", err, err2)
+ }
+ }
return nil, fmt.Errorf("provision %s: %v", mod.Name, err)
}
}
@@ -138,6 +146,7 @@ func (ctx Context) LoadModule(name string, rawMsg json.RawMessage) (interface{},
if validator, ok := val.(Validator); ok {
err := validator.Validate()
if err != nil {
+ // since the module was already provisioned, make sure we clean up
if cleanerUpper, ok := val.(CleanerUpper); ok {
err2 := cleanerUpper.Cleanup()
if err2 != nil {