Beispiel #1
0
def snapshot():
    try:
        global backup_logger, run_result, run_status, error_msg, freezer, freeze_result, snapshot_result, snapshot_done, para_parser
        freeze_result = freezer.freezeall()
        backup_logger.log('T:S freeze result ' + str(freeze_result))
        if (freeze_result is not None and len(freeze_result.errors) > 0):
            run_result = CommonVariables.error
            run_status = 'error'
            error_msg = 'T:S Enable failed with error: ' + str(freeze_result)
            backup_logger.log(error_msg, False, 'Warning')
        else:
            backup_logger.log('T:S doing snapshot now...')
            snap_shotter = Snapshotter(backup_logger)
            snapshot_result = snap_shotter.snapshotall(para_parser)
            backup_logger.log('T:S snapshotall ends...')
            if (snapshot_result is not None
                    and len(snapshot_result.errors) > 0):
                error_msg = 'T:S 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("T:S " + error_msg)
    except Exception as e:
        errMsg = 'Failed to do the snapshot with error: %s, stack trace: %s' % (
            str(e), traceback.format_exc())
        backup_logger.log(errMsg, False, 'Error')
    snapshot_done = True
def snapshot(): 
    try: 
        global backup_logger,run_result,run_status,error_msg,freezer,freeze_result,snapshot_result,snapshot_done,para_parser 
        freeze_result = freezer.freezeall() 
        backup_logger.log('T:S freeze result ' + str(freeze_result)) 
        if(freeze_result is not None and len(freeze_result.errors) > 0): 
            run_result = CommonVariables.error 
            run_status = 'error' 
            error_msg = 'T:S Enable failed with error: ' + str(freeze_result) 
            backup_logger.log(error_msg, False, 'Warning') 
        else: 
            backup_logger.log('T:S doing snapshot now...') 
            snap_shotter = Snapshotter(backup_logger) 
            snapshot_result = snap_shotter.snapshotall(para_parser) 
            backup_logger.log('T:S snapshotall ends...') 
            if(snapshot_result is not None and len(snapshot_result.errors) > 0): 
                error_msg = 'T:S 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("T:S " + error_msg) 
    except Exception as e: 
        errMsg = 'Failed to do the snapshot with error: %s, stack trace: %s' % (str(e), traceback.format_exc()) 
        backup_logger.log(errMsg, False, 'Error') 
    snapshot_done = True 
def freeze_snapshot(timeout):
    try:
        global hutil,backup_logger,run_result,run_status,error_msg,freezer,freeze_result,para_parser,snapshot_info_array
        freeze_result = freezer.freeze_safe(timeout)
        all_failed= False
        is_inconsistent_freeze = False
        is_inconsistent_snapshot =  False
        backup_logger.log('T:S freeze result ' + str(freeze_result))
        if(freeze_result is not None and len(freeze_result.errors) > 0):
            run_result = CommonVariables.error
            run_status = 'error'
            error_msg = 'T:S Enable failed with error: ' + str(freeze_result)
            hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.FailedRetryableFsFreezeFailed)
            error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(hutil.ExtErrorCode)
            backup_logger.log(error_msg, True, 'Warning')
        else:
            backup_logger.log('T:S doing snapshot now...')
            snap_shotter = Snapshotter(backup_logger)
            snapshot_result,snapshot_info_array, all_failed, is_inconsistent_snapshot = snap_shotter.snapshotall(para_parser)
            backup_logger.log('T:S snapshotall ends...')
            if(snapshot_result is not None and len(snapshot_result.errors) > 0):
                error_msg = 'T:S snapshot result: ' + str(snapshot_result)
                run_result = CommonVariables.FailedRetryableSnapshotFailedNoNetwork
                if all_failed:
                    hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.FailedRetryableSnapshotFailedNoNetwork)
                    error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(hutil.ExtErrorCode)
                else:
                    hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.FailedRetryableSnapshotFailedRestrictedNetwork)
                    error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(hutil.ExtErrorCode)
                run_status = 'error'
                backup_logger.log(error_msg, True, 'Error')
                thaw_result, is_inconsistent_freeze = freezer.thaw_safe()
                if is_inconsistent_freeze and is_inconsistent_snapshot:
                    set_do_seq_flag()
                backup_logger.log('T:S thaw result ' + str(thaw_result))
            else:
                thaw_result, is_inconsistent_freeze = freezer.thaw_safe()
                if is_inconsistent_freeze and is_inconsistent_snapshot:
                    set_do_seq_flag()
                backup_logger.log('T:S thaw result ' + str(thaw_result))
                if(thaw_result is not None and len(thaw_result.errors) > 0):
                    run_result = CommonVariables.error
                    run_status = 'error'
                    error_msg = 'T:S Enable failed with error: ' + str(thaw_result)
                    backup_logger.log(error_msg, True, 'Warning')
                else:   
                    run_result = CommonVariables.success
                    run_status = 'success'
                    error_msg = 'Enable Succeeded'
                    backup_logger.log("T:S " + error_msg, True)
    except Exception as e:
        errMsg = 'Failed to do the snapshot with error: %s, stack trace: %s' % (str(e), traceback.format_exc())
        backup_logger.log(errMsg, True, 'Error')
        run_result = CommonVariables.error
        run_status = 'error'
        error_msg = 'Enable failed with exception in freeze or snapshot ' 
