forked from mhlias/tholos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (117 loc) · 4.33 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"os"
"strings"
"log"
"io/ioutil"
"path/filepath"
"runtime"
"time"
"flag"
"gopkg.in/yaml.v2"
"github.com/mhlias/tholos/aws_helper"
"github.com/mhlias/tholos/tf_helper"
)
type conf struct {
Project string
Region string
Root_profile string `yaml:"root-profile"`
Roam_role string `yaml:"roam-role"`
Accounts_mapping map[string] string `yaml:"accounts-mapping"`
environment string
account string
}
func main() {
use_mfa := true
retries := 3
planPtr := flag.Bool("p", false, "Terraform Plan")
applyPtr := flag.Bool("a", false, "Terraform Apply Plan")
syncPtr := flag.Bool("s", false, "Sync remote S3 state")
modulesPtr := flag.Bool("u", false, "Fetch and update modules from remote repo")
outputsPtr := flag.Bool("o", false, "Display Terraform outputs")
flag.Parse()
if !*planPtr && !*syncPtr && !*modulesPtr && !*outputsPtr && !*applyPtr {
fmt.Println("Please provide one of the following parameters:")
flag.PrintDefaults()
os.Exit(0)
}
project_config := new(conf)
configFile, _ := filepath.Abs("../../../project.yaml")
yamlConf, file_err := ioutil.ReadFile(configFile)
if file_err != nil {
log.Fatalf("[ERROR] File does not exist or not accessible: ", file_err)
}
yaml_err := yaml.Unmarshal(yamlConf, &project_config)
if yaml_err != nil {
log.Fatal(yaml_err)
}
curr_dir, err := os.Getwd()
if err != nil {
log.Fatal("[ERROR] Failed to get current working directory. Aborting with error: ", err)
}
dir_separator := ""
if runtime.GOOS == "windows" {
dir_separator = "\\"
} else {
dir_separator = "/"
}
tmp := strings.Split(curr_dir, dir_separator)
project_config.environment = tmp[len(tmp)-1]
project_config.account = tmp[len(tmp)-2]
mfa_device_id := os.Getenv("MFA_DEVICE_ID")
if len(mfa_device_id) <= 0 {
log.Println("[INFO] No mfa device id is set in the env. Set `MFA_DEVICE_ID` in your environment if you want to use one.")
use_mfa = false
}
if len(project_config.Project) <= 0 {
log.Fatal("[ERROR] No project is set in your project.yaml configuration.")
}
accounts := map[string] bool {fmt.Sprintf("%s-dev", project_config.Project): true,
fmt.Sprintf("%s-prd", project_config.Project): true,
}
state_config := &tf_helper.Config{ Bucket_name: fmt.Sprintf("%s-%s-%s-tfstate", project_config.Project, project_config.account, project_config.environment),
State_filename: fmt.Sprintf("%s-%s-%s.tfstate", project_config.Project, project_config.account, project_config.environment),
Versioning: true,
}
modules := &tf_helper.Modules{}
if _, ok := accounts[project_config.account]; !ok {
log.Fatalf("[ERROR] Account directories do not match project name. Name found: %s, expected %s-dev or %s-prd\n", project_config.account, project_config.Project, project_config.Project)
}
var client interface{}
if !*modulesPtr {
awsconf := &aws_helper.Config{ Region: project_config.Region,
Profile: project_config.Root_profile,
Role: project_config.Roam_role,
Account_id: project_config.Accounts_mapping[project_config.account],
Use_mfa: use_mfa,
}
client = awsconf.Connect()
}
if *syncPtr {
bucket_created := false
for i:=1; i<=retries; i++ {
if !state_config.Create_bucket(client) {
log.Printf("[WARN] S3 Bucket %s failed to be created. Retrying.\n", state_config.Bucket_name)
} else {
log.Printf("[INFO] S3 Bucket %s created and versioning enabled.\n", state_config.Bucket_name)
bucket_created = true
break
}
time.Sleep(time.Duration(i)*time.Second)
}
if bucket_created {
state_config.Setup_remote_state()
} else {
log.Fatalf("[ERROR] S3 Bucket failed to be created after %d retries. Aborting.\n", retries)
}
} else if *planPtr {
state_config.Plan()
} else if *modulesPtr {
modules.Fetch_modules()
} else if *outputsPtr {
state_config.Outputs()
} else if *applyPtr {
state_config.Apply()
}
}