Exemple #1
0
    def test_create_listening_endpoint(self):
        self.patch_cfg(
            'pyon.container.procs.CFG', {
                'container': {
                    'messaging': {
                        'endpoint': {
                            'proc_listening_type':
                            'pyon.container.test.test_procs.TestRPCServer'
                        }
                    }
                }
            })

        fakecc = FakeContainer()
        fakecc.resource_registry = Mock()
        fakecc.resource_registry.create.return_value = ["ID", "rev"]

        with patch(
                'pyon.container.procs.ProcManager._get_execution_engine_config',
                Mock(return_value={})):
            pm = ProcManager(fakecc)
            pm._get_execution_engine_config = Mock()

            ep = pm._create_listening_endpoint(node=sentinel.node,
                                               service=sentinel.service,
                                               process=sentinel.process)

            self.assertIsInstance(ep, TestRPCServer)
Exemple #2
0
    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        self._is_started = False

        self._capabilities = []

        # set container id and cc_agent name (as they are set in base class call)
        self.id = get_default_container_id()
        self.name = "cc_agent_%s" % self.id

        Container.instance = self

        from pyon.core import bootstrap
        bootstrap.container_instance = self

        log.debug("Container (sysname=%s) initializing ..." % bootstrap.get_sys_name())

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()

        self.datastore_manager.start()
        self._capabilities.append("DATASTORE_MANAGER")

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = kwargs

        # Instantiate Directory and self-register
        # Has the additional side effect of either
        # bootstrapping the configuration into the
        # directory or read the configuration based
        # in the value of the auto_bootstrap setting
        self.directory = Directory()

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # File System - Interface to the OS File System, using correct path names and setups
        self.file_system = FileSystem(CFG)

        # Governance Controller - manages the governance related interceptors
        self.governance_controller = GovernanceController(self)

        # sFlow manager - controls sFlow stat emission
        self.sflow_manager = SFlowManager(self)

        # Coordinates the container start
        self._status = "INIT"

        # protection for when the container itself is used as a Process for clients
        self.container = self

        log.debug("Container initialized, OK.")
Exemple #3
0
    def setUp(self):
        self.container = Mock()
        self.pm = ProcManager(self.container)

        self.container.resource_registry.create.return_value = (sentinel.rid,
                                                                sentinel.rev)
        self.container.resource_registry.find_resources.return_value = ([
            sentinel.oid
        ], [sentinel.orev])
Exemple #4
0
    def test_procmanager_iso(self):
        fakecc = FakeContainer()
        fakecc.resource_registry = Mock()
        fakecc.resource_registry.create.return_value=["ID","rev"]

        pm = ProcManager(fakecc)
        self.assertTrue(hasattr(fakecc, "spawn_process"))
        pm.start()
        pm.stop()
Exemple #5
0
    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        self._is_started = False

        # set id and name (as they are set in base class call)
        self.id = string.replace('%s_%d' % (os.uname()[1], os.getpid()), ".",
                                 "_")
        self.name = "cc_agent_%s" % self.id

        Container.instance = self

        # TODO: Bug: Replacing CFG instance not work because references are already public. Update directly
        dict_merge(CFG, kwargs, inplace=True)
        from pyon.core import bootstrap
        bootstrap.container_instance = self
        bootstrap.assert_configuration(CFG)
        log.debug("Container (sysname=%s) initializing ..." %
                  bootstrap.get_sys_name())

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = kwargs

        # Load object and service registry etc.
        bootstrap_pyon()

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()

        # File System - Interface to the OS File System, using correct path names and setups
        self.file_system = FileSystem(CFG)

        # Governance Controller - manages the governance related interceptors
        self.governance_controller = GovernanceController(self)

        # sFlow manager - controls sFlow stat emission
        self.sflow_manager = SFlowManager(self)

        # Coordinates the container start
        self._is_started = False
        self._capabilities = []
        self._status = "INIT"

        # protection for when the container itself is used as a Process for clients
        self.container = self

        log.debug("Container initialized, OK.")
Exemple #6
0
    def test_create_listening_endpoint(self):
        self.patch_cfg('pyon.container.procs.CFG', {'container':{'messaging':{'endpoint':{'proc_listening_type':'pyon.container.test.test_procs.TestRPCServer'}}}})

        fakecc = FakeContainer()
        fakecc.resource_registry = Mock()
        fakecc.resource_registry.create.return_value=["ID","rev"]

        pm = ProcManager(fakecc)

        ep = pm._create_listening_endpoint(node=sentinel.node,
                                           service=sentinel.service,
                                           process=sentinel.process)

        self.assertIsInstance(ep, TestRPCServer)
Exemple #7
0
    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        # set id and name (as they are set in base class call)
        self.id = string.replace('%s_%d' % (os.uname()[1], os.getpid()), ".", "_")
        self.name = "cc_agent_%s" % self.id

        Container.instance = self

        # TODO: Bug: Replacing CFG instance not work because references are already public. Update directly
        dict_merge(CFG, kwargs)
        from pyon.core import bootstrap
        bootstrap.sys_name = CFG.system.name or bootstrap.sys_name
        log.debug("Container (sysname=%s) initializing ..." % bootstrap.sys_name)

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = DictModifier(CFG, kwargs)

        # Load object and service registry etc.
        bootstrap_pyon()

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()
        
        log.debug("Container initialized, OK.")
Exemple #8
0
    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        self._is_started = False

        # set id and name (as they are set in base class call)
        self.id = string.replace('%s_%d' % (os.uname()[1], os.getpid()), ".", "_")
        self.name = "cc_agent_%s" % self.id

        Container.instance = self

        # TODO: Bug: Replacing CFG instance not work because references are already public. Update directly
        dict_merge(CFG, kwargs, inplace=True)
        from pyon.core import bootstrap
        bootstrap.container_instance = self
        bootstrap.assert_configuration(CFG)
        log.debug("Container (sysname=%s) initializing ..." % bootstrap.get_sys_name())

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = kwargs

        # Load object and service registry etc.
        bootstrap_pyon()

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()

        # File System - Interface to the OS File System, using correct path names and setups
        self.file_system = FileSystem(CFG)

        # Governance Controller - manages the governance related interceptors
        self.governance_controller = GovernanceController(self)

        # sFlow manager - controls sFlow stat emission
        self.sflow_manager = SFlowManager(self)

        # Coordinates the container start
        self._is_started = False
        self._capabilities = []
        self._status = "INIT"

        # protection for when the container itself is used as a Process for clients
        self.container = self

        log.debug("Container initialized, OK.")
Exemple #9
0
    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        self._is_started = False
        # set container id and cc_agent name (as they are set in base class call)
        self.id = get_default_container_id()
        self.name = "cc_agent_%s" % self.id
        self._capabilities = []

        from pyon.core import bootstrap
        bootstrap.container_instance = self
        Container.instance = self

        log.debug("Container (sysname=%s) initializing ..." % bootstrap.get_sys_name())

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = kwargs

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()

        # TODO: Do not start a capability here. Symmetric start/stop
        self.datastore_manager.start()
        self._capabilities.append("DATASTORE_MANAGER")

        # Instantiate Directory
        self.directory = Directory()

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # File System - Interface to the OS File System, using correct path names and setups
        self.file_system = FileSystem(CFG)

        # Governance Controller - manages the governance related interceptors
        self.governance_controller = GovernanceController(self)

        # sFlow manager - controls sFlow stat emission
        self.sflow_manager = SFlowManager(self)

        # Coordinates the container start
        self._status = "INIT"

        # protection for when the container itself is used as a Process for clients
        self.container = self

        log.debug("Container initialized, OK.")
Exemple #10
0
    def test_procmanager_iso(self):
        fakecc = FakeContainer()
        fakecc.resource_registry = Mock()
        fakecc.resource_registry.create.return_value = ["ID", "rev"]

        pm = ProcManager(fakecc)
        self.assertTrue(hasattr(fakecc, "spawn_process"))
        pm.start()
        pm.stop()