def test_snapshotter(
    nydus_anchor: NydusAnchor,
    rafs_conf: RafsConf,
    image_url,
    nydus_snapshotter,
    local_registry,
):
    snapshotter = Snapshotter(nydus_anchor)
    containerd = Containerd(nydus_anchor, snapshotter).gen_config()
    snapshotter.set_root(containerd.root)

    nydus_anchor.put_dustbin(snapshotter)
    nydus_anchor.put_dustbin(containerd)

    converter = Nydusify(nydus_anchor)
    converter.docker_v2().convert(image_url)

    rafs_conf.set_rafs_backend(Backend.REGISTRY, repo=converter.original_repo)
    rafs_conf.enable_xattr()
    rafs_conf.dump_rafs_conf()

    snapshotter.run(rafs_conf.path())
    time.sleep(1)
    containerd.run()

    cri = Cri(containerd.address, containerd.address)
    container_name = str(uuid.uuid4())
    cri.run_container(converter.converted_image, container_name)
    id, status = cri.check_container_status(container_name, timeout=30)
    assert id is not None
    assert status
    cri.stop_rm_container(id)
    cri.remove_image(converter.converted_image)
    containerd.remove_image_sync(converter.converted_image)
def test_snapshotter_public_converted_images(
    nydus_anchor: NydusAnchor,
    rafs_conf: RafsConf,
    converted_images,
    nydus_snapshotter,
):
    snapshotter = Snapshotter(nydus_anchor)
    containerd = Containerd(nydus_anchor, snapshotter).gen_config()
    snapshotter.set_root(containerd.root)

    nydus_anchor.put_dustbin(snapshotter)
    nydus_anchor.put_dustbin(containerd)

    # We can safely pass the step provide repo configured into the rafs configuration file.
    rafs_conf.set_rafs_backend(
        Backend.REGISTRY,
        scheme="https").enable_fs_prefetch().enable_rafs_blobcache(
            work_dir=snapshotter.cache_dir())
    rafs_conf.enable_xattr().dump_rafs_conf()

    snapshotter.run(rafs_conf.path())
    time.sleep(1)
    containerd.run()
    cri = Cri(containerd.address, containerd.address)

    id_set = []
    for ref in converted_images:
        container_name = str(uuid.uuid4())
        cri.run_container(ref, container_name)
        id, status = cri.check_container_status(container_name, timeout=30)
        assert id is not None
        assert status
        id_set.append((id, ref))
        time.sleep(2)

    for id, ref in id_set:
        cri.stop_rm_container(id)
        cri.remove_image(ref)
        containerd.remove_image_sync(ref)

    snapshotter.shutdown()
    containerd.shutdown()
