
from __future__ import absolute_import
import argparse

from vdsm import utils
from vdsm.storage import xlease
from . import expose


@expose('format-xleases')
def format_xleases(*args):
    args = parse_args(args)
    backend = xlease.DirectFile(args.path)
    with utils.closing(backend):
        xlease.format_index(args.sd_id, backend)


@expose('rebuild-xleases')
def rebuild_xleases(*args):
    args = parse_args(args)
    backend = xlease.DirectFile(args.path)
    with utils.closing(backend):
        xlease.rebuild_index(args.sd_id, backend)


def parse_args(args):
    parser = argparse.ArgumentParser()
    parser.add_argument('sd_id', help="storage domain UUID")
    parser.add_argument('path', help="path to xleases volume")
    return parser.parse_args(args[1:])
