Skip to content

Commit

Permalink
Merge pull request #103 from simonsobs/koopman/config-file
Browse files Browse the repository at this point in the history
Create sorunlib yaml config file
  • Loading branch information
BrianJKoopman authored Nov 21, 2023
2 parents 528a3c8 + 4c120cd commit 7f0e0c9
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
51 changes: 51 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Configuration
=============

sorunlib tries to automatically determine as much about the OCS network as it
can, however some things still require configuration. This page details the
sorunlib configuration file.

.. note::
This format is still under development and might change. Check back here if
in doubt of formatting.

Example
-------

A full configuration file example with comments is shown here:

.. code-block:: yaml
---
# sorunlib configuration
# agents
# sorunlib automatically detects unique agents on the OCS network, so only
# non-unique agents need to be specified here.
# ocs registry agent
registry: 'registry'
Configuration Selection
-----------------------

Select the configuration file to load by setting the ``SORUNLIB_CONFIG``
environment variable:

.. code-block:: bash
$ export SORUNLIB_CONFIG=/path/to/sorunlib/config.yaml
In practice, sorunlib is typically run within a Docker container, so it's quite
natural to set this in a compose file. The following example uses the
``so-daq-sequencer`` image:

.. code-block:: yaml
sequencer-backend:
image: ghcr.io/simonsobs/so-daq-sequencer:latest
restart: always
container_name: sequencer-backend
environment:
- OCS_CONFIG_DIR=/path/to/ocs/config/
- SORUNLIB_CONFIG=/path/to/sorunlib/config.yaml
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ platform.
:caption: Documentation

design
configuration

.. toctree::
:maxdepth: 2
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
python_requires=">=3.7",
install_requires=[
'ocs==0.10.3',
'pyyaml',
],
extras_require={
"tests": ["pytest>=7.0.0", "pytest-cov>=3.0.0"],
Expand Down
20 changes: 20 additions & 0 deletions src/sorunlib/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import yaml


def load_config(filename=None):
"""Load sorunlib config file, using SORUNLIB_CONFIG by default.
Args:
filename (str): Path to sorunlib config file
Returns:
dict
"""
if filename is None:
assert (os.getenv('SORUNLIB_CONFIG') is not None)
filename = os.getenv('SORUNLIB_CONFIG')

with open(filename, 'rb') as f:
return yaml.safe_load(f)
14 changes: 7 additions & 7 deletions src/sorunlib/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from sorunlib.config import load_config

from ocs import site_config
from ocs.ocs_client import OCSClient

Expand Down Expand Up @@ -56,23 +58,21 @@ def _find_instances(agent_class, host=None, config=None):
return instances


def _find_active_instances(agent_class, config=None):
def _find_active_instances(agent_class):
"""Find all instances of an Agent Class currently online, based on the
Agents known by the registry.
Args:
agent_class (str): Agent Class name to search for, must match Agent
Class defined by an OCS Agent (and thus also defined in the SCF.)
config (str): Path to the OCS Site Config File. If None the default
file in OCS_CONFIG_DIR will be used.
Returns:
list: List of instance-id's matching the given agent_class.
"""
cfg = _load_site_config(config)
cfg = load_config()

reg_client = OCSClient(cfg.hub.data['registry_address'])
reg_client = OCSClient(cfg['registry'])
_, _, session = reg_client.main.status()

instances = []
Expand Down Expand Up @@ -112,8 +112,8 @@ def create_clients(config=None, test_mode=False):
else:
smurf_agent_class = 'PysmurfController'

acu_id = _find_active_instances('ACUAgent', config=config)
smurf_ids = _find_active_instances(smurf_agent_class, config=config)
acu_id = _find_active_instances('ACUAgent')
smurf_ids = _find_active_instances(smurf_agent_class)

if acu_id:
acu_client = OCSClient(acu_id[0])
Expand Down
2 changes: 2 additions & 0 deletions tests/data/example_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
registry: 'registry'
15 changes: 15 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os
os.environ["OCS_CONFIG_DIR"] = "./test_util/"
os.environ["SORUNLIB_CONFIG"] = "./data/example_config.yaml"

from sorunlib.config import load_config


def test_load_config():
cfg = load_config("./data/example_config.yaml")
assert cfg == {'registry': 'registry'}


def test_load_config_from_env():
cfg = load_config()
assert cfg == {'registry': 'registry'}

0 comments on commit 7f0e0c9

Please sign in to comment.