def test_snapshotter_converted_images(
    nydus_anchor: NydusAnchor,
    rafs_conf: RafsConf,
    converted_images,
    nydus_snapshotter,
):
    # snapshotter = Snapshotter(nydus_anchor).enable_nydus_overlayfs()
    snapshotter = Snapshotter(nydus_anchor)
    containerd = Containerd(nydus_anchor, snapshotter).gen_config()
    snapshotter.set_root(containerd.root)

    nydus_anchor.put_dustbin(snapshotter)
    nydus_anchor.put_dustbin(containerd)

    # We can safely pass the step provide repo configured into the rafs configuration file.
    rafs_conf.set_rafs_backend(Backend.REGISTRY, scheme="https")
    rafs_conf.enable_xattr()
    rafs_conf.dump_rafs_conf()

    snapshotter.run(rafs_conf.path())
    time.sleep(1)
    containerd.run()
    cri = Cri(containerd.address, containerd.address)

    id_set = []
    for ref in converted_images:
        container_name = str(uuid.uuid4())
        cri.run_container(ref, container_name)
        id, status = cri.check_container_status(container_name, timeout=30)
        assert id is not None
        assert status
        id_set.append((id, ref))
        time.sleep(2)

    for id, ref in id_set:
        cri.stop_rm_container(id)
        cri.remove_image(ref)
        containerd.remove_image_sync(ref)

    # TODO: Rafs won't be unmounted and and nydusd still be alive even image is removed locally
    # So kill all nydusd here to make following test verification pass. Is this a bug?

    # Ensure nydusd must have been stopped here
    time.sleep(3)
def enable():
    #this is using the most recent file timestamp.
    hutil.do_parse_context('Enable')

    freezer = FsFreezer(patching= MyPatching, logger = 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:
        # we need to freeze the file system first
        backup_logger.log('starting to enable', True)

        # handle the restoring scenario.
        mi = MachineIdentity()
        stored_identity = mi.stored_identity()
        if(stored_identity is None):
            mi.save_identity()
        else:
            current_identity = mi.current_identity()
            if(current_identity != stored_identity):
                current_seq_no = -1
                backup_logger.log("machine identity not same, set current_seq_no to " + str(current_seq_no) + " " + str(stored_identity) + " " + str(current_identity), True)
                hutil.set_last_seq(current_seq_no)
                mi.save_identity()

        hutil.exit_if_same_seq()
        hutil.save_seq()

        """
        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)

        if(para_parser.commandStartTimeUTCTicks is not None and para_parser.commandStartTimeUTCTicks != ""):
            utcTicksLong = long(para_parser.commandStartTimeUTCTicks)
            commandStartTime = convert_time(utcTicksLong)
            utcNow = datetime.datetime.utcnow()
            backup_logger.log('command start time is ' + str(commandStartTime) + " and utcNow is " + str(utcNow))
            timespan = utcNow - commandStartTime
            THIRTY_MINUTES = 30 * 60 # in seconds
            # handle the machine identity for the restoration scenario.
            total_span_in_seconds = timedelta_total_seconds(timespan)
            backup_logger.log('timespan is ' + str(timespan) + ' ' + str(total_span_in_seconds))
            if(abs(total_span_in_seconds) > THIRTY_MINUTES):
                error_msg = 'the call time stamp is out of date. so skip it.'
                exit_with_commit_log(error_msg, para_parser)

        if(para_parser.taskId is not None and para_parser.taskId != ""):
            taskIdentity = TaskIdentity()
            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.error_parameter
                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.error_parameter
            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:
        if(freeze_called):
            unfreeze_result = freezer.unfreezeall()
            backup_logger.log('unfreeze result ' + str(unfreeze_result))
            if(unfreeze_result is not None and len(unfreeze_result.errors) > 0):
                error_msg += ('Enable Succeeded with error: ' + str(unfreeze_result.errors))
                backup_logger.log(error_msg, False, 'Warning')
            backup_logger.log('unfreeze ends...')

    """
    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.error_parameter
        else:
            run_result = CommonVariables.error
        run_status = 'error'
        error_msg  += ('Enable failed.' + str(global_error_result))

    if(para_parser is not None and para_parser.statusBlobUri is not None and para_parser.statusBlobUri != ""):
        status_report_msg = do_backup_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)
    if(status_report_msg is not None):
        backup_logger.log("status report message:")
        backup_logger.log(status_report_msg)
    else:
        backup_logger.log("status_report_msg is none")
    if(para_parser is not None and para_parser.logsBlobUri is not None and para_parser.logsBlobUri != ""):
        backup_logger.commit(para_parser.logsBlobUri)
    else:
        backup_logger.log("the logs blob uri is not there, so do not upload log.")
        backup_logger.commit_to_local()
    

    hutil.do_exit(0, 'Enable', run_status, str(run_result), error_msg)
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           = None
    run_status          = None
    # precheck 
    try:
        hutil.do_parse_context('Enable')
        hutil.exit_if_enabled()
        # we need to freeze the file system first
        backup_logger.log('starting to enable', True)
        """
        protectedSettings is the privateConfig passed from Powershell.
        """
        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)

        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 = 0
            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 = 11
                run_status = 'error'
                error_msg  = 'required field empty or not correct'
            else:
                backup_logger.log('commandToExecute is ' + commandToExecute, True)
                if(commandToExecute.lower() == CommonVariables.iaas_vmbackup_command):
                    """
                    make sure the log is not doing when the file system is freezed.
                    """
                    backup_logger.log("doing freeze now...", True)
                    freeze_result   = freezer.freezeall()
                    backup_logger.log("freeze result " + str(freeze_result))
                
                    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 != None and len(snapshot_result.errors) > 0):
                        error_msg  = "snapshot result: " + str(snapshot_result.errors)
                        run_result = 2
                        run_status = 'error'
                        backup_logger.log(error_msg, False, 'Error')
                    else:
                        if(freeze_result != None and len(freeze_result.errors) > 0 ):
                            run_result = 1
                            run_status = 'warning'
                            error_msg  = 'Enable Succeeded with error' + str(freeze_result.errors)
                            backup_logger.log(error_msg, False, 'Warning')
                        else:
                            run_result = 1
                            run_status = 'success'
                            error_msg  = 'Enable Succeeded'
                            backup_logger.log(error_msg)

    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...")
        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!= 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 != None):
        if(global_error_result.errno==2):
            run_result = 12
        else:
            run_result = 2
        run_status = 'error'
        error_msg  += ('Enable failed.' + str(global_error_result))

    hutil.do_exit(run_result, 'Enable', run_status, str(run_result), error_msg)
