예제 #1
0
    def send(self, nodes_stats):
        deployment_id = helper.get_deployment_id()
        # If the deployment is not registered, skip.
        if not deployment_id:
            return

        # Send request to AppScale Portal.
        portal_path = self._portal_method.format(deployment_id=deployment_id)
        url = "{0}{1}".format(constants.PORTAL_URL, portal_path)
        data = {
            'deployment_id':
            deployment_id,
            'nodes_stats':
            json.dumps({
                node_ip: [stats_to_dict(snapshot) for snapshot in snapshots]
                for node_ip, snapshots in nodes_stats.iteritems()
            })
        }
        snapshots_num = sum(
            len(snapshots) for snapshots in nodes_stats.values())
        logger.debug(
            "Sending {snapshots} node stats snapshots about {nodes} nodes to the "
            "AppScale Portal".format(snapshots=snapshots_num,
                                     nodes=len(nodes_stats)))

        request = helper.create_request(url=url,
                                        method='POST',
                                        body=urllib.urlencode(data))
        response = helper.urlfetch(request)

        if not response[JSONTags.SUCCESS]:
            logger.error("Inaccessible resource: {}".format(url))
            return
예제 #2
0
파일: portal.py 프로젝트: AppScale/appscale
  def send(self, nodes_stats):
    deployment_id = helper.get_deployment_id()
    # If the deployment is not registered, skip.
    if not deployment_id:
      return

    # Send request to AppScale Portal.
    portal_path = self._portal_method.format(deployment_id=deployment_id)
    url = "{0}{1}".format(constants.PORTAL_URL, portal_path)
    data = {
      'deployment_id': deployment_id,
      'nodes_stats': json.dumps({
        node_ip: [stats_to_dict(snapshot) for snapshot in snapshots]
        for node_ip, snapshots in nodes_stats.iteritems()
      })
    }
    snapshots_num = sum(len(snapshots) for snapshots in nodes_stats.values())
    logger.debug(
      "Sending {snapshots} node stats snapshots about {nodes} nodes to the "
      "AppScale Portal".format(snapshots=snapshots_num, nodes=len(nodes_stats))
    )

    request = helper.create_request(url=url, method='POST',
                                    body=urllib.urlencode(data))
    response = helper.urlfetch(request)

    if not response[JSONTags.SUCCESS]:
      logger.error("Inaccessible resource: {}".format(url))
      return
예제 #3
0
    async def __call__(self, request):
        """ Handles HTTP request.

    Args:
      request: an instance of Request.
    Returns:
      An instance of Resposne.
    """
        if request.has_body:
            payload = await request.json()
        else:
            payload = {}
        include_lists = payload.get('include_lists')
        max_age = payload.get('max_age', ACCEPTABLE_STATS_AGE)

        if include_lists is not None:
            try:
                include_lists = IncludeLists(include_lists)
            except WrongIncludeLists as err:
                logger.warn("Bad request from {client} ({error})".format(
                    client=request.remote, error=err))
                return web.Response(status=http.HTTPStatus.BAD_REQUEST,
                                    reason='Wrong include_lists',
                                    text=str(err))
        else:
            include_lists = self.default_include_lists

        snapshot = None

        # Try to use cached snapshot
        if self.cached_snapshot:
            now = time.time()
            acceptable_time = now - max_age
            if self.cached_snapshot.utc_timestamp >= acceptable_time:
                snapshot = self.cached_snapshot
                logger.info(
                    "Returning cached snapshot with age {:.2f}s".format(
                        now - self.cached_snapshot.utc_timestamp))

        if not snapshot:
            snapshot = self.stats_source.get_current()
            if inspect.isawaitable(snapshot):
                snapshot = await snapshot
            self.cached_snapshot = snapshot

        return web.json_response(stats_to_dict(snapshot, include_lists))
