Skip to content

Commit 6f25af3

Browse files
authored
Merge pull request #29 from nsec/added-path
Reworked the CTF root directory and added the location global argument.
2 parents 6250a47 + c7d39db commit 6f25af3

File tree

15 files changed

+123
-87
lines changed

15 files changed

+123
-87
lines changed

ctf/__init__.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
import urllib.request
77

88
from ctf.logger import LOG
9-
from ctf.utils import (
10-
find_ctf_root_directory,
11-
get_ctf_script_schemas_directory,
12-
get_ctf_script_templates_directory,
13-
)
149

1510
VERSION = importlib.metadata.version("ctf-script")
1611

@@ -23,17 +18,6 @@
2318
for k, v in os.environ.items():
2419
ENV[k] = v
2520

26-
match sys.argv[1] if len(sys.argv) > 1 else "":
27-
case "init":
28-
CTF_ROOT_DIRECTORY = os.path.join(os.getcwd(), ".")
29-
case "version":
30-
CTF_ROOT_DIRECTORY = ""
31-
case _:
32-
CTF_ROOT_DIRECTORY = find_ctf_root_directory()
33-
34-
TEMPLATES_ROOT_DIRECTORY = get_ctf_script_templates_directory()
35-
SCHEMAS_ROOT_DIRECTORY = get_ctf_script_schemas_directory()
36-
3721

