Skip to content

Commit c57e30d

Browse files
committed
Added ansible for build container, remove require build container in module.tf once the build is over, reapply to destroy the build container once the file is extracted.
1 parent cda52d3 commit c57e30d

File tree

4 files changed

+105
-54
lines changed

4 files changed

+105
-54
lines changed

ctf/deploy.py

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
get_all_available_tracks,
2121
get_terraform_tracks_from_modules,
2222
parse_track_yaml,
23+
remove_tracks_from_terraform_modules,
2324
terraform_binary,
2425
validate_track_can_be_deployed,
2526
)
@@ -76,9 +77,7 @@ def deploy(
7677
distinct_tracks = tmp_tracks
7778

7879
add_tracks_to_terraform_modules(
79-
tracks=distinct_tracks - get_terraform_tracks_from_modules(),
80-
remote=remote,
81-
production=production,
80+
tracks=distinct_tracks - get_terraform_tracks_from_modules()
8281
)
8382
else:
8483
# Run generate first.
@@ -137,10 +136,70 @@ def deploy(
137136

138137
for track in distinct_tracks:
139138
if track.require_build_container:
140-
# TODO: Ansible build containers here
141-
142-
# TODO: Set the "build_container" OpenTofu variable to false in module.tf
143-
pass
139+
run_ansible_playbook(
140+
remote=remote,
141+
production=production,
142+
track=track.name,
143+
path=os.path.join(
144+
find_ctf_root_directory(), "challenges", track.name, "ansible"
145+
),
146+
playbook="build.yaml",
147+
execute_common=False,
148+
)
149+
150+
remove_tracks_from_terraform_modules(
151+
{track}, remote=remote, production=production
152+
)
153+
add_tracks_to_terraform_modules(
154+
{
155+
Track(
156+
name=track.name,
157+
remote=track.remote,
158+
production=track.production,
159+
require_build_container=False,
160+
)
161+
}
162+
)
163+
164+
try:
165+
subprocess.run(
166+
args=[terraform_binary(), "apply", "-auto-approve"],
167+
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
168+
check=True,
169+
)
170+
except subprocess.CalledProcessError:
171+
LOG.warning(
172+
f"The project could not deploy due to instable state. It is often due to CTRL+C while deploying as {os.path.basename(terraform_binary())} was not able to save the state of each object created."
173+
)
174+
175+
if (
176+
input("Do you want to clean and start over? [Y/n] ").lower() or "y"
177+
) != "y":
178+
exit(code=1)
179+
180+
force = True
181+
destroy(
182+
tracks=tracks, production=production, remote=remote, force=force
183+
)
184+
185+
distinct_tracks = generate(
186+
tracks=tracks, production=production, remote=remote
187+
)
188+
189+
subprocess.run(
190+
args=[terraform_binary(), "apply", "-auto-approve"],
191+
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
192+
check=True,
193+
)
194+
except KeyboardInterrupt:
195+
LOG.warning(
196+
"CTRL+C was detected during Terraform deployment. Destroying everything..."
197+
)
198+
force = True
199+
destroy(
200+
tracks=tracks, production=production, remote=remote, force=force
201+
)
202+
exit(code=0)
144203

145204
if not os.path.exists(
146205
path=(
@@ -244,31 +303,39 @@ def deploy(
244303
)
245304

246305

247-
def run_ansible_playbook(remote: str, production: bool, track: str, path: str) -> None:
306+
def run_ansible_playbook(
307+
remote: str,
308+
production: bool,
309+
track: str,
310+
path: str,
311+
playbook: str = "deploy.yaml",
312+
execute_common: bool = True,
313+
) -> None:
248314
extra_args = []
249315
if remote:
250316
extra_args += ["-e", f"ansible_incus_remote={remote}"]
251317

252318
if production:
253319
extra_args += ["-e", "nsec_production=true"]
254320

255-
LOG.info(msg=f"Running common yaml with ansible for track {track}...")
256-
ansible_args = [
257-
"ansible-playbook",
258-
os.path.join("..", "..", "..", ".deploy", "common.yaml"),
259-
"-i",
260-
"inventory",
261-
] + extra_args
262-
subprocess.run(
263-
args=ansible_args,
264-
cwd=path,
265-
check=True,
266-
)
321+
if execute_common:
322+
LOG.info(msg=f"Running common yaml with ansible for track {track}...")
323+
ansible_args = [
324+
"ansible-playbook",
325+
os.path.join("..", "..", "..", ".deploy", "common.yaml"),
326+
"-i",
327+
"inventory",
328+
] + extra_args
329+
subprocess.run(
330+
args=ansible_args,
331+
cwd=path,
332+
check=True,
333+
)
267334

268-
LOG.info(msg=f"Running deploy.yaml with ansible for track {track}...")
335+
LOG.info(msg=f"Running {playbook} with ansible for track {track}...")
269336
ansible_args = [
270337
"ansible-playbook",
271-
"deploy.yaml",
338+
playbook,
272339
"-i",
273340
"inventory",
274341
] + extra_args

ctf/destroy.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ def destroy(
117117
*(
118118
[] # If every track needs to be destroyed, destroy everything including the network zone as well.
119119
if total_deployed_tracks == len(terraform_tracks)
120-
else [f"-target=module.track-{track.name}" for track in terraform_tracks]
120+
else [
121+
f"-target=module.track-{track.name}" for track in terraform_tracks
122+
]
121123
),
122124
],
123125
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
@@ -176,7 +178,9 @@ def destroy(
176178
)
177179

178180
if (tmp_module_name := module.name[0:15]) in networks:
179-
LOG.warning(msg=f"The network {tmp_module_name} was not destroyed properly.")
181+
LOG.warning(
182+
msg=f"The network {tmp_module_name} was not destroyed properly."
183+
)
180184
if (
181185
force
182186
or (input("Do you want to destroy it? [Y/n] ").lower() or "y") == "y"
@@ -191,7 +195,9 @@ def destroy(
191195
if (tmp_module := module) in network_acls or (
192196
tmp_module := f"{module.name}-default"
193197
) in network_acls:
194-
LOG.warning(msg=f"The network ACL {tmp_module.name} was not destroyed properly.")
198+
LOG.warning(
199+
msg=f"The network ACL {tmp_module.name} was not destroyed properly."
200+
)
195201
if (
196202
force
197203
or (input("Do you want to destroy it? [Y/n] ").lower() or "y") == "y"

ctf/generate.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ def generate(
6969
)
7070
distinct_tracks = tmp_tracks
7171

72-
add_tracks_to_terraform_modules(
73-
tracks=distinct_tracks,
74-
remote=remote,
75-
production=production,
76-
)
72+
add_tracks_to_terraform_modules(tracks=distinct_tracks)
7773

7874
for track in distinct_tracks:
7975
relpath = os.path.relpath(

ctf/utils.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ def validate_track_can_be_deployed(track: Track) -> bool:
9393
)
9494

9595

96-
def add_tracks_to_terraform_modules(
97-
tracks: set[Track], remote: str, production: bool = False
98-
):
96+
def add_tracks_to_terraform_modules(tracks: set[Track]):
9997
with open(
10098
file=os.path.join(find_ctf_root_directory(), ".deploy", "modules.tf"), mode="a"
10199
) as fd:
@@ -105,16 +103,9 @@ def add_tracks_to_terraform_modules(
105103
{% for track in tracks %}
106104
module "track-{{ track.name }}" {
107105
source = "../challenges/{{ track.name }}/terraform"
108-
109106
build_container = {{ 'true' if track.require_build_container else 'false' }}
110-
111-
{% if track.production %}
112-
deploy = "production"
113-
{% endif %}
114-
{% if track.remote %}
115-
incus_remote = "{{ track.remote }}"
116-
{% endif %}
117-
107+
{% if track.production %}deploy = "production"{% endif %}
108+
{% if track.remote %}incus_remote = "{{ track.remote }}"{% endif %}
118109
depends_on = [module.common]
119110
}
120111
{% endfor %}
@@ -124,9 +115,6 @@ def add_tracks_to_terraform_modules(
124115
fd.write(
125116
template.render(
126117
tracks=tracks - get_terraform_tracks_from_modules(),
127-
build_container=True, # build_container,
128-
production=production,
129-
remote=remote,
130118
)
131119
)
132120

@@ -140,12 +128,8 @@ def create_terraform_modules_file(remote: str, production: bool = False):
140128
text="""\
141129
module "common" {
142130
source = "./common"
143-
{% if production %}
144-
deploy = "production"
145-
{% endif %}
146-
{% if remote %}
147-
incus_remote = "{{ remote }}"
148-
{% endif %}
131+
{% if production %}deploy = "production"{% endif %}
132+
{% if remote %}incus_remote = "{{ remote }}"{% endif %}
149133
}
150134
"""
151135
)
@@ -212,9 +196,7 @@ def remove_tracks_from_terraform_modules(
212196
current_tracks = get_terraform_tracks_from_modules()
213197

214198
create_terraform_modules_file(remote=remote, production=production)
215-
add_tracks_to_terraform_modules(
216-
tracks=(current_tracks - tracks), remote=remote, production=production
217-
)
199+
add_tracks_to_terraform_modules(tracks=(current_tracks - tracks))
218200

219201

220202
def get_all_file_paths_recursively(path: str) -> Generator[str, None, None]:

0 commit comments

Comments
 (0)