Exemple #11
0
    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        self._is_started = False
        # set container id and cc_agent name (as they are set in base class call)
        self.id = get_default_container_id()
        self.name = "cc_agent_%s" % self.id
        self._capabilities = []

        bootstrap.container_instance = self
        Container.instance = self

        log.debug("Container (sysname=%s) initializing ..." %
                  bootstrap.get_sys_name())

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = kwargs

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()

        # TODO: Do not start a capability here. Symmetric start/stop
        self.datastore_manager.start()
        self._capabilities.append("DATASTORE_MANAGER")

        # Instantiate Directory
        self.directory = Directory()

        # internal router
        self.local_router = None

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # File System - Interface to the OS File System, using correct path names and setups
        self.file_system = FileSystem(CFG)

        # Governance Controller - manages the governance related interceptors
        self.governance_controller = GovernanceController(self)

        # sFlow manager - controls sFlow stat emission
        self.sflow_manager = SFlowManager(self)

        # Coordinates the container start
        self._status = "INIT"

        # protection for when the container itself is used as a Process for clients
        self.container = self

        # publisher, initialized in start()
        self.event_pub = None

        # context-local storage
        self.context = LocalContextMixin()

        log.debug("Container initialized, OK.")
Exemple #12
0
class TestProcManager(PyonTestCase):
    def setUp(self):
        self.container = Mock()
        self.pm = ProcManager(self.container)

        self.container.resource_registry.create.return_value = (sentinel.rid,
                                                                sentinel.rev)
        self.container.resource_registry.find_resources.return_value = ([
            sentinel.oid
        ], [sentinel.orev])

    def test_start(self):
        self.pm.start()

        self.assertEquals(self.pm.cc_id, sentinel.rid)

    def test_start_with_org(self):
        self.patch_cfg('pyon.container.procs.CFG',
                       {'container': {
                           'org_name': 'NOT_DEFAULT'
                       }})
        self.pm.start()

        self.container.resource_registry.create_association.assert_called_once_with(
            sentinel.oid, PRED.hasResource, sentinel.rid)

    @patch('pyon.datastore.couchdb.couchdb_datastore.CouchDB_DataStore._stats')
    def test_stop(self, statsmock):
        self.pm.start()

        self.pm.stop()

        self.assertEquals(statsmock.get_stats.call_count, 2)

    def test__cleanup_method(self):
        ep = Mock()
        ep._chan._queue_auto_delete = False

        self.pm._cleanup_method(sentinel.queue, ep)

        ch = self.container.node.channel.return_value
        ch._destroy_queue.assert_called_once_with()
        self.assertIsInstance(ch._recv_name, NameTrio)
        self.assertIn(str(sentinel.queue), str(ch._recv_name))

    @patch('pyon.container.procs.log')
    def test__cleanup_method_raises_error(self, mocklog):
        ep = Mock()
        ep._chan._queue_auto_delete = False
        ch = self.container.node.channel.return_value
        ch._destroy_queue.side_effect = TransportError

        self.pm._cleanup_method(sentinel.queue, ep)

        self.assertEquals(mocklog.warn.call_count, 1)

    @patch('pyon.datastore.couchdb.couchdb_datastore.CouchDB_DataStore._stats',
           Mock())
    @patch('pyon.container.procs.log')
    def test_stop_with_error(self, mocklog):
        self.pm.start()
        self.pm.terminate_process = Mock(side_effect=BadRequest)

        procmock = Mock()
        procmock._proc_start_time = 0
        procmock.id = sentinel.pid
        self.pm.procs[sentinel.pid] = procmock
        self.pm.procs_by_name['dummy'] = procmock

        self.pm.stop()

        self.pm.terminate_process.assert_called_once_with(sentinel.pid)
        mocklog.warn.assert_has_calls([
            call("Failed to terminate process (%s): %s", sentinel.pid, ANY),
            call("ProcManager procs not empty: %s", self.pm.procs),
            call("ProcManager procs_by_name not empty: %s",
                 self.pm.procs_by_name)
        ])

    def test_list_local_processes(self):
        pmock = Mock()
        pmock.process_type = sentinel.ptype
        pmock2 = Mock()
        pmock2.process_type = sentinel.ptype2

        self.pm.procs = {'one': pmock, 'two': pmock2, 'three': pmock}

        self.assertEquals(self.pm.list_local_processes(),
                          [pmock, pmock2, pmock])

    def test_list_local_processes_proc_type_filter(self):
        pmock = Mock()
        pmock.process_type = sentinel.ptype
        pmock2 = Mock()
        pmock2.process_type = sentinel.ptype2

        self.pm.procs = {'one': pmock, 'two': pmock2, 'three': pmock}

        self.assertEquals(self.pm.list_local_processes(sentinel.ptype2),
                          [pmock2])

    def test_get_a_local_process(self):
        pmock = Mock()
        pmock.name = sentinel.name
        pmock2 = Mock()
        pmock2.name = sentinel.name2

        self.pm.procs = {'one': pmock, 'two': pmock2}

        self.assertEquals(self.pm.get_a_local_process(sentinel.name2), pmock2)

    def test_get_a_local_process_for_agent_res_id(self):
        pmock = Mock()
        pmock.process_type = 'agent'
        pmock.resource_type = sentinel.rtype
        pmock2 = Mock()
        pmock2.process_type = 'agent'
        pmock2.resource_type = sentinel.rtype2

        self.pm.procs = {'one': pmock, 'two': pmock2}

        self.assertEquals(self.pm.get_a_local_process(sentinel.rtype2), pmock2)

    def test_get_a_local_process_no_match(self):
        self.assertIsNone(self.pm.get_a_local_process())

    def test_is_local_service_process(self):
        pmock = Mock()
        pmock.name = sentinel.name
        pmock.process_type = 'simple'
        pmock2 = Mock()
        pmock2.name = sentinel.name2
        pmock2.process_type = 'service'
        pmock3 = Mock()
        pmock3.name = sentinel.name3
        pmock3.process_type = 'service'

        self.pm.procs = {'one': pmock, 'two': pmock2, 'three': pmock3}

        self.assertTrue(self.pm.is_local_service_process(sentinel.name3))

    def test_is_local_service_process_name_matches_but_type_doesnt(self):
        pmock = Mock()
        pmock.name = sentinel.name
        pmock.process_type = 'simple'
        pmock2 = Mock()
        pmock2.name = sentinel.name2
        pmock2.process_type = 'notservice'
        pmock3 = Mock()
        pmock3.name = sentinel.name3
        pmock3.process_type = 'notservice'

        self.pm.procs = {'one': pmock, 'two': pmock2, 'three': pmock3}

        self.assertFalse(self.pm.is_local_service_process(sentinel.name3))

    def test_is_local_agent_process(self):
        # agent is similar to above, but checks resource_type instead
        pmock = Mock()
        pmock.name = sentinel.name
        pmock.process_type = 'simple'
        pmock2 = Mock()
        pmock2.resource_type = sentinel.name2
        pmock2.process_type = 'agent'
        pmock3 = Mock()
        pmock3.name = sentinel.name3
        pmock3.process_type = 'notservice'

        self.pm.procs = {'one': pmock, 'two': pmock2, 'three': pmock3}

        self.assertTrue(self.pm.is_local_agent_process(sentinel.name2))

    def test_is_local_agent_process_not_found(self):
        self.assertFalse(self.pm.is_local_agent_process(sentinel.one))

    def test__unregister_process_errors(self):
        pmock = Mock()
        pmock._proc_name = '1'
        pmock._proc_type = 'service'
        pmock._proc_res_id = sentinel.presid
        pmock._proc_svc_id = sentinel.psvcid

        self.container.resource_registry.delete.side_effect = NotFound
        self.container.resource_registry.find_objects.side_effect = ResourceNotFound

        self.pm.procs[sentinel.pid] = pmock
        self.pm.procs_by_name['1'] = pmock

        self.pm._unregister_process(sentinel.pid, pmock)

        # show we tried to interact with the RR
        self.container.resource_registry.delete.assert_call(
            sentinel.presid, del_associations=True)
        self.container.resource_registry.find_objects.assert_called_once_with(
            sentinel.psvcid, "hasProcess", "Process", id_only=True)
        self.assertEquals(self.pm.procs, {})
        self.assertEquals(self.pm.procs_by_name, {})

        # NEXT: find_objects works fine and gives us an error deletion
        self.container.resource_registry.delete.reset_mock()
        self.container.resource_registry.find_objects.reset_mock()
        self.container.resource_registry.find_objects.side_effect = None
        self.container.resource_registry.find_objects.return_value = ([
            sentinel.svcid
        ], [None])

        self.pm.procs[sentinel.pid] = pmock
        self.pm.procs_by_name['1'] = pmock

        self.pm._unregister_process(sentinel.pid, pmock)

        self.container.resource_registry.delete.assert_calls([
            call(sentinel.presid, del_associations=True),
            call(sentinel.psvcid, del_associations=True)
        ])

        # NEXT: agent
        pmock = Mock()
        pmock.id = sentinel.pid
        pmock._proc_name = '1'
        pmock._proc_type = 'agent'

        self.pm.procs[sentinel.pid] = pmock
        self.pm.procs_by_name['1'] = pmock

        self.pm._unregister_process(sentinel.pid, pmock)

        self.container.directory.unregister_safe.assert_called_once_with(
            "/Agents", sentinel.pid)

    def test__create_listening_endpoint_with_cfg(self):
        self.patch_cfg('pyon.container.procs.CFG',
                       container=dict(messaging=dict(endpoint=dict(
                           proc_listening_type=
                           'pyon.container.test.test_procs.TestRPCServer'))))

        ep = self.pm._create_listening_endpoint(process=sentinel.process)

        self.assertIsInstance(ep, TestRPCServer)

    def test__create_listening_endpoint_without_cfg_and_no_conv(self):
        self.patch_cfg(
            'pyon.container.procs.CFG',
            container=dict(messaging=dict(endpoint=dict(
                proc_listening_type=None, rpc_conversation_enabled=False))))

        ep = self.pm._create_listening_endpoint(process=sentinel.process)

        self.assertIsInstance(ep, ProcessRPCServer)

    def test__create_listening_endpoint_without_cfg_and_conv(self):
        self.patch_cfg(
            'pyon.container.procs.CFG',
            container=dict(messaging=dict(endpoint=dict(
                proc_listening_type=None, rpc_conversation_enabled=True))))

        ep = self.pm._create_listening_endpoint(process=sentinel.process)

        self.assertIsInstance(ep, ConversationRPCServer)
