def _calculate_hosts(self, cluster): """ Calculates the hosts metadata for the cluster. :param cluster: The name of the cluster. :type cluster: str """ # XXX: Not sure which wil be more efficient: fetch all # the host data in one etcd call and sort through # them, or fetch the ones we need individually. # For the MVP phase, fetch all is better. # etcd_resp, error = cherrypy.engine.publish( # 'store-get', '/commissaire/hosts')[0] # if error: try: hosts = Hosts.retrieve() except: self.logger.warn( 'Etcd does not have any hosts. ' 'Cannot determine cluster stats.') return available = unavailable = total = 0 for host in hosts.hosts: if host.address in cluster.hostset: total += 1 if host.status == 'active': available += 1 else: unavailable += 1 cluster.hosts['total'] = total cluster.hosts['available'] = available cluster.hosts['unavailable'] = unavailable
def on_get(self, req, resp): """ Handles GET requests for Hosts. :param req: Request instance that will be passed through. :type req: falcon.Request :param resp: Response instance that will be passed through. :type resp: falcon.Response """ try: hosts = Hosts.retrieve() if len(hosts.hosts) == 0: raise Exception() resp.status = falcon.HTTP_200 req.context["model"] = hosts except: # This was originally a "no content" but I think a 404 makes # more sense if there are no hosts self.logger.warn("Etcd does not have any hosts. Returning [] and 404.") resp.status = falcon.HTTP_404 req.context["model"] = None return