예제 #1
0
    def _rest_post(self, path, payload):
        """
        :type path: str
        :rtype: (dict, int)
        """
        url = self.url + '/api/' + ZOE_API_VERSION + path
        try:
            req = requests.post(url,
                                json=payload,
                                headers={'Cookie': self.token})
        except requests.exceptions.Timeout:
            raise ZoeAPIException('HTTP connection timeout')
        except requests.exceptions.HTTPError:
            raise ZoeAPIException('Invalid HTTP response')
        except requests.exceptions.ConnectionError as e:
            raise ZoeAPIException('Connection error: {}'.format(e))

        if not req.ok:
            data = req.reason
        else:
            try:
                data = req.json()
            except ValueError:
                data = None
        return data, req.status_code
예제 #2
0
파일: info.py 프로젝트: www3838438/zoe
    def info(self):
        """
        Queries Zoe for versions and local configuration parameters.

        :return:
        """
        data, status_code = self._rest_get('/info')
        if status_code != 200:
            if status_code == 404:
                raise ZoeAPIException('API endpoint not found')
            raise ZoeAPIException(data['message'])
        else:
            return data
예제 #3
0
    def get_logs(self, container_id):
        """
        Retrieve service logs.

        :param container_id:
        :return:
        """
        response, status_code = self._rest_get_stream('/service/logs/' + str(container_id))
        if status_code == 200:
            for line in response.iter_lines():
                line = line.decode('utf-8').split(' ', 1)
                yield line
        elif status_code == 404:
            raise ZoeAPIException('service "{}" not found'.format(container_id))
        else:
            raise ZoeAPIException('error retrieving service {}'.format(container_id))
예제 #4
0
    def _login(self, user, password):
        url = self.url + '/api/' + ZOE_API_VERSION + '/login'
        try:
            req = requests.get(url, auth=(user, password))
        except requests.exceptions.Timeout:
            raise ZoeAPIException('HTTP connection timeout')
        except requests.exceptions.HTTPError:
            raise ZoeAPIException('Invalid HTTP response')
        except requests.exceptions.ConnectionError as e:
            raise ZoeAPIException('Connection error: {}'.format(e))

        if req.status_code == 200:
            return req.headers['Set-Cookie'], req.json()['user']
        else:
            raise ZoeAPIException('Authentication error: {}'.format(
                req.reason))
예제 #5
0
파일: api_base.py 프로젝트: www3838438/zoe
    def _rest_get_stream(self, path):
        """
        :type path: str
        :rtype: Tuple[requests.Response, int]
        """
        url = self.url + '/api/' + ZOE_API_VERSION + path
        try:
            req = requests.get(url, auth=(self.user, self.password), stream=True)
        except requests.exceptions.Timeout:
            raise ZoeAPIException('HTTP connection timeout')
        except requests.exceptions.HTTPError:
            raise ZoeAPIException('Invalid HTTP response')
        except requests.exceptions.ConnectionError as e:
            raise ZoeAPIException('Connection error: {}'.format(e))

        return req, req.status_code
예제 #6
0
    def get(self, container_id):
        """
        Retrieve service state.

        :param container_id: the service to query
        :return:

        :type container_id: int
        :rtype: dict
        """
        cont, status_code = self._rest_get('/service/' + str(container_id))
        if status_code == 200:
            return cont
        elif status_code == 404:
            raise ZoeAPIException('service "{}" not found'.format(container_id))
        else:
            raise ZoeAPIException('error retrieving service {}'.format(container_id))
예제 #7
0
    def get(self, role_id: int) -> dict:
        """
        Retrieve a role by its ID.

        :param role_id: the service to query
        :return:

        :type role_id: int
        :rtype: dict
        """
        data, status_code = self._rest_get('/role/' + str(role_id))
        if status_code == 200:
            return data['role']
        elif status_code == 404:
            raise ZoeAPIException('role "{}" not found'.format(role_id))
        else:
            raise ZoeAPIException('error retrieving role {}: {}'.format(
                role_id, data))
