Пример #1
0
    def test_read_write_json_file(self):
        test_dict = {'test': 1, 'dict': 2}
        tmp_file_path = os.path.join(self.tmpdir, 'tmp_dict.json')
        write_dict_to_json_file(tmp_file_path, test_dict)
        read_dict = read_json_file(tmp_file_path)
        self.assertEqual(test_dict, read_dict)

        test_dict = {'test': 3, 'new': 2}
        write_dict_to_json_file(tmp_file_path, test_dict)
        read_dict = read_json_file(tmp_file_path)
        self.assertEqual(3, read_dict['test'])
        self.assertEqual(test_dict, read_dict)
    def test_read_write_json_file(self):
        test_dict = {'test': 1, 'dict': 2}
        tmp_file_path = os.path.join(self.tmpdir, 'tmp_dict.json')
        write_dict_to_json_file(tmp_file_path, test_dict)
        read_dict = read_json_file(tmp_file_path)
        self.assertEqual(test_dict, read_dict)

        test_dict = {'test': 3, 'new': 2}
        write_dict_to_json_file(tmp_file_path, test_dict)
        read_dict = read_json_file(tmp_file_path)
        self.assertEqual(3, read_dict['test'])
        self.assertEqual(test_dict, read_dict)
Пример #3
0
 def post(self, maintenance_action, **_):
     maintenance_file_path = get_maintenance_file_path()
     if maintenance_action == 'activate':
         if os.path.isfile(maintenance_file_path):
             state = utils.read_json_file(maintenance_file_path)
             return state, 304
         now = utils.get_formatted_timestamp()
         try:
             user = current_user.username
         except AttributeError:
             user = ''
         remaining_executions = get_running_executions()
         status = MAINTENANCE_MODE_ACTIVATING \
             if remaining_executions else MAINTENANCE_MODE_ACTIVATED
         activated_at = '' if remaining_executions else now
         utils.mkdirs(config.instance.maintenance_folder)
         new_state = prepare_maintenance_dict(
             status=status,
             activation_requested_at=now,
             activated_at=activated_at,
             remaining_executions=remaining_executions,
             requested_by=user)
         utils.write_dict_to_json_file(maintenance_file_path, new_state)
         return new_state
     if maintenance_action == 'deactivate':
         if not os.path.isfile(maintenance_file_path):
             return prepare_maintenance_dict(
                 MAINTENANCE_MODE_DEACTIVATED), 304
         os.remove(maintenance_file_path)
         return prepare_maintenance_dict(MAINTENANCE_MODE_DEACTIVATED)
     valid_actions = ['activate', 'deactivate']
     raise BadParametersError('Invalid action: {0}, Valid action '
                              'values are: {1}'.format(
                                  maintenance_action, valid_actions))
 def post(self, maintenance_action, **_):
     maintenance_file_path = get_maintenance_file_path()
     if maintenance_action == 'activate':
         if os.path.isfile(maintenance_file_path):
             state = utils.read_json_file(maintenance_file_path)
             return state, 304
         now = utils.get_formatted_timestamp()
         try:
             user = current_user.username
         except AttributeError:
             user = ''
         remaining_executions = get_running_executions()
         status = MAINTENANCE_MODE_ACTIVATING \
             if remaining_executions else MAINTENANCE_MODE_ACTIVATED
         activated_at = '' if remaining_executions else now
         utils.mkdirs(config.instance.maintenance_folder)
         new_state = prepare_maintenance_dict(
             status=status,
             activation_requested_at=now,
             activated_at=activated_at,
             remaining_executions=remaining_executions,
             requested_by=user)
         utils.write_dict_to_json_file(maintenance_file_path, new_state)
         return new_state
     if maintenance_action == 'deactivate':
         if not os.path.isfile(maintenance_file_path):
             return prepare_maintenance_dict(
                     MAINTENANCE_MODE_DEACTIVATED), 304
         os.remove(maintenance_file_path)
         return prepare_maintenance_dict(MAINTENANCE_MODE_DEACTIVATED)
     valid_actions = ['activate', 'deactivate']
     raise BadParametersError(
             'Invalid action: {0}, Valid action '
             'values are: {1}'.format(maintenance_action, valid_actions))
