forked from rancher/terraform-rancher2-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
275 lines (261 loc) · 8.22 KB
/
main.tf
File metadata and controls
275 lines (261 loc) · 8.22 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# There are many ways to orchestrate Terraform configurations with the goal of breaking it down
# I am using Terraform resources to orchestrate Terraform
# I felt this was the best way to accomplish the goal without incurring additional dependencies
locals {
template_files = var.template_files
template_file_map = { for file in local.template_files : basename(file) => file }
# template_file_map = { for i in range(length(local.template_files)) : tostring(i) => local.template_files[i] }
# need to figure out how to copy the files sent, the for_each loop won't work due to the dynamic read of the directory
inputs = var.inputs
environment_variables = merge(var.environment_variables, { "TF_DATA_DIR" = local.tf_data_dir })
export_contents = (
local.environment_variables != null ?
join(";", [for k, v in local.environment_variables : "export ${k}=${v}"])
: ""
)
deploy_trigger = var.deploy_trigger
deploy_path = chomp(var.deploy_path)
attempts = var.attempts
interval = var.interval
timeout = var.timeout
init = var.init
init_script = (local.init ? "terraform init -reconfigure -upgrade" : "")
tf_data_dir = (var.data_path != null ? var.data_path : path.root)
skip_destroy = (var.skip_destroy ? "true" : "")
}
resource "file_local_directory" "deploy_path" {
path = local.deploy_path
permissions = "0755"
}
resource "file_local_directory" "tf_data_dir" {
count = (local.tf_data_dir != local.deploy_path ? 1 : 0)
path = local.tf_data_dir
permissions = "0755"
}
### Template Files ###
data "file_local" "template_files" {
for_each = local.template_file_map
directory = dirname(each.value)
name = each.key
}
resource "file_local_snapshot" "persist_tpl_file" {
depends_on = [
file_local_directory.deploy_path,
file_local_directory.tf_data_dir,
data.file_local.template_files,
]
for_each = local.template_file_map
directory = dirname(each.value)
name = each.key
update_trigger = local.deploy_trigger
}
resource "file_local" "instantiate_tpl_snapshot" {
depends_on = [
file_local_directory.deploy_path,
file_local_directory.tf_data_dir,
data.file_local.template_files,
file_local_snapshot.persist_tpl_file,
]
for_each = local.template_file_map
directory = local.deploy_path
name = each.key
permissions = data.file_local.template_files[each.key].permissions
contents = base64decode(file_local_snapshot.persist_tpl_file[each.key].snapshot)
}
### Inputs ###
resource "file_local" "write_tmp_inputs" {
depends_on = [
file_local_directory.deploy_path,
file_local_directory.tf_data_dir,
]
directory = local.tf_data_dir
name = "inputs.tmp"
contents = local.inputs
}
resource "file_local_snapshot" "persist_inputs" {
depends_on = [
file_local_directory.deploy_path,
file_local_directory.tf_data_dir,
file_local.write_tmp_inputs,
]
directory = local.tf_data_dir
name = "inputs.tmp"
update_trigger = local.deploy_trigger
}
resource "file_local" "instantiate_inputs_snapshot" {
depends_on = [
file_local_directory.deploy_path,
file_local_directory.tf_data_dir,
file_local.write_tmp_inputs,
file_local_snapshot.persist_inputs,
]
directory = local.deploy_path
name = "inputs.tfvars"
contents = base64decode(file_local_snapshot.persist_inputs.snapshot)
}
### Environment Variables ###
resource "file_local" "write_tmp_env" {
depends_on = [
file_local_directory.deploy_path,
file_local_directory.tf_data_dir,
]
directory = local.tf_data_dir
name = "env.tmp"
contents = local.export_contents
}
resource "file_local_snapshot" "persist_envrc" {
depends_on = [
file_local_directory.deploy_path,
file_local_directory.tf_data_dir,
file_local.write_tmp_env,
]
directory = local.tf_data_dir
name = "env.tmp"
update_trigger = local.deploy_trigger
}
resource "file_local" "instantiate_envrc_snapshot" {
depends_on = [
file_local_directory.deploy_path,
file_local_directory.tf_data_dir,
file_local.write_tmp_env,
file_local_snapshot.persist_envrc,
]
directory = local.deploy_path
name = "envrc"
contents = base64decode(file_local_snapshot.persist_envrc.snapshot)
permissions = "0700" # make it executable so it can be sourced
}
resource "terraform_data" "destroy" {
depends_on = [
file_local.instantiate_envrc_snapshot,
file_local.instantiate_inputs_snapshot,
file_local.instantiate_tpl_snapshot,
]
triggers_replace = {
trigger = local.deploy_trigger
dp = local.deploy_path
sd = local.skip_destroy
to = local.timeout
}
provisioner "local-exec" {
when = destroy
command = templatefile("${path.module}/destroy.sh.tpl", {
deploy_path = self.triggers_replace.dp
skip_destroy = self.triggers_replace.sd
timeout = self.triggers_replace.to
})
}
}
resource "terraform_data" "create" {
depends_on = [
file_local.instantiate_envrc_snapshot,
file_local.instantiate_inputs_snapshot,
file_local.instantiate_tpl_snapshot,
terraform_data.destroy,
]
triggers_replace = {
never = <<-EOT
This resource is only meant to run once,
on the initial deploy,
the secondary create manages updates.
EOT
}
provisioner "local-exec" {
command = templatefile("${path.module}/create.sh.tpl", {
deploy_path = local.deploy_path
init_script = local.init_script
attempts = local.attempts
timeout = local.timeout
interval = local.interval
})
}
}
resource "file_local_snapshot" "persist_state" {
depends_on = [
terraform_data.destroy,
terraform_data.create,
]
directory = local.deploy_path
name = "tfstate"
update_trigger = terraform_data.create.id
}
resource "file_local" "instantiate_state" {
depends_on = [
terraform_data.destroy,
terraform_data.create,
file_local_snapshot.persist_state,
]
directory = local.deploy_path
name = "tfstate"
contents = base64decode(file_local_snapshot.persist_state.snapshot)
}
resource "file_local_snapshot" "persist_outputs" {
depends_on = [
terraform_data.destroy,
terraform_data.create,
]
directory = local.deploy_path
name = "outputs.json"
update_trigger = terraform_data.create.id
}
resource "file_local" "instantiate_outputs" {
depends_on = [
terraform_data.destroy,
terraform_data.create,
file_local_snapshot.persist_outputs,
]
directory = local.deploy_path
name = "outputs.json"
contents = base64decode(file_local_snapshot.persist_outputs.snapshot)
}
# during initial create this should be an extra apply that has no effect
# when the inputs change and the template needs to be rebuilt this will allow the persist
# to rebuild the template and state file before running the create script
resource "terraform_data" "create_after_persist" {
depends_on = [
file_local.instantiate_envrc_snapshot,
file_local.instantiate_inputs_snapshot,
file_local.instantiate_tpl_snapshot,
terraform_data.destroy,
terraform_data.create,
file_local.instantiate_state,
file_local.instantiate_outputs,
]
triggers_replace = {
trigger = local.deploy_trigger
}
provisioner "local-exec" {
command = templatefile("${path.module}/create.sh.tpl", {
deploy_path = local.deploy_path
init_script = local.init_script
attempts = local.attempts
timeout = local.timeout
interval = local.interval
})
}
}
resource "terraform_data" "destroy_end" {
depends_on = [
file_local.instantiate_envrc_snapshot,
file_local.instantiate_inputs_snapshot,
file_local.instantiate_tpl_snapshot,
terraform_data.destroy,
terraform_data.create,
file_local.instantiate_state,
file_local.instantiate_outputs,
terraform_data.create_after_persist,
]
triggers_replace = {
dp = local.deploy_path
sd = local.skip_destroy
to = local.timeout
}
provisioner "local-exec" {
when = destroy
command = templatefile("${path.module}/destroy.sh.tpl", {
deploy_path = self.triggers_replace.dp
skip_destroy = self.triggers_replace.sd
timeout = self.triggers_replace.to
})
}
}