def exit_if_same_taskId(taskId):
    global backup_logger, hutil, para_parser
    trans_report_msg = None
    taskIdentity = TaskIdentity()
    last_taskId = taskIdentity.stored_identity()
    if (taskId == last_taskId):
        backup_logger.log(
            "TaskId is same as last, so skip with Processed Status, current:" +
            str(taskId) + "== last:" + str(last_taskId), True)
        status = CommonVariables.status_success
        hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.
                              SuccessAlreadyProcessedInput)
        status_code = CommonVariables.SuccessAlreadyProcessedInput
        message = 'TaskId AlreadyProcessed nothing to do'
        try:
            if (para_parser is not None):
                blob_report_msg, file_report_msg = hutil.do_status_report(operation='Enable',status=status,\
                        status_code=str(status_code),\
                        message=message,\
                        taskId=taskId,\
                        commandStartTimeUTCTicks=para_parser.commandStartTimeUTCTicks,\
                        snapshot_info=None)
                status_report_to_file(file_report_msg)
        except Exception as e:
            err_msg = 'cannot write status to the status file, Exception %s, stack trace: %s' % (
                str(e), traceback.format_exc())
            backup_logger.log(err_msg, True, 'Warning')
        sys.exit(0)
def exit_if_same_taskId(taskId):  
    global backup_logger  
    taskIdentity = TaskIdentity()  
    last_taskId = taskIdentity.stored_identity()  
    if(taskId == last_taskId):  
        backup_logger.log("TaskId is same as last, so skip, current:" + str(taskId) + "== last:" + str(last_taskId), True)  
        sys.exit(0)  
def exit_if_same_taskId(taskId):  
    global backup_logger  
    taskIdentity = TaskIdentity()  
    last_taskId = taskIdentity.stored_identity()  
    if(taskId == last_taskId):  
        backup_logger.log("TaskId is same as last, so skip, current:" + str(taskId) + "== last:" + str(last_taskId), True)  
        sys.exit(0)  
Exemple #4
0
def exit_if_same_taskId(taskId):
    global backup_logger, hutil, para_parser
    trans_report_msg = None
    taskIdentity = TaskIdentity()
    last_taskId = taskIdentity.stored_identity()
    if (taskId == last_taskId):
        backup_logger.log(
            "TaskId is same as last, so skip with Processed Status, current:" +
            str(taskId) + "== last:" + str(last_taskId), True)
        status = CommonVariables.status_success
        hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.
                              SuccessAlreadyProcessedInput)
        status_code = CommonVariables.SuccessAlreadyProcessedInput
        message = 'TaskId AlreadyProcessed nothing to do'
        backup_logger.log(message, True)
        sys.exit(0)
def exit_if_same_taskId(taskId):
    global backup_logger,hutil,para_parser
    trans_report_msg = None
    taskIdentity = TaskIdentity()
    last_taskId = taskIdentity.stored_identity()
    if(taskId == last_taskId):
        backup_logger.log("TaskId is same as last, so skip with Processed Status, current:" + str(taskId) + "== last:" + str(last_taskId), True)
        status=CommonVariables.status_success 
        hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.SuccessAlreadyProcessedInput)
        status_code=CommonVariables.SuccessAlreadyProcessedInput
        message='TaskId AlreadyProcessed nothing to do'
        try:
            if(para_parser is not None):
                blob_report_msg, file_report_msg = hutil.do_status_report(operation='Enable',status=status,\
                        status_code=str(status_code),\
                        message=message,\
                        taskId=taskId,\
                        commandStartTimeUTCTicks=para_parser.commandStartTimeUTCTicks,\
                        snapshot_info=None)
                status_report_to_file(file_report_msg)
        except Exception as e:
            err_msg='cannot write status to the status file, Exception %s, stack trace: %s' % (str(e), traceback.format_exc())
            backup_logger.log(err_msg, True, 'Warning')
        sys.exit(0)
