"""
Copyright 2017 VMware, Inc.  All rights reserved.
-- VMware Confidential
"""

'''
This file provides vSAN DPS hooks which will be invoked during VCSA B2B patching process.
'''

# Setup python path for DPS modules
import sys
import os
sys.path.append(os.path.dirname(__file__))

import logging
from extensions import extend, Hook
import vcsa_utils
from fss_utils import getTargetFSS
from patch_util import do_patching

logger = logging.getLogger(__name__)


@extend(Hook.Discovery)
def discover(ctx):
   """VCSA patching process discovers vsan-dps service using 'Discovery' hook.
      Patch upgrade is skipped if DPS directory does not exist.

      @param ctx: Patch Context provided by the PatchRunner
   """
   # Don't run patching if VSAN_Dps is not enabled on target build
   if not getTargetFSS("VSAN_Dps"):
      logger.info("VSAN_Dps not enabled on target build. Patch not applicable.")
      return None

   logger.info("Discovering vSAN Data Protection Service.")
   return vcsa_utils.getComponentDiscoveryResult('vsan-dps')


@extend(Hook.Patch)
def patch(ctx):
   """VCSA patching process performs specific patch workflow for
      the discovered service using 'Patch' hook.
      Note: This hook is invoked after the rpms are installed.

      @param ctx: Patch Context provided by the PatchRunner
   """

   logger.info("Patching vSAN Data Protection service.")

   # Perform DPS B2B patching
   do_patching()
