'''
VCDB b2b entry point
'''
import sys
import os
import json
import platform
import logging

from patch_specs import DiscoveryResult, PatchInfo # pylint: disable=E0401

from extensions import extend, Hook # pylint: disable=E0401
from l10n import msgMetadata as _T, localizedString as _ # pylint: disable=E0401
import vcsa_utils # pylint: disable=E0401
from reporting import getProgressReporter # pylint: disable=E0401

from vcdb.vcdb_db_utils import (get_db_user_linux, get_db_pass_symkey, patch_db)

LOGGER = logging.getLogger(__name__)

@extend(Hook.Discovery)
def discover(ctx): # pylint: disable=W0613
   '''DiscoveryResult discover(PatchContext ctx) throw UserError'''
   discovery_result = vcsa_utils.getComponentDiscoveryResult(
      "dbconfig",
      displayName=_(_T("VCDB.displayName", "VMware vCenter Server Database")))
   discovery_result.dependentComponents.append('vcdb_vmodl_patcher')
   return discovery_result

def _do_vcdb_patching():
   LOGGER.info("Retrieving DB user...")
   vc_user = get_db_user_linux()
   LOGGER.info("DB user retrieved: %s", vc_user)

   LOGGER.info("Retrieving DB password...")
   vc_pass = get_db_pass_symkey()
   LOGGER.info("DB password retrieved: ****")

   source_vc_version = "650"
   LOGGER.info("Source version: %s", source_vc_version)

   LOGGER.info("Execute DB patch...")
   patch_db(vc_user, vc_pass, source_vc_version)
   LOGGER.info("DB is patched.")




@extend(Hook.Patch)
def patch(ctx): # pylint: disable=W0613
   '''void patch(PatchContext ctx) throw UserError'''
   reporter = getProgressReporter()
   reporter.updateProgress(0, _(_T("vcdb.patch.begin",
                                   'Start VCDB  patching')))
   _do_vcdb_patching()
   reporter.updateProgress(100, _(_T("vcdb.patch.complete", 'patching VCDB completed')))