예제 #4
0
    def get(self):
        if self.request.headers.get(SECRET_HEADER) != options.secret:
            logging.warn("Received bad secret from {client}".format(
                client=self.request.remote_ip))
            self.set_status(HTTP_Codes.HTTP_DENIED, "Bad secret")
            return
        if self.request.body:
            payload = json.loads(self.request.body)
        else:
            payload = {}
        include_lists = payload.get('include_lists')
        max_age = payload.get('max_age', ACCEPTABLE_STATS_AGE)

        if include_lists is not None:
            try:
                include_lists = IncludeLists(include_lists)
            except WrongIncludeLists as err:
                logging.warn("Bad request from {client} ({error})".format(
                    client=self.request.remote_ip, error=err))
                json.dump({'error': str(err)}, self)
                self.set_status(HTTP_Codes.HTTP_BAD_REQUEST,
                                'Wrong include_lists')
                return
        else:
            include_lists = self._default_include_lists

        snapshot = None

        # Try to use cached snapshot
        if self._cached_snapshot:
            now = time.time()
            acceptable_time = now - max_age
            if self._cached_snapshot.utc_timestamp >= acceptable_time:
                snapshot = self._cached_snapshot
                logging.info(
                    "Returning cached snapshot with age {:.2f}s".format(
                        now - self._cached_snapshot.utc_timestamp))

        if not snapshot:
            snapshot = self._stats_source.get_current()
            if isinstance(snapshot, gen.Future):
                snapshot = yield snapshot
            self._cached_snapshot = snapshot

        json.dump(stats_to_dict(snapshot, include_lists), self)
예제 #5
0
  def get(self):
    if self.request.headers.get(SECRET_HEADER) != options.secret:
      logger.warn("Received bad secret from {client}"
                   .format(client=self.request.remote_ip))
      self.set_status(HTTP_Codes.HTTP_DENIED, "Bad secret")
      return
    if self.request.body:
      payload = json.loads(self.request.body)
    else:
      payload = {}
    include_lists = payload.get('include_lists')
    max_age = payload.get('max_age', ACCEPTABLE_STATS_AGE)

    if include_lists is not None:
      try:
        include_lists = IncludeLists(include_lists)
      except WrongIncludeLists as err:
        logger.warn("Bad request from {client} ({error})"
                     .format(client=self.request.remote_ip, error=err))
        json.dump({'error': str(err)}, self)
        self.set_status(HTTP_Codes.HTTP_BAD_REQUEST, 'Wrong include_lists')
        return
    else:
      include_lists = self._default_include_lists

    snapshot = None

    # Try to use cached snapshot
    if self._cached_snapshot:
      now = time.time()
      acceptable_time = now - max_age
      if self._cached_snapshot.utc_timestamp >= acceptable_time:
        snapshot = self._cached_snapshot
        logger.info("Returning cached snapshot with age {:.2f}s"
                     .format(now-self._cached_snapshot.utc_timestamp))

    if not snapshot:
      snapshot = self._stats_source.get_current()
      if isinstance(snapshot, gen.Future):
        snapshot = yield snapshot
      self._cached_snapshot = snapshot

    json.dump(stats_to_dict(snapshot, include_lists), self)
예제 #6
0
  def get(self):
    if self.request.headers.get(SECRET_HEADER) != options.secret:
      logger.warn("Received bad secret from {client}"
                   .format(client=self.request.remote_ip))
      self.set_status(HTTP_Codes.HTTP_DENIED, "Bad secret")
      return
    if self.request.body:
      payload = json.loads(self.request.body)
    else:
      payload = {}
    include_lists = payload.get('include_lists')
    max_age = payload.get('max_age', ACCEPTABLE_STATS_AGE)

    if include_lists is not None:
      try:
        include_lists = IncludeLists(include_lists)
      except WrongIncludeLists as err:
        logger.warn("Bad request from {client} ({error})"
                     .format(client=self.request.remote_ip, error=err))
        json.dump({'error': str(err)}, self)
        self.set_status(HTTP_Codes.HTTP_BAD_REQUEST, 'Wrong include_lists')
        return
    else:
      include_lists = self._default_include_lists

    newer_than = time.mktime(datetime.now().timetuple()) - max_age

    if (not self._default_include_lists or
        include_lists.is_subset_of(self._default_include_lists)):
      # If user didn't specify any non-default fields we can use local cache
      fresh_local_snapshots = {
        node_ip: snapshot
        for node_ip, snapshot in self._cached_snapshots.iteritems()
        if max_age and snapshot.utc_timestamp > newer_than
      }
      if fresh_local_snapshots:
        logger.debug("Returning cluster stats with {} cached snapshots"
                      .format(len(fresh_local_snapshots)))
    else:
      fresh_local_snapshots = {}

    new_snapshots_dict, failures = (
      yield self._current_cluster_stats_source.get_current(
        max_age=max_age, include_lists=include_lists,
        exclude_nodes=fresh_local_snapshots.keys()
      )
    )

    # Put new snapshots to local cache
    self._cached_snapshots.update(new_snapshots_dict)

    # Extend fetched snapshots dict with fresh local snapshots
    new_snapshots_dict.update(fresh_local_snapshots)

    rendered_snapshots = {
      node_ip: stats_to_dict(snapshot, include_lists)
      for node_ip, snapshot in new_snapshots_dict.iteritems()
    }

    json.dump({
      "stats": rendered_snapshots,
      "failures": failures
    }, self)
