Пример #1
0
 def __core_metadata(
     client: ResotoClient,
 ) -> Tuple[List[CommandInfo], List[str], List[str]]:
     try:
         log.debug("Fetching core metadata..")
         model = client.model()
         known_kinds = {
             k
             for k, v in model.kinds.items() if v.properties is not None
         }
         known_props = {
             p.name
             for k, v in model.kinds.items() if v.properties is not None
             for p in v.properties
         }
         info = client.cli_info()
         cmds = [
             jsons.load(cmd, CommandInfo)
             for cmd in info.get("commands", [])
         ]
         return cmds, sorted(known_kinds), sorted(known_props)
     except Exception as ex:
         log.warning(
             f"Can not load metadata from core: {ex}. No suggestions as fallback.",
             exc_info=ex,
         )
         return [], [], []
Пример #2
0
 async def emit_by(name: str) -> None:
     for listener in self.listeners.get(name, []):
         try:
             await listener.put(event)
         except QueueFull:
             log.warning(
                 f"Queue for listener {name} is full. Dropping message."
             )
Пример #3
0
 def get_completions(self, document: Document,
                     complete_event: CompleteEvent) -> Iterable[Completion]:
     try:
         result = self.completer.get_completions(document, complete_event)
         return [] if result is None else result
     except Exception as ex:
         log.warning(f"Error in completer: {ex}", exc_info=ex)
         return []
Пример #4
0
    def collect(self) -> None:
        log.debug("plugin: AWS collecting resources")
        if not self.authenticated:
            log.error("Failed to authenticate - skipping collection")
            return

        if Config.aws.assume_current and not Config.aws.do_not_scrape_current:
            log.warning(
                "You specified assume_current but not do_not_scrape_current! "
                "This will result in the same account being scraped twice and is likely not what you want."
            )

        if Config.aws.role and Config.aws.scrape_org:
            accounts = [
                AWSAccount(aws_account_id, {}, role=Config.aws.role)
                for aws_account_id in get_org_accounts(filter_current_account=not Config.aws.assume_current)
                if aws_account_id not in Config.aws.scrape_exclude_account
            ]
            if not Config.aws.do_not_scrape_current:
                accounts.append(AWSAccount(current_account_id(), {}))
        elif Config.aws.role and Config.aws.account:
            accounts = [AWSAccount(aws_account_id, {}, role=Config.aws.role) for aws_account_id in Config.aws.account]
        else:
            accounts = [AWSAccount(current_account_id(), {})]

        max_workers = len(accounts) if len(accounts) < Config.aws.account_pool_size else Config.aws.account_pool_size
        pool_args = {"max_workers": max_workers}
        if Config.aws.fork_process:
            pool_args["mp_context"] = multiprocessing.get_context("spawn")
            pool_args["initializer"] = resotolib.proc.initializer
            pool_executor = futures.ProcessPoolExecutor
        else:
            pool_executor = futures.ThreadPoolExecutor

        with pool_executor(**pool_args) as executor:
            wait_for = [
                executor.submit(
                    collect_account,
                    account,
                    self.regions,
                    ArgumentParser.args,
                    Config.running_config,
                )
                for account in accounts
            ]
            for future in futures.as_completed(wait_for):
                account_graph = future.result()
                if not isinstance(account_graph, Graph):
                    log.error(f"Returned account graph has invalid type {type(account_graph)}")
                    continue
                self.graph.merge(account_graph)
Пример #5
0
 def load_config(self, reload: bool = False) -> None:
     if len(Config.running_config.classes) == 0:
         raise RuntimeError("No config added")
     with self._config_lock:
         try:
             config, new_config_revision = get_config(self.config_name,
                                                      self.resotocore_uri,
                                                      verify=self.verify)
             if len(config) == 0:
                 if self._initial_load:
                     raise ConfigNotFoundError(
                         "Empty config returned - loading defaults")
                 else:
                     raise ValueError("Empty config returned")
         except ConfigNotFoundError:
             pass
         else:
             log.info(
                 f"Loaded config {self.config_name} revision {new_config_revision}"
             )
             new_config = {}
             for config_id, config_data in config.items():
                 if config_id in Config.running_config.classes:
                     log.debug(f"Loading config section {config_id}")
                     new_config[config_id] = jsons.load(
                         config_data,
                         Config.running_config.classes[config_id])
                 else:
                     log.warning(f"Unknown config section {config_id}")
             if reload and self.restart_required(new_config):
                 restart()
             Config.running_config.data = new_config
             Config.running_config.revision = new_config_revision
         self.init_default_config()
         if self._initial_load:
             # Try to store the generated config. Handle failure gracefully.
             try:
                 self.save_config()
             except RuntimeError as e:
                 log.error(f"Failed to save config: {e}")
         self.override_config(Config.running_config)
         self._initial_load = False
         if not self._ce.is_alive():
             log.debug("Starting config event listener")
             self._ce.start()