forked from EngineerBetter/concourse-up
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy.go
79 lines (63 loc) · 1.74 KB
/
destroy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package concourse
import (
"fmt"
"io"
"github.com/EngineerBetter/concourse-up/bosh"
"github.com/EngineerBetter/concourse-up/config"
"github.com/EngineerBetter/concourse-up/terraform"
)
// Destroy destroys a concourse instance
func (client *Client) Destroy() error {
conf, err := client.configClient.Load()
if err != nil {
return err
}
terraformClient, err := client.buildTerraformClient(conf)
if err != nil {
return err
}
defer terraformClient.Cleanup()
metadata, err := terraformClient.Output()
if err != nil {
return err
}
if err = client.deleteBosh(conf, metadata); err != nil {
fmt.Printf("Warning error deleting bosh: \n%s\n\nContinuing with terraform destroy\n", err.Error())
}
if err := terraformClient.Destroy(); err != nil {
return err
}
if err := client.configClient.DeleteAll(conf); err != nil {
return err
}
return writeDestroySuccessMessage(client.stdout)
}
func (client *Client) deleteBosh(conf *config.Config, metadata *terraform.Metadata) error {
boshClient, err := client.buildBoshClient(conf, metadata)
if err != nil {
return err
}
defer boshClient.Cleanup()
boshStateBytes, err := loadDirectorState(client.configClient)
if err != nil {
return nil
}
boshStateBytes, err = boshClient.Delete(boshStateBytes)
if err != nil {
// If there was an error,
// attempt to save the bosh state before propagating
// This prevents issues when re-trying
if boshStateBytes != nil {
client.configClient.StoreAsset(bosh.StateFilename, boshStateBytes)
}
return err
}
if err = client.configClient.DeleteAsset(bosh.StateFilename); err != nil {
return err
}
return nil
}
func writeDestroySuccessMessage(stdout io.Writer) error {
_, err := stdout.Write([]byte("\nDESTROY SUCCESSFUL\n\n"))
return err
}