Пример #5
0
    def test_any_cmd_activates_maintenance_mode(self):
        execution = self._start_maintenance_transition_mode(bp_id='bp1',
                                                            dep_id='dep1')
        self._terminate_execution(execution.id)
        self.assertRaises(exceptions.MaintenanceModeActiveError,
                          self.client.blueprints.upload,
                          self.get_mock_blueprint_path(), 'b1')
        self.client.maintenance_mode.deactivate()

        maintenance_file = os.path.join(self.maintenance_mode_dir,
                                        MAINTENANCE_MODE_STATUS_FILE)

        execution = self._start_maintenance_transition_mode(bp_id='bp2',
                                                            dep_id='dep2')
        self._terminate_execution(execution.id)
        state = utils.read_json_file(maintenance_file)
        self.assertEqual(state['status'], MAINTENANCE_MODE_ACTIVATING)
        self.client.manager.get_version()
        state = utils.read_json_file(maintenance_file)
        self.assertEqual(state['status'], MAINTENANCE_MODE_ACTIVATED)
    def test_any_cmd_activates_maintenance_mode(self):
        execution = self._start_maintenance_transition_mode(
            bp_id='bp1',
            dep_id='dep1')
        self._terminate_execution(execution.id)
        self.assertRaises(exceptions.MaintenanceModeActiveError,
                          self.client.blueprints.upload,
                          self.get_mock_blueprint_path(),
                          'b1')
        self.client.maintenance_mode.deactivate()

        maintenance_file = os.path.join(self.maintenance_mode_dir,
                                        MAINTENANCE_MODE_STATUS_FILE)

        execution = self._start_maintenance_transition_mode(
            bp_id='bp2',
            dep_id='dep2')
        self._terminate_execution(execution.id)
        state = utils.read_json_file(maintenance_file)
        self.assertEqual(state['status'], MAINTENANCE_MODE_ACTIVATING)
        self.client.manager.get_version()
        state = utils.read_json_file(maintenance_file)
        self.assertEqual(state['status'], MAINTENANCE_MODE_ACTIVATED)
    def get(self, **_):
        maintenance_file_path = get_maintenance_file_path()
        if os.path.isfile(maintenance_file_path):
            state = utils.read_json_file(maintenance_file_path)

            if state['status'] == MAINTENANCE_MODE_ACTIVATED:
                return state
            if state['status'] == MAINTENANCE_MODE_ACTIVATING:
                running_executions = get_running_executions()

                # If there are no running executions,
                # maintenance mode would have been activated at the
                # maintenance handler hook (server.py)
                state['remaining_executions'] = running_executions
                return state
        else:
            return prepare_maintenance_dict(MAINTENANCE_MODE_DEACTIVATED)
