def _update_slurm_reservation()

in src/slurm_plugin/capacity_block_manager.py [0:0]


    def _update_slurm_reservation(capacity_block: CapacityBlock, do_update: bool):
        """
        Update Slurm reservation associated to the given Capacity Block.

        A CB has five possible states: payment-pending, pending, active, expired and payment-failed,
        we need to create/delete Slurm reservation accordingly.

        Pass do_update to True only if you want to update already created reservations
        (e.g. to update the node list or when the CapacityBlockManager is not yet initialized).

        Returns True if slurm action is completed correctly, False otherwise.
        """

        def _log_cb_info(action_info):
            logger.info(
                "Capacity Block reservation %s is in state %s. %s Slurm reservation %s for nodes %s.",
                capacity_block.capacity_block_id,
                capacity_block.state(),
                action_info,
                slurm_reservation_name,
                capacity_block_nodenames,
            )

        # retrieve list of nodes associated to a given slurm reservation/capacity block
        slurm_reservation_name = capacity_block.slurm_reservation_name()
        capacity_block_nodenames = ",".join(capacity_block.nodenames())

        try:
            reservation_exists = is_slurm_reservation(name=slurm_reservation_name)
            # if CB is active we need to remove Slurm reservation and start nodes
            if capacity_block.is_active():
                # if Slurm reservation exists, delete it.
                if reservation_exists:
                    _log_cb_info("Deleting")
                    delete_slurm_reservation(name=slurm_reservation_name)
                else:
                    _log_cb_info("Nothing to do. No existing")

            # if CB is expired or not active we need to (re)create Slurm reservation
            # to avoid considering nodes as unhealthy
            else:
                # create or update Slurm reservation
                if reservation_exists:
                    if do_update:
                        _log_cb_info("Updating existing")
                        update_slurm_reservation(name=slurm_reservation_name, nodes=capacity_block_nodenames)
                    else:
                        _log_cb_info("Nothing to do. Already existing")
                else:
                    _log_cb_info("Creating")
                    # The reservation should start now, and will be removed when the capacity block will become active
                    create_slurm_reservation(
                        name=slurm_reservation_name,
                        start_time="now",
                        nodes=capacity_block_nodenames,
                        duration="infinite",
                    )

            action_completed = True
        except SlurmCommandError as e:
            logger.error(
                "Unable to update slurm reservation %s for Capacity Block %s. Skipping it. %s",
                slurm_reservation_name,
                capacity_block.capacity_block_id,
                e,
            )
            action_completed = False

        return action_completed