def enable():
    #this is using the most recent file timestamp.
    hutil.do_parse_context('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:
        # we need to freeze the file system first
        backup_logger.log('starting to enable', True)

        # handle the restoring scenario.
        mi = MachineIdentity()
        stored_identity = mi.stored_identity()
        if (stored_identity is None):
            mi.save_identity()
        else:
            current_identity = mi.current_identity()
            if (current_identity != stored_identity):
                current_seq_no = -1
                backup_logger.log(
                    "machine identity not same, set current_seq_no to " +
                    str(current_seq_no) + " " + str(stored_identity) + " " +
                    str(current_identity), True)
                hutil.set_inused_config_seq(current_seq_no)
                mi.save_identity()

        hutil.save_seq()
        """
        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)

        if (para_parser.commandStartTimeUTCTicks is not None
                and para_parser.commandStartTimeUTCTicks != ""):
            utcTicksLong = long(para_parser.commandStartTimeUTCTicks)
            commandStartTime = convert_time(utcTicksLong)
            utcNow = datetime.datetime.utcnow()
            backup_logger.log('command start time is ' +
                              str(commandStartTime) + " and utcNow is " +
                              str(utcNow))
            timespan = utcNow - commandStartTime
            THIRTY_MINUTES = 30 * 60  # in seconds
            # handle the machine identity for the restoration scenario.
            total_span_in_seconds = timespan.days * 24 * 60 * 60 + timespan.seconds
            backup_logger.log('timespan is ' + str(timespan) + ' ' +
                              str(total_span_in_seconds))
            if (abs(total_span_in_seconds) > THIRTY_MINUTES):
                error_msg = 'the call time stamp is out of date. so skip it.'
                exit_with_commit_log(error_msg, para_parser)

        if (para_parser.taskId is not None and para_parser.taskId != ""):
            taskIdentity = TaskIdentity()
            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.error_parameter
                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.error_parameter
            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))
            if (unfreeze_result is not None
                    and len(unfreeze_result.errors) > 0):
                error_msg += ('Enable Succeeded with error: ' +
                              str(unfreeze_result.errors))
                backup_logger.log(error_msg, False, 'Warning')
            backup_logger.log('unfreeze ends...')

    if (para_parser is not None and para_parser.logsBlobUri is not None):
        backup_logger.commit(para_parser.logsBlobUri)
    else:
        backup_logger.commit_to_local()
    """
    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.error_parameter
        else:
            run_result = CommonVariables.error
        run_status = 'error'
        error_msg += ('Enable failed.' + str(global_error_result))

    if (para_parser is not None and para_parser.statusBlobUri is not None):
        do_backup_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)
