def get_facts(self, res): """ Get status """ try: provider = Commander.get_provider(self, res.id) except Exception: LOGGER.error("Unable to find a handler for %s" % res.id) return provider.facts(res)
def deploy_code(self, version): """ Deploy code to the server """ if not self._offline: LOGGER.info("Sending resources and handler source to server") sources = resource.sources() sources.update(Commander.sources()) LOGGER.info("Uploading source files") conn = self._server_connection() conn.request("POST", "/code/%d" % version, body = json.dumps(sources)) res = conn.getresponse() if res.status != 200: raise Exception("Unable to upload handler plugin code to the server") conn.close() else: LOGGER.info("Offline mode, so not sending configuration to the server")
def deploy_config(self): """ Deploy a configuration is there are items in the queue """ LOGGER.debug("Execute deploy config") LOGGER.info("Need to update %d resources" % self._queue.size()) while self._queue.size() > 0: resource = self._queue.pop() if resource is None: LOGGER.info("No resources ready for deploy.") break try: provider = Commander.get_provider(self, resource.id) except Exception as e: LOGGER.exception("Unable to find a handler for %s" % resource.id, e) # TODO: submit failure self._queue.remove(resource) continue try: provider.execute(resource, self.deploy) if resource.do_reload and provider.can_reload(): LOGGER.warning("Reloading %s because of updated dependencies" % resource.id) provider.do_reload(resource) LOGGER.debug("Finished %s" % resource) self._queue.remove(resource) except Exception as e: LOGGER.exception(e) # TODO: report back self._queue.remove(resource) return
def close(self): """ Cleanup """ Commander.close()
def _handle_op(self, operation, message): """ Handle an operation """ if operation == "PING": LOGGER.info("Got ping request, sending pong back") response = {"hostname" : self._hostnames } self._mq_send("control", "PONG", response) elif operation == "UPDATE": LOGGER.debug("Received update for %s", message["resource"]["id"]) resource = Resource.deserialize(message["resource"]) self.update(resource) elif operation == "UPDATED": rid = Id.parse_id(message["id"]) version = message["version"] reload = message["reload"] self._dm.resource_update(rid.resource_str(), version, reload) elif operation == "STATUS": resource = Id.parse_id(message["id"]).get_instance() if resource is None: self._mq_send("control", "STATUS_REPLY", {"code" : 404}) return try: provider = Commander.get_provider(self, resource.id) except Exception: LOGGER.exception("Unable to find a handler for %s" % resource) try: result = provider.check_resource(resource) self._mq_send("control", "STATUS_REPLY", result) except Exception: LOGGER.exception("Unable to check status of %s" % resource) self._mq_send("control", "STATUS_REPLY", {"code" : 404}) elif operation == "FACTS": resource_id = Id.parse_id(message["id"]) try: resource = Resource.deserialize(message["resource"]) provider = Commander.get_provider(self, resource_id) try: result = provider.facts(resource) response = {"operation" : "FACTS_REPLY", "subject" : str(resource_id), "facts" : result} self._mq_send("control", "FACTS_REPLY", response) except Exception: LOGGER.exception("Unable to retrieve fact") self._mq_send("control", "FACTS_REPLY", {"subject" : str(resource_id), "code": 404}) except Exception: LOGGER.exception("Unable to find a handler for %s" % resource_id) elif operation == "QUEUE": response = {"queue" : ["%s,v=%d" % (x.id, x.version) for x in self._queue.all()]} self._mq_send("control", "QUEUE_REPLY", response) elif operation == "DEPLOY": self.deploy_config() elif operation == "INFO": response = {"threads" : [x.name for x in enumerate()], "queue length" : self._queue.size(), "queue ready length" : self._queue.ready_size(), } self._mq_send("control", "INFO_REPLY", response) elif operation == "DUMP": LOGGER.info("Dumping!") self._queue.dump() elif operation == "MODULE_UPDATE": version = message["version"] modules = message["modules"] self._loader.deploy_version(version, modules)