#!/opt/vmware/bin/python

#
# Copyright 2015-2016 VMware, Inc.  All rights reserved. -- VMware Confidential
#

import os
import sys
import logging

sys.path.append(os.environ['VMWARE_PYTHON_PATH'])
from cis.utils import StartType, get_deployment_nodetype
from cis.exceptions import (SetServiceStartTypeException,
                            ServiceNotFoundException,
                            CISVariableNotDefined)
from cis.svcsController import update_services_startuptype


def main():
    try:
        nodeType = get_deployment_nodetype()
        if nodeType != 'management':
            logging.info('Skipping restore workflow.')
            return 0
        # Change the startup type for all installed services to MANUAL
        update_services_startuptype('stop', StartType.DISABLED, False,
                                    False, False, svc_names=['vmware-vmon'])
    except CISVariableNotDefined as e:
        logging.error('Unable to get one or more environment variables. '
                      'Exception:\n%s' % e)
        raise
    except (SetServiceStartTypeException, ServiceNotFoundException) as e:
        logging.error('Unable to set start type to MANUAL for vmware-vmon. '
                      'Exception: %s' % e)
        raise

    logging.info('Service vmware-vmon startup type successfully changed to '
                 'MANUAL. Please run restore script to complete the restore.')
    return 0


if __name__ == '__main__':
    try:
        sys.exit(main())
    except Exception:
        os.system('shutdown now')