Exemple #13
0
class TestProcManager(PyonTestCase):
    def setUp(self):
        self.container = Mock()
        self.pm = ProcManager(self.container)

        self.container.resource_registry.create.return_value = (sentinel.rid, sentinel.rev)
        self.container.resource_registry.find_resources.return_value = ([sentinel.oid], [sentinel.orev])

    def test_start(self):
        self.pm.start()

        self.assertEquals(self.pm.cc_id, sentinel.rid)

    def test_start_with_org(self):
        self.patch_cfg('pyon.container.procs.CFG', {'container':{'org_name':'NOT_DEFAULT'}})
        self.pm.start()

        self.container.resource_registry.create_association.assert_called_once_with(sentinel.oid, PRED.hasResource, sentinel.rid)

    @patch('pyon.container.procs.log')
    def test_stop_with_error(self, mocklog):
        self.pm.start()
        self.pm.terminate_process = Mock(side_effect=BadRequest)

        procmock = Mock()
        procmock._proc_start_time = 0
        procmock.id = sentinel.pid
        self.pm.procs[sentinel.pid] = procmock
        self.pm.procs_by_name['dummy'] = procmock

        self.pm.stop()

        self.pm.terminate_process.assert_called_once_with(sentinel.pid)
        mocklog.warn.assert_has_calls([call("Failed to terminate process (%s): %s", sentinel.pid, ANY),
                                       call("ProcManager procs not empty: %s", self.pm.procs),
                                       call("ProcManager procs_by_name not empty: %s", self.pm.procs_by_name)])

    def test_list_local_processes(self):
        pmock = Mock()
        pmock.process_type = sentinel.ptype
        pmock2 = Mock()
        pmock2.process_type = sentinel.ptype2

        self.pm.procs = {'one':pmock,
                         'two':pmock2,
                         'three':pmock}

        self.assertEquals(self.pm.list_local_processes(),
                          [pmock, pmock2, pmock])

    def test_list_local_processes_proc_type_filter(self):
        pmock = Mock()
        pmock.process_type = sentinel.ptype
        pmock2 = Mock()
        pmock2.process_type = sentinel.ptype2

        self.pm.procs = {'one':pmock,
                         'two':pmock2,
                         'three':pmock}

        self.assertEquals(self.pm.list_local_processes(sentinel.ptype2),
                          [pmock2])

    def test_get_a_local_process(self):
        pmock = Mock()
        pmock.name = sentinel.name
        pmock2 = Mock()
        pmock2.name = sentinel.name2

        self.pm.procs = {'one':pmock,
                         'two':pmock2}

        self.assertEquals(self.pm.get_a_local_process(sentinel.name2),
                          pmock2)

    def test_get_a_local_process_for_agent_res_id(self):
        pmock = Mock()
        pmock.process_type = 'agent'
        pmock.resource_type = sentinel.rtype
        pmock2 = Mock()
        pmock2.process_type = 'agent'
        pmock2.resource_type = sentinel.rtype2

        self.pm.procs = {'one':pmock,
                         'two':pmock2}

        self.assertEquals(self.pm.get_a_local_process(sentinel.rtype2),
                          pmock2)

    def test_get_a_local_process_no_match(self):
        self.assertIsNone(self.pm.get_a_local_process())

    def test_is_local_service_process(self):
        pmock = Mock()
        pmock.name          = sentinel.name
        pmock.process_type = 'simple'
        pmock2 = Mock()
        pmock2.name         = sentinel.name2
        pmock2.process_type = 'service'
        pmock3 = Mock()
        pmock3.name         = sentinel.name3
        pmock3.process_type = 'service'

        self.pm.procs = {'one':pmock,
                         'two':pmock2,
                         'three':pmock3}

        self.assertTrue(self.pm.is_local_service_process(sentinel.name3))

    def test_is_local_service_process_name_matches_but_type_doesnt(self):
        pmock = Mock()
        pmock.name          = sentinel.name
        pmock.process_type = 'simple'
        pmock2 = Mock()
        pmock2.name         = sentinel.name2
        pmock2.process_type = 'notservice'
        pmock3 = Mock()
        pmock3.name         = sentinel.name3
        pmock3.process_type = 'notservice'

        self.pm.procs = {'one':pmock,
                         'two':pmock2,
                         'three':pmock3}

        self.assertFalse(self.pm.is_local_service_process(sentinel.name3))

    def test_is_local_agent_process(self):
        # agent is similar to above, but checks resource_type instead
        pmock = Mock()
        pmock.name          = sentinel.name
        pmock.process_type = 'simple'
        pmock2 = Mock()
        pmock2.resource_type = sentinel.name2
        pmock2.process_type = 'agent'
        pmock3 = Mock()
        pmock3.name         = sentinel.name3
        pmock3.process_type = 'notservice'

        self.pm.procs = {'one':pmock,
                         'two':pmock2,
                         'three':pmock3}

        self.assertTrue(self.pm.is_local_agent_process(sentinel.name2))

    def test_is_local_agent_process_not_found(self):
        self.assertFalse(self.pm.is_local_agent_process(sentinel.one))

    def test__unregister_process_errors(self):
        pmock = Mock()
        pmock._proc_name = '1'
        pmock._proc_type = 'service'
        pmock._proc_res_id = sentinel.presid
        pmock._proc_svc_id = sentinel.psvcid

        self.container.resource_registry.delete.side_effect = NotFound

        self.pm.procs[sentinel.pid] = pmock
        self.pm.procs_by_name['1'] = pmock

        self.container.resource_registry.find_objects.reset_mock()
        self.container.resource_registry.find_objects.side_effect = None
        self.container.resource_registry.find_objects.return_value = ([],[])

        self.pm._unregister_process(sentinel.pid, pmock)

        # show we tried to interact with the RR
        self.container.resource_registry.delete.assert_call(sentinel.presid, del_associations=True)
        self.container.resource_registry.find_objects.assert_called_once_with(sentinel.psvcid, "hasProcess", "Process", id_only=True)
        self.assertEquals(self.pm.procs, {})
        self.assertEquals(self.pm.procs_by_name, {})

        # NEXT: find_objects works fine and gives us an error deletion
        self.container.resource_registry.delete.reset_mock()
        self.container.resource_registry.find_objects.reset_mock()
        self.container.resource_registry.find_objects.side_effect = None
        self.container.resource_registry.find_objects.return_value = ([sentinel.svcid],[None])

        self.pm.procs[sentinel.pid] = pmock
        self.pm.procs_by_name['1'] = pmock

        self.pm._unregister_process(sentinel.pid, pmock)

        self.container.resource_registry.delete.assert_calls([call(sentinel.presid, del_associations=True),
                                                              call(sentinel.psvcid, del_associations=True)])

        # NEXT: agent
        pmock = Mock()
        pmock.id = sentinel.pid
        pmock._proc_name = '1'
        pmock._proc_type = 'agent'

        self.pm.procs[sentinel.pid] = pmock
        self.pm.procs_by_name['1'] = pmock

        self.pm._unregister_process(sentinel.pid, pmock)

        self.container.directory.unregister_safe.assert_called_once_with("/Agents", sentinel.pid)

    def test__create_listening_endpoint_with_cfg(self):
        self.patch_cfg('pyon.container.procs.CFG', container=dict(messaging=dict(endpoint=dict(proc_listening_type='pyon.container.test.test_procs.TestRPCServer'))))

        ep = self.pm._create_listening_endpoint(process=sentinel.process)

        self.assertIsInstance(ep, TestRPCServer)

    def test__create_listening_endpoint_without_cfg_and_no_conv(self):
        self.patch_cfg('pyon.container.procs.CFG', container=dict(messaging=dict(endpoint=dict(proc_listening_type=None))))

        ep = self.pm._create_listening_endpoint(process=sentinel.process)

        self.assertIsInstance(ep, ProcessRPCServer)

    def test_failed_process(self):
        self.pm.start()
        self.container.fail_fast = Mock()

        self.pm.procs['pid1'] = Mock()

        proc2 = BadProcess()
        self.pm.proc_sup.spawn(name="bad", service=proc2, target=proc2.fail_target)
        gevent.sleep(0)  # Allow the new thread to fail and trigger the chain

        self.assertFalse(self.container.fail_fast.called)

        del self.pm.procs['pid1']

        proc3 = BadProcess()
        self.pm.proc_sup.spawn(name="bad", service=proc3, target=proc3.fail_target)
        gevent.sleep(0)  # Allow the new thread to fail and trigger the chain

        self.assertTrue(self.container.fail_fast.called)