Пример #8
0
    def get(self, **_):
        maintenance_file_path = get_maintenance_file_path()
        if os.path.isfile(maintenance_file_path):
            state = utils.read_json_file(maintenance_file_path)

            if state['status'] == MAINTENANCE_MODE_ACTIVATED:
                return state
            if state['status'] == MAINTENANCE_MODE_ACTIVATING:
                running_executions = get_running_executions()

                # If there are no running executions,
                # maintenance mode would have been activated at the
                # maintenance handler hook (server.py)
                state['remaining_executions'] = running_executions
                return state
        else:
            return prepare_maintenance_dict(MAINTENANCE_MODE_DEACTIVATED)
    def test_any_cmd_activates_maintenance_mode(self):
        response = self.client.maintenance_mode.activate()
        self.assertEqual(MAINTENANCE_MODE_ACTIVATING, response.status)
        self.assertRaises(exceptions.MaintenanceModeActiveError,
                          self.client.blueprints.upload,
                          blueprint_path=self.get_mock_blueprint_path(),
                          blueprint_id='b1')

        self.client.maintenance_mode.deactivate()

        response = self.client.maintenance_mode.activate()
        self.assertEqual(MAINTENANCE_MODE_ACTIVATING, response.status)
        self.client.manager.get_version()

        maintenance_file = os.path.join(self.maintenance_mode_dir,
                                        MAINTENANCE_MODE_STATUS_FILE)
        state = utils.read_json_file(maintenance_file)
        self.assertEqual(state['status'], MAINTENANCE_MODE_ACTIVATED)
    def test_any_cmd_activates_maintenance_mode(self):
        response = self.client.maintenance_mode.activate()
        self.assertEqual(MAINTENANCE_MODE_ACTIVATING, response.status)
        self.assertRaises(exceptions.MaintenanceModeActiveError,
                          self.client.blueprints.upload,
                          blueprint_path=self.get_mock_blueprint_path(),
                          blueprint_id='b1')

        self.client.maintenance_mode.deactivate()

        response = self.client.maintenance_mode.activate()
        self.assertEqual(MAINTENANCE_MODE_ACTIVATING, response.status)
        self.client.manager.get_version()

        maintenance_file = os.path.join(self.maintenance_mode_dir,
                                        MAINTENANCE_MODE_STATUS_FILE)
        state = utils.read_json_file(maintenance_file)
        self.assertEqual(state['status'], MAINTENANCE_MODE_ACTIVATED)
Пример #11
0
def maintenance_mode_handler():

    # failed to route the request - this is a 404. Abort early.
    if not request.endpoint:
        return

    # enabling internal requests
    if utils.is_internal_request() and is_bypass_maintenance_mode():
        return

    # Removing v*/ from the endpoint
    index = request.endpoint.find('/')
    request_endpoint = request.endpoint[index+1:]
    maintenance_file = os.path.join(
            config.instance.maintenance_folder,
            MAINTENANCE_MODE_STATUS_FILE)

    if os.path.isfile(maintenance_file):
        state = utils.read_json_file(maintenance_file)
        if state['status'] == MAINTENANCE_MODE_ACTIVATING:
            running_executions = get_running_executions()
            if not running_executions:
                now = utils.get_formatted_timestamp()
                state = prepare_maintenance_dict(
                        MAINTENANCE_MODE_ACTIVATED,
                        activated_at=now,
                        remaining_executions=[],
                        requested_by=state['requested_by'],
                        activation_requested_at=state[
                            'activation_requested_at'])
                utils.write_dict_to_json_file(maintenance_file, state)
            else:
                return _handle_activating_mode(
                       state=state,
                       request_endpoint=request_endpoint)

        if utils.check_allowed_endpoint(ALLOWED_MAINTENANCE_ENDPOINTS):
            return

        if state['status'] == MAINTENANCE_MODE_ACTIVATED:
            return _maintenance_mode_error()
def maintenance_mode_handler():

    # failed to route the request - this is a 404. Abort early.
    if not request.endpoint:
        return

    # enabling internal requests
    if _is_internal_request() and is_bypass_maintenance_mode():
        return

    # Removing v*/ from the endpoint
    index = request.endpoint.find('/')
    request_endpoint = request.endpoint[index+1:]
    maintenance_file = os.path.join(
            config.instance().maintenance_folder,
            MAINTENANCE_MODE_STATUS_FILE)

    if os.path.isfile(maintenance_file):
        state = utils.read_json_file(maintenance_file)
        if state['status'] == MAINTENANCE_MODE_ACTIVATING:
            running_executions = get_running_executions()
            if not running_executions:
                now = utils.get_formatted_timestamp()
                state = prepare_maintenance_dict(
                        MAINTENANCE_MODE_ACTIVATED,
                        activated_at=now,
                        remaining_executions=None,
                        requested_by=state['requested_by'],
                        activation_requested_at=state[
                            'activation_requested_at'])
                utils.write_dict_to_json_file(maintenance_file, state)
            else:
                return _handle_activating_mode(
                       state=state,
                       request_endpoint=request_endpoint)

        if _check_allowed_endpoint(request_endpoint):
            return

        if state['status'] == MAINTENANCE_MODE_ACTIVATED:
            return _maintenance_mode_error()