-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
162 lines (138 loc) · 4.27 KB
/
main.tf
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
data "template_file" "_log_configuration" {
count = var.log_driver == "__NOT_DEFINED__" ? 0 : 1
# depends_on = ["data.template_file._log_driver_options"]
template = <<JSON
$${ jsonencode("logConfiguration") } : {
$${ jsonencode("logDriver") } : $${ jsonencode(log_driver) },
$${ jsonencode("options") } : {
$${ log_driver_options }
}
}
JSON
vars = {
log_driver = var.log_driver
log_driver_options = join(",\n", data.template_file._log_driver_options.*.rendered)
}
}
data "template_file" "_log_driver_options" {
count = length(keys(var.log_driver_options))
template = <<JSON
$${ jsonencode(key) }: $${ jsonencode(value)}
JSON
vars = {
key = keys(var.log_driver_options)[count.index]
value = var.log_driver_options[keys(var.log_driver_options)[count.index]]
}
}
data "template_file" "_port_mappings" {
# depends_on = ["data.template_file._port_mapping"]
template = <<JSON
$${val}
JSON
#host_port == "__NOT_DEFINED__" && container_port == "__NOT_DEFINED__" && protocol == "__NOT_DEFINED__" ? $${ jsonencode([])} : $${val}
vars = {
val = join(",\n", data.template_file._port_mapping.*.rendered)
host_port = lookup(var.port_mappings[0], "hostPort", "")
container_port = var.port_mappings[0]["containerPort"]
protocol = lookup(var.port_mappings[0], "protocol", "")
}
}
data "template_file" "_port_mapping" {
count = var.port_mappings[0]["containerPort"] == "__NOT_DEFINED__" ? 0 : length(var.port_mappings)
template = <<JSON
{
$${join(",\n",
compact(
list(
host_port == "" || host_port == "__NOT_DEFINED_" ? "" : "$${ jsonencode("hostPort") }: $${host_port}",
container_port == "" || container_port == "__NOT_DEFINED_" ? "" : "$${jsonencode("containerPort")}: $${container_port}",
protocol == "" || protocol == "__NOT_DEFINED_" ? "" : "$${ jsonencode("protocol") }: $${jsonencode(protocol)}"
)
)
)}
}
JSON
vars = {
host_port = lookup(var.port_mappings[count.index], "hostPort", "")
container_port = var.port_mappings[count.index]["containerPort"]
protocol = lookup(var.port_mappings[count.index], "protocol", "")
}
}
data "template_file" "_environment_vars" {
count = lookup(var.environment_vars, "__NOT_DEFINED__", "__ITS_DEFINED__") == "__NOT_DEFINED__" ? 0 : 1
depends_on = [
data.template_file._environment_var]
template = <<JSON
$${ jsonencode("environment") } : [
$${val}
]
JSON
vars = {
val = join(",\n", data.template_file._environment_var.*.rendered)
}
}
data "template_file" "_environment_var" {
count = length(keys(var.environment_vars)) > 0 ? length(keys(var.environment_vars)) : 0
template = <<JSON
{
$${join(",\n",
compact(
list(
var_name == "__NOT_DEFINED__" ? "" : "$${ jsonencode("name") }: $${ jsonencode(var_name)}",
var_value == "__NOT_DEFINED__" ? "" : "$${ jsonencode("value") }: $${ jsonencode(var_value)}"
)
)
)}
}
JSON
vars = {
var_name = sort(keys(var.environment_vars))[count.index]
var_value = lookup(
var.environment_vars,
sort(keys(var.environment_vars))[count.index],
"",
)
}
}
data "template_file" "_ulimit" {
template = jsonencode([
{
name = "nofile",
softLimit = 30000
hardLimit = 50000
}
])
}
data "template_file" "_final" {
depends_on = [
data.template_file._environment_vars,
data.template_file._port_mappings,
data.template_file._log_configuration,
]
template = <<JSON
[{
$${val}
}]
JSON
vars = {
val = join(
",\n ",
compact(
[
"${jsonencode("cpu")}: ${var.cpu}",
"${jsonencode("memory")}: ${var.memory}",
"${jsonencode("entryPoint")}: ${jsonencode(compact(split(" ", var.entrypoint)))}",
"${jsonencode("command")}: ${jsonencode(compact(split(" ", var.service_command)))}",
"${jsonencode("links")}: ${jsonencode(var.links)}",
"${jsonencode("portMappings")}: [${data.template_file._port_mappings.rendered}]",
join("", data.template_file._environment_vars.*.rendered),
join("", data.template_file._log_configuration.*.rendered),
"${jsonencode("name")}: ${jsonencode(var.service_name)}",
"${jsonencode("image")}: ${jsonencode(var.service_image)}",
"${jsonencode("essential")}: ${var.essential ? true : false}",
"${jsonencode("ulimits")}: ${data.template_file._ulimit.rendered}"
],
),
)
}
}