def test_state(self): dsm = DatastoreManager() state_repo = StateRepository(dsm) state_repo1 = StateRepository(dsm) state1 = {'key': 'value1'} state_repo.put_state("id1", state1) state2 = state_repo.get_state("id1") self.assertEquals(state1, state2) state3 = {'key': 'value2', 'key2': {}} state_repo.put_state("id1", state3) state4 = state_repo.get_state("id1") self.assertEquals(state3, state4)
def test_state(self): dsm = DatastoreManager() state_repo = StateRepository(dsm) state_repo1 = StateRepository(dsm) state1 = {'key':'value1'} state_repo.put_state("id1", state1) state2 = state_repo.get_state("id1") self.assertEquals(state1, state2) state3 = {'key':'value2', 'key2': {}} state_repo.put_state("id1", state3) state4 = state_repo.get_state("id1") self.assertEquals(state3, state4)
def start(self): log.debug("Container starting...") if self._is_started: raise ContainerError("Container already started") # Check if this UNIX process already runs a Container. self.pidfile = "cc-pid-%d" % os.getpid() if os.path.exists(self.pidfile): raise ContainerError("Container.on_start(): Container is a singleton per UNIX process. Existing pid file found: %s" % self.pidfile) # write out a PID file containing our agent messaging name with open(self.pidfile, 'w') as f: pid_contents = {'messaging': dict(CFG.server.amqp), 'container-agent': self.name, 'container-xp': bootstrap.get_sys_name() } f.write(msgpack.dumps(pid_contents)) atexit.register(self._cleanup_pid) self._capabilities.append("PID_FILE") # set up abnormal termination handler for this container def handl(signum, frame): try: self._cleanup_pid() # cleanup the pidfile first self.quit() # now try to quit - will not error on second cleanup pidfile call finally: signal.signal(signal.SIGTERM, self._normal_signal) os.kill(os.getpid(), signal.SIGTERM) self._normal_signal = signal.signal(signal.SIGTERM, handl) self.datastore_manager.start() self._capabilities.append("DATASTORE_MANAGER") # Self-register with Directory self.directory.register("/Containers", self.id, cc_agent=self.name) self.directory.register("/Containers/%s" % self.id, "Processes") self._capabilities.append("DIRECTORY") # Event repository self.event_repository = EventRepository() self.event_pub = EventPublisher() self._capabilities.append("EVENT_REPOSITORY") # Local resource registry self.resource_registry = ResourceRegistry() self._capabilities.append("RESOURCE_REGISTRY") # Persistent objects self.datastore_manager.get_datastore("objects", DataStore.DS_PROFILE.OBJECTS) # State repository self.state_repository = StateRepository() self._capabilities.append("STATE_REPOSITORY") # Start ExchangeManager, which starts the node (broker connection) self.ex_manager.start() self._capabilities.append("EXCHANGE_MANAGER") self.proc_manager.start() self._capabilities.append("PROC_MANAGER") self.app_manager.start() self._capabilities.append("APP_MANAGER") self.governance_controller.start() self._capabilities.append("GOVERNANCE_CONTROLLER") if CFG.container.get('sflow', {}).get('enabled', False): self.sflow_manager.start() self._capabilities.append("SFLOW_MANAGER") # Start the CC-Agent API rsvc = ProcessRPCServer(node=self.node, from_name=self.name, service=self, process=self) # Start an ION process with the right kind of endpoint factory proc = self.proc_manager.proc_sup.spawn(name=self.name, listeners=[rsvc], service=self) self.proc_manager.proc_sup.ensure_ready(proc) self._capabilities.append("CONTAINER_AGENT") self.event_pub.publish_event(event_type="ContainerLifecycleEvent", origin=self.id, origin_type="CapabilityContainer", sub_type="START", state=ContainerStateEnum.START) self._is_started = True self._status = "RUNNING" log.info("Container started, OK.")
def test_state(self): dsm = DatastoreManager() state_repo = StateRepository(dsm) state_repo.start() state_repo1 = StateRepository(dsm) state1 = {'key':'value1'} state_repo.put_state("id1", state1) state2, state_obj2 = state_repo.get_state("id1") self.assertEquals(state1, state2) self.assertEquals(state_obj2.state, state2) self.assertTrue(state_obj2.ts) self.assertTrue(state_obj2._rev) self.assertEquals(state_obj2._id, "id1") state3 = {'key':'value2', 'key2': {}} state_repo.put_state("id1", state3) state4, state_obj4 = state_repo.get_state("id1") self.assertEquals(state3, state4) # Test persisting with a prior state object to save a read state5 = {'key':'value5', 'key2': {}} state_repo.put_state("id1", state5, state_obj=state_obj4) state6, state_obj6 = state_repo.get_state("id1") self.assertEquals(state5, state6) # Test that using an old state object will not screw up state7 = {'key':'value7', 'key2': {}} state_repo.put_state("id1", state7, state_obj=state_obj4)