Beispiel #10
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')

        # handle the restoring scenario.
        mi = MachineIdentity()
        stored_identity = mi.stored_identity()
        if (stored_identity is None):
            mi.save_identity()
            hutil.exit_if_enabled()
        else:
            current_identity = mi.current_identity()
            hutil.log(" current identity " + current_identity)
            if (current_identity != stored_identity):
                current_seq_no = hutil._get_current_seq_no(
                    hutil._context._config_dir)
                backup_logger.log(
                    "machine identity not same, set current_seq_no to " +
                    str(current_seq_no) + " " + str(stored_identity) + " " +
                    str(current_identity), True)
                #remove other .config files.  or the waagent would report the 3
                #status...
                for subdir, dirs, files in os.walk(hutil._context._config_dir):
                    for file in files:
                        try:
                            cur_seq_no = int(
                                os.path.basename(file).split('.')[0])
                            if (cur_seq_no != current_seq_no):
                                os.remove(
                                    join(hutil._context._config_dir, file))
                        except ValueError:
                            continue
                hutil.set_inused_config_seq(current_seq_no)
                mi.save_identity()
            else:
                hutil.exit_if_enabled()

        # we need to freeze the file system first
        backup_logger.log('starting to enable', True)
        """
        protectedSettings is the privateConfig passed from Powershell.
        """
        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)

        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 = 0
            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 = 11
                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 = 2
                    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 = 2
                        run_status = 'error'
                        backup_logger.log(error_msg, False, 'Error')
                    else:
                        run_result = 1
                        run_status = 'success'
                        error_msg = 'Enable Succeeded'
                        backup_logger.log(error_msg)
        else:
            run_status = 'error'
            run_result = 11
            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')
        print(errMsg)
        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 = 12
        elif (para_parser is None):
            run_result = 11
        else:
            run_result = 2
        run_status = 'error'
        error_msg += ('Enable failed.' + str(global_error_result))

    hutil.do_exit(0, 'Enable', run_status, str(run_result), error_msg)
