
from __future__ import absolute_import
from __future__ import division
import argparse
import logging
import logging.config
import os

from vdsm.common import fileutils

from ..constants import P_VDSM_LIB


def _get_upgrade_log():
    return logging.getLogger('upgrade')


def _upgrade_seal_path(upgrade):
    return os.path.join(P_VDSM_LIB, 'upgrade', upgrade.name)


def _upgrade_needed(upgrade):
    return not os.path.exists(_upgrade_seal_path(upgrade))


def _upgrade_seal(upgrade):
    seal_file = _upgrade_seal_path(upgrade)
    try:
        fileutils.touch_file(seal_file)
    except (OSError, IOError):
        _get_upgrade_log().exception("Failed to seal upgrade %s", upgrade.name)
    else:
        _get_upgrade_log().debug("Upgrade %s successfully performed",
                                 upgrade.name)


def apply_upgrade(upgrade, *args):
    argparser = argparse.ArgumentParser('vdsm-tool %s' % args[0])
    argparser.add_argument(
        '--run-again',
        dest='runAgain',
        default=False,
        action='store_true',
        help='Run the upgrade again, even if it was ran before'
    )
    if hasattr(upgrade, 'extendArgParser'):
        upgrade.extendArgParser(argparser)
    ns, args = argparser.parse_known_args(args[1:])
    if (_upgrade_needed(upgrade) or ns.runAgain):
        _get_upgrade_log().debug("Running upgrade %s", upgrade.name)
        try:
            upgrade.run(ns, args)
        except Exception:
            _get_upgrade_log().exception("Failed to run %s", upgrade.name)
            return 1
        else:
            _upgrade_seal(upgrade)
    return 0
