Пример #1
0
def apimethod_check_asynchronous_command_return_code(system_id, rc_file):
    """Check the return code of a previously asynchronous request

    Args:
      system_ip (str): The system_id where you want to know
                       if the process is running
      rc_file(str): The return code file

    Returns:
      (boolean, str): A tuple containing the result of the execution

    Examples:
      apimethod_ansible_check_asynchronous_command_return_code("11111111-1111-1111-1111-1111222244445555", "/var/log/alienvault/update/system_reconfigure.log.rc")
      (True,0)
    """

    (success, system_ip) = get_system_ip_from_system_id(system_id)
    if not success:
        error_msg = "[apimethod_ansible_check_" + \
                    "asynchronous_command_return_code] " + \
                    "Error retrieving the system ip " + \
                    "for the system id %s -> %s" % (system_ip, str(system_ip))
        return success, error_msg

    return ansible_check_asynchronous_command_return_code(system_ip, rc_file)
Пример #2
0
def apimethod_check_asynchronous_command_return_code(system_id, rc_file):
    """Check the return code of a previously asynchronous request

    Args:
      system_ip (str): The system_id where you want to know
                       if the process is running
      rc_file(str): The return code file

    Returns:
      (boolean, str): A tuple containing the result of the execution

    Examples:
      apimethod_ansible_check_asynchronous_command_return_code("11111111-1111-1111-1111-1111222244445555", "/var/log/alienvault/update/system_reconfigure.log.rc")
      (True,0)
    """

    (success, system_ip) = get_system_ip_from_system_id(system_id)
    if not success:
        error_msg = "[apimethod_ansible_check_" + \
                    "asynchronous_command_return_code] " + \
                    "Error retrieving the system ip " + \
                    "for the system id %s -> %s" % (system_ip, str(system_ip))
        return success, error_msg

    return ansible_check_asynchronous_command_return_code(system_ip, rc_file)
Пример #3
0
def alienvault_asynchronous_update(system_ip, only_feed=False, update_key=""):
    """Runs an asynchronous  alienvault update
    Args:
      system_ip (str): The system IP where we would like to run the alienvault-update
      only_feed (boolean): A boolean indicating whether we should update only the feed or not.
      update_key (str): Upgrade key.
    Returns:
      JobResult (dict): where obj['result'] == True if success or False otherwise.
    """
    running = exist_task_running(task_type='alienvault_asynchronous_update',
                                 current_task_request=current_task.request,
                                 param_to_compare=system_ip,
                                 argnum=0)
    if running:
        return JobResult(False, "An existing task running", "", "300090", system_ip=system_ip).serialize

    try:
        logger.info("Start asynchronous update <%s>" % system_ip)

        rt, error_str = ansible_run_async_update(system_ip, only_feed=only_feed, update_key=update_key)
        # When the task has been launched properly the error_str variable will contain the log file.
        if not rt:
            error_msg = "Something wrong happened while running the alienvault update %s" % error_str
            if 'unreachable' in error_msg:
                error_msg = 'System unreachable'
            return JobResult(False, error_msg, "", "300091", system_ip=system_ip).serialize
        logger.info(" alienvault-update <%s> waiting to finish...." % system_ip)
        time.sleep(1)  # Wait until the task is launched.
        n_process = 1
        while int(n_process) > 0:
            success, n_process = ansible_check_if_process_is_running(system_ip, error_str)
            time.sleep(1)

        flush_cache(namespace='system_packages')

        rt, log_file = ansible_get_asynchronous_command_log_file(system_ip, error_str)
        if not rt:
            return JobResult(
                False,
                "Something wrong happened while retrieving the alienvault-update log file: %s" % log_file,
                "", "300092", system_ip=system_ip).serialize
        rt, return_code_msg = ansible_check_asynchronous_command_return_code(system_ip, error_str+".rc")
        if not rt:
            error_msg = "Something wrong happened while retrieving the alienvault-return code <%s>" % return_code_msg
            error_id = "300093"
            if return_code_msg.startswith("Return code is different from 0"):
                return_code = return_code_msg.split("<")[1].split(">")[0]
                error_msg = SYSTEM_UPDATE_ERROR_STRINGS[return_code][1]
                error_id = SYSTEM_UPDATE_ERROR_STRINGS[return_code][0]

            return JobResult(False, error_msg, log_file, error_id, system_ip=system_ip).serialize
        logger.info("Running alienvault-update ... end %s - %s" % (rt, error_str))

    except Exception, e:
        logger.error("An error occurred running alienvault-update: %s, %s" % (str(e), traceback.format_exc()))
        return JobResult(False, "An error occurred running alienvault-update <%s>" % str(e), "", "300099").serialize