Beispiel #11
0
def freeze_snapshot(timeout):
    try:
        global hutil, backup_logger, run_result, run_status, error_msg, freezer, freeze_result, para_parser, snapshot_info_array, g_fsfreeze_on
        if (get_value_from_configfile('doseq') == '2'):
            set_value_to_configfile('doseq', '1')
        if (get_value_from_configfile('doseq') != '1'):
            set_value_to_configfile('doseq', '2')
        run_result = CommonVariables.success
        run_status = 'success'
        all_failed = False
        is_inconsistent = False
        if g_fsfreeze_on:
            backup_logger.log('doing freeze now...', True)
            time_before_freeze = datetime.datetime.now()
            freeze_result = freezer.freeze_safe(timeout)
            time_after_freeze = datetime.datetime.now()
            HandlerUtil.HandlerUtility.add_to_telemetery_data(
                "FreezeTime",
                str(time_after_freeze - time_before_freeze -
                    datetime.timedelta(seconds=5)))
            backup_logger.log('T:S freeze result ' + str(freeze_result))
            if (freeze_result is not None and len(freeze_result.errors) > 0):
                run_result = CommonVariables.FailedFsFreezeFailed
                run_status = 'error'
                error_msg = 'T:S Enable failed with error: ' + str(
                    freeze_result)
                hutil.SetExtErrorCode(
                    ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.
                    FailedRetryableFsFreezeFailed)
                error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(
                    hutil.ExtErrorCode)
                backup_logger.log(error_msg, True, 'Warning')
                if (get_value_from_configfile('doseq') == '2'):
                    set_value_to_configfile('doseq', '0')
        if run_result == CommonVariables.success:
            snap_shotter = Snapshotter(backup_logger)
            backup_logger.log('T:S doing snapshot now...')
            time_before_snapshot = datetime.datetime.now()
            snapshot_result, snapshot_info_array, all_failed, is_inconsistent, unable_to_sleep = snap_shotter.snapshotall(
                para_parser, freezer, g_fsfreeze_on)
            time_after_snapshot = datetime.datetime.now()
            HandlerUtil.HandlerUtility.add_to_telemetery_data(
                "SnapshotTime",
                str(time_after_snapshot - time_before_snapshot))
            backup_logger.log('T:S snapshotall ends...', True)
            if (get_value_from_configfile('doseq') == '2'):
                set_value_to_configfile('doseq', '0')
            if (snapshot_result is not None
                    and len(snapshot_result.errors) > 0):
                if unable_to_sleep:
                    run_result = CommonVariables.error
                    run_status = 'error'
                    error_msg = 'T:S Enable failed with error: ' + str(
                        snapshot_result)
                    backup_logger.log(error_msg, True, 'Warning')
                elif is_inconsistent == True:
                    set_value_to_configfile('doseq', '1')
                    run_result = CommonVariables.error
                    run_status = 'error'
                    error_msg = 'T:S Enable failed with error: ' + str(
                        snapshot_result)
                    backup_logger.log(error_msg, True, 'Warning')
                else:
                    error_msg = 'T:S snapshot result: ' + str(snapshot_result)
                    run_result = CommonVariables.FailedRetryableSnapshotFailedNoNetwork
                    if all_failed:
                        hutil.SetExtErrorCode(
                            ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.
                            FailedRetryableSnapshotFailedNoNetwork)
                        error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(
                            hutil.ExtErrorCode)
                    else:
                        hutil.SetExtErrorCode(
                            ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.
                            FailedRetryableSnapshotFailedRestrictedNetwork)
                        error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(
                            hutil.ExtErrorCode)
                    run_status = 'error'
                    backup_logger.log(error_msg, True, 'Error')
            elif check_snapshot_array_fail() == True:
                run_result = CommonVariables.error
                run_status = 'error'
                error_msg = 'T:S Enable failed with error in snapshot_array index'
                backup_logger.log(error_msg, True, 'Error')
            else:
                run_result = CommonVariables.success
                run_status = 'success'
                error_msg = 'Enable Succeeded'
                backup_logger.log("T:S " + error_msg, True)
    except Exception as e:
        if (get_value_from_configfile('doseq') == '2'):
            set_value_to_configfile('doseq', '0')
        errMsg = 'Failed to do the snapshot with error: %s, stack trace: %s' % (
            str(e), traceback.format_exc())
        backup_logger.log(errMsg, True, 'Error')
        run_result = CommonVariables.error
        run_status = 'error'
        error_msg = 'Enable failed with exception in safe freeze or snapshot '
        hutil.SetExtErrorCode(
            ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.error)
