Skip to content

Commit

Permalink
Enumerate the OCI_ENV_PATH automatically. (#128)
Browse files Browse the repository at this point in the history
The OCI_ENV_PATH is almost always the checkout directory of oci_env. Using python import paths and git commands, we can automatically determine the root path for the checkout. This alleviates the need to set the env var for most cases.

[noissue]


---------

Signed-off-by: James Tanner <[email protected]>
  • Loading branch information
jctanner authored Jul 31, 2023
1 parent 4d0e000 commit e73d367
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 19 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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/[email protected]

- name: (Mac) Install docker compose
if: matrix.TEST == 'docker' && matrix.os == 'macos-12'
uses: docker-practice/[email protected]
Expand Down
46 changes: 46 additions & 0 deletions client/oci_env/logger.py
Original file line number Diff line number Diff line change
@@ -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)
55 changes: 42 additions & 13 deletions client/oci_env/utils.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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),
Expand All @@ -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)


Expand Down Expand Up @@ -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)
Expand All @@ -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."
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion client/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
long_description_content_type="text/markdown",
url='https://github.com/newswangerd/oci-env',
description='CLI for running OCI Env.'
)
)

0 comments on commit e73d367

Please sign in to comment.