コード例 #1
0
ファイル: views.py プロジェクト: ANSSI-FR/cornetto
def do_commit() -> Dict[str, Any]:
    """
    Call the service that will commit and push the statification on the git repository
    and rename the logfile by the commit SHA.
    Need an X-Forwarded-User in the request

    If an error happen during the process the error will be caught and a python dict will be returned with a specific
    error code like the following example :

      -- In the case an error happen with the subprocess operation :
    {
        'success': False,
        'operation': 'subprocess'
    }

      -- If everything goes smoothly the following dict will be returned :
    {
        'success': True,
    }

    When the success python dict will be returned the process of commit will still be running, information about it's
    state will be given by the status background file

    :return a python dict as specified above
    """
    try:
        # test if the lock file is unlocked
        if is_access_locked():
            # the lock file was locked
            raise RuntimeError('route_access')

        # block the route for other users
        lock_access()

        current_app.logger.info('> Starting commit operations')

        # get the request parameter
        # get the user forwarded by kerberos
        s_user = request.headers.get('X-Forwarded-User')

        # if no process is running we authorize to commit
        if current_app.statifProcess.is_running():
            raise RuntimeError('process_running')

        current_app.logger.info('> Starting commit operations')

        # commit and push the statification to the git repository
        return service_do_commit(s_user)

    except RuntimeError as e:
        # return an ajax error code
        return {'success': False, 'error': str(e)}
    finally:
        # unlock the route before return
        unlock_access(current_app.config['LOCKFILE'])
コード例 #2
0
ファイル: views.py プロジェクト: ANSSI-FR/cornetto
def do_start_statif() -> Dict[str, Any]:
    """
    Do the following :
        - Check if the operation is locked by another process, if that's the case it will return an error
        - Get the lock, so other user can't do anything that would interfere with the operation
        - Initialize and start a statification

    If an error happen during the process the error will be caught and a python dict will be returned with a specific
    error code like the following example :

      -- In the case an error happen with the subprocess operation :
        {
            'success': False,
            'operation': 'subprocess'
        }

      -- If everything goes smoothly the following dict will be returned :
        {
            'success': True,
        }
    """
    # Get the request parameters
    s_designation = request.get_json()['designation']

    try:
        s_description = request.get_json()['description']
    except KeyError:
        s_description = ''

    s_user = request.headers.get('X-Forwarded-User')

    try:
        # test if the lock file is unlocked
        if is_access_locked():
            # the lock file was locked
            raise RuntimeError('route_access')

        # block the lock file for other users
        lock_access()

        current_app.logger.info('> Starting statification process')

        return service_do_start_statif(s_user, s_designation, s_description)

    except RuntimeError as e:
        # return an ajax error code
        return {'success': False, 'error': str(e)}
    finally:
        # unlock the route before return
        unlock_access(current_app.config['LOCKFILE'])
コード例 #3
0
ファイル: views.py プロジェクト: ANSSI-FR/cornetto
def do_apply_prod() -> Dict[str, Any]:
    """
    Push the desired statification (commit) in production.

    If an error happen during the process the error will be caught and a python dict will be returned with a specific
    error code like the following example :

      -- In the case an error happen with the subprocess operation :
    {
        'success': False,
        'operation': 'subprocess'
    }

      -- If everything goes smoothly the following dict will be returned :
    {
        'success': True,
    }

    :return a python dict as described above
    """
    try:
        # test if the lock file is unlocked
        if is_access_locked():
            # the lock file was locked
            raise RuntimeError('route_access')

        # block the route for other users
        lock_access()

        # get the request parameters
        s_commit = request.values.get('commit')
        # get the user forwarded by kerberos
        s_user = request.headers.get('X-Forwarded-User')

        current_app.logger.info('> Starting push to prod operations')

        # push the statification to the production server
        return service_do_apply_prod(s_user, s_commit)
    except RuntimeError as e:
        # return an ajax error code
        return {'success': False, 'error': str(e)}
    finally:
        # unlock the route before return
        unlock_access(current_app.config['LOCKFILE'])
