#
# Copyright 2017 VMware, Inc.  All rights reserved. -- VMware Confidential
#
"""PatchRunner integration hook for Hybrid vCenter Service

This module integrates Hybrid vCenter Service (HVC) patching scripts with the
PatchRunner Framework.

"""
__author__ = 'VMware, Inc.'
__copyright__ = 'Copyright 2017 VMware, Inc. All rights reserved.'

import logging
import os
import sys

from extensions import extend, Hook
from l10n import msgMetadata as _T, localizedString as _
from patch_specs import ValidationResult, Question, Mismatch, Requirements, \
    PatchInfo, RequirementsResult
from reporting import getProgressReporter
import vcsa_utils

sys.path.append(os.path.join(os.path.dirname(__file__), "patches"))
patches = [
    # In order to update from version 0 to version 1, execute patch_01.py
    ("1","patch_01"),
    ("2","patch_02"),
]

COMPONENT_NAME = "hvc"

logger = logging.getLogger(__name__)


@extend(Hook.Discovery)
def discover(patch_context):
    logger.info("Executing Discovery Hook for Hybric vCenter Service")

    if not is_hvc_enabled():
        logger.info("hvc.patch: HVC not enabled on source build")
        return None

    if not is_patch_needed():
        logger.info("hvc.patch: Patch version up-to-date.")
        return None
    return vcsa_utils.getComponentDiscoveryResult(COMPONENT_NAME,
                                                  displayName=_(
                                                   _T("hvc.displayName",
                                                   "Hybrid vCenter Service")))


@extend(Hook.Patch)
def execute_patch(patch_context):
    #This hook is invoked after the rpms are installed.
    logger.info("Executing Patch Hook for Hybrid vCenter Service")
    progressReporter = getProgressReporter()
    progressReporter.updateProgress(0, _(_T("hvc.patch.begin",
                                            "Start hvc patching")))
    _do_incremental_patching(patch_context)
    progressReporter.updateProgress(100, _(_T("hvc.patch.complete",
                                              "patching hvc completed")))


def _do_incremental_patching(ctx):
    for ver, modulePath in patches:
        logger.info("Checking if need patch %s on version %s"
                     % (ver, modulePath))
        mod = __import__(modulePath)

        if mod.is_patch_needed():
            logger.info("Patch %s needed" % (modulePath))
            mod.doPatching(ctx)
            logger.info('Patch %s applied' % (modulePath))


def is_patch_needed():
    for ver, modulePath in patches:
        logger.info("Checking if need patch %s on version %s"
                     % (ver, modulePath))
        mod = __import__(modulePath)
        if hasattr(mod, 'is_patch_needed'):
            if mod.is_patch_needed():
                return True
    return False


def is_hvc_enabled():
    try:
        sys.path.append(os.environ['VMWARE_PYTHON_PATH'])
        import featureState
        featureState.init()
        if hasattr(featureState, 'getHybridVC'):
            return (featureState.getHybridVC() and
                   os.path.exists('/usr/lib/vmware-hvc'))
    except Exception as e:
        logger.warning('Failed to getHybridVC: %s' % str(e))
        if os.path.exists('/usr/lib/vmware-hvc'):
            logger.info('hvc lib exists, so assume source enabled')
            return True
    return False
