def update_resource(self, resource_data): """ Update a resource. Broadcast it on the bus and store the update in the database. """ ds = DataStore.instance() resource = R.deserialize(resource_data) version = Version(resource.id) version.data = resource_data version.save() if not ds.contains(Resource, version.resource_id): res = Resource(version.resource_id) res.save() if not ds.contains(Agent, res.agent_name): agent = Agent(res.agent_name) agent.save() # broadcast topic = "%s.%s" % (resource.id.agent_name, resource.id.entity_type) msg = amqp.Message(json.dumps({"operation" : "UPDATE", "resource": resource_data})) msg.content_type = "application/json" self._channel.basic_publish(msg, exchange = self._exchange_name, routing_key = "resources.%s" % topic)
def deploy(config, root_scope, remote = None, dry_run = True, ip_address = None): deploy_host = None all_names = [] if remote is None: hostname = socket.gethostname() else: hostname = remote print("Deploying on %s (dry-run = %s)" % (hostname, dry_run)) try: servers = root_scope.get_variable("Host", ["std"]).value for server in servers: all_names.append(server.name) if server.name == hostname: deploy_host = server except Exception: print("The std module is not loaded or does not contain the definition of Host") return if deploy_host is None: print("Unable to find a host to deploy on the current machine %s" % hostname) print("Host found in model: " + ", ".join(all_names)) return export = Exporter(config) json_data = export.run(root_scope, offline = True) files = export.get_offline_files() if remote is not None: ip_address = remote agent = Agent(config, False, hostname, offline = True, deploy = not dry_run, remote = ip_address) agent._offline_files = files host_id = "[%s," % deploy_host.name for item in json.loads(json_data.decode("utf-8")): if host_id in item["id"]: agent.update(Resource.deserialize(item)) if agent._queue.size() == 0: print("No configuration found for host %s" % hostname) return print("Deploying config") agent.deploy_config() #agent.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)