Exemple #14
0
 def test_procmanager_iso(self):
     fakecc = FakeContainer()
     pm = ProcManager(fakecc)
     self.assertTrue(hasattr(fakecc, "spawn_process"))
     pm.start()
     pm.stop()
Exemple #15
0
class Container(BaseContainerAgent):
    """
    The Capability Container. Its purpose is to spawn/monitor processes and services
    that do the bulk of the work in the ION system. It also manages connections to the Exchange
    and the various forms of datastores in the systems.
    """

    # Singleton static variables
    #node        = None
    id          = None
    name        = None
    pidfile     = None
    instance    = None

    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        self._is_started = False

        self._capabilities = []

        # set container id and cc_agent name (as they are set in base class call)
        self.id = get_default_container_id()
        self.name = "cc_agent_%s" % self.id

        Container.instance = self

        from pyon.core import bootstrap
        bootstrap.container_instance = self

        log.debug("Container (sysname=%s) initializing ..." % bootstrap.get_sys_name())

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()

        self.datastore_manager.start()
        self._capabilities.append("DATASTORE_MANAGER")

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = kwargs

        # Instantiate Directory and self-register
        # Has the additional side effect of either
        # bootstrapping the configuration into the
        # directory or read the configuration based
        # in the value of the auto_bootstrap setting
        self.directory = Directory()

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # File System - Interface to the OS File System, using correct path names and setups
        self.file_system = FileSystem(CFG)

        # Governance Controller - manages the governance related interceptors
        self.governance_controller = GovernanceController(self)

        # sFlow manager - controls sFlow stat emission
        self.sflow_manager = SFlowManager(self)

        # Coordinates the container start
        self._status = "INIT"

        # protection for when the container itself is used as a Process for clients
        self.container = self

        log.debug("Container initialized, OK.")

    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.")

    @property
    def node(self):
        """
        Returns the active/default Node that should be used for most communication in the system.

        Defers to exchange manager, but only if it has been started, otherwise returns None.
        """
        if "EXCHANGE_MANAGER" in self._capabilities:
            return self.ex_manager.default_node

        return None

    @contextmanager
    def _push_status(self, new_status):
        """
        Temporarily sets the internal status flag.
        Use this as a decorator or in a with-statement before calling a temporary status changing
        method, like start_rel_from_url.
        """
        curstatus = self._status
        self._status = new_status
        try:
            yield
        finally:
            self._status = curstatus

    def serve_forever(self):
        """ Run the container until killed. """
        log.debug("In Container.serve_forever")
        
        if not self.proc_manager.proc_sup.running:
            self.start()

        # serve forever short-circuits if immediate is on and children len is ok
        num_procs = len(self.proc_manager.proc_sup.children)
        immediate = CFG.system.get('immediate', False)
        if not (immediate and num_procs == 1):  # only spawned greenlet is the CC-Agent

            # print a warning just in case
            if immediate and num_procs != 1:
                log.warn("CFG.system.immediate=True but number of spawned processes is not 1 (%d)", num_procs)

            try:
                # This just waits in this Greenlet for all child processes to complete,
                # which is triggered somewhere else.
                self.proc_manager.proc_sup.join_children()
            except (KeyboardInterrupt, SystemExit) as ex:
                log.info('Received a kill signal, shutting down the container.')
                watch_parent = CFG.system.get('watch_parent', None)
                if watch_parent:
                    watch_parent.kill()
            except:
                log.exception('Unhandled error! Forcing container shutdown')
        else:
            log.debug("Container.serve_forever short-circuiting due to CFG.system.immediate")

        self.proc_manager.proc_sup.shutdown(CFG.cc.timeout.shutdown)

    def status(self):
        """
        Returns the internal status.
        """
        return self._status
            
    def _cleanup_pid(self):
        if self.pidfile:
            log.debug("Cleanup pidfile: %s", self.pidfile)
            try:
                os.remove(self.pidfile)
            except Exception, e:
                log.warn("Pidfile could not be deleted: %s" % str(e))
            self.pidfile = None
Exemple #16
0
 def test_procmanager_iso(self):
     fakecc = FakeContainer()
     pm = ProcManager(fakecc)
     self.assertTrue(hasattr(fakecc, "spawn_process"))
     pm.start()
     pm.stop()
