Skip to content

Commit b0767b9

Browse files
committed
pants-plugins/uses_services: prepare to allow alternative st2cluster 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.
1 parent 50c91cc commit b0767b9

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

pants-plugins/uses_services/scripts/is_st2cluster_running.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
from contextlib import closing
2020

2121

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

3838

3939
if __name__ == "__main__":
40-
hostname = "127.0.0.1"
41-
service_ports = list(sys.argv[1:])
42-
if not service_ports:
43-
# st2.tests*.conf ends in /, but the default ends in //
44-
service_ports = ["9100", "9101", "9102"]
45-
46-
is_running = _is_st2cluster_running(hostname, service_ports)
40+
args_iter = iter(sys.argv[1:])
41+
# Turn the list into 2 tuples (zip with query the same iterator twice for each entry)
42+
endpoints = list(zip(args_iter, args_iter))
43+
if not endpoints:
44+
endpoints = [
45+
("127.0.0.1", "9100"),
46+
("127.0.0.1", "9101"),
47+
("127.0.0.1", "9102"),
48+
]
49+
50+
is_running = _is_st2cluster_running(endpoints)
4751
exit_code = 0 if is_running else 1
4852
sys.exit(exit_code)

pants-plugins/uses_services/st2cluster_rules.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,36 @@
4646
class UsesSt2ClusterRequest:
4747
"""One or more targets need a running st2 cluster with all st2* services."""
4848

49+
auth_host: str = "127.0.0.1"
4950
auth_port: int = 9100
51+
api_host: str = "127.0.0.1"
5052
api_port: int = 9101
53+
stream_host: str = "127.0.0.1"
5154
stream_port: int = 9102
5255

5356
@property
54-
def ports(self) -> tuple[str, ...]:
55-
return str(self.auth_port), str(self.api_port), str(self.stream_port)
57+
def endpoints(self) -> tuple[tuple[str, str], ...]:
58+
return (
59+
(self.auth_host, str(self.auth_port)),
60+
(self.api_host, str(self.api_port)),
61+
(self.stream_host, str(self.stream_port)),
62+
)
63+
64+
# @classmethod
65+
# def from_env(cls, env: EnvironmentVars) -> UsesSt2ClusterRequest:
66+
# return cls()
67+
# TODO: consider adding a from_env method using one or both of client => server vars:
68+
# ST2_CONFIG_FILE => ST2_CONFIG_PATH (used by many tests, so not safe) or
69+
# ST2_CONF (only in launchdev.sh and st2ctl)
70+
# ST2_BASE_URL => ST2_WEBUI__WEBUI_BASE_URL
71+
# ST2_API_URL => ST2_AUTH__API_URL or
72+
# http{'s' if ST2_API__USE_SSL else ''}://{ST2_API__HOST}:{ST2_API__PORT}
73+
# ST2_AUTH_URL => http://{ST2_AUTH__HOST}:{ST2_AUTH__PORT}
74+
# ST2_STREAM_URL => http://{ST2_STREAM__HOST}:{ST2_STREAM__PORT}
75+
# ST2_CACERT (might be needed if using a self-signed cert) => n/a
76+
# These st2client env vars are irrelevant for the connectivity check:
77+
# ST2_AUTH_TOKEN or ST2_API_KEY
78+
# ST2_API_VERSION (always "v1" since we don't have anything else)
5679

5780

5881
@dataclass(frozen=True)
@@ -113,7 +136,11 @@ async def st2cluster_is_running(
113136
FallibleProcessResult,
114137
VenvPexProcess(
115138
script_pex,
116-
argv=request.ports,
139+
argv=[
140+
host_or_port
141+
for endpoint in request.endpoints
142+
for host_or_port in endpoint
143+
],
117144
input_digest=script_digest,
118145
description="Checking to see if ST2 Cluster is up and accessible.",
119146
# this can change from run to run, so don't cache results.

0 commit comments

Comments
 (0)