Exemplo n.º 1
0
    def SubmitJob(self, op, cl=None):
        """Generic wrapper for submit job, for better http compatibility.

    @type op: list
    @param op: the list of opcodes for the job
    @type cl: None or luxi.Client
    @param cl: optional luxi client to use
    @rtype: string
    @return: the job ID

    """
        if cl is None:
            cl = self.GetClient()
        try:
            for opcode in op:
                # Add an authorized user name to the reason trail
                trail = getattr(opcode, constants.OPCODE_REASON, [])
                trail.append(self.GetAuthReason())
                setattr(opcode, constants.OPCODE_REASON, trail)
            return cl.SubmitJob(op)
        except errors.JobQueueFull:
            raise http.HttpServiceUnavailable(
                "Job queue is full, needs archiving")
        except errors.JobQueueDrainError:
            raise http.HttpServiceUnavailable(
                "Job queue is drained, cannot submit")
        except rpcerr.NoMasterError, err:
            raise http.HttpBadGateway("Master seems to be unreachable: %s" %
                                      err)
Exemplo n.º 2
0
    def SubmitJob(self, op, cl=None):
        """Generic wrapper for submit job, for better http compatibility.

    @type op: list
    @param op: the list of opcodes for the job
    @type cl: None or luxi.Client
    @param cl: optional luxi client to use
    @rtype: string
    @return: the job ID

    """
        if cl is None:
            cl = self.GetClient()
        try:
            return cl.SubmitJob(op)
        except errors.JobQueueFull:
            raise http.HttpServiceUnavailable(
                "Job queue is full, needs archiving")
        except errors.JobQueueDrainError:
            raise http.HttpServiceUnavailable(
                "Job queue is drained, cannot submit")
        except rpcerr.NoMasterError as err:
            raise http.HttpBadGateway("Master seems to be unreachable: %s" %
                                      err)
        except rpcerr.PermissionError:
            raise http.HttpInternalServerError(
                "Internal error: no permission to"
                " connect to the master daemon")
        except rpcerr.TimeoutError as err:
            raise http.HttpGatewayTimeout("Timeout while talking to the master"
                                          " daemon: %s" % err)
Exemplo n.º 3
0
Arquivo: rapi.py Projeto: azet/ganeti
  def HandleRequest(self, req):
    """Handles a request.

    """
    ctx = self._GetRequestContext(req)

    # Deserialize request parameters
    if req.request_body:
      # RFC2616, 7.2.1: Any HTTP/1.1 message containing an entity-body SHOULD
      # include a Content-Type header field defining the media type of that
      # body. [...] If the media type remains unknown, the recipient SHOULD
      # treat it as type "application/octet-stream".
      req_content_type = req.request_headers.get(http.HTTP_CONTENT_TYPE,
                                                 http.HTTP_APP_OCTET_STREAM)
      if req_content_type.lower() != http.HTTP_APP_JSON.lower():
        raise http.HttpUnsupportedMediaType()

      try:
        ctx.body_data = serializer.LoadJson(req.request_body)
      except Exception:
        raise http.HttpBadRequest(message="Unable to parse JSON data")
    else:
      ctx.body_data = None

    try:
      result = ctx.handler_fn()
    except rpcerr.TimeoutError:
      raise http.HttpGatewayTimeout()
    except rpcerr.ProtocolError, err:
      raise http.HttpBadGateway(str(err))
Exemplo n.º 4
0
    def GET(self):
        """Return a list of all OSes.

    Can return error 500 in case of a problem.

    Example: ["debian-etch"]

    """
        cl = self.GetClient()
        op = opcodes.OpOsDiagnose(output_fields=["name", "variants"], names=[])
        cancel_fn = (lambda: _CheckIfConnectionDropped(self._req.request_sock))
        job_id = self.SubmitJob([op], cl=cl)
        # we use custom feedback function, instead of print we log the status
        result = cli.PollJob(job_id,
                             cl,
                             feedback_fn=baserlib.FeedbackFn,
                             cancel_fn=cancel_fn)
        diagnose_data = result[0]

        if not isinstance(diagnose_data, list):
            raise http.HttpBadGateway(message="Can't get OS list")

        os_names = []
        for (name, variants) in diagnose_data:
            os_names.extend(cli.CalculateOSNames(name, variants))

        return os_names
Exemplo n.º 5
0
  def GetClient(self):
    """Wrapper for L{luxi.Client} with HTTP-specific error handling.

    """
    # Could be a function, pylint: disable=R0201
    try:
      return self._client_cls()
    except rpcerr.NoMasterError, err:
      raise http.HttpBadGateway("Can't connect to master daemon: %s" % err)
Exemplo n.º 6
0
  def GetClient(self):
    """Wrapper for L{luxi.Client} with HTTP-specific error handling.

    """
    # Could be a function, pylint: disable=R0201
    try:
      return self._client_cls()
    except rpcerr.NoMasterError as err:
      raise http.HttpBadGateway("Can't connect to master daemon: %s" % err)
    except rpcerr.PermissionError:
      raise http.HttpInternalServerError("Internal error: no permission to"
                                         " connect to the master daemon")
Exemplo n.º 7
0
    def GetClient(self, query=True):
        """Wrapper for L{luxi.Client} with HTTP-specific error handling.

    @param query: this signifies that the client will only be used for
        queries; if the build-time parameter enable-split-queries is
        enabled, then the client will be connected to the query socket
        instead of the masterd socket

    """
        if query:
            address = pathutils.QUERY_SOCKET
        else:
            address = None
        # Could be a function, pylint: disable=R0201
        try:
            return self._client_cls(address=address)
        except rpcerr.NoMasterError, err:
            raise http.HttpBadGateway("Can't connect to master daemon: %s" %
                                      err)
Exemplo n.º 8
0
  def SubmitJob(self, op, cl=None):
    """Generic wrapper for submit job, for better http compatibility.

    @type op: list
    @param op: the list of opcodes for the job
    @type cl: None or luxi.Client
    @param cl: optional luxi client to use
    @rtype: string
    @return: the job ID

    """
    if cl is None:
      cl = self.GetClient()
    try:
      return cl.SubmitJob(op)
    except errors.JobQueueFull:
      raise http.HttpServiceUnavailable("Job queue is full, needs archiving")
    except errors.JobQueueDrainError:
      raise http.HttpServiceUnavailable("Job queue is drained, cannot submit")
    except rpcerr.NoMasterError, err:
      raise http.HttpBadGateway("Master seems to be unreachable: %s" % err)