def freeze_snapshot(timeout):
    try:
        global hutil,backup_logger,run_result,run_status,error_msg,freezer,freeze_result,para_parser,snapshot_info_array,g_fsfreeze_on
        if(get_value_from_configfile('doseq') == '2'):
            set_value_to_configfile('doseq', '1')
        if(get_value_from_configfile('doseq') != '1'):
            set_value_to_configfile('doseq', '2')
        snap_shotter = Snapshotter(backup_logger)
        time_before_freeze = datetime.datetime.now()
        freeze_result = freezer.freeze_safe(timeout) 
        time_after_freeze = datetime.datetime.now()
        HandlerUtil.HandlerUtility.add_to_telemetery_data("FreezeTime", str(time_after_freeze-time_before_freeze-datetime.timedelta(seconds=5)))
        run_result = CommonVariables.success
        run_status = 'success'
        all_failed= False
        is_inconsistent =  False
        backup_logger.log('T:S freeze result ' + str(freeze_result))
        if(freeze_result is not None and len(freeze_result.errors) > 0):
            run_result = CommonVariables.FailedFsFreezeFailed
            run_status = 'error'
            error_msg = 'T:S Enable failed with error: ' + str(freeze_result)
            hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.FailedRetryableFsFreezeFailed)
            error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(hutil.ExtErrorCode)
            backup_logger.log(error_msg, True, 'Warning')
            if(get_value_from_configfile('doseq') == '2'):
                set_value_to_configfile('doseq', '0')
        else:
            backup_logger.log('T:S doing snapshot now...')
            time_before_snapshot = datetime.datetime.now()
            snapshot_result,snapshot_info_array, all_failed, is_inconsistent, unable_to_sleep  = snap_shotter.snapshotall(para_parser, freezer)
            time_after_snapshot = datetime.datetime.now()
            HandlerUtil.HandlerUtility.add_to_telemetery_data("SnapshotTime", str(time_after_snapshot-time_before_snapshot))
            backup_logger.log('T:S snapshotall ends...', True)
            if(get_value_from_configfile('doseq') == '2'):
                set_value_to_configfile('doseq', '0')
            if(snapshot_result is not None and len(snapshot_result.errors) > 0):
                if unable_to_sleep:
                    run_result = CommonVariables.error
                    run_status = 'error'
                    error_msg = 'T:S Enable failed with error: ' + str(snapshot_result)
                    backup_logger.log(error_msg, True, 'Warning')
                elif is_inconsistent == True :
                    set_value_to_configfile('doseq', '1') 
                    run_result = CommonVariables.error
                    run_status = 'error'
                    error_msg = 'T:S Enable failed with error: ' + str(snapshot_result)
                    backup_logger.log(error_msg, True, 'Warning')
                else:
                    error_msg = 'T:S snapshot result: ' + str(snapshot_result)
                    run_result = CommonVariables.FailedRetryableSnapshotFailedNoNetwork
                    if all_failed:
                        hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.FailedRetryableSnapshotFailedNoNetwork)
                        error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(hutil.ExtErrorCode)
                    else:
                        hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.FailedRetryableSnapshotFailedRestrictedNetwork)
                        error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(hutil.ExtErrorCode)
                    run_status = 'error'
                    backup_logger.log(error_msg, True, 'Error')
            elif check_snapshot_array_fail() == True:
                run_result = CommonVariables.error
                run_status = 'error'
                error_msg = 'T:S Enable failed with error in snapshot_array index'
                backup_logger.log(error_msg, True, 'Error')
            else:
                run_result = CommonVariables.success
                run_status = 'success'
                error_msg = 'Enable Succeeded'
                backup_logger.log("T:S " + error_msg, True)
    except Exception as e:
        if(get_value_from_configfile('doseq') == '2'):
            set_value_to_configfile('doseq', '0')
        errMsg = 'Failed to do the snapshot with error: %s, stack trace: %s' % (str(e), traceback.format_exc())
        backup_logger.log(errMsg, True, 'Error')
        run_result = CommonVariables.error
        run_status = 'error'
        error_msg = 'Enable failed with exception in safe freeze or snapshot ' 
        hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.error)
Beispiel #13
0
from snapshotter import Snapshotter

#
# Environment Variables:
#   PORT - The port on which the api service runs [default=8080]
#   ROOTDIR - The path to the root directory where the data/screenshots will be saved. [default='./snapshots']
#   CHROME_DRIVER_PATH - The path to the chrome driver [default='./chromedriver']
#

rootdir = os.environ.get('ROOTDIR') or './snapshots'

app = Flask(__name__)
api = Api(app)

snap = Snapshotter()


##### begin api route definitions #####
@app.route('/api/take-screenshot/soundcloud/charts/<chart_descriptor>',
           methods=['POST'])
def takeSnapshots(chart_descriptor):
    try:
        print(
            'POST /api/take-screenshot/soundcloud/charts/<chart_descriptor={}>'
            .format(chart_descriptor))

        now = datetime.now()
        subdir = now.strftime("%Y%m")
        date_time = now.strftime("%Y%m%dT%H%M%S")