예제 #7
0
    def get(self):
        if self.request.headers.get(SECRET_HEADER) != options.secret:
            logging.warn("Received bad secret from {client}".format(
                client=self.request.remote_ip))
            self.set_status(HTTP_Codes.HTTP_DENIED, "Bad secret")
            return
        if self.request.body:
            payload = json.loads(self.request.body)
        else:
            payload = {}
        include_lists = payload.get('include_lists')
        max_age = payload.get('max_age', ACCEPTABLE_STATS_AGE)

        if include_lists is not None:
            try:
                include_lists = IncludeLists(include_lists)
            except WrongIncludeLists as err:
                logging.warn("Bad request from {client} ({error})".format(
                    client=self.request.remote_ip, error=err))
                json.dump({'error': str(err)}, self)
                self.set_status(HTTP_Codes.HTTP_BAD_REQUEST,
                                'Wrong include_lists')
                return
        else:
            include_lists = self._default_include_lists

        newer_than = time.mktime(datetime.now().timetuple()) - max_age

        if (not self._default_include_lists
                or include_lists.is_subset_of(self._default_include_lists)):
            # If user didn't specify any non-default fields we can use local cache
            fresh_local_snapshots = {
                node_ip: snapshot
                for node_ip, snapshot in self._cached_snapshots.iteritems()
                if max_age and snapshot.utc_timestamp > newer_than
            }
            if fresh_local_snapshots:
                logging.debug(
                    "Returning cluster stats with {} cached snapshots".format(
                        len(fresh_local_snapshots)))
        else:
            fresh_local_snapshots = {}

        new_snapshots_dict, failures = (
            yield self._current_cluster_stats_source.get_current(
                max_age=max_age,
                include_lists=include_lists,
                exclude_nodes=fresh_local_snapshots.keys()))

        # Put new snapshots to local cache
        self._cached_snapshots.update(new_snapshots_dict)

        # Extend fetched snapshots dict with fresh local snapshots
        new_snapshots_dict.update(fresh_local_snapshots)

        rendered_snapshots = {
            node_ip: stats_to_dict(snapshot, include_lists)
            for node_ip, snapshot in new_snapshots_dict.iteritems()
        }

        json.dump({"stats": rendered_snapshots, "failures": failures}, self)
예제 #8
0
    async def __call__(self, request):
        """ Handles HTTP request.

    Args:
      request: an instance of Request.
    Returns:
      An instance of Response.
    """
        if request.has_body:
            payload = await request.json()
        else:
            payload = {}
        include_lists = payload.get('include_lists')
        max_age = payload.get('max_age', ACCEPTABLE_STATS_AGE)

        if include_lists is not None:
            try:
                include_lists = IncludeLists(include_lists)
            except WrongIncludeLists as err:
                logger.warn("Bad request from {client} ({error})".format(
                    client=request.remote, error=err))
                return web.Response(status=http.HTTPStatus.BAD_REQUEST,
                                    reason='Wrong include_lists',
                                    text=str(err))
        else:
            include_lists = self.default_include_lists

        newer_than = time.mktime(datetime.now().timetuple()) - max_age

        if (not self.default_include_lists
                or include_lists.is_subset_of(self.default_include_lists)):
            # If user didn't specify any non-default fields we can use local cache
            fresh_local_snapshots = {
                node_ip: snapshot
                for node_ip, snapshot in self.cached_snapshots.items()
                if max_age and snapshot.utc_timestamp > newer_than
            }
            if fresh_local_snapshots:
                logger.debug(
                    "Returning cluster stats with {} cached snapshots".format(
                        len(fresh_local_snapshots)))
        else:
            fresh_local_snapshots = {}

        new_snapshots_dict, failures = (await self.stats_source.get_current(
            max_age=max_age,
            include_lists=include_lists,
            exclude_nodes=list(fresh_local_snapshots.keys())))

        # Put new snapshots to local cache
        self.cached_snapshots.update(new_snapshots_dict)

        # Extend fetched snapshots dict with fresh local snapshots
        new_snapshots_dict.update(fresh_local_snapshots)

        rendered_snapshots = {
            node_ip: stats_to_dict(snapshot, include_lists)
            for node_ip, snapshot in new_snapshots_dict.items()
        }

        return web.json_response({
            "stats": rendered_snapshots,
            "failures": failures
        })