Skip to content

Commit

Permalink
Merge pull request #888 from BCDA-APS/887-apsu-subnet-check
Browse files Browse the repository at this point in the history
check proper subnet for APSU beamlines
  • Loading branch information
prjemian authored Nov 17, 2023
2 parents 276ba5d + 98a5ff9 commit 0d2348e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ New Features
* Add (ophyd) device support for
* DG-645 digital delay/pulse generator
* Measurement Computing USB CTR08 High-Speed Counter/Timer
* Add subnet check for APSU beamlines.
* New lineup2() plan can be used in console, notebooks, and queueserver.

1.6.17
Expand Down
2 changes: 2 additions & 0 deletions apstools/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from .aps_data_management import dm_setup

from .apsu_controls_subnet import warn_if_not_aps_controls_subnet

from .catalog import copy_filtered_catalog
from .catalog import findCatalogsInNamespace
from .catalog import getCatalog
Expand Down
65 changes: 65 additions & 0 deletions apstools/utils/apsu_controls_subnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
APS-U controls are on private subnets. Check and advise as applicable.
.. autosummary::
~warn_if_not_aps_controls_subnet
"""

APSU_CONTROLS_SUBNET = "10.54."
APSU_XRAY_SUBNET = ".xray.aps.anl.gov"


def warn_if_not_aps_controls_subnet():
"""
APS-U controls are on private subnets. Check and advise as applicable.
Call this function early in the startup procedure. It could explain easily
the reason for subsequent EPICS PV connection failures.
For workstations on subnets that do not match the criteria, this function
should not post any warnings.
"""

import socket
import warnings

host_name = socket.gethostname()
if host_name.endswith(APSU_XRAY_SUBNET):
host_ip_addr = socket.gethostbyname(host_name)
if not host_ip_addr.startswith(APSU_CONTROLS_SUBNET):
warnings.warn(
f"Your APS workstation ({host_name}) has IP {host_ip_addr!r}."
" If you experience EPICS connection timeouts,"
" consider switching to a workstation on the controls subnet"
f" which has an IP starting with {APSU_CONTROLS_SUBNET!r}"
)


def warn_if_not_aps_controls_subnet():
"""
APS-U controls are on private subnets. Check and advise as applicable.
Call this function early in the startup procedure. It could explain easily
the reason for subsequent EPICS PV connection failures.
For workstations on subnets that do not match the criteria, this function
should not post any warnings.
"""

import socket
import warnings

xray_subnet = ".xray.aps.anl.gov"
controls_subnet = "10.54."

host_name = socket.gethostname()
if host_name.endswith(xray_subnet):
host_ip_addr = socket.gethostbyname(host_name)
if not host_ip_addr.startswith(controls_subnet):
warnings.warn(
f"Your APS workstation ({host_name}) has IP {host_ip_addr!r}."
" If you experience EPICS connection timeouts,"
" consider switching to a workstation on the controls subnet"
f" which has an IP starting with {controls_subnet!r}"
)
21 changes: 21 additions & 0 deletions apstools/utils/tests/test_subnet_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import socket
from contextlib import nullcontext as does_not_raise

import pytest

from ..apsu_controls_subnet import APSU_CONTROLS_SUBNET
from ..apsu_controls_subnet import APSU_XRAY_SUBNET
from ..apsu_controls_subnet import warn_if_not_aps_controls_subnet


def test_subnet_check():
host_name = socket.gethostname()
host_ip_addr = socket.gethostbyname(host_name)
# fmt: off
warns = (
host_name.endswith(APSU_XRAY_SUBNET)
and not host_ip_addr.startswith(APSU_CONTROLS_SUBNET)
)
# fmt: on
with pytest.raises(UserWarning) if warns else does_not_raise():
warn_if_not_aps_controls_subnet()
4 changes: 4 additions & 0 deletions docs/source/api/_utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Other Utilities
.. autosummary::

~apstools.utils.aps_data_management.dm_setup
~apstools.utils.apsu_controls_subnet.warn_if_not_aps_controls_subnet
~apstools.utils.misc.cleanupText
~apstools.utils.misc.connect_pvlist
~apstools.utils.email.EmailNotifications
Expand Down Expand Up @@ -132,6 +133,9 @@ Submodules
.. automodule:: apstools.utils.aps_data_management
:members:

.. automodule:: apstools.utils.apsu_controls_subnet
:members:

.. automodule:: apstools.utils.catalog
:members:

Expand Down

0 comments on commit 0d2348e

Please sign in to comment.