AUTHOR
FIREWHEEL Team
DONE
DESCRIPTION

This helper has two primary functions: 1) to submit a :py:attr:`RESUME <firewheel.vm_resource_manager.schedule_event.ScheduleEventType.RESUME>` event to a set of VMs within an experiment, and 2) to resume the specified set of VMs which are in the `PAUSED` state.
These actions can be applied to a set of VMs or to all VMs within the experiment. This is primarily used for resuming VMs which have
created a *break* within the VM resource schedule (for more information see :ref:`vm-resource-schedule`) and for resuming an experiment after a :ref:`helper_save` event.

**Usage:**  ``firewheel vm resume [-h] (-a | vm_name [vm_name ...])``

Arguments
+++++++++

Named Arguments
^^^^^^^^^^^^^^^

.. option:: -h, --help

    Show help message and exit.

.. option:: -a, --all

    Start all paused VMs and send a :py:attr:`RESUME <firewheel.vm_resource_manager.schedule_event.ScheduleEventType.RESUME>` event to all VMs in the experiment.

Positional Arguments
^^^^^^^^^^^^^^^^^^^^

.. option:: <vm_name>

    The hostname of the VM within the experiment that should be resumed.


Example
+++++++

``firewheel vm resume host.root.net``

``firewheel vm resume host.root.net bgp.root.net``

``firewheel vm resume --all``

DONE
RUN LocalPython ON control
#!/usr/bin/env python

import sys
import math
import pickle
import argparse

from rich.console import Console

from firewheel.lib.minimega.api import minimegaAPI
from firewheel.lib.experiment_utils import create_resume_schedule_entry
from firewheel.vm_resource_manager.schedule_db import ScheduleDb
from firewheel.vm_resource_manager.schedule_entry import ScheduleEntry

if __name__ == "__main__":
    # Set the arguments
    parser = argparse.ArgumentParser(
        description="Resume any VMs that were paused via a `break` VM Resource.",
        prog="firewheel vm resume",
    )

    grouping = parser.add_mutually_exclusive_group(required=True)
    grouping.add_argument(
        "-a",
        "--all",
        action="store_true",
        default=False,
        help="Resume all paused VMs.",
    )

    grouping.add_argument(
        "vm_name",
        nargs="*",
        default=[],
        help="The hostname of the VM within the experiment from which to pull the files",
    )

    args = parser.parse_args()

    console = Console()
    schedule_db = ScheduleDb()
    # Check if we need all VMs
    schedules = []
    mm_api = minimegaAPI()
    mm_dict = mm_api.mm_vms()
    if not args.all:
        for name in args.vm_name:
            try:
                valid = mm_dict[name]
                sched = create_resume_schedule_entry(schedule_db, console, name)
                schedules.append(
                    {"server_name": name, "text": pickle.dumps(sched), "ip": None}
                )
            except KeyError:
                console.print(f"[b red]ERROR: VM `[cyan]{name}[/cyan]` does not exist!")
                sys.exit(1)
        # Now check if any of the VMs are in a paused state and need to be resumed
        not_paused = []
        for name in args.vm_name:
            vm_info = mm_dict[name]
            if vm_info["state"] == "PAUSED":
                not_paused.append(name)
        if not_paused:
            mm_api.mm.vm_start(",".join(not_paused))
    else:
        for name in mm_dict:
            sched = create_resume_schedule_entry(schedule_db, console, name)
            schedules.append(
                {"server_name": name, "text": pickle.dumps(sched), "ip": None}
            )
        mm_api.mm.vm_start("all")
    schedule_db.batch_put(schedules, True)
    console.print(f"[b green]Resumed VM Resource Handling for {len(schedules)} VMs.")
DONE
