Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
Fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-Toosi committed May 8, 2023
1 parent 8d8115b commit 566ed5f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ The game worker is responsible for:
1. Fetching a match from the teams portal
2. Downloading the clients of that match and the latest game server
3. Running the match by connecting the clients and game server through GCS
4. Sending the results back to the teams portal
4. Sending the results back to the teams portal
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests==2.28.2
docker==6.0.1
boto3==1.26.105
pre-commit==3.3.1
21 changes: 19 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
[flake8]
max-line-length = 120
exclude = .tox,.git,*/static/CACHE/*,docs,node_modules,venv
exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules,venv

[pycodestyle]
max-line-length = 120
exclude = .tox,.git,*/static/CACHE/*,docs,node_modules,venv
exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules,venv

[isort]
line_length = 88
multi_line_output = 3
default_section = THIRDPARTY
skip = venv/
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true

[mypy]
python_version = 3.10
check_untyped_defs = True
ignore_missing_imports = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
21 changes: 14 additions & 7 deletions src/docker_tools.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import base64
import os
import shutil
import pathlib
import shutil

import boto3
import docker
from docker.errors import ImageNotFound, NotFound, APIError
from docker.errors import APIError, ImageNotFound, NotFound

AWS_REGION = "ap-southeast-2"
ECR_REGISTRY = os.environ.get("ECR_REGISTRY")
Expand All @@ -28,14 +28,16 @@
)
registry = token["authorizationData"][0]["proxyEndpoint"]
if str(registry).startswith("https://"):
registry = registry[len("https://"):]
registry = registry[8:]

DOCKER_CLIENT = docker.from_env()
DOCKER_CLIENT.login(username, password, registry=registry)


def get_submission_image_tag(submission_id):
return f"{ECR_REGISTRY}/{SUBMISSIONS_ECR_REPO}:{IMAGE_TAG_PREFIX}{str(submission_id)}"
return (
f"{ECR_REGISTRY}/{SUBMISSIONS_ECR_REPO}:{IMAGE_TAG_PREFIX}{str(submission_id)}"
)


def get_server_image_tag():
Expand Down Expand Up @@ -63,11 +65,16 @@ def pull_submissions(submissions: list):
DOCKER_CLIENT.images.get(submission_image)
print(f"Image for submission {str(submission['id'])} already exists.")
except ImageNotFound:
print(f"Image for submission {str(submission['id'])} doesn't exist, pulling... ", end="")
print(
f"Image for submission {str(submission['id'])} doesn't exist, pulling... ",
end="",
)
DOCKER_CLIENT.images.pull(submission_image)
print("done!")
except NotFound:
print(f"Image for submission {str(submission['id'])} not found! Match failed.")
print(
f"Image for submission {str(submission['id'])} not found! Match failed."
)
return False

return True
Expand Down Expand Up @@ -103,7 +110,7 @@ def copy_replay_files():
remove=True,
volumes={
volume_name: {"bind": "/data", "mode": "ro"},
f"{pwd}/{local_dir}": {"bind": f"/{local_dir}", "mode": "rw"}
f"{pwd}/{local_dir}": {"bind": f"/{local_dir}", "mode": "rw"},
},
)

Expand Down
39 changes: 27 additions & 12 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import json
import os
import subprocess
import sys
import time
import os
import shutil

import api
import docker_tools


GCS_DIR = "game-communication-system/src"
CLIENTS_FILE_ADDRESS = "_cq-gcs-clients.json"
MATCH_TIMEOUT_SECONDS = 12 * 60
Expand All @@ -17,11 +15,13 @@
def run_gcs(match):
clients_file_content = []
for submission in match["submissions"]:
clients_file_content.append({
"id": submission["team_id"],
"name": submission["team_name"],
"image": docker_tools.get_submission_image_tag(submission["id"])
})
clients_file_content.append(
{
"id": submission["team_id"],
"name": submission["team_name"],
"image": docker_tools.get_submission_image_tag(submission["id"]),
}
)

with open(GCS_DIR + "/" + CLIENTS_FILE_ADDRESS, "w") as f:
f.write(json.dumps(clients_file_content))
Expand All @@ -30,7 +30,7 @@ def run_gcs(match):
sys.executable,
"controller.py",
docker_tools.get_server_image_tag(),
CLIENTS_FILE_ADDRESS
CLIENTS_FILE_ADDRESS,
]
subprocess.run(subprocess_args, timeout=MATCH_TIMEOUT_SECONDS, cwd=GCS_DIR)
os.remove(GCS_DIR + "/" + CLIENTS_FILE_ADDRESS)
Expand All @@ -39,8 +39,21 @@ def run_gcs(match):
if __name__ == "__main__":
print("Starting the main loop...")
while True:
# match = api.get_pending_match()
match = {'id': 8, 'submissions': [{'id': 2, 'team_id': 1, 'team_name': 'Test Team 1'}, {'id': 3, 'team_id': 3, 'team_name': 'Test TEAM 2'}], 'context': {'map': 'map2.txt', 'game': 'game2'}, 'last_requested_at': '2023-05-08T21:36:49.460208+10:00', 'played_status': 'playing', 'play_attempts': 2, 'results': None, 'match_group': 1}
match = api.get_pending_match()
# For testing:
# match = {
# "id": 8,
# "submissions": [
# {"id": 2, "team_id": 1, "team_name": "Test Team 1"},
# {"id": 3, "team_id": 3, "team_name": "Test TEAM 2"},
# ],
# "context": {"map": "map2.txt", "game": "game2"},
# "last_requested_at": "2023-05-08T21:36:49.460208+10:00",
# "played_status": "playing",
# "play_attempts": 2,
# "results": None,
# "match_group": 1,
# }
print("Received match:")
print(match)

Expand All @@ -51,7 +64,9 @@ def run_gcs(match):
fetch_result &= docker_tools.pull_submissions(match["submissions"])

if not fetch_result:
print("Something went wrong with pulling the images! Going to the next match...")
print(
"Something went wrong with pulling the images! Going to the next match..."
)
continue

print("Running the GCS...")
Expand Down

0 comments on commit 566ed5f

Please sign in to comment.