Skip to content

Commit f368d0b

Browse files
authored
Merge pull request #34 from nsec/build-container-feature
Build container feature
2 parents 803e1ea + 59aab2c commit f368d0b

File tree

27 files changed

+575
-245
lines changed

27 files changed

+575
-245
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ jobs:
160160
- name: Test deployment looping through tracks
161161
working-directory: test-ctf
162162
run: |
163-
IFS=" " read -r -a tracks <<< "$(python3 -c 'from ctf.utils import get_all_available_tracks,validate_track_can_be_deployed;print(str([t for t in get_all_available_tracks() if validate_track_can_be_deployed(t)]).strip("[]\x27").replace("\x27, \x27"," "))')"
163+
IFS=" " read -r -a tracks <<< "$(python3 -c 'from ctf.utils import get_all_available_tracks,validate_track_can_be_deployed;print(str([t.name for t in get_all_available_tracks() if validate_track_can_be_deployed(t)]).strip("[]\x27").replace("\x27, \x27"," "))')"
164164
165165
[ "${#tracks[@]}" -eq 0 ] && exit 1
166166

challenges/mock-track-apache-php/ansible/deploy.yaml

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@
1111
ansible.builtin.set_fact:
1212
track_flags: "{{ track_flags | default({}) | combine({key: value}) }}"
1313

14-
- name: Initial System Upgrade
15-
ansible.builtin.apt:
16-
update_cache: true
17-
install_recommends: false
18-
upgrade: full
14+
# Removed APT commands to avoid workflow failure
15+
# - name: Initial System Upgrade
16+
# ansible.builtin.apt:
17+
# update_cache: true
18+
# install_recommends: false
19+
# upgrade: full
1920

20-
- name: Install PHP and Apache2
21-
ansible.builtin.apt:
22-
name:
23-
- php
24-
- apache2
25-
- libapache2-mod-php
26-
state: present
21+
# - name: Install PHP and Apache2
22+
# ansible.builtin.apt:
23+
# name:
24+
# - php
25+
# - apache2
26+
# - libapache2-mod-php
27+
# state: present
2728

28-
- name: Remove default file "/var/www/html/index.html"
29-
ansible.builtin.file:
30-
path: "/var/www/html/index.html"
31-
state: absent
29+
# - name: Remove default file "/var/www/html/index.html"
30+
# ansible.builtin.file:
31+
# path: "/var/www/html/index.html"
32+
# state: absent
3233

3334
- name: Copy the main site file (index.php)
3435
ansible.builtin.template:
@@ -38,13 +39,13 @@
3839
group: root
3940
mode: '0644'
4041

41-
- name: Restart Apache2 on failure
42-
ansible.builtin.replace:
43-
path: "/lib/systemd/system/apache2.service"
44-
regexp: 'Restart=.+$'
45-
replace: 'Restart=on-failure'
42+
# - name: Restart Apache2 on failure
43+
# ansible.builtin.replace:
44+
# path: "/lib/systemd/system/apache2.service"
45+
# regexp: 'Restart=.+$'
46+
# replace: 'Restart=on-failure'
4647

47-
- name: Restart Apache2
48-
ansible.builtin.service:
49-
name: apache2
50-
state: restarted
48+
# - name: Restart Apache2
49+
# ansible.builtin.service:
50+
# name: apache2
51+
# state: restarted

challenges/mock-track-apache-php/terraform/variables.tf

Lines changed: 0 additions & 1 deletion
This file was deleted.

challenges/mock-track-apache-php/terraform/versions.tf

Lines changed: 0 additions & 1 deletion
This file was deleted.

challenges/mock-track-python-service/ansible/deploy.yaml

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@
1111
ansible.builtin.set_fact:
1212
track_flags: "{{ track_flags | default({}) | combine({key: value}) }}"
1313

14-
- name: Initial System Upgrade
15-
ansible.builtin.apt:
16-
update_cache: true
17-
install_recommends: false
18-
upgrade: full
14+
# Removed APT commands to avoid workflow failure
15+
# - name: Initial System Upgrade
16+
# ansible.builtin.apt:
17+
# update_cache: true
18+
# install_recommends: false
19+
# upgrade: full
1920

20-
- name: Install Python3 and dependencies
21-
ansible.builtin.apt:
22-
name:
23-
- python3
24-
- python3-pip
25-
- virtualenv
26-
state: present
21+
# - name: Install Python3 and dependencies
22+
# ansible.builtin.apt:
23+
# name:
24+
# - python3
25+
# - python3-pip
26+
# - virtualenv
27+
# state: present
2728

2829
- name: Create service user
2930
ansible.builtin.user:
@@ -48,13 +49,13 @@
4849
group: service
4950
mode: '0600'
5051

