From 0127282201ee1a6eda01df4783069e227ca0c593 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Tue, 17 Mar 2020 11:33:20 +0100 Subject: [PATCH] mgr/cephadm: Fix `ceph orch apply -i` * We had two implementations for `ceph orcha apply` * Remove yaml pasing in `mgr/cephadm` Signed-off-by: Sebastian Wagner --- src/pybind/mgr/cephadm/module.py | 30 ++--------- src/pybind/mgr/cephadm/tests/test_cephadm.py | 12 ----- src/pybind/mgr/orchestrator/_interface.py | 2 +- src/pybind/mgr/orchestrator/module.py | 56 +++++++------------- 4 files changed, 26 insertions(+), 74 deletions(-) diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index 09da4fdf982..743df4c5293 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -2547,7 +2547,7 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): # type: (ServiceSpec) -> orchestrator.Completion return self._add_daemon('mgr', spec, self._create_mgr) - def _apply(self, spec): + def _apply(self, spec: ServiceSpec): if spec.placement.is_empty(): # fill in default placement defaults = { @@ -2574,6 +2574,9 @@ class CephadmOrchestrator(orchestrator.Orchestrator, MgrModule): self._kick_serve_loop() return trivial_result("Scheduled %s update..." % spec.service_type) + def apply(self, specs: List[ServiceSpec]) -> AsyncCompletion: + return self._apply(specs) + def apply_mgr(self, spec): return self._apply(spec) @@ -3104,30 +3107,7 @@ receivers: """ Loads all entries from the service_spec mon_store root. """ - specs = list() - for spec in self.spec_store.find(service_name=service_name): - specs.append('---') - specs.append(yaml.safe_dump(spec.to_json())) - return trivial_result(specs) - - def apply_service_config(self, spec_document: str) -> orchestrator.Completion: - """ - Parse a multi document yaml file (represented in a inbuf object) - and loads it with it's respective ServiceSpec to validate the - initial input. - If no errors are raised, save them. - """ - content: Iterator[Any] = yaml.load_all(spec_document) - # Load all specs from a multi document yaml file. - loaded_specs: List[ServiceSpec] = list() - for spec in content: - # load ServiceSpec once to validate - spec_o = ServiceSpec.from_json(spec) - loaded_specs.append(spec_o) - for spec in loaded_specs: - self.spec_store.save(spec) - self._kick_serve_loop() - return trivial_result("ServiceSpecs saved") + return trivial_result(self.spec_store.find(service_name=service_name)) class BaseScheduler(object): diff --git a/src/pybind/mgr/cephadm/tests/test_cephadm.py b/src/pybind/mgr/cephadm/tests/test_cephadm.py index 09eb4606f48..82cb4df3b96 100644 --- a/src/pybind/mgr/cephadm/tests/test_cephadm.py +++ b/src/pybind/mgr/cephadm/tests/test_cephadm.py @@ -360,15 +360,3 @@ class TestCephadm(object): c = cephadm_module.apply_node_exporter(spec) _save_spec.assert_called_with(spec) assert wait(cephadm_module, c) == 'Scheduled node_exporter update...' - - @mock.patch("cephadm.module.CephadmOrchestrator._run_cephadm", _run_cephadm('{}')) - @mock.patch("cephadm.module.SpecStore.save") - @mock.patch("cephadm.module.yaml.load_all", return_value=[{'service_type': 'rgw', 'placement': {'count': 1}, 'spec': {'rgw_realm': 'realm1', 'rgw_zone': 'zone1'}}]) - @mock.patch("cephadm.module.ServiceSpec") - def test_apply_service_config(self, _sspec, _yaml, _save_spec, cephadm_module): - with self._with_host(cephadm_module, 'test'): - c = cephadm_module.apply_service_config('dummy') - _save_spec.assert_called_once() - _sspec.from_json.assert_called_once() - assert wait(cephadm_module, c) == 'ServiceSpecs saved' - diff --git a/src/pybind/mgr/orchestrator/_interface.py b/src/pybind/mgr/orchestrator/_interface.py index 6f3f57566ef..5331fdb9780 100644 --- a/src/pybind/mgr/orchestrator/_interface.py +++ b/src/pybind/mgr/orchestrator/_interface.py @@ -844,7 +844,7 @@ class Orchestrator(object): """ raise NotImplementedError() - def apply_service_config(self, spec_document: str) -> Completion: + def apply(self, specs: List[ServiceSpec]) -> Completion: """ Saves Service Specs from a yaml|json file """ diff --git a/src/pybind/mgr/orchestrator/module.py b/src/pybind/mgr/orchestrator/module.py index 9d479a02244..e1355ef375e 100644 --- a/src/pybind/mgr/orchestrator/module.py +++ b/src/pybind/mgr/orchestrator/module.py @@ -11,7 +11,7 @@ from prettytable import PrettyTable from mgr_util import format_bytes, to_pretty_timedelta try: - from typing import List, Set, Optional, Dict + from typing import List, Set, Optional, Dict, Iterator except ImportError: pass # just for type checking. @@ -567,15 +567,6 @@ Usage: raise_if_exception(completion) return HandleCommandResult(stdout=completion.result_str()) - @_cli_write_command( - 'orch apply', - desc='Applies a Service Specification from a file. ceph orch apply -i $file') - def _apply_services(self, inbuf): - completion = self.apply_service_config(inbuf) - self._orchestrator_wait([completion]) - raise_if_exception(completion) - return HandleCommandResult(stdout=completion.result_str()) - @_cli_write_command( 'orch daemon add mds', 'name=fs_name,type=CephString ' @@ -703,39 +694,32 @@ Usage: completion = self.list_specs(service_name=service_name) self._orchestrator_wait([completion]) raise_if_exception(completion) - specs = completion.result_str() - return HandleCommandResult(stdout=specs) + specs = completion.result() + return HandleCommandResult(stdout=yaml.safe_dump_all(specs)) @_cli_write_command( 'orch apply', - 'name=service_type,type=CephChoices,strings=mon|mgr|rbd-mirror|crash|alertmanager|grafana|node-exporter|prometheus ' + 'name=service_type,type=CephChoices,strings=mon|mgr|rbd-mirror|crash|alertmanager|grafana|node-exporter|prometheus,req=false ' 'name=placement,type=CephString,req=false ' 'name=unmanaged,type=CephBool,req=false', - 'Update the size or placement for a service') - def _apply_misc(self, service_type, placement=None, unmanaged=False): - placement = PlacementSpec.from_string(placement) - placement.validate() + 'Update the size or placement for a service or apply a large yaml spec') + def _apply_misc(self, service_type=None, placement=None, unmanaged=False, inbuf=None): + usage = """Usage: + ceph orch apply -i + ceph orch apply [--unmanaged] + """ + if inbuf: + if service_type or placement or unmanaged: + raise OrchestratorValidationError(usage) + content: Iterator = yaml.load_all(inbuf) + specs = [ServiceSpec.from_json(s) for s in content] + else: + placement = PlacementSpec.from_string(placement) + placement.validate() - spec = ServiceSpec(service_type, placement=placement, - unmanaged=unmanaged) - - if service_type == 'mgr': - completion = self.apply_mgr(spec) - elif service_type == 'mon': - completion = self.apply_mon(spec) - elif service_type == 'rbd-mirror': - completion = self.apply_rbd_mirror(spec) - elif service_type == 'crash': - completion = self.apply_crash(spec) - elif service_type == 'alertmanager': - completion = self.apply_alertmanager(spec) - elif service_type == 'grafana': - completion = self.apply_grafana(spec) - elif service_type == 'node-exporter': - completion = self.apply_node_exporter(spec) - elif service_type == 'prometheus': - completion = self.apply_prometheus(spec) + specs = [ServiceSpec(service_type, placement=placement, unmanaged=unmanaged)] + completion = self.apply(specs) self._orchestrator_wait([completion]) raise_if_exception(completion) return HandleCommandResult(stdout=completion.result_str()) -- 2.39.5