From 0aa7240ccf99e56715ed91e572162f0f49a6be1e Mon Sep 17 00:00:00 2001 From: Somraj Saha Date: Tue, 24 Sep 2024 16:01:21 +0530 Subject: [PATCH 1/2] chore: add a script to install necessary software and dependencies --- scripts/install-deps.py | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 scripts/install-deps.py diff --git a/scripts/install-deps.py b/scripts/install-deps.py new file mode 100755 index 0000000..d87df6f --- /dev/null +++ b/scripts/install-deps.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 + +"""Script to install the tools and dependencies on the server hosting the runners.""" + +import logging +import os +import subprocess +import sys + +# The ANSI colour codes for the log messages +RED = "\033[31m" +BLUE = "\033[34m" +RESET = "\033[0m" + +formatter = logging.Formatter("%(color)s[%(levelname)s]%(reset)s %(message)s") + + +def add_color_to_record(record: logging.LogRecord) -> logging.LogRecord: + """A utility function to add some colours to the log messages. + + Args: + record: A LogRecord instance + + Returns: + A customised LogRecord instance + + Raises: + None + """ + match record.levelno: + case logging.ERROR: + record.color = RED + case record.levelno: + record.color = BLUE + case _: + record.color = "" + + record.reset = RESET + + return record + + +logger = logging.getLogger(__name__) +logger.setLevel(logging.INFO) + +console_handler = logging.StreamHandler() +console_handler.setFormatter(formatter) + +logger.addHandler(console_handler) +logger.addFilter(add_color_to_record) + + +def install_unzip() -> None: + """Install the "unzip" tool on the server.""" + logger.info('Installing "unzip"') + subprocess.run(["sudo", "apt-get", "install", "unzip"]) # noqa: S607, S603 + + +def main() -> None: + """The entrypoint of the script. + + Args: + None + + Returns: + None + + Raises: + None + """ + install_unzip() + + +if __name__ == "__main__": + # Check if script invoked by the "root" user, if so throw error message and exit + if not os.geteuid() == 0: + main() + else: + logger.error("Must run script as non-root user!") + sys.exit(1) From 866ec2f66ef00216992a1b25180f621de2b0bcb8 Mon Sep 17 00:00:00 2001 From: Somraj Saha Date: Tue, 24 Sep 2024 16:20:42 +0530 Subject: [PATCH 2/2] chore: add an incomplete function to install Docker --- scripts/install-deps.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/scripts/install-deps.py b/scripts/install-deps.py index d87df6f..9bdecab 100755 --- a/scripts/install-deps.py +++ b/scripts/install-deps.py @@ -56,6 +56,35 @@ def install_unzip() -> None: subprocess.run(["sudo", "apt-get", "install", "unzip"]) # noqa: S607, S603 +def install_docker() -> None: + """Install Docker on the server. + + Args: + None + + Returns: + None + + Raises: + None + """ + keyrings_dir = "/etc/apt/keyrings" + gpg_key_url = "https://download.docker.com/linux/debian/gpg" + + logger.info('Install "Docker"') + + # Add Docker's official GPG key + subprocess.run( # noqa: S603 + ["install", "--mode", "0755", "--directory", keyrings_dir] # noqa: S607 + ) + subprocess.run( # noqa: S603 + ["curl", "-fsSL", gpg_key_url, f"{keyrings_dir}/docker.asc"] # noqa: S607 + ) + subprocess.run(["chmod", "a+r", f"{keyrings_dir}/docker.asc"]) # noqa: S607, S603 + + # Add the repository to the APT sources + + def main() -> None: """The entrypoint of the script. @@ -70,6 +99,8 @@ def main() -> None: """ install_unzip() + install_docker() + if __name__ == "__main__": # Check if script invoked by the "root" user, if so throw error message and exit