|
1 | 1 | import os |
2 | 2 | import subprocess |
| 3 | +import sys |
3 | 4 | from pathlib import Path |
4 | 5 | from urllib.parse import urlparse |
5 | 6 |
|
|
11 | 12 | create_challenge, |
12 | 13 | lint_challenge, |
13 | 14 | load_challenge, |
| 15 | + load_installed_challenge, |
14 | 16 | load_installed_challenges, |
15 | 17 | sync_challenge, |
16 | 18 | ) |
@@ -353,3 +355,56 @@ def push(self, challenge=None): |
353 | 355 | click.echo( |
354 | 356 | "Couldn't process that challenge path. Please check that the challenge is added to .ctf/config and that your path matches." |
355 | 357 | ) |
| 358 | + |
| 359 | + def healthcheck(self, challenge): |
| 360 | + config = load_config() |
| 361 | + challenges = config["challenges"] |
| 362 | + |
| 363 | + # challenge_path = challenges[challenge] |
| 364 | + path = Path(challenge) |
| 365 | + if path.name.endswith(".yml") is False: |
| 366 | + path = path / "challenge.yml" |
| 367 | + |
| 368 | + challenge = load_challenge(path) |
| 369 | + click.secho(f'Loaded {challenge["name"]}', fg="yellow") |
| 370 | + try: |
| 371 | + healthcheck = challenge["healthcheck"] |
| 372 | + except KeyError: |
| 373 | + click.secho(f'{challenge["name"]} missing healthcheck parameter', fg="red") |
| 374 | + return |
| 375 | + |
| 376 | + # Get challenges installed from CTFd and try to find our challenge |
| 377 | + installed_challenges = load_installed_challenges() |
| 378 | + target = None |
| 379 | + for c in installed_challenges: |
| 380 | + if c["name"] == challenge["name"]: |
| 381 | + target = c |
| 382 | + break |
| 383 | + else: |
| 384 | + click.secho( |
| 385 | + f'Couldn\'t find challenge {c["name"]} on CTFd', fg="red", |
| 386 | + ) |
| 387 | + return |
| 388 | + |
| 389 | + # Get the actual challenge data |
| 390 | + installed_challenge = load_installed_challenge(target["id"]) |
| 391 | + connection_info = installed_challenge["connection_info"] |
| 392 | + |
| 393 | + # Run healthcheck |
| 394 | + if connection_info: |
| 395 | + rcode = subprocess.call( |
| 396 | + [healthcheck, "--connection-info", connection_info], cwd=path.parent |
| 397 | + ) |
| 398 | + else: |
| 399 | + rcode = subprocess.call([healthcheck], cwd=path.parent) |
| 400 | + |
| 401 | + if rcode != 0: |
| 402 | + click.secho( |
| 403 | + f"Healcheck failed", fg="red", |
| 404 | + ) |
| 405 | + sys.exit(1) |
| 406 | + else: |
| 407 | + click.secho( |
| 408 | + f"Success", fg="green", |
| 409 | + ) |
| 410 | + sys.exit(0) |
0 commit comments