Skip to content

Commit

Permalink
ENH: Use envvar to specify service configuration location by default
Browse files Browse the repository at this point in the history
skipci
  • Loading branch information
cortadocodes committed Jul 1, 2024
1 parent 4ef076c commit 1e7feb1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
6 changes: 4 additions & 2 deletions octue/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ def octue_cli(id, logger_uri, log_level, force_reset):
"-c",
"--service-config",
type=click.Path(dir_okay=False),
default="octue.yaml",
help="The path to an `octue.yaml` file defining the service to run.",
default=None,
help="The path to an `octue.yaml` file defining the service to run. If not provided, the "
"`OCTUE_SERVICE_CONFIGURATION_PATH` environment variable is used if present, otherwise the local path `octue.yaml` "
"is used.",
)
@click.option(
"--input-dir",
Expand Down
4 changes: 2 additions & 2 deletions octue/cloud/deployment/google/answer_pub_sub_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from octue.cloud.events.counter import EventCounter
from octue.cloud.pub_sub.service import Service
from octue.cloud.service_id import create_sruid, get_sruid_parts
from octue.configuration import DEFAULT_SERVICE_CONFIGURATION_PATH, load_service_and_app_configuration
from octue.configuration import load_service_and_app_configuration
from octue.resources.service_backends import GCPPubSubBackend
from octue.runner import Runner
from octue.utils.objects import get_nested_attribute
Expand All @@ -19,7 +19,7 @@ def answer_question(question, project_name):
:param str project_name:
:return None:
"""
service_configuration, app_configuration = load_service_and_app_configuration(DEFAULT_SERVICE_CONFIGURATION_PATH)
service_configuration, app_configuration = load_service_and_app_configuration()
service_namespace, service_name, service_revision_tag = get_sruid_parts(service_configuration)

service_sruid = create_sruid(
Expand Down
4 changes: 2 additions & 2 deletions octue/cloud/deployment/google/cloud_run/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from octue.cloud.deployment.google.answer_pub_sub_question import answer_question
from octue.cloud.pub_sub.bigquery import get_events
from octue.configuration import DEFAULT_SERVICE_CONFIGURATION_PATH, ServiceConfiguration
from octue.configuration import ServiceConfiguration


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -66,7 +66,7 @@ def _acknowledge_and_drop_redelivered_questions(question_uuid, retry_count):
:param int retry_count: the retry count of the question to check
:return (str, int)|None: an empty response with a 204 HTTP code if the question should be dropped
"""
service_configuration = ServiceConfiguration.from_file(DEFAULT_SERVICE_CONFIGURATION_PATH)
service_configuration = ServiceConfiguration.from_file()

if not service_configuration.event_store_table_id:
logger.warning(
Expand Down
16 changes: 9 additions & 7 deletions octue/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ def __init__(
logger.warning(f"The following keyword arguments were not used by {type(self).__name__}: {kwargs!r}.")

@classmethod
def from_file(cls, path):
"""Load a service configuration from a file.
def from_file(cls, path=None):
"""Load a service configuration from a YAML file.
:param str path:
:return ServiceConfiguration:
:param str|None path: the path to the service configuration YAML file; if not provided, the `OCTUE_SERVICE_CONFIGURATION_PATH` environment variable is used if present, otherwise the local path `octue.yaml` is used
:return ServiceConfiguration: the service configuration loaded from the file
"""
path = path or os.environ.get("OCTUE_SERVICE_CONFIGURATION_PATH", DEFAULT_SERVICE_CONFIGURATION_PATH)

with open(path) as f:
raw_service_configuration = yaml.load(f, Loader=yaml.SafeLoader)

Expand Down Expand Up @@ -137,12 +139,12 @@ def from_file(cls, path):
return cls(**raw_app_configuration)


def load_service_and_app_configuration(service_configuration_path):
def load_service_and_app_configuration(service_configuration_path=None):
"""Load the service configuration from the given YAML file and the app configuration referenced in it. If no app
configuration is referenced, an empty one is returned.
:param str service_configuration_path: path to service configuration file
:return (octue.configuration.ServiceConfiguration, octue.configuration.AppConfiguration):
:param str|None service_configuration_path: the path to the service configuration YAML file; if not provided, the `OCTUE_SERVICE_CONFIGURATION_PATH` environment variable is used if present, otherwise the local path `octue.yaml` is used
:return (octue.configuration.ServiceConfiguration, octue.configuration.AppConfiguration): the service configuration loaded from the YAML file and the app configuration specified by the service configuration (or an empty app configuration if none is specified)
"""
service_configuration = ServiceConfiguration.from_file(service_configuration_path)
app_configuration = AppConfiguration()
Expand Down

0 comments on commit 1e7feb1

Please sign in to comment.