51-
- name: Python PIP install virtual environment
52-
ansible.builtin.pip:
53-
chdir: /home/service/
54-
virtualenv: /home/service/
55-
state: present
56-
name:
57-
- flask
52+
# - name: Python PIP install virtual environment
53+
# ansible.builtin.pip:
54+
# chdir: /home/service/
55+
# virtualenv: /home/service/
56+
# state: present
57+
# name:
58+
# - flask
5859

5960
- name: Create flag file
6061
ansible.builtin.copy:
@@ -87,9 +88,9 @@
8788
[Install]
8889
WantedBy=default.target
8990
90-
- name: Start my_track service
91-
ansible.builtin.service:
92-
name: my_track.service
93-
state: restarted
94-
enabled: true
95-
daemon_reload: true
91+
# - name: Start my_track service
92+
# ansible.builtin.service:
93+
# name: my_track.service
94+
# state: restarted
95+
# enabled: true
96+
# daemon_reload: true

challenges/mock-track-python-service/terraform/variables.tf

Lines changed: 0 additions & 1 deletion
This file was deleted.

challenges/mock-track-python-service/terraform/versions.tf

Lines changed: 0 additions & 1 deletion
This file was deleted.

ctf/deploy.py

Lines changed: 100 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
from ctf.destroy import destroy
1212
from ctf.generate import generate
1313
from ctf.logger import LOG
14+
from ctf.models import Track
1415
from ctf.utils import (
1516
add_tracks_to_terraform_modules,
1617
check_git_lfs,
1718
find_ctf_root_directory,
18-
get_all_available_tracks,
19-
get_terraform_tracks_from_modules,
2019
parse_track_yaml,
20+
remove_tracks_from_terraform_modules,
2121
terraform_binary,
22-
validate_track_can_be_deployed,
2322
)
2423

2524
app = typer.Typer()
@@ -54,21 +53,10 @@ def deploy(
5453
] = False,
5554
):
5655
ENV["INCUS_REMOTE"] = remote
57-
if redeploy:
58-
distinct_tracks = set(
59-
track
60-
for track in get_all_available_tracks()
61-
if validate_track_can_be_deployed(track=track) and track in tracks
62-
)
63-
64-
add_tracks_to_terraform_modules(
65-
tracks=distinct_tracks - get_terraform_tracks_from_modules(),
66-
remote=remote,
67-
production=production,
68-
)
69-
else:
70-
# Run generate first.
71-
distinct_tracks = generate(tracks=tracks, production=production, remote=remote)
56+
# Run generate first.
57+
distinct_tracks = generate(
58+
tracks=tracks, production=production, remote=remote, redeploy=redeploy
59+
)
7260

