def prepare_task(action, obj, selector, conf, attr, spec, old_hc, delta, host_map, cluster, # pylint: disable=too-many-locals hosts, event, verbose): lock_objects(obj, event) if not attr: attr = {} if host_map: api.save_hc(cluster, host_map) if action.type == 'task': task = create_task( action, selector, obj, conf, attr, old_hc, delta, hosts, event, verbose ) else: task = create_one_job_task( action, selector, obj, conf, attr, old_hc, hosts, event, verbose ) _job = create_job(action, None, selector, event, task.id) if conf: new_conf = process_config_and_attr(task, conf, attr, spec) process_file_type(task, spec, conf) task.config = new_conf task.save() return task
def test_save_hc(self, mock_post_event, mock_save_issue, mock_load_service_map): cluster_object = models.ClusterObject.objects.create( prototype=self.prototype, cluster=self.cluster) host = models.Host.objects.create(prototype=self.prototype, cluster=self.cluster) component = models.Prototype.objects.create(parent=self.prototype, type='component', bundle_id=self.bundle.id, name='node') service_component = models.ServiceComponent.objects.create( cluster=self.cluster, service=cluster_object, prototype=component) models.HostComponent.objects.create(cluster=self.cluster, host=host, service=cluster_object, component=service_component) host_comp_list = [(cluster_object, host, service_component)] hc_list = api_module.save_hc(self.cluster, host_comp_list) self.assertListEqual(hc_list, [models.HostComponent.objects.get(id=2)]) mock_post_event.assert_called_once_with('change_hostcomponentmap', 'cluster', self.cluster.id) mock_save_issue.assert_called_once_with(self.cluster) mock_load_service_map.assert_called_once()
def test_save_hc__big_update__locked_hierarchy(self, mock_issue, mock_load, ctx): """ Update bigger HC map - move `component_2` from `host_2` to `host_3` On locked hierarchy (from ansible task) Test: host_1 remains the same host_2 is unlocked host_3 became locked """ service = utils.gen_service(self.cluster) component_1 = utils.gen_component(service) component_2 = utils.gen_component(service) provider = utils.gen_provider() host_1 = utils.gen_host(provider, cluster=self.cluster) host_2 = utils.gen_host(provider, cluster=self.cluster) host_3 = utils.gen_host(provider, cluster=self.cluster) utils.gen_host_component(component_1, host_1) utils.gen_host_component(component_2, host_2) task = utils.gen_task_log(service) utils.gen_job_log(task) tree = cm.hierarchy.Tree(self.cluster) affected = (node.value for node in tree.get_all_affected(tree.built_from)) task.lock_affected(affected) ctx.lock = task.lock # refresh due to new instances were updated in task.lock_affected() host_1.refresh_from_db() host_2.refresh_from_db() host_3.refresh_from_db() self.assertTrue(host_1.locked) self.assertTrue(host_2.locked) self.assertFalse(host_3.locked) new_hc_list = [ (service, host_1, component_1), (service, host_3, component_2), ] api_module.save_hc(self.cluster, new_hc_list) # refresh due to new instances were updated in save_hc() host_1.refresh_from_db() host_2.refresh_from_db() host_3.refresh_from_db() self.assertTrue(host_1.locked) self.assertFalse(host_2.locked) self.assertTrue(host_3.locked)
def restore_hc(task, action, status): if status != config.Job.FAILED: return if not action.hostcomponentmap: return selector = task.selector if 'cluster' not in selector: log.error('no cluster in task #%s selector', task.id) return cluster = Cluster.objects.get(id=selector['cluster']) host_comp_list = [] for hc in task.hostcomponentmap: host = Host.objects.get(id=hc['host_id']) service = ClusterObject.objects.get(id=hc['service_id'], cluster=cluster) comp = ServiceComponent.objects.get(id=hc['component_id'], cluster=cluster, service=service) host_comp_list.append((service, host, comp)) log.warning('task #%s is failed, restore old hc', task.id) api.save_hc(cluster, host_comp_list)
def test_save_hc__big_update__unlocked_hierarchy(self, mock_update, mock_load): """ Update bigger HC map - move `component_2` from `host_2` to `host_3` On unlocked hierarchy (from API) Test: host_1 remains unlocked host_2 remains unlocked host_3 remains unlocked """ service = utils.gen_service(self.cluster) component_1 = utils.gen_component(service) component_2 = utils.gen_component(service) provider = utils.gen_provider() host_1 = utils.gen_host(provider, cluster=self.cluster) host_2 = utils.gen_host(provider, cluster=self.cluster) host_3 = utils.gen_host(provider, cluster=self.cluster) utils.gen_host_component(component_1, host_1) utils.gen_host_component(component_2, host_2) host_1.refresh_from_db() host_2.refresh_from_db() host_3.refresh_from_db() self.assertFalse(host_1.locked) self.assertFalse(host_2.locked) self.assertFalse(host_3.locked) new_hc_list = [ (service, host_1, component_1), (service, host_3, component_2), ] api_module.save_hc(self.cluster, new_hc_list) # refresh due to new instances were updated in save_hc() host_1.refresh_from_db() host_2.refresh_from_db() host_3.refresh_from_db() self.assertFalse(host_1.locked) self.assertFalse(host_2.locked) self.assertFalse(host_3.locked)
def restore_hc(task: TaskLog, action: Action, status: str): if status != config.Job.FAILED: return if not action.hostcomponentmap: return cluster = get_object_cluster(task.task_object) if cluster is None: log.error('no cluster in task #%s', task.pk) return host_comp_list = [] for hc in task.hostcomponentmap: host = Host.objects.get(id=hc['host_id']) service = ClusterObject.objects.get(id=hc['service_id'], cluster=cluster) comp = ServiceComponent.objects.get(id=hc['component_id'], cluster=cluster, service=service) host_comp_list.append((service, host, comp)) log.warning('task #%s is failed, restore old hc', task.pk) api.save_hc(cluster, host_comp_list)
def prepare_task( action: Action, obj: ADCMEntity, conf: dict, attr: dict, hc: List[HostComponent], hosts: List[Host], verbose: bool, ) -> TaskLog: # pylint: disable=too-many-locals cluster = get_object_cluster(obj) check_action_state(action, obj, cluster) _, spec = check_action_config(action, obj, conf, attr) if conf and not spec: err("CONFIG_VALUE_ERROR", "Absent config in action prototype") host_map = check_hostcomponentmap(cluster, action, hc) check_action_hosts(action, obj, cluster, hosts) old_hc = api.get_hc(cluster) if not attr: attr = {} with transaction.atomic(): # pylint: disable=too-many-locals DummyData.objects.filter(id=1).update(date=timezone.now()) task = create_task(action, obj, conf, attr, old_hc, hosts, verbose) if host_map: api.save_hc(cluster, host_map) if conf: new_conf = process_config_and_attr(task, conf, attr, spec) process_file_type(task, spec, conf) task.config = new_conf task.save() return task