diff --git a/lib/MilkCheck/Engine/ServiceGroup.py b/lib/MilkCheck/Engine/ServiceGroup.py index a41930d..80a5988 100755 --- a/lib/MilkCheck/Engine/ServiceGroup.py +++ b/lib/MilkCheck/Engine/ServiceGroup.py @@ -127,7 +127,7 @@ def has_subservice(self, name): """ Check if the service is referenced within the group """ - return name in self._subservices + return self.search(name) is not None def has_action(self, action_name): """ @@ -202,6 +202,27 @@ def __update_edges(self, create_links=False): elif abs_ser is self._sink and len(dep.target.parents) > 1: dep.target.remove_dep('sink') + def cleanup_unused(self, services): + """ + Cleanup unused dependencies recursively + """ + parent = not self._algo_reversed + if parent: + spot = self._source + else: + spot = self._sink + + # Remove unused services + for service in list(spot.deps().keys()): + for target_service in services: + if '.' in target_service: + subservice, subservice_tree = target_service.split('.', 1) + self._subservices[subservice].cleanup_unused([subservice_tree]) + if subservice != service: + spot.remove_dep(service, parent=parent) + else: + if target_service != service: + spot.remove_dep(service, parent=parent) def remove_inter_dep(self, dep_name): """ diff --git a/lib/MilkCheck/ServiceManager.py b/lib/MilkCheck/ServiceManager.py index d67123b..29989a8 100755 --- a/lib/MilkCheck/ServiceManager.py +++ b/lib/MilkCheck/ServiceManager.py @@ -126,13 +126,11 @@ def select_services(self, services): spot = self._sink # Remove unused services - for service in list(spot.deps().keys()): - if service not in services: - spot.remove_dep(service, parent=parent) + self.cleanup_unused(services) # Add direct link to important services for service in services: if service not in spot.deps(): - svc = self._subservices[service] + svc = self.search(service) spot.add_dep(svc, parent=parent) def _disable_deps(self): diff --git a/tests/MilkCheckTests/ServiceManagerTest.py b/tests/MilkCheckTests/ServiceManagerTest.py index 34637ab..8d3d567 100755 --- a/tests/MilkCheckTests/ServiceManagerTest.py +++ b/tests/MilkCheckTests/ServiceManagerTest.py @@ -406,3 +406,28 @@ def test_tagged_run(self): self.assertFalse(srv.to_skip('start')) manager._apply_config({'tags': set(['bar'])}) self.assertTrue(srv.to_skip('start')) + + def test_subservice_lauch(self): + """ Test that we can launch an action on a subservice """ + manager = ServiceManager() + sergrp = ServiceGroup('S1') + sergrp.fromdict( + {'services': + {'srv1': + {'actions': + {'start': {'cmd':'/bin/True'}}, + 'desc': "I'm the service srv1" + }, + 'subgroup': + {'services': + {'subservice': + {, + 'actions': + {'start': {'cmd': '/bin/True'}}, + 'desc': 'I am the subservice $NAME'}, + 'desc': "I'm the service $NAME"}}, + }}) + manager.add_service(sergrp) + manager.call_services(['subgroup.subservice'], 'start') + self.assertEqual(sergrp['subgroup']['subservice'].status, DONE) + self.assertEqual(sergrp['srv1'].status, DONE)