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))
Example #3
0
 def test_requested_by_secured(self):
     try:
         with self.use_secured_client(username='******',
                                      password='******'):
             self.client.maintenance_mode.activate()
             response = self.client.maintenance_mode.status()
         self.assertEqual(response.requested_by, 'alice')
     finally:
         try:
             os.remove(get_maintenance_file_path())
         except OSError:
             pass
 def test_requested_by_secured(self):
     try:
         with self.use_secured_client(username='******',
                                      password='******'):
             self.client.maintenance_mode.activate()
             response = self.client.maintenance_mode.status()
         self.assertEqual(response.requested_by, 'alice')
     finally:
         try:
             os.remove(get_maintenance_file_path())
         except OSError:
             pass
    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 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 tearDown(self):
     super(MaintenanceModeTest, self).tearDown()
     try:
         os.remove(get_maintenance_file_path())
     except OSError:
         pass
def write_maintenance_state(state):
    maintenance_file_path = get_maintenance_file_path()
    with open(maintenance_file_path, 'w') as f:
        f.write(state)
def write_maintenance_state(state):
    maintenance_file_path = get_maintenance_file_path()
    with open(maintenance_file_path, 'w') as f:
        f.write(state)