AUTHOR
FIREWHEEL Team
DONE
DESCRIPTION
This will delete all lock directories and optionally the associated cache_files in the requested :class:`FileStore <firewheel.lib.minimega.file_store.FileStore>`.

**Usage:**  ``firewheel mm flush_locks <caches>``

Arguments
+++++++++

A user must either provide a list of caches (one of ``images``, ``schedules``, ``vm_resources``) or the :option:`mm flush_locks --all` parameter.

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

.. option:: -h, --help

    Show help message and exit.

.. option:: --all

    Clear lock files for all caches, including ``images``, ``schedules``, and ``vm_resources``.

.. option:: --clear

    Clear cache files associated with found lock files.

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

.. option:: <caches>

    The name(s) of the caches to check (i.e. ``images``, ``schedules``, and/or ``vm_resources``).

Example
+++++++

``firewheel mm flush_locks images``

``firewheel mm flush_locks --clear images schedules``

``firewheel mm flush_locks --all``

DONE

RUN Python ON compute
#!/usr/bin/env python
import os
import sys
import argparse
from pathlib import Path

from firewheel.config import config

parser = argparse.ArgumentParser(description="Clear lock files and optionally the related cache.")
parser.add_argument(
    "--clear",
    action="store_true",
    default=False,
    required=False,
    help="Clear any cache files associated with the existing lock files.",
)
parser.add_argument(
    "--all",
    action="store_true",
    default=False,
    required=False,
    help="Clear all caches, including images, schedules, and vm_resources",
)
parser.add_argument("stores", nargs="*", default=None)

cmd_args = parser.parse_args()

if not (cmd_args.all or cmd_args.stores):
    raise Exception("A list of caches must be provided or the --all flag must be set.")

valid_stores = {"images", "schedules", "vm_resources"}
if cmd_args.all:
    stores = valid_stores
else:
    stores = {store.lower() for store in cmd_args.stores}
    if not stores.issubset(valid_stores):
        print(f"The only valid caches are {valid_stores}")
        sys.exit(1)


locks = []
for cache_type in stores:
    cache = Path(os.path.join(config["minimega"]["files_dir"], cache_type))

    if not cache.exists():
        print(f"The {cache} cache doesn't exist.")
        continue

    for f in cache.iterdir():
        if f.name.endswith("-lock"):
            locks.append(f.resolve())

    for lock in locks:
        # Clear the associated file as well
        if cmd_args.clear:
            cache_file = Path(str(lock)[:-5])
            if cache_type == "images":
                compressed_image = Path(str(cache_file) + ".xz")
                if compressed_image.exists():
                    compressed_image.unlink()
            if cache_file.exists():
                cache_file.unlink()
        # Clear the lock file
        if lock.exists():
            lock.rmdir()
DONE
