diff --git a/pants-plugins/uses_services/scripts/is_st2cluster_running.py b/pants-plugins/uses_services/scripts/is_st2cluster_running.py index fce910c786..46ad55ca6e 100644 --- a/pants-plugins/uses_services/scripts/is_st2cluster_running.py +++ b/pants-plugins/uses_services/scripts/is_st2cluster_running.py @@ -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 @@ -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) diff --git a/pants-plugins/uses_services/st2cluster_rules.py b/pants-plugins/uses_services/st2cluster_rules.py index 5b4a55768e..4f2eb02929 100644 --- a/pants-plugins/uses_services/st2cluster_rules.py +++ b/pants-plugins/uses_services/st2cluster_rules.py @@ -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) @@ -113,7 +136,11 @@ 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.