def test_start_and_stop_with_no_gears(self):
     ariane_connector = ArianeConnector(self.config)
     injector_ui_procos_entity_id = ariane_connector.injector_ui_procos_entity.id
     self.assertIsNotNone(InjectorUITreeService.find_ui_tree_entity(injector_ui_procos_entity_id))
     ariane_connector.stop()
     test_injector_service = InjectorService(self.rbmq_args)
     self.assertIsNone(InjectorUITreeService.find_ui_tree_entity(injector_ui_procos_entity_id))
     test_injector_service.stop()
    def test_start_and_stop_with_one_gear(self):
        ariane_connector = ArianeConnector(self.config)
        injector_ui_procos_entity_id = ariane_connector.injector_ui_procos_entity.id
        self.assertIsNotNone(InjectorUITreeService.find_ui_tree_entity(injector_ui_procos_entity_id))

        gear = InjectorGearSkeleton.start(gear_id='ariane.community.plugin.procos.gears.cache.localhost',
                                          gear_name='procos@localhost',
                                          gear_description='Ariane remote injector for localhost',
                                          gear_admin_queue='ariane.community.plugin.procos.gears.cache.localhost',
                                          running=False).proxy()
        self.assertTrue(gear.cache(running=True).get())
        ariane_connector.stop()
        test_injector_service = InjectorService(self.rbmq_args)
        self.assertIsNotNone(InjectorUITreeService.find_ui_tree_entity(injector_ui_procos_entity_id))
        test_injector_service.stop()
    def __init__(self, docker_config):
        self.ready = False
        rest_args = {
            'type': 'REST',
            'base_url': docker_config.rest_base_url,
            'user': docker_config.rest_user,
            'password': docker_config.rest_password
        }
        client_properties = {
            'product': 'Ariane',
            'information': 'Ariane Plugin Docker - Map your Docker containers interaction and more ...',
            'ariane.pgurl': 'ssh://' + socket.gethostname() + "/$[/usr/local/bin/adocker {start|stop}]",
            'ariane.osi': socket.gethostname(),
            'ariane.otm': 'AROps',
            'ariane.app': 'Ariane Plugin Docker',
            'ariane.cmp': 'echinopsii',
            'ariane.pid':  os.getpid()
        }
        nats_args = {
            'type': DriverFactory.DRIVER_NATS,
            'user': docker_config.nats_user,
            'password': docker_config.nats_password,
            'host': docker_config.nats_host,
            'port': docker_config.nats_port,
            'client_properties': client_properties
        }
        rbmq_args = {
            'type': 'RBMQ',
            'user': docker_config.rbmq_user,
            'password': docker_config.rbmq_password,
            'host': docker_config.rbmq_host,
            'port': docker_config.rbmq_port,
            'vhost': docker_config.rbmq_vhost,
            'client_properties': client_properties
        }
        self.gears_registry_cache_id = 'ariane.community.plugin.docker.gears.cache'
        docker_gears_registry_conf = {
            'registry.name': 'Ariane Docker plugin gears registry',
            'registry.cache.id': self.gears_registry_cache_id,
            'registry.cache.name': 'Ariane Docker plugin gears cache',
            'cache.mgr.name': 'ARIANE_PLUGIN_DOCKER_GEARS_CACHE_MGR'
        }
        self.components_registry_cache_id = 'ariane.community.plugin.docker.components.cache'
        docker_components_registry_conf = {
            'registry.name': 'Ariane Docker plugin components registry',
            'registry.cache.id': self.components_registry_cache_id,
            'registry.cache.name': 'Ariane Docker plugin components cache',
            'cache.mgr.name': 'ARIANE_PLUGIN_DOCKER_COMPONENTS_CACHE_MGR'
        }

        no_error = True
        DirectoryService(rest_args)
        # Test Directory Service
        try:
            LocationService.get_locations()
        except Exception as e:
            LOGGER.error("Problem while initializing Ariane directory service.")
            LOGGER.error(e.__str__())
            no_error = False

        if no_error:
            if docker_config.mapping_driver_type == DriverFactory.DRIVER_RBMQ:
                LOGGER.info("Starting Mapping Service through RabbitMQ")
                MappingService(rbmq_args)
            elif docker_config.mapping_driver_type == DriverFactory.DRIVER_NATS:
                LOGGER.info("Starting Mapping Service through NATS")
                MappingService(nats_args)
            else:
                LOGGER.info("Starting Mapping Service through REST")
                MappingService(rest_args)
            # Open session and Test Mapping Service
            try:
                SessionService.open_session("ArianeDocker_test" + socket.gethostname())
                SessionService.close_session()
            except Exception as e:
                LOGGER.error("Problem while initializing Ariane mapping service.")
                LOGGER.error(e.__str__())
                no_error = False

        if no_error:
            try:
                if docker_config.injector_driver_type == DriverFactory.DRIVER_RBMQ:
                    LOGGER.info("Starting Injector Service through RabbitMQ")
                    self.injector_service = InjectorService(
                        driver_args=rbmq_args, gears_registry_args=docker_gears_registry_conf,
                        components_registry_args=docker_components_registry_conf
                    )
                elif docker_config.injector_driver_type == DriverFactory.DRIVER_NATS:
                    LOGGER.info("Starting Injector Service through NATS")
                    self.injector_service = InjectorService(
                        driver_args=nats_args, gears_registry_args=docker_gears_registry_conf,
                        components_registry_args=docker_components_registry_conf
                    )
            except Exception as e:
                LOGGER.error("Problem while initializing Ariane injector service.")
                LOGGER.error(e.__str__())
                no_error = False

        if no_error:
            # Register UI entity if needed (and so test)
            self.injector_ui_mapping_entity = InjectorUITreeService.find_ui_tree_entity('mappingDir')
            if self.injector_ui_mapping_entity is None:
                self.injector_ui_mapping_entity = InjectorUITreeEntity(uitid="mappingDir", value="Mapping",
                                                                       uitype=InjectorUITreeEntity.entity_dir_type)
                self.injector_ui_mapping_entity.save()
            self.injector_ui_system_entity = InjectorUITreeEntity(uitid="systemDir", value="System",
                                                                  uitype=InjectorUITreeEntity.entity_dir_type,
                                                                  context_address="", description="",
                                                                  parent_id=self.injector_ui_mapping_entity.id,
                                                                  display_roles=["sysreviewer"],
                                                                  display_permissions=["injMapSysDocker:display"])
            self.injector_ui_system_entity.save()
            self.injector_ui_procos_entity = InjectorUITreeEntity(uitid="docker", value="Docker",
                                                                  uitype=InjectorUITreeEntity.entity_leaf_type,
                                                                  context_address=
                                                                  "/ariane/views/injectors/external.jsf?id=docker",
                                                                  description="Docker injector", icon="icon-docker-injector",
                                                                  parent_id=self.injector_ui_system_entity.id,
                                                                  display_roles=["sysadmin", "sysreviewer"],
                                                                  display_permissions=["injMapSysDocker:display"],
                                                                  other_actions_roles={"action": ["sysadmin"]},
                                                                  other_actions_perms={"action": ["injMapSysDocker:action"]},
                                                                  remote_injector_tree_entity_gears_cache_id=
                                                                  self.gears_registry_cache_id,
                                                                  remote_injector_tree_entity_components_cache_id=
                                                                  self.components_registry_cache_id)
            self.injector_ui_procos_entity.save()
            self.ready = True
