def main()

in OmsAgent/omsagent.py [0:0]


def main():
    """
    Main method
    Parse out operation from argument, invoke the operation, and finish.
    """
    init_waagent_logger()
    waagent_log_info('OmsAgentForLinux started to handle.')
    global IsUpgrade

    # Determine the operation being executed
    operation = None
    try:
        option = sys.argv[1]
        if re.match('^([-/]*)(disable)', option):
            operation = 'Disable'
        elif re.match('^([-/]*)(uninstall)', option):
            operation = 'Uninstall'
        elif re.match('^([-/]*)(install)', option):
            operation = 'Install'
        elif re.match('^([-/]*)(enable)', option):
            operation = 'Enable'
        elif re.match('^([-/]*)(update)', option):
            operation = 'Update'
            IsUpgrade = True
        elif re.match('^([-/]*)(telemetry)', option):
            operation = 'Telemetry'
    except Exception as e:
        waagent_log_error(str(e))

    if operation is None:
        log_and_exit('Unknown', 1, 'No valid operation provided')

    # Set up for exit code and any error messages
    exit_code = 0
    message = '{0} succeeded'.format(operation)

    # Clean status file to mitigate diskspace issues on small VMs
    status_files = [
            "/var/opt/microsoft/omsconfig/status/dscperformconsistency",
            "/var/opt/microsoft/omsconfig/status/dscperforminventory",
            "/var/opt/microsoft/omsconfig/status/dscsetlcm",
            "/var/opt/microsoft/omsconfig/status/omsconfighost"
        ]
    for sf in status_files:
        if os.path.isfile(sf):
            if sf.startswith("/var/opt/microsoft/omsconfig/status"):
                try:
                    os.remove(sf)
                except Exception as e:
                    hutil_log_info('Error removing telemetry status file before installation: {0}'.format(sf))
                    hutil_log_info('Exception info: {0}'.format(traceback.format_exc()))

    exit_code = check_disk_space_availability()
    if exit_code != 0:
        message = '{0} failed due to low disk space'.format(operation)
        log_and_exit(operation, exit_code, message)

    exit_if_gpg_unavailable(operation)

    # Invoke operation
    try:
        global HUtilObject
        HUtilObject = parse_context(operation)

        # Verify shell bundle signing
        try:
            hutil_log_info("Start signing verification")
            verifyShellBundleSigningAndChecksum()
            hutil_log_info("ShellBundle signing verification succeeded")
        except Exception as ex:
            errmsg = "ShellBundle signing verification failed with '%s'" % ex.message
            if ProceedOnSigningVerificationFailure:
                hutil_log_error(errmsg)
            else:
                log_and_exit(operation, errmsg)

        # invoke operation
        exit_code, output = operations[operation]()

        # Exit code 1 indicates a general problem that doesn't have a more
        # specific error code; it often indicates a missing dependency
        if exit_code == 1 and operation == 'Install':
            message = 'Install failed with exit code 1. Please check that ' \
                      'dependencies are installed. For details, check logs ' \
                      'in /var/log/azure/Microsoft.EnterpriseCloud.' \
                      'Monitoring.OmsAgentForLinux'
        elif exit_code == 127 and operation == 'Install':
            # happens if shell bundle couldn't be extracted due to low space or missing dependency
            exit_code = 52 # since it is a missing dependency
            message = 'Install failed with exit code 127. Please check that ' \
                      'dependencies are installed. For details, check logs ' \
                      'in /var/log/azure/Microsoft.EnterpriseCloud.' \
                      'Monitoring.OmsAgentForLinux'
        elif exit_code is DPKGLockedErrorCode and operation == 'Install':
            message = 'Install failed with exit code {0} because the ' \
                      'package manager on the VM is currently locked: ' \
                      'please wait and try again'.format(DPKGLockedErrorCode)
        elif exit_code != 0:
            message = '{0} failed with exit code {1} {2}'.format(operation,
                                                             exit_code, output)
    except OmsAgentForLinuxException as e:
        exit_code = e.error_code
        message = e.get_error_message(operation)
    except Exception as e:
        exit_code = 1
        message = '{0} failed with error: {1}\n' \
                  'Stacktrace: {2}'.format(operation, e,
                                           traceback.format_exc())

    # Finish up and log messages
    log_and_exit(operation, exit_code, message)