예제 #8
0
    def get(self, user_id: int) -> dict:
        """
        Retrieve a user by its ID.

        :param user_id: the service to query
        :return:

        :type user_id: int
        :rtype: dict
        """
        data, status_code = self._rest_get('/user/' + str(user_id))
        if status_code == 200:
            return data['user']
        elif status_code == 404:
            raise ZoeAPIException('user "{}" not found'.format(user_id))
        else:
            raise ZoeAPIException('error retrieving user {}: {}'.format(
                user_id, data))
예제 #9
0
    def get(self, quota_id: int) -> dict:
        """
        Retrieve a quota by its ID.

        :param quota_id: the service to query
        :return:

        :type quota_id: int
        :rtype: dict
        """
        data, status_code = self._rest_get('/quota/' + str(quota_id))
        if status_code == 200:
            return data['quota']
        elif status_code == 404:
            raise ZoeAPIException('quota "{}" not found'.format(quota_id))
        else:
            raise ZoeAPIException('error retrieving quota {}: {}'.format(
                quota_id, data))
예제 #10
0
    def create(self, role: dict):
        """
        Create a role.

        :param role:
        :return:
        """
        data, status_code = self._rest_post('/role', role)
        if status_code != 201:
            raise ZoeAPIException(data)
        return data['role_id']
예제 #11
0
    def scheduler(self):
        """
        Queries Zoe for scheduler statistics.

        :return:
        """
        data, status_code = self._rest_get('/statistics/scheduler')
        if status_code != 200:
            raise ZoeAPIException(data['message'])
        else:
            return data
예제 #12
0
파일: api_base.py 프로젝트: nuaays/zoe
    def _rest_get(self, path):
        """
        :type path: str
        :rtype: (dict, int)
        """
        url = self.url + '/api/' + ZOE_API_VERSION + path
        try:
            req = requests.get(url, auth=(self.user, self.password))
        except requests.exceptions.Timeout:
            raise ZoeAPIException('HTTP connection timeout')
        except requests.exceptions.HTTPError:
            raise ZoeAPIException('Invalid HTTP response')
        except requests.exceptions.ConnectionError as e:
            raise ZoeAPIException('Connection error: {}'.format(e))

        try:
            data = req.json()
        except ValueError:
            data = None
        return data, req.status_code
예제 #13
0
파일: executions.py 프로젝트: townie/zoe
    def list(self):
        """
        Returns a list of all executions for the calling user, all of them if the user is admin.

        :return:
        """
        data, status_code = self._rest_get('/execution')
        if status_code == 200:
            return data
        else:
            raise ZoeAPIException(data['message'])
예제 #14
0
    def create(self, quota: dict):
        """
        Create a quota.

        :param quota:
        :return:
        """
        data, status_code = self._rest_post('/quota', quota)
        if status_code != 201:
            raise ZoeAPIException(data)
        return data['quota_id']
예제 #15
0
    def info(self):
        """
        Queries Zoe for versions and local configuration parameters.

        :return:
        """
        data, status_code = self._rest_get('/info')
        if status_code != 200:
            raise ZoeAPIException(data)
        else:
            return data
예제 #16
0
    def create(self, user: dict):
        """
        Create a user.

        :param user:
        :return:
        """
        data, status_code = self._rest_post('/user', user)
        if status_code != 201:
            raise ZoeAPIException(data)
        return data['user_id']
예제 #17
0
    def list(self, filters):
        """
        List users, with an optional filter.

        :param filters: a dictionary with zero or more keys: username, email, priority, enables, auth_source, role_id, quota_id
        :return:
        """
        data, status_code = self._rest_get('/user', filters)
        if status_code != 200:
            raise ZoeAPIException(data)
        return list(data.values())
예제 #18
0
    def update(self, role_id: int, entries: dict) -> None:
        """
        Update a role.

        :param role_id:
        :param entries:
        :return:
        """
        data, status_code = self._rest_post('/role/{}'.format(role_id),
                                            entries)
        if status_code != 201:
            raise ZoeAPIException(data)
예제 #19
0
    def update(self, user_id: int, entries) -> None:
        """
        Update a user.

        :param user_id:
        :param entries:
        :return:
        """
        data, status_code = self._rest_post('/user/{}'.format(user_id),
                                            entries)
        if status_code != 201:
            raise ZoeAPIException(data)