Exemple #17
0
class Container(BaseContainerAgent):
    """
    The Capability Container. Its purpose is to spawn/monitor processes and services
    that do the bulk of the work in the ION system. It also manages connections to the Exchange
    and the various forms of datastores in the systems.
    """

    # Singleton static variables
    #node        = None
    id          = None
    name        = None
    pidfile     = None
    instance    = None

    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        self._is_started = False
        # set container id and cc_agent name (as they are set in base class call)
        self.id = get_default_container_id()
        self.name = "cc_agent_%s" % self.id
        self._capabilities = []

        bootstrap.container_instance = self
        Container.instance = self

        log.debug("Container (sysname=%s) initializing ..." % bootstrap.get_sys_name())

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = kwargs

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()

        # TODO: Do not start a capability here. Symmetric start/stop
        self.datastore_manager.start()
        self._capabilities.append("DATASTORE_MANAGER")

        # Instantiate Directory
        self.directory = Directory()

        # internal router
        self.local_router = None

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # File System - Interface to the OS File System, using correct path names and setups
        self.file_system = FileSystem(CFG)

        # Governance Controller - manages the governance related interceptors
        self.governance_controller = GovernanceController(self)

        # sFlow manager - controls sFlow stat emission
        self.sflow_manager = SFlowManager(self)

        # Coordinates the container start
        self._status = "INIT"

        # protection for when the container itself is used as a Process for clients
        self.container = self

        # publisher, initialized in start()
        self.event_pub = None

        # context-local storage
        self.context = LocalContextMixin()

        log.debug("Container initialized, OK.")

    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)

        # set up greenlet debugging signal handler
        gevent.signal(signal.SIGUSR2, self._handle_sigusr2)

        self.datastore_manager.start()
        self._capabilities.append("DATASTORE_MANAGER")

        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")

        # internal router for local transports
        self.local_router = LocalRouter(bootstrap.get_sys_name())
        self.local_router.start()
        self.local_router.ready.wait(timeout=2)
        self._capabilities.append("LOCAL_ROUTER")

        # 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.get_safe('container.sflow.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)

        cleanup = lambda _: self.proc_manager._cleanup_method(self.name, rsvc)

        # 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, cleanup_method=cleanup)
        self.proc_manager.proc_sup.ensure_ready(proc)
        proc.start_listeners()
        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 (%s) started, OK." , self.id)

    def _handle_sigusr2(self):#, signum, frame):
        """
        Handles SIGUSR2, prints debugging greenlet information.
        """
        gls = GreenletLeak.get_greenlets()

        allgls = []

        for gl in gls:
            status = GreenletLeak.format_greenlet(gl)

            # build formatted output:
            # Greenlet at 0xdeadbeef
            #     self: <EndpointUnit at 0x1ffcceef>
            #     func: bound, EndpointUnit.some_func

            status[0].insert(0, "%s at %s:" % (gl.__class__.__name__, hex(id(gl))))
            # indent anything in status a second time
            prefmt = [s.replace("\t", "\t\t") for s in status[0]]
            prefmt.append("traceback:")

            for line in status[1]:
                for subline in line.split("\n")[0:2]:
                    prefmt.append(subline)

            glstr = "\n\t".join(prefmt)

            allgls.append(glstr)

        # print it out!
        print >>sys.stderr, "\n\n".join(allgls)
        with open("gls-%s" % os.getpid(), "w") as f:
            f.write("\n\n".join(allgls))


    @property
    def node(self):
        """
        Returns the active/default Node that should be used for most communication in the system.

        Defers to exchange manager, but only if it has been started, otherwise returns None.
        """
        if "EXCHANGE_MANAGER" in self._capabilities:
            return self.ex_manager.default_node

        return None

    @contextmanager
    def _push_status(self, new_status):
        """
        Temporarily sets the internal status flag.
        Use this as a decorator or in a with-statement before calling a temporary status changing
        method, like start_rel_from_url.
        """
        curstatus = self._status
        self._status = new_status
        try:
            yield
        finally:
            self._status = curstatus

    def serve_forever(self):
        """ Run the container until killed. """
        log.debug("In Container.serve_forever")

        if not self.proc_manager.proc_sup.running:
            self.start()

        # serve forever short-circuits if immediate is on and children len is ok
        num_procs = len(self.proc_manager.proc_sup.children)
        immediate = CFG.system.get('immediate', False)
        if not (immediate and num_procs == 1):  # only spawned greenlet is the CC-Agent

            # print a warning just in case
            if immediate and num_procs != 1:
                log.warn("CFG.system.immediate=True but number of spawned processes is not 1 (%d)", num_procs)

            try:
                # This just waits in this Greenlet for all child processes to complete,
                # which is triggered somewhere else.
                self.proc_manager.proc_sup.join_children()
            except (KeyboardInterrupt, SystemExit) as ex:
                log.info('Received a kill signal, shutting down the container.')

                if hasattr(self, 'gl_parent_watch') and self.gl_parent_watch is not None:
                    self.gl_parent_watch.kill()

            except:
                log.exception('Unhandled error! Forcing container shutdown')
        else:
            log.debug("Container.serve_forever short-circuiting due to CFG.system.immediate")

        self.proc_manager.proc_sup.shutdown(CFG.cc.timeout.shutdown)

    def status(self):
        """
        Returns the internal status.
        """
        return self._status

    def _cleanup_pid(self):
        if self.pidfile:
            log.debug("Cleanup pidfile: %s", self.pidfile)
            try:
                os.remove(self.pidfile)
            except Exception, e:
                log.warn("Pidfile could not be deleted: %s" % str(e))
            self.pidfile = None