3822
def check_tool_version() -> None:
3923
with urllib.request.urlopen(

ctf/__main__.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
from typer import Typer
77
from typing_extensions import Annotated
88

9-
from ctf import (
10-
CTF_ROOT_DIRECTORY,
11-
LOG,
12-
)
9+
from ctf import ENV, LOG
1310
from ctf.check import app as check_app
1411
from ctf.deploy import app as deploy_app
1512
from ctf.destroy import app as destroy_app
@@ -21,6 +18,7 @@
2118
from ctf.redeploy import app as redeploy_app
2219
from ctf.services import app as services_app
2320
from ctf.stats import app as stats_app
21+
from ctf.utils import find_ctf_root_directory
2422
from ctf.validate import app as validate_app
2523
from ctf.version import app as version_app
2624

@@ -44,6 +42,9 @@
4442

4543
@app.callback()
4644
def global_options(
45+
location: Annotated[
46+
str, typer.Option("--location", help="CTF root directory location.")
47+
] = "",
4748
verbose: Annotated[
4849
bool, typer.Option("--verbose", "-v", help="Enable DEBUG logging.")
4950
] = False,
@@ -52,6 +53,9 @@ def global_options(
5253
LOG.setLevel(logging.DEBUG)
5354
LOG.handlers[0].setLevel(logging.DEBUG)
5455

56+
if location:
57+
ENV["CTF_ROOT_DIR"] = location
58+
5559

5660
def main():
5761
app()
@@ -61,7 +65,9 @@ def main():
6165
import sys
6266

6367
if "version" not in sys.argv and "init" not in sys.argv:
64-
if not os.path.isdir(s=(p := os.path.join(CTF_ROOT_DIRECTORY, "challenges"))):
68+
if not os.path.isdir(
69+
s=(p := os.path.join(find_ctf_root_directory(), "challenges"))
70+
):
6571
LOG.error(
6672
msg=f"Directory `{p}` not found. Make sure this script is ran from the root directory OR set the CTF_ROOT_DIR environment variable to the root directory."
6773
)

ctf/check.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import typer
55
from typing_extensions import Annotated
66

7-
from ctf import CTF_ROOT_DIRECTORY, ENV
7+
from ctf import ENV
88
from ctf.generate import generate
99
from ctf.logger import LOG
10-
from ctf.utils import check_git_lfs, terraform_binary
10+
from ctf.utils import check_git_lfs, find_ctf_root_directory, terraform_binary
1111

1212
app = typer.Typer()
1313

@@ -40,7 +40,7 @@ def check(
4040
# Then run terraform plan.
4141
subprocess.run(
4242
args=[terraform_binary(), "plan"],
43-
cwd=os.path.join(CTF_ROOT_DIRECTORY, ".deploy"),
43+
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
4444
check=True,
4545
)
4646

ctf/deploy.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
import typer
88
from typing_extensions import Annotated
99

10-
from ctf import CTF_ROOT_DIRECTORY, ENV
10+
from ctf import ENV
1111
from ctf.destroy import destroy
1212
from ctf.generate import generate
1313
from ctf.logger import LOG
1414
from ctf.utils import (
1515
add_tracks_to_terraform_modules,
1616
check_git_lfs,
17+
find_ctf_root_directory,
1718
get_all_available_tracks,
1819
get_terraform_tracks_from_modules,
1920
parse_track_yaml,
@@ -91,7 +92,7 @@ def deploy(
9192
try:
9293
subprocess.run(
9394
args=[terraform_binary(), "apply", "-auto-approve"],
94-
cwd=os.path.join(CTF_ROOT_DIRECTORY, ".deploy"),
95+
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
9596
check=True,
9697
)
9798
except subprocess.CalledProcessError:
@@ -107,7 +108,7 @@ def deploy(
107108

108109
subprocess.run(
109110
args=[terraform_binary(), "apply", "-auto-approve"],
110-
cwd=os.path.join(CTF_ROOT_DIRECTORY, ".deploy"),
111+
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
111112
check=True,
112113
)
113114
except KeyboardInterrupt:
@@ -121,7 +122,9 @@ def deploy(
121122
for track in distinct_tracks:
122123
if not os.path.exists(
123124
path=(
124-
path := os.path.join(CTF_ROOT_DIRECTORY, "challenges", track, "ansible")
125+
path := os.path.join(
126+
find_ctf_root_directory(), "challenges", track, "ansible"
127+
)
125128
)
126129
):
127130
continue

ctf/destroy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import typer
66
from typing_extensions import Annotated
77

8-
from ctf import CTF_ROOT_DIRECTORY, ENV
8+
from ctf import ENV
99
from ctf.logger import LOG
1010
from ctf.utils import (
11+
find_ctf_root_directory,
1112
get_terraform_tracks_from_modules,
1213
remove_tracks_from_terraform_modules,
1314
terraform_binary,
@@ -50,7 +51,7 @@ def destroy(
5051
LOG.info(msg="tofu destroy...")
5152

5253
if not os.path.exists(
53-
path=os.path.join(CTF_ROOT_DIRECTORY, ".deploy", "modules.tf")
54+
path=os.path.join(find_ctf_root_directory(), ".deploy", "modules.tf")
5455
):
5556
LOG.critical(msg="Nothing to destroy.")
5657
exit(code=1)
@@ -118,7 +119,7 @@ def destroy(
118119
else [f"-target=module.track-{track}" for track in terraform_tracks]
119120
),
120121
],
121-
cwd=os.path.join(CTF_ROOT_DIRECTORY, ".deploy"),
122+
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
122123
check=False,
123124
)
124125

ctf/flags.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
import yaml
1010
from typing_extensions import Annotated
1111

12-
from ctf import CTF_ROOT_DIRECTORY
1312
from ctf.logger import LOG
14-
from ctf.utils import parse_track_yaml
13+
from ctf.utils import find_ctf_root_directory, parse_track_yaml
1514

1615
app = typer.Typer()
1716

@@ -41,7 +40,11 @@ def flags(
4140
distinct_tracks: set[str] = set()
4241

4342
for entry in os.listdir(
44-
path=(challenges_directory := os.path.join(CTF_ROOT_DIRECTORY, "challenges"))
43+
path=(
44+
challenges_directory := os.path.join(
45+
find_ctf_root_directory(), "challenges"
46+
)
47+
)
4548
):
4649
if os.path.isdir(
4750
s=(track_directory := os.path.join(challenges_directory, entry))

ctf/generate.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import typer
55
from typing_extensions import Annotated
66

7-
from ctf import CTF_ROOT_DIRECTORY, ENV
7+
from ctf import ENV
88
from ctf.logger import LOG
99
from ctf.utils import (
1010
add_tracks_to_terraform_modules,
1111
create_terraform_modules_file,
12+
find_ctf_root_directory,
1213
get_all_available_tracks,
1314
terraform_binary,
1415
validate_track_can_be_deployed,
@@ -61,10 +62,10 @@ def generate(
6162

6263
for track in distinct_tracks:
6364
relpath = os.path.relpath(
64-
os.path.join(CTF_ROOT_DIRECTORY, ".deploy", "common"),
65+
os.path.join(find_ctf_root_directory(), ".deploy", "common"),
6566
(
6667
terraform_directory := os.path.join(
67-
CTF_ROOT_DIRECTORY, "challenges", track, "terraform"
68+
find_ctf_root_directory(), "challenges", track, "terraform"
6869
)
6970
),
7071
)
@@ -103,13 +104,13 @@ def generate(
103104

104105
subprocess.run(
105106
args=[terraform_binary(), "init", "-upgrade"],
106-
cwd=os.path.join(CTF_ROOT_DIRECTORY, ".deploy"),
107+
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
107108
stdout=subprocess.DEVNULL,
108109
check=True,
109110
)
110111
subprocess.run(
111112
args=[terraform_binary(), "validate"],
112-
cwd=os.path.join(CTF_ROOT_DIRECTORY, ".deploy"),
113+
cwd=os.path.join(find_ctf_root_directory(), ".deploy"),
113114
check=True,
114115
)
115116
else:

ctf/init.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import typer
55
from typing_extensions import Annotated
66

7-
from ctf import CTF_ROOT_DIRECTORY, TEMPLATES_ROOT_DIRECTORY
7+
from ctf import ENV
88
from ctf.logger import LOG
9+
from ctf.utils import get_ctf_script_templates_directory
910

1011
app = typer.Typer()
1112

@@ -16,14 +17,22 @@
1617
def init(
1718
path: Annotated[
1819
str, typer.Argument(help="Directory in which to initialize a CTF")
19-
] = CTF_ROOT_DIRECTORY,
20+
] = "",
2021
force: Annotated[
2122
bool,
2223
typer.Option(
2324
"--force", help="Overwrite the directory if it's already initialized"
2425
),
2526
] = False,
2627
) -> None:
28+
# If path is not set, take the one from --location or CTF_ROOT_DIR, else it's the current directory.
29+
if not path:
30+
path = (
31+
ENV.get("CTF_ROOT_DIR")
32+
if "CTF_ROOT_DIR" in ENV
33+
else os.path.join(os.getcwd(), ".")
34+
)
35+
2736
created_directory = False
2837
created_assets: list[str] = []
2938
try:
@@ -40,7 +49,9 @@ def init(
4049
)
4150
exit(code=1)
4251

43-
for asset in os.listdir(p := os.path.join(TEMPLATES_ROOT_DIRECTORY, "init")):
52+
for asset in os.listdir(
53+
p := os.path.join(get_ctf_script_templates_directory(), "init")
54+
):
4455
dst_asset = os.path.join(path, asset)
4556
if os.path.isdir(src_asset := os.path.join(p, asset)):
4657
shutil.copytree(src_asset, dst_asset, dirs_exist_ok=True)

ctf/list.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from rich.table import Table
77
from typing_extensions import Annotated
88

9-
from ctf import CTF_ROOT_DIRECTORY
10-
from ctf.utils import parse_post_yamls, parse_track_yaml
9+
from ctf.utils import find_ctf_root_directory, parse_post_yamls, parse_track_yaml
1110

1211
app = typer.Typer()
1312

@@ -23,11 +22,13 @@ def list_tracks(
2322
] = ListOutputFormat.PRETTY,
2423
) -> None:
2524
tracks: set[str] = set()
26-
for track in os.listdir(path=os.path.join(CTF_ROOT_DIRECTORY, "challenges")):
25+
for track in os.listdir(path=os.path.join(find_ctf_root_directory(), "challenges")):
2726
if os.path.isdir(
28-
s=os.path.join(CTF_ROOT_DIRECTORY, "challenges", track)
27+
s=os.path.join(find_ctf_root_directory(), "challenges", track)
2928
) and os.path.exists(
30-
path=os.path.join(CTF_ROOT_DIRECTORY, "challenges", track, "track.yaml")
29+
path=os.path.join(
30+
find_ctf_root_directory(), "challenges", track, "track.yaml"
31+
)
3132
):
3233
tracks.add(track)
3334

ctf/new.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import typer
99
from typing_extensions import Annotated
1010

11-
from ctf import CTF_ROOT_DIRECTORY, TEMPLATES_ROOT_DIRECTORY
1211
from ctf.logger import LOG
12+
from ctf.utils import find_ctf_root_directory, get_ctf_script_templates_directory
1313

1414
app = typer.Typer()
1515

@@ -63,7 +63,7 @@ def new(
6363
if os.path.exists(
6464
path=(
6565
new_challenge_directory := os.path.join(
66-
CTF_ROOT_DIRECTORY, "challenges", name
66+
find_ctf_root_directory(), "challenges", name
6767
)
6868
)
6969
):
@@ -82,7 +82,7 @@ def new(
8282

8383
env = jinja2.Environment(
8484
loader=jinja2.FileSystemLoader(
85-
searchpath=TEMPLATES_ROOT_DIRECTORY, encoding="utf-8"
85+
searchpath=get_ctf_script_templates_directory(), encoding="utf-8"
8686
)
8787
)
8888

@@ -191,7 +191,8 @@ def new(
191191
LOG.debug(msg=f"Wrote {p}.")
192192

193193
relpath = os.path.relpath(
194-
os.path.join(CTF_ROOT_DIRECTORY, ".deploy", "common"), terraform_directory
194+
os.path.join(find_ctf_root_directory(), ".deploy", "common"),
195+
terraform_directory,
195196
)
196197

197198
os.symlink(
@@ -278,7 +279,7 @@ def new(
278279
if template == Template.RUST_WEBSERVICE:
279280
# Copy the entire challenge template
280281
shutil.copytree(
281-
os.path.join(TEMPLATES_ROOT_DIRECTORY, "rust-webservice"),
282+
os.path.join(get_ctf_script_templates_directory(), "rust-webservice"),
282283
ansible_challenge_directory,
283284
dirs_exist_ok=True,
284285
)

0 commit comments

Comments
 (0)