def enable():
    freezer = FsFreezer(backup_logger)
    unfreeze_result = None
    snapshot_result = None
    freeze_result = None
    global_error_result = None
    para_parser = None
    run_result = 1
    error_msg = ''
    run_status = None
    # precheck
    freeze_called = False
    try:
        hutil.do_parse_context('Enable')

        # we need to freeze the file system first
        backup_logger.log('starting to enable', True)

        """
        protectedSettings is the privateConfig passed from Powershell.
        WATCHOUT that, the _context_config are using the most freshest timestamp.
        if the time sync is alive, this should be right.
        """
        protected_settings = hutil._context._config['runtimeSettings'][0]['handlerSettings'].get('protectedSettings')
        public_settings = hutil._context._config['runtimeSettings'][0]['handlerSettings'].get('publicSettings')
        para_parser = ParameterParser(protected_settings, public_settings)
        commandStartTime = datetime.datetime(1, 1, 1) + datetime.timedelta(microseconds = para_parser.commandStartTimeUTCTicks / 10)
        
        utcNow = datetime.datetime.utcnow()
        backup_logger.log('command start time is ' + str(commandStartTime) + " and utcNow is " + str(utcNow))
        timespan = utcNow - commandStartTime
        TWENTY_MINUTES = 20 * 60
        taskIdentity = TaskIdentity()
        currentTaskIdentity = taskIdentity.stored_identity()
        # handle the machine identity for the restoration scenario.
        backup_logger.log('timespan is '+str(timespan))
        if(abs(timespan.total_seconds()) > TWENTY_MINUTES):
            error_msg = 'the call time stamp is out of date.'
            exit_with_commit_log(error_msg,para_parser)

        elif(para_parser.taskId == currentTaskIdentity):
            error_msg = 'the task id is handled.'
            exit_with_commit_log(error_msg,para_parser)
        else:
            taskIdentity.save_identity(para_parser.taskId)
            commandToExecute = para_parser.commandToExecute
            #validate all the required parameter here
            if(commandToExecute.lower() == CommonVariables.iaas_install_command):
                backup_logger.log("install succeed.",True)
                run_status = 'success'
                error_msg = 'Install Succeeded'
                run_result = CommonVariables.success
                backup_logger.log(error_msg)
            elif(commandToExecute.lower() == CommonVariables.iaas_vmbackup_command):
                if(para_parser.backup_metadata is None or para_parser.public_config_obj is None or para_parser.private_config_obj is None):
                    run_result = CommonVariables.parameter_error
                    run_status = 'error'
                    error_msg = 'required field empty or not correct'
                    backup_logger.log(error_msg, False, 'Error')
                else:
                    backup_logger.log('commandToExecute is ' + commandToExecute, True)
                    """
                    make sure the log is not doing when the file system is freezed.
                    """
                    backup_logger.log("doing freeze now...", True)
                    freeze_called = True
                    freeze_result = freezer.freezeall()
                    backup_logger.log("freeze result " + str(freeze_result))
                    
                    # check whether we freeze succeed first?
                    if(freeze_result is not None and len(freeze_result.errors) > 0):
                        run_result = CommonVariables.error
                        run_status = 'error'
                        error_msg = 'Enable failed with error' + str(freeze_result)
                        backup_logger.log(error_msg, False, 'Warning')
                    else:
                        backup_logger.log("doing snapshot now...")
                        snap_shotter = Snapshotter(backup_logger)
                        snapshot_result = snap_shotter.snapshotall(para_parser)
                        backup_logger.log("snapshotall ends...")
                        if(snapshot_result is not None and len(snapshot_result.errors) > 0):
                            error_msg = "snapshot result: " + str(snapshot_result)
                            run_result = CommonVariables.error
                            run_status = 'error'
                            backup_logger.log(error_msg, False, 'Error')
                        else:
                            run_result = CommonVariables.success
                            run_status = 'success'
                            error_msg = 'Enable Succeeded'
                            backup_logger.log(error_msg)
            else:
                run_status = 'error'
                run_result = CommonVariables.parameter_error
                error_msg = 'command is not correct'
                backup_logger.log(error_msg, False, 'Error')
    except Exception as e:
        errMsg = "Failed to enable the extension with error: %s, stack trace: %s" % (str(e), traceback.format_exc())
        backup_logger.log(errMsg, False, 'Error')
        global_error_result = e
    finally:
        backup_logger.log("doing unfreeze now...")
        if(freeze_called):
            unfreeze_result = freezer.unfreezeall()
            backup_logger.log("unfreeze result " + str(unfreeze_result))
            error_msg += ('Enable Succeeded with error: ' + str(unfreeze_result.errors))
            if(unfreeze_result is not None and len(unfreeze_result.errors) > 0):
                backup_logger.log(error_msg, False, 'Warning')
            backup_logger.log("unfreeze ends...")

    if(para_parser is not None):
        backup_logger.commit(para_parser.logsBlobUri)
    """
    we do the final report here to get rid of the complex logic to handle the logging when file system be freezed issue.
    """
    if(global_error_result is not None):
        if(hasattr(global_error_result,'errno') and global_error_result.errno == 2):
            run_result = CommonVariables.error_12
        elif(para_parser is None):
            run_result = CommonVariables.parameter_error
        else:
            run_result = CommonVariables.error
        run_status = 'error'
        error_msg  += ('Enable failed.' + str(global_error_result))
    print("para_parser is "+str(para_parser))

    do_status_report(operation='Enable',status = run_status,status_code=str(run_result),message=error_msg,taskId=para_parser.taskId,commandStartTimeUTCTicks=para_parser.commandStartTimeUTCTicks,blobUri=para_parser.statusBlobUri)

    hutil.do_exit(0, 'Enable', run_status, str(run_result), error_msg)