def InitNornir( config_file: str = "", dry_run: bool = False, **kwargs: Any, ) -> Nornir: """ Arguments: config_file(str): Path to the configuration file (optional) dry_run(bool): Whether to simulate changes or not configure_logging: Whether to configure logging or not. This argument is being deprecated. Please use logging.enabled parameter in the configuration instead. **kwargs: Extra information to pass to the :obj:`nornir.core.configuration.Config` object Returns: :obj:`nornir.core.Nornir`: fully instantiated and configured """ ConnectionPluginRegister.auto_register() if config_file: config = Config.from_file(config_file, **kwargs) else: config = Config.from_dict(**kwargs) data = GlobalState(dry_run=dry_run) config.logging.configure() return Nornir( inventory=load_inventory(config), runner=load_runner(config), config=config, data=data, )
def __init__( self, inventory, dry_run, config=None, config_file=None, available_connections=None, logger=None, data=None, ): self.logger = logger or logging.getLogger("nornir") self.data = data or Data() self.inventory = inventory self.inventory.nornir = self self.data.dry_run = dry_run if config_file: self.config = Config(config_file=config_file) else: self.config = config or Config() self.configure_logging() if available_connections is not None: self.available_connections = available_connections else: self.available_connections = connections.available_connections
def test_get_user_defined_from_env(self): os.environ["USER_DEFINED"] = "zxczxc" config = Config(config_file=os.path.join(dir_path, "config.yaml")) assert ( config.get("user_defined", env="USER_DEFINED", default="qweqwe") == "zxczxc" ) os.environ.pop("USER_DEFINED")
def test_config_defaults(self): c = Config() assert c.dict() == { "core": { "raise_on_error": False }, "runner": { "options": {}, "plugin": "threaded" }, "inventory": { "plugin": "SimpleInventory", "options": {}, "transform_function": "", "transform_function_options": {}, }, "ssh": { "config_file": str(Path("~/.ssh/config").expanduser()) }, "logging": { "enabled": True, "level": "INFO", "log_file": "nornir.log", "format": DEFAULT_LOG_FORMAT, "to_console": False, "loggers": ["nornir"], }, "user_defined": {}, }
def __init__(self, inventory, config=None, logger=None, data=None): self.data = data if data is not None else GlobalState() self.logger = logger or logging.getLogger(__name__) self.inventory = inventory self.config = config or Config()
def test_configuration_normal_override_argument(self): config = Config( config_file=os.path.join(dir_path, "config.yaml"), num_workers=20, raise_on_error=True, ) assert config.num_workers == 20 assert config.raise_on_error
def test_configuration_file_override_env(self): os.environ["NORNIR_CORE_RAISE_ON_ERROR"] = "1" os.environ["NORNIR_SSH_CONFIG_FILE"] = "/user/ssh_config" config = Config.from_dict(inventory={"plugin": "an-inventory"}) assert config.core.raise_on_error assert config.ssh.config_file == "/user/ssh_config" os.environ.pop("NORNIR_CORE_RAISE_ON_ERROR") os.environ.pop("NORNIR_SSH_CONFIG_FILE")
def test_configuration_normal_override_env(self): os.environ["BRIGADE_NUM_WORKERS"] = "30" os.environ["BRIGADE_RAISE_ON_ERROR"] = "1" config = Config(config_file=os.path.join(dir_path, "config.yaml")) assert config.num_workers == 30 assert config.raise_on_error os.environ.pop("BRIGADE_NUM_WORKERS") os.environ.pop("BRIGADE_RAISE_ON_ERROR")
def __init__(self, inventory: Inventory, config: Config = None, data: GlobalState = None) -> None: self.data = data if data is not None else GlobalState() self.inventory = inventory self.config = config or Config()
def __init__( self, inventory: Inventory, config: Config = None, data: GlobalState = None, processors: Optional[Processors] = None, ) -> None: self.data = data if data is not None else GlobalState() self.inventory = inventory self.config = config or Config() self.processors = processors or Processors()
def test_configuration_normal(self): config = Config( config_file=os.path.join(dir_path, "config.yaml"), arg1=1, arg2=False, arg3=None, arg4="asd", ) assert config.num_workers == 10 assert not config.raise_on_error assert config.arg1 == 1 assert config.arg2 is False assert config.arg3 is None assert config.arg4 == "asd"
def test_config_basic(self): c = Config.from_dict( inventory={"plugin": "an-inventory"}, runner={ "plugin": "serial", "options": { "a": 1, "b": 2 } }, logging={"log_file": ""}, user_defined={"my_opt": True}, ) assert c.dict() == { "inventory": { "plugin": "an-inventory", "options": {}, "transform_function": "", "transform_function_options": {}, }, "runner": { "options": { "a": 1, "b": 2 }, "plugin": "serial" }, "ssh": { "config_file": str(Path("~/.ssh/config").expanduser()) }, "logging": { "enabled": True, "level": "INFO", "log_file": "", "format": DEFAULT_LOG_FORMAT, "to_console": False, "loggers": ["nornir"], }, "core": { "raise_on_error": False }, "user_defined": { "my_opt": True }, }
def InitNornir(config_file="", dry_run=False, **kwargs): """ Arguments: config_file(str): Path to the configuration file (optional) dry_run(bool): Whether to simulate changes or not **kwargs: Extra information to pass to the :obj:`nornir.core.configuration.Config` object Returns: :obj:`nornir.core.Nornir`: fully instantiated and configured """ conf = Config(config_file=config_file, **kwargs) inv_class = conf.inventory inv_args = getattr(conf, inv_class.__name__, {}) transform_function = conf.transform_function inv = inv_class(transform_function=transform_function, **inv_args) return Nornir(inventory=inv, dry_run=dry_run, config=conf)
def test_order_of_resolution_code_is_higher_than_env(self): os.environ["NORNIR_CORE_RAISE_ON_ERROR"] = "0" config = Config.from_file(os.path.join(dir_path, "config.yaml"), core={"raise_on_error": True}) os.environ.pop("NORNIR_CORE_RAISE_ON_ERROR") assert config.core.raise_on_error is True
def test_get_user_defined_from_file(self): config = Config.from_file(os.path.join(dir_path, "config.yaml")) assert config.user_defined["asd"] == "qwe"
def test_configuration_bool_env(self): os.environ["BRIGADE_RAISE_ON_ERROR"] = "0" config = Config() assert config.num_workers == 20 assert not config.raise_on_error
def test_configuration_bool_env(self): os.environ["NORNIR_CORE_RAISE_ON_ERROR"] = "0" config = Config.from_dict(inventory={"plugin": "an-inventory"}) assert not config.core.raise_on_error
def test_get_user_defined_from_file(self): config = Config(config_file=os.path.join(dir_path, "config.yaml")) assert ( config.get("user_defined", env="USER_DEFINED", default="qweqwe") == "asdasd" )
def test_configuration_file_override_argument(self): config = Config.from_file( os.path.join(dir_path, "config.yaml"), core={"raise_on_error": True}, ) assert config.core.raise_on_error
def test_get_user_defined_from_default(self): config = Config() assert ( config.get("user_defined", env="USER_DEFINED", default="qweqwe") == "qweqwe" )
def test_get_user_defined_from_env_bool(self): os.environ["USER_DEFINED"] = "0" config = Config() assert not config.get("user_defined", env="USER_DEFINED", parameter_type="bool") os.environ.pop("USER_DEFINED")
def test_get_user_defined_nested(self): config = Config(config_file=os.path.join(dir_path, "config.yaml")) assert config.get("user_defined", root="my_root") == "i am nested"