Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Git LFS Pull for deployment
- name: Update APT cache
run: |
sudo apt-get update

- name: Git LFS Pull for deployment
run: |
echo "Pulling all Git LFS"
git lfs pull -I **/challenges/**/ansible/**/*

Expand Down Expand Up @@ -77,7 +81,6 @@ jobs:

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends --yes zfsutils-linux

- name: Setup squid
Expand Down
2 changes: 1 addition & 1 deletion challenges/mock-track-apache-php/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ resource "incus_instance" "this" {
resource "incus_network_zone_record" "this" {
for_each = local.instances

zone = "ctf"
zone = var.ctf_dns_network_zone

name = each.value["record"]
description = each.value["description"]
Expand Down
2 changes: 1 addition & 1 deletion challenges/mock-track-python-service/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ resource "incus_instance" "this" {
resource "incus_network_zone_record" "this" {
for_each = local.instances

zone = "ctf"
zone = var.ctf_dns_network_zone

name = each.value["record"]
description = each.value["description"]
Expand Down
4 changes: 4 additions & 0 deletions ctf/templates/init/.deploy/common/dns.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ resource "incus_network_zone" "this" {
name = "ctf"
description = "DNS zone for the internal .ctf TLD"
}

output "ctf_dns_network_zone" {
value = incus_network_zone.this.name
}
4 changes: 4 additions & 0 deletions ctf/templates/init/.deploy/common/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ variable "build_container" {
type = bool
}

variable "ctf_dns_network_zone" {
default = "ctf"
type = string
}

locals {
track = yamldecode(file("${path.module}/../track.yaml"))
Expand Down
2 changes: 1 addition & 1 deletion ctf/templates/new/common/main.tf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ resource "incus_network_zone_record" "this" {
# This resource is generated for each instances (the `locals` section of this file)
for_each = local.instances

zone = "ctf"
zone = var.ctf_dns_network_zone

name = each.value["record"]
description = each.value["description"]
Expand Down
83 changes: 82 additions & 1 deletion ctf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def add_tracks_to_terraform_modules(tracks: set[Track]):
build_container = {{ 'true' if track.require_build_container else 'false' }}
{% if track.production %}deploy = "production"{% endif %}
{% if track.remote %}incus_remote = "{{ track.remote }}"{% endif %}
depends_on = [module.common]
{% for ov in output_variables %}
{{ ov }} = module.common.{{ ov }}
{% endfor %}
}
{% endfor %}
"""
Expand All @@ -115,6 +117,7 @@ def add_tracks_to_terraform_modules(tracks: set[Track]):
fd.write(
template.render(
tracks=tracks - get_terraform_tracks_from_modules(),
output_variables=get_common_modules_output_variables(),
)
)

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


def get_common_modules_output_variables() -> set[str]:
output_variables: set[str] = set()
output_variable_regex: re.Pattern = re.compile(
r'^output\s*"([a-zA-Z_\-]+)"\s*{', re.MULTILINE
)
variable_regex: re.Pattern = re.compile(
r'^variable\s*"([a-zA-Z_\-]+)"\s*{', re.MULTILINE
)

variables: set[str] = set()

for file in os.listdir(
path := os.path.join(find_ctf_root_directory(), ".deploy", "common")
):
if file == "versions.tf":
continue

with open(os.path.join(path, file), "r") as f:
match file:
case "variables.tf":
for i in variable_regex.findall(f.read()):
variables.add(i)
case _:
for i in output_variable_regex.findall(f.read()):
output_variables.add(i)

for variable in output_variables - variables:
LOG.error(
msg
:= f'Variable "{variable}" could not be found in "variables.tf". This could cause an issue when creating/destroying an environment.'
)

if (
input(f'Do you want to add "{variable}" to "variables.tf"? [y/N] ').lower()
or "n"
) == "n":
raise Exception(msg)

try:
print("Do CTRL+C to cancel...")
while not (default := input("What is the default value? ")):
print("Do CTRL+C to cancel...")

var_type = input("What is the type? [string] ") or "string"

with open(os.path.join(path, "variables.tf"), "a") as f:
f.write("\n")
template = jinja2.Environment().from_string(
source=textwrap.dedent(
text="""\
variable "{{variable}}" {
default = "{{default}}"
type = {{type}}
}
"""
)
)
f.write(
template.render(variable=variable, default=default, type=var_type)
)
variables.add(variable)
except KeyboardInterrupt:
LOG.warning(
f'Cancelling the addition of the "{variable}" to "variables.tf".'
)

raise Exception(msg)

if len(output_variables - variables) != 0:
LOG.critical(
msg
:= f'Some output variables were not found in "variables.tf": {", ".join(output_variables - variables)}'
)
raise Exception(msg)

return output_variables & variables


def get_terraform_tracks_from_modules() -> set[Track]:
with open(
file=os.path.join(find_ctf_root_directory(), ".deploy", "modules.tf"), mode="r"
Expand Down