From fb2321ec6988075777d8fc838f1d19034855264a Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Mon, 29 Nov 2021 11:36:51 +0100 Subject: [PATCH] mgr/cephadm: serve.py: put _calc_client_files into it's own method Signed-off-by: Sebastian Wagner --- src/pybind/mgr/cephadm/serve.py | 128 ++++++++++++++++---------------- 1 file changed, 66 insertions(+), 62 deletions(-) diff --git a/src/pybind/mgr/cephadm/serve.py b/src/pybind/mgr/cephadm/serve.py index e4fd8f91d972a..078f86fb827ca 100644 --- a/src/pybind/mgr/cephadm/serve.py +++ b/src/pybind/mgr/cephadm/serve.py @@ -177,69 +177,10 @@ class CephadmServe: bad_hosts = [] failures = [] - # host -> path -> (mode, uid, gid, content, digest) - client_files: Dict[str, Dict[str, Tuple[int, int, int, bytes, str]]] = {} - - # ceph.conf if self.mgr.manage_etc_ceph_ceph_conf or self.mgr.keys.keys: - config = self.mgr.get_minimal_ceph_conf().encode('utf-8') - config_digest = ''.join('%02x' % c for c in hashlib.sha256(config).digest()) - - if self.mgr.manage_etc_ceph_ceph_conf: - try: - pspec = PlacementSpec.from_string(self.mgr.manage_etc_ceph_ceph_conf_hosts) - ha = HostAssignment( - spec=ServiceSpec('mon', placement=pspec), - hosts=self.mgr.cache.get_schedulable_hosts(), - unreachable_hosts=self.mgr.cache.get_unreachable_hosts(), - daemons=[], - networks=self.mgr.cache.networks, - ) - all_slots, _, _ = ha.place() - for host in {s.hostname for s in all_slots}: - if host not in client_files: - client_files[host] = {} - client_files[host]['/etc/ceph/ceph.conf'] = ( - 0o644, 0, 0, bytes(config), str(config_digest) - ) - except Exception as e: - self.mgr.log.warning( - f'unable to calc conf hosts: {self.mgr.manage_etc_ceph_ceph_conf_hosts}: {e}') - - # client keyrings - for ks in self.mgr.keys.keys.values(): - assert config - assert config_digest - try: - ret, keyring, err = self.mgr.mon_command({ - 'prefix': 'auth get', - 'entity': ks.entity, - }) - if ret: - self.log.warning(f'unable to fetch keyring for {ks.entity}') - continue - digest = ''.join('%02x' % c for c in hashlib.sha256( - keyring.encode('utf-8')).digest()) - ha = HostAssignment( - spec=ServiceSpec('mon', placement=ks.placement), - hosts=self.mgr.cache.get_schedulable_hosts(), - unreachable_hosts=self.mgr.cache.get_unreachable_hosts(), - daemons=[], - networks=self.mgr.cache.networks, - ) - all_slots, _, _ = ha.place() - for host in {s.hostname for s in all_slots}: - if host not in client_files: - client_files[host] = {} - client_files[host]['/etc/ceph/ceph.conf'] = ( - 0o644, 0, 0, bytes(config), str(config_digest) - ) - client_files[host][ks.path] = ( - ks.mode, ks.uid, ks.gid, keyring.encode('utf-8'), digest - ) - except Exception as e: - self.log.warning( - f'unable to calc client keyring {ks.entity} placement {ks.placement}: {e}') + client_files = self._calc_client_files() + else: + client_files = {} agents_down: List[str] = [] @@ -1034,6 +975,69 @@ class CephadmServe: # FIXME: we assume the first digest here is the best self.mgr.set_container_image(entity, image_info.repo_digests[0]) + def _calc_client_files(self) -> Dict[str, Dict[str, Tuple[int, int, int, bytes, str]]]: + # host -> path -> (mode, uid, gid, content, digest) + client_files: Dict[str, Dict[str, Tuple[int, int, int, bytes, str]]] = {} + + # ceph.conf + config = self.mgr.get_minimal_ceph_conf().encode('utf-8') + config_digest = ''.join('%02x' % c for c in hashlib.sha256(config).digest()) + + if self.mgr.manage_etc_ceph_ceph_conf: + try: + pspec = PlacementSpec.from_string(self.mgr.manage_etc_ceph_ceph_conf_hosts) + ha = HostAssignment( + spec=ServiceSpec('mon', placement=pspec), + hosts=self.mgr.cache.get_schedulable_hosts(), + unreachable_hosts=self.mgr.cache.get_unreachable_hosts(), + daemons=[], + networks=self.mgr.cache.networks, + ) + all_slots, _, _ = ha.place() + for host in {s.hostname for s in all_slots}: + if host not in client_files: + client_files[host] = {} + client_files[host]['/etc/ceph/ceph.conf'] = ( + 0o644, 0, 0, bytes(config), str(config_digest) + ) + except Exception as e: + self.mgr.log.warning( + f'unable to calc conf hosts: {self.mgr.manage_etc_ceph_ceph_conf_hosts}: {e}') + + # client keyrings + for ks in self.mgr.keys.keys.values(): + try: + ret, keyring, err = self.mgr.mon_command({ + 'prefix': 'auth get', + 'entity': ks.entity, + }) + if ret: + self.log.warning(f'unable to fetch keyring for {ks.entity}') + continue + digest = ''.join('%02x' % c for c in hashlib.sha256( + keyring.encode('utf-8')).digest()) + ha = HostAssignment( + spec=ServiceSpec('mon', placement=ks.placement), + hosts=self.mgr.cache.get_schedulable_hosts(), + unreachable_hosts=self.mgr.cache.get_unreachable_hosts(), + daemons=[], + networks=self.mgr.cache.networks, + ) + all_slots, _, _ = ha.place() + for host in {s.hostname for s in all_slots}: + if host not in client_files: + client_files[host] = {} + client_files[host]['/etc/ceph/ceph.conf'] = ( + 0o644, 0, 0, bytes(config), str(config_digest) + ) + client_files[host][ks.path] = ( + ks.mode, ks.uid, ks.gid, keyring.encode('utf-8'), digest + ) + except Exception as e: + self.log.warning( + f'unable to calc client keyring {ks.entity} placement {ks.placement}: {e}') + return client_files + async def _create_daemon(self, daemon_spec: CephadmDaemonDeploySpec, reconfig: bool = False, -- 2.39.5