7361
# Check if Git LFS is installed on the system as it is required for deployment.
7462
if not check_git_lfs():
@@ -84,7 +72,7 @@ def deploy(
8472
"git",
8573
"lfs",
8674
"pull",
87-
f"--include={','.join([os.path.join('challenges', track, 'ansible', '*') for track in distinct_tracks])}",
75+
f"--include={','.join([os.path.join('challenges', track.name, 'ansible', '*') for track in distinct_tracks])}",
8876
],
8977
check=True,
9078
)
@@ -103,8 +91,9 @@ def deploy(
10391
if (input("Do you want to clean and start over? [Y/n] ").lower() or "y") != "y":
10492
exit(code=1)
10593

106-
force = True
107-
destroy(tracks=tracks, production=production, remote=remote, force=force)
94+
destroy(tracks=tracks, production=production, remote=remote, force=True)
95+
96+
distinct_tracks = generate(tracks=tracks, production=production, remote=remote)
10897

10998
subprocess.run(
11099
args=[terraform_binary(), "apply", "-auto-approve"],
@@ -115,22 +104,81 @@ def deploy(
115104
LOG.warning(
116105
"CTRL+C was detected during Terraform deployment. Destroying everything..."
117106
)
118-
force = True
119-
destroy(tracks=tracks, production=production, remote=remote, force=force)
107+
destroy(tracks=tracks, production=production, remote=remote, force=True)
120108
exit(code=0)
121109

122110
for track in distinct_tracks:
111+
if track.require_build_container:
112+
run_ansible_playbook(
113+
remote=remote,
114+
production=production,
115+
track=track.name,
116+
path=os.path.join(
117+
find_ctf_root_directory(), "challenges", track.name, "ansible"
118+
),
119+
playbook="build.yaml",
120+
execute_common=False,
121+
)
122+
123+
remove_tracks_from_terraform_modules(
124+
{track}, remote=remote, production=production
125+
)
126+
add_tracks_to_terraform_modules(
127+
{
128+
Track(
129+
name=track.name,
130+
remote=track.remote,
131+
production=track.production,
132+
require_build_container=False,
133+
)
134+
}
135+
)
136+
137+
try:
138+
subprocess.run(
139+
args=[terraform_binary(), "apply", "-auto-approve"],
140+
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
141+
check=True,
142+
)
143+
except subprocess.CalledProcessError:
144+
LOG.warning(
145+
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."
146+
)
147+
148+
if (
149+
input("Do you want to clean and start over? [Y/n] ").lower() or "y"
150+
) != "y":
151+
exit(code=1)
152+
153+
destroy(tracks=tracks, production=production, remote=remote, force=True)
154+
155+
distinct_tracks = generate(
156+
tracks=tracks, production=production, remote=remote
157+
)
158+
159+
subprocess.run(
160+
args=[terraform_binary(), "apply", "-auto-approve"],
161+
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
162+
check=True,
163+
)
164+
except KeyboardInterrupt:
165+
LOG.warning(
166+
"CTRL+C was detected during Terraform deployment. Destroying everything..."
167+
)
168+
destroy(tracks=tracks, production=production, remote=remote, force=True)
169+
exit(code=0)
170+
123171
if not os.path.exists(
124172
path=(
125173
path := os.path.join(
126-
find_ctf_root_directory(), "challenges", track, "ansible"
174+
find_ctf_root_directory(), "challenges", track.name, "ansible"
127175
)
128176
)
129177
):
130178
continue
131179

132180
run_ansible_playbook(
133-
remote=remote, production=production, track=track, path=path
181+
remote=remote, production=production, track=track.name, path=path
134182
)
135183

136184
if not production:
@@ -154,7 +202,7 @@ def deploy(
154202

155203
if remote == "local":
156204
LOG.debug(msg=f"Parsing track.yaml for track {track}")
157-
track_yaml = parse_track_yaml(track_name=track)
205+
track_yaml = parse_track_yaml(track_name=track.name)
158206

159207
for service in track_yaml["services"]:
160208
if service.get("dev_port_mapping"):
@@ -175,12 +223,12 @@ def deploy(
175223
"device",
176224
"add",
177225
machine_name,
178-
f"proxy-{track}-{service['dev_port_mapping']}-to-{service['port']}",
226+
f"proxy-{track.name}-{service['dev_port_mapping']}-to-{service['port']}",
179227
"proxy",
180228
f"listen=tcp:0.0.0.0:{service['dev_port_mapping']}",
181229
f"connect=tcp:127.0.0.1:{service['port']}",
182230
"--project",
183-
track,
231+
track.name,
184232
],
185233
cwd=path,
186234
check=True,
@@ -212,7 +260,7 @@ def deploy(
212260
msg=f"Running `incus project switch {tracks_list[track_index - 1]}`"
213261
)
214262
subprocess.run(
215-
args=["incus", "project", "switch", tracks_list[track_index - 1]],
263+
args=["incus", "project", "switch", tracks_list[track_index - 1].name],
216264
check=True,
217265
env=ENV,
218266
)
@@ -222,7 +270,14 @@ def deploy(
222270
)
223271

224272

225-
def run_ansible_playbook(remote: str, production: bool, track: str, path: str) -> None:
273+
def run_ansible_playbook(
274+
remote: str,
275+
production: bool,
276+
track: str,
277+
path: str,
278+
playbook: str = "deploy.yaml",
279+
execute_common: bool = True,
280+
) -> None:
226281
extra_args = []
227282
if STATE["verbose"]:
228283
extra_args.append("-vvv")
@@ -232,23 +287,24 @@ def run_ansible_playbook(remote: str, production: bool, track: str, path: str) -
232287
if production:
233288
extra_args += ["-e", "nsec_production=true"]
234289

235-
LOG.info(msg=f"Running common yaml with ansible for track {track}...")
236-
ansible_args = [
237-
"ansible-playbook",
238-
os.path.join(find_ctf_root_directory(), ".deploy", "common.yaml"),
239-
"-i",
240-
"inventory",
241-
] + extra_args
242-
subprocess.run(
243-
args=ansible_args,
244-
cwd=path,
245-
check=True,
246-
)
290+
if execute_common:
291+
LOG.info(msg=f"Running common yaml with ansible for track {track}...")
292+
ansible_args = [
293+
"ansible-playbook",
294+
os.path.join("..", "..", "..", ".deploy", "common.yaml"),
295+
"-i",
296+
"inventory",
297+
] + extra_args
298+
subprocess.run(
299+
args=ansible_args,
300+
cwd=path,
301+
check=True,
302+
)
247303

248-
LOG.info(msg=f"Running deploy.yaml with ansible for track {track}...")
304+
LOG.info(msg=f"Running {playbook} with ansible for track {track}...")
249305
ansible_args = [
250306
"ansible-playbook",
251-
"deploy.yaml",
307+
playbook,
252308
"-i",
253309
"inventory",
254310
] + extra_args

0 commit comments

Comments
 (0)