Beispiel #14
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)
def freeze_snapshot(timeout):
    try:
        global hutil,backup_logger,run_result,run_status,error_msg,freezer,freeze_result,para_parser,snapshot_info_array,g_fsfreeze_on
        freeze_result = freezer.freeze_safe(timeout) 
        run_result = CommonVariables.success
        run_status = 'success'
        all_failed= False
        is_inconsistent =  False
        backup_logger.log('T:S freeze result ' + str(freeze_result))
        if(freeze_result is not None and len(freeze_result.errors) > 0):
            run_result = CommonVariables.error
            run_status = 'error'
            error_msg = 'T:S Enable failed with error: ' + str(freeze_result)
            hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.FailedRetryableFsFreezeFailed)
            error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(hutil.ExtErrorCode)
            backup_logger.log(error_msg, True, 'Warning')
        else:
            backup_logger.log('T:S doing snapshot now...')
            snap_shotter = Snapshotter(backup_logger)
            snapshot_result,snapshot_info_array, all_failed, is_inconsistent, unable_to_sleep  = snap_shotter.snapshotall(para_parser, freezer)
            backup_logger.log('T:S snapshotall ends...', True)
            if(snapshot_result is not None and len(snapshot_result.errors) > 0):
                if unable_to_sleep:
                    run_result = CommonVariables.error
                    run_status = 'error'
                    error_msg = 'T:S Enable failed with error: ' + str(snapshot_result)
                    backup_logger.log(error_msg, True, 'Warning')
                elif is_inconsistent == True :
                    set_do_seq_flag()
                    run_result = CommonVariables.error
                    run_status = 'error'
                    error_msg = 'T:S Enable failed with error: ' + str(snapshot_result)
                    backup_logger.log(error_msg, True, 'Warning')
                else:
                    error_msg = 'T:S snapshot result: ' + str(snapshot_result)
                    run_result = CommonVariables.FailedRetryableSnapshotFailedNoNetwork
                    if all_failed:
                        hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.FailedRetryableSnapshotFailedNoNetwork)
                        error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(hutil.ExtErrorCode)
                    else:
                        hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.FailedRetryableSnapshotFailedRestrictedNetwork)
                        error_msg = error_msg + ExtensionErrorCodeHelper.ExtensionErrorCodeHelper.StatusCodeStringBuilder(hutil.ExtErrorCode)
                    run_status = 'error'
                    backup_logger.log(error_msg, True, 'Error')
            elif check_snapshot_array_fail() == True:
                run_result = CommonVariables.error
                run_status = 'error'
                error_msg = 'T:S Enable failed with error in snapshot_array index'
                backup_logger.log(error_msg, True, 'Error')
            else:
                run_result = CommonVariables.success
                run_status = 'success'
                error_msg = 'Enable Succeeded'
                backup_logger.log("T:S " + error_msg, True)
    except Exception as e:
        errMsg = 'Failed to do the snapshot with error: %s, stack trace: %s' % (str(e), traceback.format_exc())
        backup_logger.log(errMsg, True, 'Error')
        run_result = CommonVariables.error
        run_status = 'error'
        error_msg = 'Enable failed with exception in safe freeze or snapshot ' 
        hutil.SetExtErrorCode(ExtensionErrorCodeHelper.ExtensionErrorCodeEnum.error)
Beispiel #16
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')

        # handle the restoring scenario.
        mi = MachineIdentity()
        stored_identity = mi.stored_identity()
        if(stored_identity is None):
            mi.save_identity()
            hutil.exit_if_enabled()
        else:
            current_identity = mi.current_identity()
            hutil.log(" current identity " + current_identity)
            if(current_identity != stored_identity):
                current_seq_no = hutil._get_current_seq_no(hutil._context._config_dir)
                backup_logger.log("machine identity not same, set current_seq_no to " + str(current_seq_no) + " " + str(stored_identity) + " " + str(current_identity), True)
                #remove other .config files.  or the waagent would report the 3
                #status...
                
                for subdir, dirs, files in os.walk(hutil._context._config_dir):
                    for file in files:
                        try:
                            cur_seq_no = int(os.path.basename(file).split('.')[0])
                            if(cur_seq_no != current_seq_no):
                                os.remove(join(hutil._context._config_dir,file))
                        except ValueError:
                            continue
                hutil.set_inused_config_seq(current_seq_no)
                mi.save_identity()
            else:
                hutil.exit_if_enabled()

        # we need to freeze the file system first
        backup_logger.log('starting to enable', True)
        """
        protectedSettings is the privateConfig passed from Powershell.
        """
        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)

        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 = 0
            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 = 11
                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 = 2
                    run_status = 'error'
                    error_msg  = 'Enable failed with error' + str(freeze_result.errors)
                    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.errors)
                        run_result = 2
                        run_status = 'error'
                        backup_logger.log(error_msg, False, 'Error')
                    else:
                        run_result = 1
                        run_status = 'success'
                        error_msg  = 'Enable Succeeded'
                        backup_logger.log(error_msg)
        else:
            run_status = 'error'
            run_result = 11
            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')
        print(errMsg)
        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 = 12
        elif(para_parser is None):
            run_result = 11
        else:
            run_result = 2
        run_status = 'error'
        error_msg  += ('Enable failed.' + str(global_error_result))

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