예제 #20
0
    def update(self, quota_id: int, entries: dict) -> None:
        """
        Update a quota.

        :param quota_id:
        :param entries:
        :return:
        """
        data, status_code = self._rest_post('/quota/{}'.format(quota_id),
                                            entries)
        if status_code != 201:
            raise ZoeAPIException(data)
예제 #21
0
    def delete(self, user_id: int) -> None:
        """
        Delete a user.

        :param user_id:
        :return:

        :type user_id: int
        :rtype: dict
        """
        data, status_code = self._rest_delete('/user/{}'.format(user_id))
        if status_code != 204:
            raise ZoeAPIException(data)
예제 #22
0
    def delete(self, quota_id: int) -> None:
        """
        Delete a quota.

        :param quota_id:
        :return:

        :type quota_id: int
        :rtype: dict
        """
        data, status_code = self._rest_delete('/quota/{}'.format(quota_id))
        if status_code != 204:
            raise ZoeAPIException(data)
예제 #23
0
    def delete(self, role_id: int) -> None:
        """
        Delete a role.

        :param role_id:
        :return:

        :type role_id: int
        :rtype: dict
        """
        data, status_code = self._rest_delete('/role/{}'.format(role_id))
        if status_code != 204:
            raise ZoeAPIException(data)
예제 #24
0
파일: services.py 프로젝트: townie/zoe
    def died(self, container_id):
        """
        Inform the master that a service died. Used by the observer process.

        :param container_id: ID of the service that died
        :return:

        :type container_id: int
        :rtype: None
        """
        data, status_code = self._rest_delete(
            '/service/{}'.format(container_id))
        if status_code != 204:
            raise ZoeAPIException(data['message'])
예제 #25
0
    def delete(self, execution_id):
        """
        Deletes an execution.

        :param execution_id: the execution to delete
        :return: True if the operation was successful, False otherwise

        :type execution_id: int
        :rtype: bool
        """
        data, status_code = self._rest_delete('/execution/delete/' + str(execution_id))
        if status_code == 204:
            return
        else:
            raise ZoeAPIException(data['message'])
예제 #26
0
    def query(self, topic, **kwargs):
        """
        Queries Zoe state.

        :param topic:
        :param kwargs:
        :return:
        """
        q = {'what': topic, 'filters': kwargs}

        data, status_code = self._rest_post('/query', q)
        if status_code != 200:
            raise ZoeAPIException(data['message'])
        else:
            return data
예제 #27
0
    def get(self, execution_id):
        """
        Retrieve the Execution object for an existing execution.

        :param execution_id: the execution to load from the master
        :return: the Execution object, or None

        :type execution_id: int
        :rtype: dict
        """
        data, status_code = self._rest_get('/execution/' + str(execution_id))
        if status_code == 200:
            return data
        elif status_code == 404:
            return None
        else:
            raise ZoeAPIException(data)
예제 #28
0
    def start(self, name, application_description):
        """
        Submit an application to the master to start a new execution.

        :param name: user-provided name of the execution
        :param application_description: the application to start
        :return: the new Execution object, or None in case of error

        :type name: str
        :type application_description: dict
        :rtype: int
        """
        execution = {"application": application_description, 'name': name}
        data, status_code = self._rest_post('/execution', execution)
        if status_code != 201:
            raise ZoeAPIException(data)
        else:
            return data['execution_id']
예제 #29
0
    def list(self, **kwargs):
        """
        Returns a list of all executions for the calling user, all of them if the user is admin.

        The list can be filtered by passing arguments. Any combination of the following filters is supported:

        * status: one of submitted, queued, starting, error, running, cleaning up, terminated
        * name: execution mane
        * user_id: user_id owning the execution (admin only)
        * limit: limit the number of returned entries
        * earlier_than_submit: all execution that where submitted earlier than this timestamp
        * earlier_than_start: all execution that started earlier than this timestamp
        * earlier_than_end: all execution that ended earlier than this timestamp
        * later_than_submit: all execution that where submitted later than this timestamp
        * later_than_start: all execution that started later than this timestamp
        * later_than_end: all execution that started later than this timestamp

        :return:
        """
        data, status_code = self._rest_get('/execution', kwargs)
        if status_code == 200:
            return list(data.values())
        else:
            raise ZoeAPIException(data)