Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import requests
import time
from metrics import MetricsHandler
from typing import List


from prometheus_client import generate_latest
Expand Down Expand Up @@ -49,6 +50,9 @@ class RepoToWatch:
name: str
branch: str
path: str
# list of all the containers
# makes sure theres a new list made for each repotowatch object
containers: List[str] = dataclasses.field(default_factory=list)


@dataclasses.dataclass
Expand All @@ -61,17 +65,22 @@ class RepoUpdateResult:
docker_stdout: str = ""
docker_stderr: str = ""


# dis one loads the config.yml file
# turns it into a dictionary
# result is the dictionary
def load_config(development: bool):
result = {}
if development:
return result
with open("config.yml") as f:
loaded_yaml = yaml.safe_load(f)
# for each repo in the config
for config in loaded_yaml.get("repos", []):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we move the load_containers somehow to this section, and add the list of containers as a field to RepoToWatch?

like if the force_recreate field is not empty, later we run the command, using the names stored in the dataclass

# make a new entry into the result dictionary
# the key is a tuple of the repo name and branch
# the value is a RepoToWatch object
parsed = RepoToWatch(**config)
result[(parsed.name, parsed.branch)] = parsed

return result


Expand All @@ -84,7 +93,22 @@ def get_args():
args = get_args()

config = load_config(args.development)

# now we must get the list of containers that need to be recreated
# we look into the config.yml file

# twan idk man lets just make a method to get the docker containers
# from the config.yml file
# lets just make it similar to the load_config
# def load_containers(development: bool):
# if development:
# return []
# with open("config.yml") as f:
# loaded_yaml = yaml.safe_load(f)
# # if the dictionary config does not contain a force_recreate key
# # then we just get an empty list
# containers_to_force_recreate = loaded_yaml.get("force_recreate", [])
# return containers_to_force_recreate
# force_recreate = load_containers(args.development)

def push_update_success_as_discord_embed(
repo_config: RepoToWatch, result: RepoUpdateResult
Expand Down Expand Up @@ -158,8 +182,24 @@ def update_repo(repo_config: RepoToWatch) -> RepoUpdateResult:
result.git_stderr = git_result.stderr
result.git_exit_code = git_result.returncode

# if the list containing the containers we want to force recreate
# is empty, then we dont need to do anything to the docker command
# but if the list contains stuff
# then we have to change the docker command

docker_command = ["docker-compose", "up", "--build", "-d"]
force_recreate = repo_config.containers
if force_recreate:
docker_command.extend(["--force-recreate", "--no-deps"])
# go through the list force_recreate
# add each container to the docker command list
for container in force_recreate:
docker_command.append(container)

# subprocess.run executes commands like in the terminal iirc
# this one runs the docker
docker_result = subprocess.run(
["docker-compose", "up", "--build", "-d"],
docker_command,
cwd=repo_config.path,
capture_output=True,
text=True,
Expand Down