Exemple #18
0
class Container(BaseContainerAgent):
    """
    The Capability Container. Its purpose is to spawn/monitor processes and services
    that do the bulk of the work in the ION system.
    """

    # Singleton static variables
    node        = None
    id          = None
    name        = None
    pidfile     = None
    instance    = None

    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        self._is_started = False

        # set id and name (as they are set in base class call)
        self.id = string.replace('%s_%d' % (os.uname()[1], os.getpid()), ".", "_")
        self.name = "cc_agent_%s" % self.id

        Container.instance = self

        # TODO: Bug: Replacing CFG instance not work because references are already public. Update directly
        dict_merge(CFG, kwargs, inplace=True)
        from pyon.core import bootstrap
        bootstrap.container_instance = self
        bootstrap.assert_configuration(CFG)
        bootstrap.sys_name = CFG.system.name or bootstrap.sys_name
        log.debug("Container (sysname=%s) initializing ..." % bootstrap.sys_name)

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = kwargs

        # Load object and service registry etc.
        bootstrap_pyon()

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()

        # File System - Interface to the OS File System, using correct path names and setups
        self.file_system = FileSystem(CFG)

        # Coordinates the container start
        self._is_started = False
        self._capabilities = []

        log.debug("Container initialized, OK.")


    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:
            from pyon.core.bootstrap import get_sys_name
            pid_contents = {'messaging': dict(CFG.server.amqp),
                            'container-agent': self.name,
                            'container-xp': 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._capabilities.append("EXCHANGE_CONNECTION")

        self.datastore_manager.start()
        self._capabilities.append("DATASTORE_MANAGER")

        # Instantiate Directory and self-register
        self.directory = Directory()
        self.directory.register("/Containers", self.id, cc_agent=self.name)
        self._capabilities.append("DIRECTORY")

        # Create other repositories to make sure they are there and clean if needed
        self.datastore_manager.get_datastore("resources", DataStore.DS_PROFILE.RESOURCES)

        self.datastore_manager.get_datastore("objects", DataStore.DS_PROFILE.OBJECTS)

        self.state_repository = StateRepository()
        self._capabilities.append("STATE_REPOSITORY")

        self.event_repository = EventRepository()
        self._capabilities.append("EVENT_REPOSITORY")

        # Start ExchangeManager. In particular establish broker connection
        self.ex_manager.start()

        # TODO: Move this in ExchangeManager - but there is an error
        self.node, self.ioloop = messaging.make_node() # TODO: shortcut hack
        self._capabilities.append("EXCHANGE_MANAGER")

        self.proc_manager.start()
        self._capabilities.append("PROC_MANAGER")

        self.app_manager.start()
        self._capabilities.append("APP_MANAGER")

        # Start the CC-Agent API
        rsvc = ProcessRPCServer(node=self.node, 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((CFG.cc.proctype or 'green', None), listener=rsvc)
        self.proc_manager.proc_sup.ensure_ready(proc)
        self._capabilities.append("CONTAINER_AGENT")

        self._is_started = True

        log.info("Container started, OK.")

    def serve_forever(self):
        """ Run the container until killed. """
        log.debug("In Container.serve_forever")
        
        if not self.proc_manager.proc_sup.running:
            self.start()
            
        try:
            # This just waits in this Greenlet for all child processes to complete,
            # which is triggered somewhere else.
            self.proc_manager.proc_sup.join_children()
        except (KeyboardInterrupt, SystemExit) as ex:
            log.info('Received a kill signal, shutting down the container.')
        except:
            log.exception('Unhandled error! Forcing container shutdown')

        self.proc_manager.proc_sup.shutdown(CFG.cc.timeout.shutdown)
            
    def _cleanup_pid(self):
        if self.pidfile:
            log.debug("Cleanup pidfile: %s", self.pidfile)
            try:
                os.remove(self.pidfile)
            except Exception, e:
                log.warn("Pidfile could not be deleted: %s" % str(e))
            self.pidfile = None
Exemple #19
0
class Container(BaseContainerAgent):
    """
    The Capability Container. Its purpose is to spawn/monitor processes and services
    that do the bulk of the work in the ION system.
    """

    # Singleton static variables
    node        = None
    id          = None
    name        = None
    pidfile     = None
    instance    = None

    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        self._is_started = False

        # set id and name (as they are set in base class call)
        self.id = string.replace('%s_%d' % (os.uname()[1], os.getpid()), ".", "_")
        self.name = "cc_agent_%s" % self.id

        Container.instance = self

        # TODO: Bug: Replacing CFG instance not work because references are already public. Update directly
        dict_merge(CFG, kwargs, inplace=True)
        from pyon.core import bootstrap
        bootstrap.container_instance = self
        bootstrap.assert_configuration(CFG)
        log.debug("Container (sysname=%s) initializing ..." % bootstrap.get_sys_name())

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = kwargs

        # Load object and service registry etc.
        bootstrap_pyon()

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()

        # File System - Interface to the OS File System, using correct path names and setups
        self.file_system = FileSystem(CFG)

        # Governance Controller - manages the governance related interceptors
        self.governance_controller = GovernanceController(self)

        # sFlow manager - controls sFlow stat emission
        self.sflow_manager = SFlowManager(self)

        # Coordinates the container start
        self._is_started = False
        self._capabilities = []
        self._status = "INIT"

        # protection for when the container itself is used as a Process for clients
        self.container = self

        log.debug("Container initialized, OK.")


    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._capabilities.append("EXCHANGE_CONNECTION")

        self.datastore_manager.start()
        self._capabilities.append("DATASTORE_MANAGER")

        # Instantiate Directory and self-register
        self.directory = 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.node, self.ioloop = 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((CFG.cc.proctype or 'green', None), listener=rsvc)
        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.")

    @contextmanager
    def _push_status(self, new_status):
        """
        Temporarily sets the internal status flag.
        Use this as a decorator or in a with-statement before calling a temporary status changing
        method, like start_rel_from_url.
        """
        curstatus = self._status
        self._status = new_status
        try:
            yield
        finally:
            self._status = curstatus

    def serve_forever(self):
        """ Run the container until killed. """
        log.debug("In Container.serve_forever")
        
        if not self.proc_manager.proc_sup.running:
            self.start()

        # serve forever short-circuits if immediate is on and children len is ok
        num_procs = len(self.proc_manager.proc_sup.children)
        immediate = CFG.system.get('immediate', False)
        if not (immediate and num_procs == 1):  # only spawned greenlet is the CC-Agent

            # print a warning just in case
            if immediate and num_procs != 1:
                log.warn("CFG.system.immediate=True but number of spawned processes is not 1 (%d)", num_procs)

            try:
                # This just waits in this Greenlet for all child processes to complete,
                # which is triggered somewhere else.
                self.proc_manager.proc_sup.join_children()
            except (KeyboardInterrupt, SystemExit) as ex:
                log.info('Received a kill signal, shutting down the container.')
                watch_parent = CFG.system.get('watch_parent', None)
                if watch_parent:
                    watch_parent.kill()
            except:
                log.exception('Unhandled error! Forcing container shutdown')
        else:
            log.debug("Container.serve_forever short-circuiting due to CFG.system.immediate")

        self.proc_manager.proc_sup.shutdown(CFG.cc.timeout.shutdown)

    def status(self):
        """
        Returns the internal status.
        """
        return self._status
            
    def _cleanup_pid(self):
        if self.pidfile:
            log.debug("Cleanup pidfile: %s", self.pidfile)
            try:
                os.remove(self.pidfile)
            except Exception, e:
                log.warn("Pidfile could not be deleted: %s" % str(e))
            self.pidfile = None
Exemple #20
0
class Container(BaseContainerAgent):
    """
    The Capability Container. Its purpose is to spawn/monitor processes and services
    that do the bulk of the work in the ION system. It also manages connections to the Exchange
    and the various forms of datastores in the systems.
    """

    # Singleton static variables
    #node        = None
    id = None
    name = None
    pidfile = None
    instance = None

    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        self._is_started = False
        # set container id and cc_agent name (as they are set in base class call)
        self.id = get_default_container_id()
        self.name = "cc_agent_%s" % self.id
        self._capabilities = []

        bootstrap.container_instance = self
        Container.instance = self

        log.debug("Container (sysname=%s) initializing ..." %
                  bootstrap.get_sys_name())

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = kwargs

        # DatastoreManager - controls access to Datastores (both mock and couch backed)
        self.datastore_manager = DatastoreManager()

        # TODO: Do not start a capability here. Symmetric start/stop
        self.datastore_manager.start()
        self._capabilities.append("DATASTORE_MANAGER")

        # Instantiate Directory
        self.directory = Directory()

        # internal router
        self.local_router = None

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)

        # File System - Interface to the OS File System, using correct path names and setups
        self.file_system = FileSystem(CFG)

        # Governance Controller - manages the governance related interceptors
        self.governance_controller = GovernanceController(self)

        # sFlow manager - controls sFlow stat emission
        self.sflow_manager = SFlowManager(self)

        # Coordinates the container start
        self._status = "INIT"

        # protection for when the container itself is used as a Process for clients
        self.container = self

        # publisher, initialized in start()
        self.event_pub = None

        # context-local storage
        self.context = LocalContextMixin()

        log.debug("Container initialized, OK.")

    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)

        # set up greenlet debugging signal handler
        gevent.signal(signal.SIGUSR2, self._handle_sigusr2)

        self.datastore_manager.start()
        self._capabilities.append("DATASTORE_MANAGER")

        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")

        # internal router for local transports
        self.local_router = LocalRouter(bootstrap.get_sys_name())
        self.local_router.start()
        self.local_router.ready.wait(timeout=2)
        self._capabilities.append("LOCAL_ROUTER")

        # 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.get_safe('container.sflow.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)

        cleanup = lambda _: self.proc_manager._cleanup_method(self.name, rsvc)

        # 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,
                                                cleanup_method=cleanup)
        self.proc_manager.proc_sup.ensure_ready(proc)
        proc.start_listeners()
        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 (%s) started, OK.", self.id)

    def _handle_sigusr2(self):  #, signum, frame):
        """
        Handles SIGUSR2, prints debugging greenlet information.
        """
        gls = GreenletLeak.get_greenlets()

        allgls = []

        for gl in gls:
            status = GreenletLeak.format_greenlet(gl)

            # build formatted output:
            # Greenlet at 0xdeadbeef
            #     self: <EndpointUnit at 0x1ffcceef>
            #     func: bound, EndpointUnit.some_func

            status[0].insert(
                0, "%s at %s:" % (gl.__class__.__name__, hex(id(gl))))
            # indent anything in status a second time
            prefmt = [s.replace("\t", "\t\t") for s in status[0]]
            prefmt.append("traceback:")

            for line in status[1]:
                for subline in line.split("\n")[0:2]:
                    prefmt.append(subline)

            glstr = "\n\t".join(prefmt)

            allgls.append(glstr)

        # print it out!
        print >> sys.stderr, "\n\n".join(allgls)
        with open("gls-%s" % os.getpid(), "w") as f:
            f.write("\n\n".join(allgls))

    @property
    def node(self):
        """
        Returns the active/default Node that should be used for most communication in the system.

        Defers to exchange manager, but only if it has been started, otherwise returns None.
        """
        if "EXCHANGE_MANAGER" in self._capabilities:
            return self.ex_manager.default_node

        return None

    @contextmanager
    def _push_status(self, new_status):
        """
        Temporarily sets the internal status flag.
        Use this as a decorator or in a with-statement before calling a temporary status changing
        method, like start_rel_from_url.
        """
        curstatus = self._status
        self._status = new_status
        try:
            yield
        finally:
            self._status = curstatus

    def serve_forever(self):
        """ Run the container until killed. """
        log.debug("In Container.serve_forever")

        if not self.proc_manager.proc_sup.running:
            self.start()

        # serve forever short-circuits if immediate is on and children len is ok
        num_procs = len(self.proc_manager.proc_sup.children)
        immediate = CFG.system.get('immediate', False)
        if not (immediate
                and num_procs == 1):  # only spawned greenlet is the CC-Agent

            # print a warning just in case
            if immediate and num_procs != 1:
                log.warn(
                    "CFG.system.immediate=True but number of spawned processes is not 1 (%d)",
                    num_procs)

            try:
                # This just waits in this Greenlet for all child processes to complete,
                # which is triggered somewhere else.
                self.proc_manager.proc_sup.join_children()
            except (KeyboardInterrupt, SystemExit) as ex:
                log.info(
                    'Received a kill signal, shutting down the container.')

                if hasattr(self, 'gl_parent_watch'
                           ) and self.gl_parent_watch is not None:
                    self.gl_parent_watch.kill()

            except:
                log.exception('Unhandled error! Forcing container shutdown')
        else:
            log.debug(
                "Container.serve_forever short-circuiting due to CFG.system.immediate"
            )

        self.proc_manager.proc_sup.shutdown(CFG.cc.timeout.shutdown)

    def status(self):
        """
        Returns the internal status.
        """
        return self._status

    def _cleanup_pid(self):
        if self.pidfile:
            log.debug("Cleanup pidfile: %s", self.pidfile)
            try:
                os.remove(self.pidfile)
            except Exception, e:
                log.warn("Pidfile could not be deleted: %s" % str(e))
            self.pidfile = None
