diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 411e394..ad7029a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -33,8 +33,8 @@ jobs: - name: Set environment variables run: | echo "TEST=${{ matrix.TEST }}" >> $GITHUB_ENV - echo "OCI_ENV_PATH=${GITHUB_WORKSPACE}/oci_env/" >> $GITHUB_ENV echo "COMPOSE_INTERACTIVE_NO_CLI=1" >> $GITHUB_ENV + #echo "OCI_ENV_PATH=${GITHUB_WORKSPACE}/oci_env/" >> $GITHUB_ENV - name: clone pulpcore, pulp_file, pulp-openapi-generator run: | @@ -62,10 +62,6 @@ jobs: sudo dpkg -i containernetworking-plugins_1.1.1+ds1-1_amd64.deb fi - - name: (Ubuntu) Install docker compose - if: matrix.TEST == 'docker' && matrix.os == 'ubuntu-latest' - uses: docker-practice/actions-setup-docker@1.0.11 - - name: (Mac) Install docker compose if: matrix.TEST == 'docker' && matrix.os == 'macos-12' uses: docker-practice/actions-setup-docker@1.0.11 diff --git a/client/oci_env/logger.py b/client/oci_env/logger.py new file mode 100644 index 0000000..3e68954 --- /dev/null +++ b/client/oci_env/logger.py @@ -0,0 +1,46 @@ +# logger.py - reimplement some functionality of logzero + +import logging +import os + +# Define ANSI escape codes for different colors +ANSI_RESET = "\033[0m" +ANSI_RED = "\033[31m" +ANSI_GREEN = "\033[32m" +ANSI_YELLOW = "\033[33m" +ANSI_CYAN = "\033[36m" + + +# unique color per level +LOG_LEVEL_COLORS = { + 'DEBUG': ANSI_CYAN, + 'INFO': ANSI_GREEN, + 'WARNING': ANSI_YELLOW, + 'ERROR': ANSI_RED, + 'CRITICAL': ANSI_RED, +} + + +# need a custom class to have color per level +class LogColor(logging.LogRecord): + def __init__(self, *args, **kwargs): + super(LogColor, self).__init__(*args, **kwargs) + self.log_color = LOG_LEVEL_COLORS[self.levelname] + self.reset = ANSI_RESET + + +formatter = logging.Formatter('%(asctime)s - %(log_color)s%(levelname)s%(reset)s - %(filename)s:%(lineno)d - %(message)s') +logging.setLogRecordFactory(LogColor) +logger = logging.getLogger('oci_env') +logger.setLevel(logging.INFO) +console_handler = logging.StreamHandler() +console_handler.setFormatter(formatter) + + +if os.environ.get("OCI_ENV_DEBUG"): + logger.setLevel(logging.DEBUG) + console_handler.setLevel(logging.DEBUG) + + +# Add the console handler to the logger +logger.addHandler(console_handler) diff --git a/client/oci_env/utils.py b/client/oci_env/utils.py index ce82bbf..60c469f 100644 --- a/client/oci_env/utils.py +++ b/client/oci_env/utils.py @@ -1,19 +1,48 @@ -from genericpath import isfile import os -import subprocess import pathlib +import subprocess import time +from genericpath import isfile from urllib import request +from oci_env.logger import logger + def get_oci_env_path(): - return os.environ.get("OCI_ENV_PATH", os.getcwd()) + """This returns the root directory of the oci-env checkout.""" + + if OCI_ENV_PATH := os.environ.get("OCI_ENV_PATH"): + OCI_ENV_PATH = OCI_ENV_PATH.rstrip('/') + logger.info('USING OCI_ENV_PATH FROM ENV: {OCI_ENV_PATH}') + return OCI_ENV_PATH + + # this is the $CHECKOUT/client/oci_env/utils.py path ... + path = os.path.dirname(__file__) + + # use git to find the root dir ... + pid = subprocess.run( + "git rev-parse --show-toplevel", + cwd=path, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + if pid.returncode != 0: + cwd = os.getcwd().rstrip('/') + logger.warning(f'USING CWD {cwd} FOR OCI_ENV_PATH BECAUSE OF GIT CMD FAILURE {pid.stdout}') + return cwd + + gitroot = pid.stdout.decode('utf-8').strip().rstrip('/') + logger.info(f'USING {gitroot} FOR OCI_ENV_PATH BASED ON GIT CMD OUTPUT') + return gitroot + def exit_with_error(msg): - print(msg) + logger.error(msg) exit(1) + def read_env_file(path, exit_on_error=True): """ Read the contents of a .env file into a dictionary. @@ -75,7 +104,7 @@ def get_config(env_file): # A port dedicated for exposing generated docs "DOCS_PORT": "12345", "NGINX_DOCS_PORT": user_preferences.get("DOCS_PORT", "12345"), - + # nginx port to run in the container. This defaults to 5001 if nothing is set or # the value of API_HOST if that is set. "NGINX_PORT": user_preferences.get("API_PORT", "5001"), @@ -268,8 +297,8 @@ def get_env_file(path, env_file): for f in files: if os.path.isfile(f): return f - print(f"No compose.env or .compose.env file found in {path}.") - + logger.error(f"No compose.env or .compose.env file found in {path}.") + else: files = [ os.path.abspath(env_file), @@ -279,8 +308,8 @@ def get_env_file(path, env_file): for f in files: if os.path.isfile(f): return f - print(f"Could not find file {env_file}") - + logger.error(f"Could not find file {env_file}") + exit(1) @@ -318,7 +347,7 @@ def compose_command(self, cmd, interactive=False, pipe_output=False): cmd = binary + compose_files + cmd if self.is_verbose: - print(f"Running command in container: {' '.join(cmd)}") + logger.info(f"Running command in container: {' '.join(cmd)}") if interactive: return subprocess.call(cmd) @@ -339,7 +368,7 @@ def container_name(self, service=None): def _exit_no_container_found(): service_name = service if service[-1].isdigit() else f"{service}_1" name = f"{project_name}_{service_name}" - print( + logger.error( f"Could not find a running container named: {name} \n" f"instead of {service!r} did you mean 'pulp' or 'ui'?\n" "Run `oci-env compose ps` to see all running containers." @@ -396,7 +425,7 @@ def exec(self, args, service=None, interactive=False, pipe_output=False, privile cmd = cmd[:2] + ["--privileged"] + cmd[2:] if self.is_verbose: - print(f"Running command in container: {' '.join(cmd)}") + logger.info(f"Running command in container: {' '.join(cmd)}") if interactive: proc = subprocess.call(cmd) @@ -454,7 +483,7 @@ def poll(self, attempts, wait_time): ) try: if request.urlopen(status_api).code == 200: - print(f"[{container_name}] {status_api} online after {(i * wait_time)} seconds") + logger.info(f"[{container_name}] {status_api} online after {(i * wait_time)} seconds") return except: time.sleep(wait_time) diff --git a/client/setup.py b/client/setup.py index af05a46..d78f03f 100644 --- a/client/setup.py +++ b/client/setup.py @@ -15,4 +15,4 @@ long_description_content_type="text/markdown", url='https://github.com/newswangerd/oci-env', description='CLI for running OCI Env.' -) \ No newline at end of file +)