Skip to content

Commit 63a7914

Browse files
committed
Adapting dependencies by outputting a variable out of the common module and then, adding it to each track's module so that OpenTOFU understands how assets depends on each other and properly create/destroy assets.
1 parent a2aca72 commit 63a7914

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

.github/workflows/tests.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ jobs:
2424
- name: Checkout
2525
uses: actions/checkout@v4
2626

27+
- name: Update APT cache
28+
run: |
29+
sudo apt-get update
30+
2731
- name: Git LFS Pull for deployment
2832
run: |
2933
echo "Pulling all Git LFS"
@@ -77,7 +81,6 @@ jobs:
7781
7882
- name: Install dependencies
7983
run: |
80-
sudo apt-get update
8184
sudo apt-get install --no-install-recommends --yes zfsutils-linux
8285
8386
- name: Setup squid

ctf/templates/init/.deploy/common/dns.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ resource "incus_network_zone" "this" {
44
name = "ctf"
55
description = "DNS zone for the internal .ctf TLD"
66
}
7+
8+
output "ctf_dns_network_zone" {
9+
value = incus_network_zone.this.name
10+
}

ctf/templates/init/.deploy/common/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ variable "build_container" {
1313
type = bool
1414
}
1515

16+
variable "ctf_dns_network_zone" {
17+
default = "ctf"
18+
type = string
19+
}
1620

1721
locals {
1822
track = yamldecode(file("${path.module}/../track.yaml"))

ctf/templates/new/common/main.tf.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ resource "incus_network_zone_record" "this" {
157157
# This resource is generated for each instances (the `locals` section of this file)
158158
for_each = local.instances
159159

160-
zone = "ctf"
160+
zone = var.ctf_dns_network_zone
161161

162162
name = each.value["record"]
163163
description = each.value["description"]

ctf/utils.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ def add_tracks_to_terraform_modules(tracks: set[Track]):
106106
build_container = {{ 'true' if track.require_build_container else 'false' }}
107107
{% if track.production %}deploy = "production"{% endif %}
108108
{% if track.remote %}incus_remote = "{{ track.remote }}"{% endif %}
109-
depends_on = [module.common]
109+
{% for ov in output_variables %}
110+
{{ ov }} = module.common.{{ ov }}
111+
{% endfor %}
110112
}
111113
{% endfor %}
112114
"""
@@ -115,6 +117,7 @@ def add_tracks_to_terraform_modules(tracks: set[Track]):
115117
fd.write(
116118
template.render(
117119
tracks=tracks - get_terraform_tracks_from_modules(),
120+
output_variables=get_common_modules_output_variables(),
118121
)
119122
)
120123

@@ -138,6 +141,84 @@ def create_terraform_modules_file(remote: str, production: bool = False):
138141
fd.write(template.render(production=production, remote=remote))
139142

140143

144+
def get_common_modules_output_variables() -> set[str]:
145+
output_variables: set[str] = set()
146+
output_variable_regex: re.Pattern = re.compile(
147+
r'^output\s*"([a-zA-Z_\-]+)"\s*{', re.MULTILINE
148+
)
149+
variable_regex: re.Pattern = re.compile(
150+
r'^variable\s*"([a-zA-Z_\-]+)"\s*{', re.MULTILINE
151+
)
152+
153+
variables: set[str] = set()
154+
155+
for file in os.listdir(
156+
path := os.path.join(find_ctf_root_directory(), ".deploy", "common")
157+
):
158+
if file == "versions.tf":
159+
continue
160+
161+
with open(os.path.join(path, file), "r") as f:
162+
match file:
163+
case "variables.tf":
164+
for i in variable_regex.findall(f.read()):
165+
variables.add(i)
166+
case _:
167+
for i in output_variable_regex.findall(f.read()):
168+
output_variables.add(i)
169+
170+
for variable in output_variables - variables:
171+
LOG.error(
172+
msg
173+
:= f'Variable "{variable}" could not be found in "variables.tf". This could cause an issue when creating/destroying an environment.'
174+
)
175+
176+
if (
177+
input(f'Do you want to add "{variable}" to "variables.tf"? [y/N] ').lower()
178+
or "n"
179+
) == "n":
180+
raise Exception(msg)
181+
182+
try:
183+
print("Do CTRL+C to cancel...")
184+
while not (default := input("What is the default value? ")):
185+
print("Do CTRL+C to cancel...")
186+
187+
var_type = input("What is the type? [string] ") or "string"
188+
189+
with open(os.path.join(path, "variables.tf"), "a") as f:
190+
f.write("\n")
191+
template = jinja2.Environment().from_string(
192+
source=textwrap.dedent(
193+
text="""\
194+
variable "{{variable}}" {
195+
default = "{{default}}"
196+
type = {{type}}
197+
}
198+
"""
199+
)
200+
)
201+
f.write(
202+
template.render(variable=variable, default=default, type=var_type)
203+
)
204+
variables.add(variable)
205+
except KeyboardInterrupt:
206+
LOG.warning(
207+
f'Cancelling the addition of the "{variable}" to "variables.tf".'
208+
)
209+
210+
raise Exception(msg)
211+
212+
if len(output_variables - variables) != 0:
213+
LOG.critical(
214+
msg
215+
:= f'Some output variables were not found in "variables.tf": {", ".join(output_variables - variables)}'
216+
)
217+
raise Exception(msg)
218+
219+
return output_variables & variables
220+
221+
141222
def get_terraform_tracks_from_modules() -> set[Track]:
142223
with open(
143224
file=os.path.join(find_ctf_root_directory(), ".deploy", "modules.tf"), mode="r"

0 commit comments

Comments
 (0)