Exemple #21
0
class TestProcManager(PyonTestCase):
    def setUp(self):
        self.container = Mock()
        self.pm = ProcManager(self.container)

        self.container.resource_registry.create.return_value = (sentinel.rid, sentinel.rev)
        self.container.resource_registry.find_resources.return_value = ([sentinel.oid], [sentinel.orev])

    def test_start(self):
        self.pm.start()

        self.assertEquals(self.pm.cc_id, sentinel.rid)

    def test_start_with_org(self):
        self.patch_cfg('pyon.container.procs.CFG', {'container':{'org_name':'NOT_DEFAULT'}})
        self.pm.start()

        self.container.resource_registry.create_association.assert_called_once_with(sentinel.oid, PRED.hasResource, sentinel.rid)

#    @patch('pyon.datastore.couchdb.couchdb_datastore.CouchDB_DataStore._stats')
#    def test_stop(self, statsmock):
#        self.pm.start()
#
#        self.pm.stop()
#
#        self.assertEquals(statsmock.get_stats.call_count, 2)

    def test__cleanup_method(self):
        ep = Mock()
        ep._chan._queue_auto_delete = False

        self.pm._cleanup_method(sentinel.queue, ep)

        ch = self.container.node.channel.return_value
        ch._destroy_queue.assert_called_once_with()
        self.assertIsInstance(ch._recv_name, NameTrio)
        self.assertIn(str(sentinel.queue), str(ch._recv_name))

    @patch('pyon.container.procs.log')
    def test__cleanup_method_raises_error(self, mocklog):
        ep = Mock()
        ep._chan._queue_auto_delete = False
        ch = self.container.node.channel.return_value
        ch._destroy_queue.side_effect = TransportError

        self.pm._cleanup_method(sentinel.queue, ep)

        self.assertEquals(mocklog.warn.call_count, 1)

#    @patch('pyon.datastore.couchdb.couchdb_datastore.CouchDB_DataStore._stats', Mock())
    @patch('pyon.container.procs.log')
    def test_stop_with_error(self, mocklog):
        self.pm.start()
        self.pm.terminate_process = Mock(side_effect=BadRequest)

        procmock = Mock()
        procmock._proc_start_time = 0
        procmock.id = sentinel.pid
        self.pm.procs[sentinel.pid] = procmock
        self.pm.procs_by_name['dummy'] = procmock

        self.pm.stop()

        self.pm.terminate_process.assert_called_once_with(sentinel.pid)
        mocklog.warn.assert_has_calls([call("Failed to terminate process (%s): %s", sentinel.pid, ANY),
                                       call("ProcManager procs not empty: %s", self.pm.procs),
                                       call("ProcManager procs_by_name not empty: %s", self.pm.procs_by_name)])

    def test_list_local_processes(self):
        pmock = Mock()
        pmock.process_type = sentinel.ptype
        pmock2 = Mock()
        pmock2.process_type = sentinel.ptype2

        self.pm.procs = {'one':pmock,
                         'two':pmock2,
                         'three':pmock}

        self.assertEquals(self.pm.list_local_processes(),
                          [pmock, pmock2, pmock])

    def test_list_local_processes_proc_type_filter(self):
        pmock = Mock()
        pmock.process_type = sentinel.ptype
        pmock2 = Mock()
        pmock2.process_type = sentinel.ptype2

        self.pm.procs = {'one':pmock,
                         'two':pmock2,
                         'three':pmock}

        self.assertEquals(self.pm.list_local_processes(sentinel.ptype2),
                          [pmock2])

    def test_get_a_local_process(self):
        pmock = Mock()
        pmock.name = sentinel.name
        pmock2 = Mock()
        pmock2.name = sentinel.name2

        self.pm.procs = {'one':pmock,
                         'two':pmock2}

        self.assertEquals(self.pm.get_a_local_process(sentinel.name2),
                          pmock2)

    def test_get_a_local_process_for_agent_res_id(self):
        pmock = Mock()
        pmock.process_type = 'agent'
        pmock.resource_type = sentinel.rtype
        pmock2 = Mock()
        pmock2.process_type = 'agent'
        pmock2.resource_type = sentinel.rtype2

        self.pm.procs = {'one':pmock,
                         'two':pmock2}

        self.assertEquals(self.pm.get_a_local_process(sentinel.rtype2),
                          pmock2)

    def test_get_a_local_process_no_match(self):
        self.assertIsNone(self.pm.get_a_local_process())

    def test_is_local_service_process(self):
        pmock = Mock()
        pmock.name          = sentinel.name
        pmock.process_type = 'simple'
        pmock2 = Mock()
        pmock2.name         = sentinel.name2
        pmock2.process_type = 'service'
        pmock3 = Mock()
        pmock3.name         = sentinel.name3
        pmock3.process_type = 'service'

        self.pm.procs = {'one':pmock,
                         'two':pmock2,
                         'three':pmock3}

        self.assertTrue(self.pm.is_local_service_process(sentinel.name3))

    def test_is_local_service_process_name_matches_but_type_doesnt(self):
        pmock = Mock()
        pmock.name          = sentinel.name
        pmock.process_type = 'simple'
        pmock2 = Mock()
        pmock2.name         = sentinel.name2
        pmock2.process_type = 'notservice'
        pmock3 = Mock()
        pmock3.name         = sentinel.name3
        pmock3.process_type = 'notservice'

        self.pm.procs = {'one':pmock,
                         'two':pmock2,
                         'three':pmock3}

        self.assertFalse(self.pm.is_local_service_process(sentinel.name3))

    def test_is_local_agent_process(self):
        # agent is similar to above, but checks resource_type instead
        pmock = Mock()
        pmock.name          = sentinel.name
        pmock.process_type = 'simple'
        pmock2 = Mock()
        pmock2.resource_type = sentinel.name2
        pmock2.process_type = 'agent'
        pmock3 = Mock()
        pmock3.name         = sentinel.name3
        pmock3.process_type = 'notservice'

        self.pm.procs = {'one':pmock,
                         'two':pmock2,
                         'three':pmock3}

        self.assertTrue(self.pm.is_local_agent_process(sentinel.name2))

    def test_is_local_agent_process_not_found(self):
        self.assertFalse(self.pm.is_local_agent_process(sentinel.one))

    def test__unregister_process_errors(self):
        pmock = Mock()
        pmock._proc_name = '1'
        pmock._proc_type = 'service'
        pmock._proc_res_id = sentinel.presid
        pmock._proc_svc_id = sentinel.psvcid

        self.container.resource_registry.delete.side_effect = NotFound
        self.container.resource_registry.find_objects.side_effect = ResourceNotFound

        self.pm.procs[sentinel.pid] = pmock
        self.pm.procs_by_name['1'] = pmock

        self.pm._unregister_process(sentinel.pid, pmock)

        # show we tried to interact with the RR
        self.container.resource_registry.delete.assert_call(sentinel.presid, del_associations=True)
        self.container.resource_registry.find_objects.assert_called_once_with(sentinel.psvcid, "hasProcess", "Process", id_only=True)
        self.assertEquals(self.pm.procs, {})
        self.assertEquals(self.pm.procs_by_name, {})

        # NEXT: find_objects works fine and gives us an error deletion
        self.container.resource_registry.delete.reset_mock()
        self.container.resource_registry.find_objects.reset_mock()
        self.container.resource_registry.find_objects.side_effect = None
        self.container.resource_registry.find_objects.return_value = ([sentinel.svcid],[None])

        self.pm.procs[sentinel.pid] = pmock
        self.pm.procs_by_name['1'] = pmock

        self.pm._unregister_process(sentinel.pid, pmock)

        self.container.resource_registry.delete.assert_calls([call(sentinel.presid, del_associations=True),
                                                              call(sentinel.psvcid, del_associations=True)])

        # NEXT: agent
        pmock = Mock()
        pmock.id = sentinel.pid
        pmock._proc_name = '1'
        pmock._proc_type = 'agent'

        self.pm.procs[sentinel.pid] = pmock
        self.pm.procs_by_name['1'] = pmock

        self.pm._unregister_process(sentinel.pid, pmock)

        self.container.directory.unregister_safe.assert_called_once_with("/Agents", sentinel.pid)

    def test__create_listening_endpoint_with_cfg(self):
        self.patch_cfg('pyon.container.procs.CFG', container=dict(messaging=dict(endpoint=dict(proc_listening_type='pyon.container.test.test_procs.TestRPCServer'))))

        ep = self.pm._create_listening_endpoint(process=sentinel.process)

        self.assertIsInstance(ep, TestRPCServer)

    def test__create_listening_endpoint_without_cfg_and_no_conv(self):
        self.patch_cfg('pyon.container.procs.CFG', container=dict(messaging=dict(endpoint=dict(proc_listening_type=None, rpc_conversation_enabled=False))))

        ep = self.pm._create_listening_endpoint(process=sentinel.process)

        self.assertIsInstance(ep, ProcessRPCServer)

    def test__create_listening_endpoint_without_cfg_and_conv(self):
        self.patch_cfg('pyon.container.procs.CFG', container=dict(messaging=dict(endpoint=dict(proc_listening_type=None, rpc_conversation_enabled=True))))

        ep = self.pm._create_listening_endpoint(process=sentinel.process)

        self.assertIsInstance(ep, ConversationRPCServer)

    def test_failed_process(self):
        self.pm.start()
        self.container.fail_fast = Mock()

        self.pm.procs['pid1'] = Mock()

        proc2 = BadProcess()
        self.pm.proc_sup.spawn(name="bad", service=proc2, target=proc2.fail_target)
        gevent.sleep(0)  # Allow the new thread to fail and trigger the chain

        self.assertFalse(self.container.fail_fast.called)

        del self.pm.procs['pid1']

        proc3 = BadProcess()
        self.pm.proc_sup.spawn(name="bad", service=proc3, target=proc3.fail_target)
        gevent.sleep(0)  # Allow the new thread to fail and trigger the chain

        self.assertTrue(self.container.fail_fast.called)