class ArianeConnector(object):

    def __init__(self, procos_config):
        self.ready = False
        rest_args = {
            'type': 'REST',
            'base_url': procos_config.rest_base_url,
            'user': procos_config.rest_user,
            'password': procos_config.rest_password
        }
        client_properties = {
            'product': 'Ariane',
            'information': 'Ariane Plugin ProcOS - Map your Operating System Process interaction and more ...',
            'ariane.pgurl': 'ssh://' + socket.gethostname() + "/$[/usr/local/bin/aprocos {start|stop}]",
            'ariane.osi': socket.gethostname(),
            'ariane.otm': 'AROps',
            'ariane.app': 'Ariane Plugin ProcOS',
            'ariane.cmp': 'echinopsii',
            'ariane.pid':  os.getpid()
        }
        nats_args = {
            'type': DriverFactory.DRIVER_NATS,
            'user': procos_config.nats_user,
            'password': procos_config.nats_password,
            'host': procos_config.nats_host,
            'port': procos_config.nats_port,
            'rpc_timeout': procos_config.rpc_timeout,
            'rpc_retry': procos_config.rpc_retry,
            'client_properties': client_properties
        }
        rbmq_args = {
            'type': 'RBMQ',
            'user': procos_config.rbmq_user,
            'password': procos_config.rbmq_password,
            'host': procos_config.rbmq_host,
            'port': procos_config.rbmq_port,
            'vhost': procos_config.rbmq_vhost,
            'rpc_timeout': procos_config.rpc_timeout,
            'rpc_retry': procos_config.rpc_retry,
            'client_properties': client_properties
        }
        self.gears_registry_cache_id = 'ariane.community.plugin.procos.gears.cache'
        procos_gears_registry_conf = {
            'registry.name': 'Ariane ProcOS Plugin Gears Registry',
            'registry.cache.id': self.gears_registry_cache_id,
            'registry.cache.name': 'Ariane ProcOS Plugin Gears Cache',
            'cache.mgr.name': 'ARIANE_PLUGIN_PROCOS_GEARS_CACHE_MGR'
        }
        self.components_registry_cache_id = 'ariane.community.plugin.procos.components.cache'
        procos_components_registry_conf = {
            'registry.name': 'Ariane ProcOS Plugin Components Registry',
            'registry.cache.id': self.components_registry_cache_id,
            'registry.cache.name': 'Ariane ProcOS Plugin Components Cache',
            'cache.mgr.name': 'ARIANE_PLUGIN_PROCOS_COMPONENTS_CACHE_MGR'
        }

        no_error = True
        DirectoryService(rest_args)
        # Test Directory Service
        try:
            LocationService.get_locations()
        except Exception as e:
            LOGGER.error("Problem while initializing Ariane directory service.")
            LOGGER.error(e.__str__())
            no_error = False

        if no_error:
            if procos_config.mapping_driver_type == DriverFactory.DRIVER_RBMQ:
                MappingService(rbmq_args)
            elif procos_config.mapping_driver_type == DriverFactory.DRIVER_NATS:
                MappingService(nats_args)
            else:
                MappingService(rest_args)
            # Open session and Test Mapping Service
            try:
                SessionService.open_session("ArianeProcOS_test" + socket.gethostname())
                SessionService.close_session()
            except Exception as e:
                LOGGER.error("Problem while initializing Ariane mapping service.")
                LOGGER.error(e.__str__())
                no_error = False

        if no_error:
            try:
                if procos_config.injector_driver_type == DriverFactory.DRIVER_RBMQ:
                    self.injector_service = InjectorService(
                        driver_args=rbmq_args, gears_registry_args=procos_gears_registry_conf,
                        components_registry_args=procos_components_registry_conf
                    )
                elif procos_config.injector_driver_type == DriverFactory.DRIVER_NATS:
                    self.injector_service = InjectorService(
                        driver_args=nats_args, gears_registry_args=procos_gears_registry_conf,
                        components_registry_args=procos_components_registry_conf
                    )
            except Exception as e:
                LOGGER.error("Problem while initializing Ariane injector service.")
                LOGGER.error(e.__str__())
                no_error = False

        if no_error:
            # Register UI entity if needed (and so test)
            self.injector_ui_mapping_entity = InjectorUITreeService.find_ui_tree_entity('mappingDir')
            if self.injector_ui_mapping_entity is None:
                self.injector_ui_mapping_entity = InjectorUITreeEntity(uitid="mappingDir", value="Mapping",
                                                                       uitype=InjectorUITreeEntity.entity_dir_type)
                self.injector_ui_mapping_entity.save()
            self.injector_ui_system_entity = InjectorUITreeEntity(uitid="systemDir", value="System",
                                                                  uitype=InjectorUITreeEntity.entity_dir_type,
                                                                  context_address="", description="",
                                                                  parent_id=self.injector_ui_mapping_entity.id,
                                                                  display_roles=["sysreviewer"],
                                                                  display_permissions=["injMapSysProcOS:display"])
            self.injector_ui_system_entity.save()
            self.injector_ui_procos_entity = InjectorUITreeEntity(uitid="procos", value="ProcOS",
                                                                  uitype=InjectorUITreeEntity.entity_leaf_type,
                                                                  context_address=
                                                                  "/ariane/views/injectors/external.jsf?id=procos",
                                                                  description="ProcOS injector", icon="icon-procos-injector",
                                                                  parent_id=self.injector_ui_system_entity.id,
                                                                  display_roles=["sysadmin", "sysreviewer"],
                                                                  display_permissions=["injMapSysProcOS:display"],
                                                                  other_actions_roles={"action": ["sysadmin"]},
                                                                  other_actions_perms={"action": ["injMapSysProcOS:action"]},
                                                                  remote_injector_tree_entity_gears_cache_id=
                                                                  self.gears_registry_cache_id,
                                                                  remote_injector_tree_entity_components_cache_id=
                                                                  self.components_registry_cache_id)
            self.injector_ui_procos_entity.save()
            self.ready = True

    def stop(self):
        if self.ready:
            if InjectorCachedGearService.get_gears_cache_size() == 0 and \
                    InjectorCachedComponentService.get_components_cache_size() == 0:
                self.injector_ui_procos_entity.remove()
            self.injector_service.stop()
            self.ready = False