Пример #4
0
def alienvault_asynchronous_reconfigure(system_ip, new_system_ip):
    """Runs an asynchronous  alienvault reconfig
    Args:
      system_ip (str): The system IP where we would like to run the alienvault-reconfig
      system_ip (str): The new system admin IP. This is used for the case of changing the ansible IP
    Returns:
      success (boolean): True if success false otherwise
    , job_log=job_log
    """
    running = exist_task_running(task_type='alienvault_asynchronous_reconfigure',
                                 current_task_request=current_task.request,
                                 param_to_compare=system_ip,
                                 argnum=0)
    if running:
        return JobResult(False, "An existing task running", "", "0").serialize

    try:
        logger.info("Start asynchronous reconfigure <%s>" % system_ip)
        (success, log_file) = ansible_run_async_reconfig(system_ip)
        if not success:
            logger.error("Error running alienvault reconfig: %s" % log_file)
            return JobResult(False, "Something wrong happend while running alienvault reconfig %s" % log_file, log_file, "0").serialize

        logger.info("reconfigure <%s> waiting to finish...." % system_ip)

        # Wait until the task is launched.
        time.sleep(1)

        # Wait until the process is finished
        n_process = 1
        while n_process > 0:
            (success, n_process) = ansible_check_if_process_is_running(system_ip, log_file)
            if not success:
                if new_system_ip is not None and system_ip != new_system_ip:
                    system_ip = new_system_ip
                else:
                    logger.error("Cannot retrieve the process status from %s" % system_ip)
                    return JobResult(False, "Cannot retrieve the process status from %s" % system_ip, log_file, "0").serialize
            time.sleep(1)

        logger.info("Running alienvault-reconfig ... end %s - %s" % (success, log_file))

        # Get the log file
        (success, log_file_path) = ansible_get_asynchronous_command_log_file(system_ip, log_file)
        if not success:
            return JobResult(False, "Something wrong happened while retrieving the alienvault-reconfig log file %s" % log_file_path, log_file, "0").serialize

        # Get the return code
        success, return_code_msg = ansible_check_asynchronous_command_return_code(system_ip, log_file + ".rc")
        if not success:
            logger.error("Something wrong happened while retrieving the alienvault-reconfig return code %s" % return_code_msg)
            return JobResult(False, "Something wrong happened while retrieving the return code %s" % return_code_msg, log_file, "0").serialize

    except Exception, e:
        logger.error("An error occurred running alienvault-reconfig: %s, %s" % (str(e), traceback.format_exc()))
        return JobResult(False, "An error occurred running alienvault-reconfig:  %s" % (str(e)), "", "0").serialize
Пример #5
0
def alienvault_asynchronous_reconfigure(system_ip, new_system_ip):
    """Runs an asynchronous  alienvault reconfig
    Args:
      system_ip (str): The system IP where we would like to run the alienvault-reconfig
      system_ip (str): The new system admin IP. This is used for the case of changing the ansible IP
    Returns:
      success (boolean): True if success false otherwise
    , job_log=job_log
    """
    running = exist_task_running(
        task_type='alienvault_asynchronous_reconfigure',
        current_task_request=current_task.request,
        param_to_compare=system_ip,
        argnum=0)
    if running:
        return JobResult(False, "An existing task running", "", "0").serialize

    try:
        logger.info("Start asynchronous reconfigure <%s>" % system_ip)
        (success, log_file) = ansible_run_async_reconfig(system_ip)
        if not success:
            logger.error("Error running alienvault reconfig: %s" % log_file)
            return JobResult(
                False,
                "Something wrong happend while running alienvault reconfig %s"
                % log_file, log_file, "0").serialize

        logger.info("reconfigure <%s> waiting to finish...." % system_ip)

        # Wait until the task is launched.
        time.sleep(1)

        # Wait until the process is finished
        n_process = 1
        while n_process > 0:
            (success, n_process) = ansible_check_if_process_is_running(
                system_ip, log_file)
            if not success:
                if new_system_ip is not None and system_ip != new_system_ip:
                    system_ip = new_system_ip
                else:
                    logger.error("Cannot retrieve the process status from %s" %
                                 system_ip)
                    return JobResult(
                        False, "Cannot retrieve the process status from %s" %
                        system_ip, log_file, "0").serialize
            time.sleep(1)

        logger.info("Running alienvault-reconfig ... end %s - %s" %
                    (success, log_file))

        # Get the log file
        (success, log_file_path) = ansible_get_asynchronous_command_log_file(
            system_ip, log_file)
        if not success:
            return JobResult(
                False,
                "Something wrong happened while retrieving the alienvault-reconfig log file %s"
                % log_file_path, log_file, "0").serialize

        # Get the return code
        success, return_code_msg = ansible_check_asynchronous_command_return_code(
            system_ip, log_file + ".rc")
        if not success:
            logger.error(
                "Something wrong happened while retrieving the alienvault-reconfig return code %s"
                % return_code_msg)
            return JobResult(
                False,
                "Something wrong happened while retrieving the return code %s"
                % return_code_msg, log_file, "0").serialize

    except Exception, e:
        logger.error("An error occurred running alienvault-reconfig: %s, %s" %
                     (str(e), traceback.format_exc()))
        return JobResult(
            False,
            "An error occurred running alienvault-reconfig:  %s" % (str(e)),
            "", "0").serialize