def execute(self): # locking required tasks locked_tasks = objects.TaskCollection.lock_cluster_tasks( self.cluster.id) # locking cluster objects.Cluster.get_by_uid(self.cluster.id, fail_if_not_found=True, lock_for_update=True) # locking nodes nodes = objects.NodeCollection.filter_by(None, cluster_id=self.cluster.id) nodes = objects.NodeCollection.order_by(nodes, 'id') objects.NodeCollection.lock_for_update(nodes).all() current_cluster_tasks = objects.TaskCollection.filter_by_list( locked_tasks, 'name', (consts.TASK_NAMES.cluster_deletion, )) deploy_running = objects.TaskCollection.filter_by( None, cluster_id=self.cluster.id, name=consts.TASK_NAMES.deploy, status=consts.TASK_STATUSES.running) deploy_running = objects.TaskCollection.order_by(deploy_running, 'id').first() if deploy_running: logger.error(u"Deleting cluster '{0}' " "while deployment is still running".format( self.cluster.name)) # Updating action logs for deploy task TaskHelper.set_ready_if_not_finished(deploy_running) logger.debug("Removing cluster tasks") for task in current_cluster_tasks: if task.status == consts.TASK_STATUSES.running: db().rollback() raise errors.DeletionAlreadyStarted() elif task.status in (consts.TASK_STATUSES.ready, consts.TASK_STATUSES.error): for subtask in task.subtasks: db().delete(subtask) db().delete(task) db().flush() logger.debug("Labeling cluster nodes to delete") for node in self.cluster.nodes: node.pending_deletion = True db().add(node) db().flush() self.cluster.status = consts.CLUSTER_STATUSES.remove db().add(self.cluster) logger.debug("Creating cluster deletion task") task = Task(name=consts.TASK_NAMES.cluster_deletion, cluster=self.cluster) db().add(task) db().commit() self._call_silently(task, tasks.ClusterDeletionTask) return task
def execute(self): current_cluster_tasks = db().query(Task).filter_by( cluster=self.cluster, name='cluster_deletion' ).all() deploy_running = db().query(Task).filter_by( cluster=self.cluster, name='deploy', status='running' ).first() if deploy_running: logger.error( u"Deleting cluster '{0}' " "while deployment is still running".format( self.cluster.name ) ) logger.debug("Removing cluster tasks") for task in current_cluster_tasks: if task.status == "running": raise errors.DeletionAlreadyStarted() elif task.status in ("ready", "error"): for subtask in task.subtasks: db().delete(subtask) db().delete(task) db().commit() logger.debug("Labeling cluster nodes to delete") for node in self.cluster.nodes: node.pending_deletion = True db().add(node) db().commit() self.cluster.status = 'remove' db().add(self.cluster) db().commit() logger.debug("Creating cluster deletion task") task = Task(name="cluster_deletion", cluster=self.cluster) db().add(task) db().commit() self._call_silently( task, tasks.ClusterDeletionTask ) return task