Exemple #22
0
    def setUp(self):
        self.container = Mock()
        self.pm = ProcManager(self.container)

        self.container.resource_registry.create.return_value = (sentinel.rid, sentinel.rev)
        self.container.resource_registry.find_resources.return_value = ([sentinel.oid], [sentinel.orev])
Exemple #23
0
class Container(BaseContainerAgent):
    """
    The Capability Container. Its purpose is to spawn/monitor processes and services
    that do the bulk of the work in the ION system.
    """

    node        = None
    id          = None
    name        = None
    pidfile     = None
    instance    = None

    def __init__(self, *args, **kwargs):
        BaseContainerAgent.__init__(self, *args, **kwargs)

        # set id and name (as they are set in base class call)
        self.id = string.replace('%s_%d' % (os.uname()[1], os.getpid()), ".", "_")
        self.name = "cc_agent_%s" % self.id

        Container.instance = self

        # TODO: Bug: Replacing CFG instance not work because references are already public. Update directly
        dict_merge(CFG, kwargs)
        from pyon.core import bootstrap
        bootstrap.sys_name = CFG.system.name or bootstrap.sys_name
        log.debug("Container (sysname=%s) initializing ..." % bootstrap.sys_name)

        # Keep track of the overrides from the command-line, so they can trump app/rel file data
        self.spawn_args = DictModifier(CFG, kwargs)

        # Load object and service registry
        bootstrap_pyon()

        # Create this Container's specific ExchangeManager instance
        self.ex_manager = ExchangeManager(self)

        # Create this Container's specific ProcManager instance
        self.proc_manager = ProcManager(self)

        # Create this Container's specific AppManager instance
        self.app_manager = AppManager(self)
        
        log.debug("Container initialized, OK.")


    def start(self):
        log.debug("Container starting...")

        # Check if this UNIX process already runs a Container.
        self.pidfile = "cc-pid-%d" % os.getpid()
        if os.path.exists(self.pidfile):
            raise Exception("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:
            from pyon.core.bootstrap import sys_name
            pid_contents = {'messaging': dict(CFG.server.amqp),
                            'container-agent': self.name,
                            'container-xp': sys_name }
            f.write(msgpack.dumps(pid_contents))
            atexit.register(self._cleanup_pid)

        # 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)


        # Start ExchangeManager. In particular establish broker connection
        self.ex_manager.start()

        # TODO: Move this in ExchangeManager - but there is an error
        self.node, self.ioloop = messaging.make_node() # TODO: shortcut hack


        # Instantiate Directory singleton and self-register
        # TODO: At this point, there is no special config override
        self.directory = Directory()
        self.directory.register("/Containers", self.id, cc_agent=self.name)

        self.proc_manager.start()

        self.app_manager.start()

        # Start the CC-Agent API
        rsvc = ProcessRPCServer(node=self.node, name=self.name, service=self, process=self)

        # Start an ION process with the right kind of endpoint factory
        self.proc_manager.proc_sup.spawn((CFG.cc.proctype or 'green', None), listener=rsvc)
        rsvc.get_ready_event().wait(timeout=10)   # @TODO: no hardcode
        log.info("Container started, OK.")

    def serve_forever(self):
        """ Run the container until killed. """
        log.debug("In Container.serve_forever")
        
        if not self.proc_manager.proc_sup.running:
            self.start()
            
        try:
            # This just waits in this Greenlet for all child processes to complete,
            # which is triggered somewhere else.
            self.proc_manager.proc_sup.join_children()
        except (KeyboardInterrupt, SystemExit) as ex:
            log.info('Received a kill signal, shutting down the container.')
        except:
            log.exception('Unhandled error! Forcing container shutdown')

        self.proc_manager.proc_sup.shutdown(CFG.cc.timeout.shutdown)
            
    def _cleanup_pid(self):
        if self.pidfile:
            log.debug("Cleanup pidfile: %s", self.pidfile)
            try:
                os.remove(self.pidfile)
            except Exception, e:
                log.warn("Pidfile could not be deleted: %s" % str(e))
            self.pidfile = None