Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumerate the OCI_ENV_PATH automatically. #128

Merged
merged 15 commits into from
Jul 31, 2023
11 changes: 7 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ jobs:
with:
python-version: "3.8"

- name: DEBUG VAR
run: echo "${GITHUB_WORKSPACE}"

jctanner marked this conversation as resolved.
Show resolved Hide resolved
- 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,9 +65,9 @@ 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: (Ubuntu) Install docker compose
# if: matrix.TEST == 'docker' && matrix.os == 'ubuntu-latest'
# uses: docker-practice/[email protected]
jctanner marked this conversation as resolved.
Show resolved Hide resolved

- name: (Mac) Install docker compose
if: matrix.TEST == 'docker' && matrix.os == 'macos-12'
Expand Down
52 changes: 39 additions & 13 deletions client/oci_env/utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
from genericpath import isfile
import os
import subprocess
import pathlib
import subprocess
import time

from genericpath import isfile
from urllib import request
from logzero import logger

jctanner marked this conversation as resolved.
Show resolved Hide resolved

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 os.environ.get("OCI_ENV_PATH"):
logger.info('USING OCI_ENV_PATH FROM ENVIRONMENT: {os.environ.get("OCI_ENV_PATH")}')
return os.environ.get("OCI_ENV_PATH")

jctanner marked this conversation as resolved.
Show resolved Hide resolved
# 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()
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()
logger.info(f'USING {gitroot} FOR OCI_ENV_PATH BASED ON GIT CMD OUTPUT')
return gitroot.rstrip('/') + '/'

jctanner marked this conversation as resolved.
Show resolved Hide resolved

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 +101,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 +294,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 +305,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 +344,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 +365,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 +422,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 +480,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
4 changes: 2 additions & 2 deletions client/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
entry_points={
'console_scripts': ['oci-env=oci_env.main:main'],
},
install_requires=[],
install_requires=['logzero'],
packages=find_packages(exclude=["tests", "tests.*"]),
long_description=long_description,
long_description_content_type="text/markdown",
url='https://github.com/newswangerd/oci-env',
description='CLI for running OCI Env.'
)
)
Loading