Skip to content

Commit

Permalink
pants-plugins/uses_services: prepare to allow alternative st2cluster …
Browse files Browse the repository at this point in the history
…hosts/ports

In the uses_services plugin, I am now adding env var support
(in several PRs), but so far I've avoiding parsing conf files.

Plus, allowing reconfiguration of the st2cluster hosts/ports
would require the st2client and st2 dev cluster config to align.

Today, the st2cluster integration tests hardcode st2client's
base_url to `http://127.0.0.1`. Technically someone could override
the endpoint URLs with ST2_{AUTH,API,STREAM}_URL env vars, but
they would also need to make sure whatever cluster they use
has the examples pack and other things handled by `launchdev.sh`.

Today, the other alternative for using a different cluster would
be to forward the service ports to the localhost at the standard
ports.

For now, just use a separate host for each endpoint so it is
easier to support overriding via env vars and/or conf files
at some point in the future--if someone needs that. Until then,
we'll go with the minimal implementation that assumes the
default hosts and ports.
  • Loading branch information
cognifloyd committed Nov 18, 2024
1 parent 50c91cc commit 491a4a2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
22 changes: 13 additions & 9 deletions pants-plugins/uses_services/scripts/is_st2cluster_running.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
from contextlib import closing


def _is_st2cluster_running(host: str, ports: list[str]) -> bool:
def _is_st2cluster_running(endpoints: list[tuple[str, str]]) -> bool:
"""Check for listening ports of st2auth, st2api, and st2stream services.
This should not import the st2 code as it should be self-contained.
"""
# TODO: Once each service gains a reliable health check endpoint, use that.
# https://github.com/StackStorm/st2/issues/4020
for port in ports:
for host, port in endpoints:
# based on https://stackoverflow.com/a/35370008/1134951
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
# errno=0 means the connection succeeded
Expand All @@ -37,12 +37,16 @@ def _is_st2cluster_running(host: str, ports: list[str]) -> bool:


if __name__ == "__main__":
hostname = "127.0.0.1"
service_ports = list(sys.argv[1:])
if not service_ports:
# st2.tests*.conf ends in /, but the default ends in //
service_ports = ["9100", "9101", "9102"]

is_running = _is_st2cluster_running(hostname, service_ports)
args_iter = iter(sys.argv[1:])
# Turn the list into 2 tuples (zip with query the same iterator twice for each entry)
endpoints = list(zip(args_iter, args_iter))
if not endpoints:
endpoints = [
("127.0.0.1", "9100"),
("127.0.0.1", "9101"),
("127.0.0.1", "9102"),
]

is_running = _is_st2cluster_running(endpoints)
exit_code = 0 if is_running else 1
sys.exit(exit_code)
29 changes: 26 additions & 3 deletions pants-plugins/uses_services/st2cluster_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,36 @@
class UsesSt2ClusterRequest:
"""One or more targets need a running st2 cluster with all st2* services."""

auth_host: str = "127.0.0.1"
auth_port: int = 9100
api_host: str = "127.0.0.1"
api_port: int = 9101
stream_host: str = "127.0.0.1"
stream_port: int = 9102

@property
def ports(self) -> tuple[str, ...]:
return str(self.auth_port), str(self.api_port), str(self.stream_port)
def endpoints(self) -> tuple[tuple[str, str], ...]:
return (
(self.auth_host, str(self.auth_port)),
(self.api_host, str(self.api_port)),
(self.stream_host, str(self.stream_port)),
)

# @classmethod
# def from_env(cls, env: EnvironmentVars) -> UsesSt2ClusterRequest:
# return cls()
# TODO: consider adding a from_env method using one or both of client => server vars:
# ST2_CONFIG_FILE => ST2_CONFIG_PATH (used by many tests, so not safe) or
# ST2_CONF (only in launchdev.sh and st2ctl)
# ST2_BASE_URL => ST2_WEBUI__WEBUI_BASE_URL
# ST2_API_URL => ST2_AUTH__API_URL or
# http{'s' if ST2_API__USE_SSL else ''}://{ST2_API__HOST}:{ST2_API__PORT}
# ST2_AUTH_URL => http://{ST2_AUTH__HOST}:{ST2_AUTH__PORT}
# ST2_STREAM_URL => http://{ST2_STREAM__HOST}:{ST2_STREAM__PORT}
# ST2_CACERT (might be needed if using a self-signed cert) => n/a
# These st2client env vars are irrelevant for the connectivity check:
# ST2_AUTH_TOKEN or ST2_API_KEY
# ST2_API_VERSION (always "v1" since we don't have anything else)


@dataclass(frozen=True)
Expand Down Expand Up @@ -113,7 +136,7 @@ async def st2cluster_is_running(
FallibleProcessResult,
VenvPexProcess(
script_pex,
argv=request.ports,
argv=[host_or_port for endpoint in request.endpoints for host_or_port in endpoint],
input_digest=script_digest,
description="Checking to see if ST2 Cluster is up and accessible.",
# this can change from run to run, so don't cache results.
Expand Down

0 comments on commit 491a4a2

Please sign in to comment.