コード例 #4
0
ファイル: views.py プロジェクト: ANSSI-FR/cornetto
def do_visualize() -> Dict[str, Any]:
    """
    Checkout a specified commit into the 'Visualize' repository to visualize a precedent statification

    If an error happen during the process the error will be caught and a python dict will be returned with a specific
    error code like the following example :

      -- In the case an error happen with the subprocess operation :
    {
        'success': False,
        'operation': 'subprocess'
    }

      -- If everything goes smoothly the following dict will be returned :
    {
        'success': True,
    }

    :return a python dict as described above
    """
    try:
        # test if the lock file is unlocked
        if is_access_locked():
            # the lock file was locked
            raise RuntimeError('route_access')

        # block the route for other users
        lock_access()

        # get the request parameter
        s_commit = request.values.get('commit')

        # get the user forwarded by kerberos
        s_user = request.headers.get('X-Forwarded-User')

        current_app.logger.info('> Doing visualization operations')

        return do_visualize(s_commit, s_user)
    except RuntimeError as e:
        # return an ajax error code
        return {'success': False, 'error': str(e)}
    finally:
        # unlock the route before return
        unlock_access(current_app.config['LOCKFILE'])
コード例 #5
0
ファイル: views.py プロジェクト: ANSSI-FR/cornetto
def statification_status() -> Dict[str, Any]:
    """
    Check the status of the api. The following information will be returned in a python dict :
    - isRunning         :   a boolean that indicate if a statification Process is running
    - commit            :   a string that contain the sha of the last statification,
                          if the last is a new and unsaved statification it will be empty
    - designation       :   the designation of the last statification, or empty
    - description       :   the description of the last statification, or empty
    - status            :   the status of the last statification :  CREATED = 0
                                                                    STATIFIED = 1
                                                                    SAVED = 2
                                                                    PRODUCTION = 3
                                                                    VISUALIZED = 4
                            Default status will be 3, if there is no statification in the database the user will still
                            be able to create a new one, if there are ongoing statification to be push to prod it still
                            give the hand to the user that have saved it.
    - i_nb_item_to_crawl :  the number of item that have been crawled during the last statification, it will be used
                            as a reference of the number of items to crawl to the next statification. If there is no
                            statification in the database it will be set to 100 by default.
    :return a python dict containing all the above information :
            **Example**:

              The default status when launching the api for the first time should be this one.
              .. code-block:: dict

                    {
                        'isRunning': false,
                        'commit': '',
                        'designation': '',
                        'description': '',
                        'currentNbItemCrawled': 0,
                        'nbItemToCrawl': 100,
                        'status': 3,
                        'isLocked': false,
                        'statusBackground': {}
                    }
    """
    # check if process is running
    b_is_running = current_app.statifProcess.is_running()

    # initialize the nb of item crawled and the number of item to crawl to 0
    i_current_nb_item_crawled = 0

    # get the number of page crawled if a statificationProcess is running
    if b_is_running:
        i_current_nb_item_crawled = get_nb_page_crawled()

    # get the last statification informations
    last_statif_infos = service_get_last_statif_infos()

    commit = last_statif_infos['commit']
    designation = last_statif_infos['designation']
    description = last_statif_infos['description']
    status = last_statif_infos['status']
    i_nb_item_to_crawl = last_statif_infos['i_nb_item_to_crawl']

    # if i_current_nb_item_crawled is raising higher than i_nb_item_to_crawl, then raise i_nb_item_to_crawl
    # we don't want i_nb_item_to_crawl be lower than i_current_nb_item_crawled
    if i_current_nb_item_crawled >= i_nb_item_to_crawl:
        i_nb_item_to_crawl = i_current_nb_item_crawled + 100

    # read the status background file and extract the json from it
    json_status_background = get_background_status_file_content()

    return {
        'isRunning': b_is_running,
        'commit': commit,
        'designation': designation,
        'description': description,
        'currentNbItemCrawled': i_current_nb_item_crawled,
        'nbItemToCrawl': i_nb_item_to_crawl,
        'status': status,
        'isLocked': is_access_locked(),
        'statusBackground': json_status_background
    }