-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatt.go
90 lines (78 loc) · 2.72 KB
/
matt.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
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"log"
"os"
"strings"
"sync"
"github.com/bryanlabs/matt/utils/account"
"github.com/bryanlabs/matt/utils/terraform"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/ini.v1"
)
var (
awsAccountSource = kingpin.Flag("aws-account-source", "Account Source.").Required().Short('a').String()
mattConf = kingpin.Flag("conf", "Conf File.").Required().Short('f').String()
tfCmd = kingpin.Flag("tf-cmd", "Terraform Command.").Short('c').String()
tfPath = kingpin.Arg("tf-path", "Path to terraform modules.").Required().String()
wg sync.WaitGroup
)
// Loop over all profiles and terraform them.
func main() {
kingpin.Version("0.0.1")
kingpin.Parse()
slice := strings.Split(*awsAccountSource, ",")
wg.Add(len(slice))
os.MkdirAll("matt", os.ModePerm)
for _, i := range slice {
matt(i, *tfCmd, *tfPath, *mattConf)
}
wg.Wait()
}
// goTerraform will used a named profile to apply a terraform state.
func matt(accountnum string, tfcmd string, statepath string, conf string) {
cfg, err := ini.Load(conf)
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
var slice []string
keys := cfg.Section("matt").KeyStrings()
for _, key := range keys {
val := cfg.Section("matt").Key(key).String()
slice = append(slice, key+"="+val+"")
}
log.Printf("Terraforming %v with %v\n", accountnum, statepath)
//Update account_id for provider and tfvars.
modulename := account.GetModuleName(statepath)
evoaccounts := account.GetEvoAccounts()
info := account.GetAccountInfo(evoaccounts)
arnstr := "all_accountarns=" + info.Arns
accountsstr := "all_accountnumbers=" + info.Accounts
accountstr := "account_id=" + accountnum
slice = append(slice, arnstr, accountstr, accountsstr)
options := "-var '" + strings.Join(slice, "' -var '") + "'"
domain := strings.ToLower(os.Getenv("userdomain"))
bucket := domain + "-terraform-remote-state-storage-s3"
if len(tfcmd) > 1 {
switch cmd := tfcmd; cmd {
case "create":
terraform.Init(statepath, options, accountnum, modulename, bucket)
terraform.Plan(accountnum, statepath, options, modulename)
case "apply":
terraform.Init(statepath, options, accountnum, modulename, bucket)
terraform.Apply(accountnum, statepath, options, modulename)
case "destroy":
terraform.Init(statepath, options, accountnum, modulename, bucket)
terraform.Destroy(accountnum, statepath, options, modulename)
default:
log.Printf("Command: %v not supported\n", tfcmd)
}
} else {
terraform.Init(statepath, options, accountnum, modulename, bucket)
terraform.Plan(accountnum, statepath, options, modulename)
terraform.Apply(accountnum, statepath, options, modulename)
}
log.Printf("Terraforming %v Complete\n", accountnum)
wg.Done()
}