Пример #1
0
    def callmanager(self, service_id, method, post, data, files=[]):
        """Call the manager API.

        'service_id': an integer holding the service id of the manager.
        'method': a string representing the API method name.
        'post': boolean value. True for POST method, false for GET.
        'data': a dictionary representing the data to be sent to the director.
        'files': sequence of (name, filename, value) tuples for data to be uploaded as files.

        callmanager loads the manager JSON response and returns it as a Python
        object.
        """
        service = self.service_dict(service_id)

        # File upload
        if files:
            res = client.https_post(service['manager'], 443, '/', data, files)
        # POST
        elif post:
            res = client.jsonrpc_post(service['manager'], 443, '/', method, data)
        # GET
        else:
            res = client.jsonrpc_get(service['manager'], 443, '/', method, data)

        if res[0] == 200:
            try:
                data = simplejson.loads(res[1])
            except simplejson.decoder.JSONDecodeError:
                # Not JSON, simply return what we got
                return res[1]

            return data.get('result', data)

        raise Exception, "Call to method %s on %s failed: %s.\nParams = %s" % (
            method, service['manager'], res[1], data)
Пример #2
0
def callmanager(app_id, service_id, method, post, data, files=[]):
    """Call the manager API.

    'service_id': an integer holding the service id of the manager.
    'method': a string representing the API method name.
    'post': boolean value. True for POST method, false for GET.
    'data': a dictionary representing the data to be sent to the director.
    'files': sequence of (name, filename, value) tuples for data to be uploaded as files.

    callmanager loads the manager JSON response and returns it as a Python
    object.
    """
    client.conpaas_init_ssl_ctx('/etc/cpsdirector/certs', 'director')

    application = get_app_by_id(g.user.uid, app_id)

    if application is None:
        msg = "Application %s not found." % app_id
        log_error(msg)
        return error_response(msg)
    elif application.to_dict()['manager'] is None:
        msg = "Application %s has not started. Try to start it first." % app_id
        log_error(msg)
        return error_response(msg)

    application = application.to_dict()
    # File upload
    if files:
        data['service_id'] = service_id
        res = client.https_post(application['manager'], 443, '/', data, files)
    # POST
    elif post:
        res = client.jsonrpc_post(application['manager'], 443, '/', method, service_id, data)
    # GET
    else:
        res = client.jsonrpc_get(application['manager'], 443, '/', method, service_id, data)

    if res[0] == 200:
        try:
            data = simplejson.loads(res[1])
        except simplejson.decoder.JSONDecodeError:
            # Not JSON, simply return what we got
            return res[1]

        return data.get('result', data)

    raise Exception, "Call to method %s on %s failed: %s.\nParams = %s" % (method, application['manager'], res[1], data)
Пример #3
0
def callmanager(service_id, method, post, data, files=[]):
    """Call the manager API.

    'service_id': an integer holding the service id of the manager.
    'method': a string representing the API method name.
    'post': boolean value. True for POST method, false for GET.
    'data': a dictionary representing the data to be sent to the director.
    'files': sequence of (name, filename, value) tuples for data to be uploaded as files.

    callmanager loads the manager JSON response and returns it as a Python
    object.
    """
    service = get_service(g.user.uid, service_id)

    client.conpaas_init_ssl_ctx('/etc/cpsdirector/certs', 'director')

    # File upload
    if files:
        res = client.https_post(service.manager, 443, '/', data, files)
    # POST
    elif post:
        res = client.jsonrpc_post(service.manager, 443, '/', method, data)
    # GET
    else:
        #res = client.jsonrpc_get(service.manager, 443, '/', method, data)
        if service.manager_port:
            node = service.manager_port.split(":")
            if len(node) == 2:
                _ip, _port = node
            else:
                _ip, _port = (service.manager, 443)
        res = client.jsonrpc_get(_ip, int(_port), '/', method, data)

    if res[0] == 200:
        try:
            data = simplejson.loads(res[1])
        except simplejson.decoder.JSONDecodeError:
            # Not JSON, simply return what we got
            return res[1]

        return data.get('result', data)

    raise Exception, "Call to method %s on %s failed: %s.\nParams = %s" % (
        method, service.manager, res[1], data)
Пример #4
0
def check_agent_process(host, port):
    """GET () check_agent_process"""
    method = 'check_agent_process'
    return client.check_response(client.jsonrpc_get(host, port, '/', method))
Пример #5
0
    def call_manager(self, app_id, service_id, method, post, data=None, files=None):
        """Call the manager API.
        Loads the manager JSON response and returns it as a Python object.

        'service_id': an integer holding the service id of the manager.
        'method': a string representing the API method name.
        'post': boolean value. True for POST method, false for GET.
        'data': a dictionary representing the data to be sent to the director.
        'files': sequence of (name, filename, value) tuples for data to be
                 uploaded as files.
        """

        application = self.application_dict(app_id)

        if application["manager"] is None:
            raise Exception("Application %s has not started. Try to start it first." % app_id)

        if data is None:
            data = {}
        if files is None:
            files = []

        url = "https://%s/" % application["manager"]

        # Certificates are always used, the ssl context should already have
        # been initialized with the user's certificates in __init__
        if not client.is_ssl_ctx_initialized():
            raise Exception(
                "Cannot call the manager: "
                "user certificates are not present.\n"
                "Try to run 'cps-user get_certificate' "
                "first."
            )
        if self.debug:
            self.logger.debug("User certificates are present and will be used.")
            self.logger.info(
                "Requesting '%s' with aid %s, sid %s, method '%s', "
                "data %s and %s files." % (url, app_id, service_id, method, data, len(files))
            )

        try:
            # File upload
            if files:
                status, body = client.https_post(application["manager"], 443, "/", data, files)
            # POST
            elif post:
                status, body = client.jsonrpc_post(application["manager"], 443, "/", method, service_id, data)
            # GET
            else:
                status, body = client.jsonrpc_get(application["manager"], 443, "/", method, service_id, data)
        except Exception as ex:
            if "certificate verify failed" in str(ex):
                raise Exception(
                    "The ConPaaS manager's SSL certificate "
                    "failed verification.\n"
                    "You may be the victim of a man-in-the-middle "
                    "attack or, if you recently changed the "
                    "ConPaaS deployment, you need to run "
                    "'cps-user get_certificate' first."
                )
            if self.debug:
                traceback.print_exc()
            raise Exception("Cannot contact the manager at URL '%s': %s" % (url, ex))

        if status != 200:
            raise Exception(
                "Call to method '%s' on '%s' failed: HTTP status %s.\n"
                "Params = %s" % (method, application["manager"], status, data)
            )

        try:
            data = simplejson.loads(body)
        except simplejson.decoder.JSONDecodeError:
            # Not JSON, simply return what we got
            if self.debug:
                self.logger.info("Call succeeded, result is not json.")
            return body

        res = data.get("result", data)
        if type(res) is dict and "error" in res:
            if self.debug:
                self.logger.info("Call succeeded, result contains an error")
            raise Exception(res["error"])

        self.logger.info("Call succeeded, result is: %s" % res)
        return res