From: Guillaume Abrioux Date: Thu, 14 Sep 2023 15:32:38 +0000 (+0000) Subject: cephadm/binary: add `query_endpoint()` method X-Git-Tag: v18.2.4~314^2~70 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=6712ca866ecbb6859defc75c8fbfcd61d0186ab3;p=ceph.git cephadm/binary: add `query_endpoint()` method This encapsulates the existing code in a new method `query_endpoint()`. The idea is to avoid duplicating code if we need to make multiple calls to the agent endpoint from the `run()` method. Signed-off-by: Guillaume Abrioux (cherry picked from commit 7544406be33a579b3d0c63ee4c78ae91b02dfb0e) --- diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index d8af9d3b9185e..0674377a4162e 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -4893,6 +4893,28 @@ WantedBy=ceph-{fsid}.target self.device_enhanced_scan = True self.volume_gatherer.update_func(lambda: self._ceph_volume(enhanced=self.device_enhanced_scan)) + def query_endpoint(self, + addr: str = '', + port: str = '', + data: Optional[Union[Dict[str, str], str]] = None, + endpoint: str = '', + ssl_ctx: Optional[Any] = None) -> str: + _addr = addr if addr else self.target_ip + _port = port if port else self.target_port + url = f'https://{_addr}:{_port}{endpoint}' + + try: + req = Request(url, data, {'Content-Type': 'application/json'}) + send_time = time.monotonic() + with urlopen(req, context=ssl_ctx) as response: + response_str = response.read() + response_json = json.loads(response_str) + total_request_time = datetime.timedelta(seconds=(time.monotonic() - send_time)).total_seconds() + logger.info(f'Received mgr response: "{response_json["result"]}" {total_request_time} seconds after sending request.') + except Exception: + raise + return response_str + def run(self) -> None: self.pull_conf_settings() @@ -4949,15 +4971,10 @@ WantedBy=ceph-{fsid}.target 'port': self.listener_port}) data = data.encode('ascii') - url = f'https://{self.target_ip}:{self.target_port}/data/' try: - req = Request(url, data, {'Content-Type': 'application/json'}) - send_time = time.monotonic() - with urlopen(req, context=ssl_ctx) as response: - response_str = response.read() - response_json = json.loads(response_str) - total_request_time = datetime.timedelta(seconds=(time.monotonic() - send_time)).total_seconds() - logger.info(f'Received mgr response: "{response_json["result"]}" {total_request_time} seconds after sending request.') + self.query_endpoint(data=data, + endpoint='/data/', + ssl_ctx=ssl_ctx) except Exception as e: logger.error(f'Failed to send metadata to mgr: {e}')