def test_should_be_able_to_create_a_config_ini_file_starting_from_the_data_of_another(self, reset_folder_structure, have_same_content): """A config ini should be able to create a config.ini file starting from the data of another.""" control = join(test_resources, "config_commented.ini") config_file = join(self.test_path, "config.ini") data = ConfigIni.read_file(control) ConfigIni().create_file(config_file, data) assert isfile(config_file) assert have_same_content(config_file, control)
def test_should_be_able_to_create_a_config_ini_file(self, reset_folder_structure, have_same_content): """A config ini should be able to create a config ini file.""" data = { "config": { "hostname": "TEST SERVER", "password": "******", "password_admin": "p4ssw0rd!", "mission_template": "mission.name" }, "bat": { "server_title": "TEST SERVER", "server_port": "2202", "server_max_mem": "8192", "server_config_file_name": "serverConfig.cfg", "server_cfg_file_name": "Arma3Training.cfg" }, "ODKSM": { "server_instance_name": "training" } } config_file = join(self.test_path, "config.ini") ConfigIni().create_file(config_file, data) assert isfile(config_file) control = join(test_resources, "config_commented.ini") assert have_same_content(config_file, control)
def test_it_should_compile_the_config_ini_file(self, reset_folder_structure, mocker): """When bootstrapping it should compile the config.ini file.""" mocker.patch("builtins.input", side_effect=["training", "n"]) self.sm.bootstrap(self.default_file) instance_folder = join(self.test_path, "training") config_file = join(instance_folder, "config.ini") assert isfile(config_file) # now try and read back that file data = ConfigIni.read_file(config_file) assert data["bat"]["server_max_mem"] == "8192" assert data["ODKSM"]["server_instance_name"] == "training" assert data["ODKSM"]["server_instance_root"] == abspath( join("tests/resources/Arma", "training"))
def test_should_be_able_to_read_a_config_ini_file(self): """A config ini should be able to read a config ini file.""" config_file = join(test_resources, "config.ini") data = ConfigIni.read_file(config_file) for key in ["ODKSM", "bat", "config", "mod_fix_settings"]: assert key in data # check some fields assert data["bat"]["server_title"] == "TEST SERVER" assert data["config"]["password"] == "p4ssw0rd" for supposed_list in ["server_mods_list", "mods_to_be_copied", "user_mods_list", "skip_keys"]: assert isinstance(data["ODKSM"][supposed_list], list) assert isinstance(data["mod_fix_settings"]["enabled_fixes"], list) assert data["mod_fix_settings"]["cba_settings"] == "tests/resources/server.cfg"
def test_should_ask_and_eventually_provide_for_custom_templates(self, reset_folder_structure, mocker, have_same_content): """When bootstrapping should ask and eventually provide for custom templates.""" mocker.patch("builtins.input", side_effect=["training", "y", "y", "y"]) self.sm.bootstrap(self.default_file) instance_folder = join(self.test_path, "training") bat_template = join(instance_folder, "run_server_template.txt") config_template = join(instance_folder, "server_cfg_template.txt") assert isfile(bat_template) assert have_same_content(bat_template, self.sm._get_resource_file( "templates/run_server_template.txt")) assert isfile(config_template) assert have_same_content(config_template, self.sm._get_resource_file( "templates/server_cfg_template.txt")) config_file = join(instance_folder, "config.ini") data = ConfigIni.read_file(config_file) assert data["bat"]["bat_template"] == bat_template
def _parse_config(self) -> None: """Parse the config file and create all settings container object.""" # Recover data in the file data = ConfigIni.read_file(self.config_file) # Create settings containers config_settings = ServerConfigSettings(**data["config"]) bat_settings = ServerBatSettings(**data["bat"]) enabled_fixes = [] if "enabled_fixes" in data["mod_fix_settings"]: enabled_fixes = data["mod_fix_settings"].pop("enabled_fixes") fix_settings = ModFixSettings( enabled_fixes=enabled_fixes, mod_fix_settings=data["mod_fix_settings"]) # create the global settings container self.settings = ServerInstanceSettings(**data["ODKSM"], bat_settings=bat_settings, config_settings=config_settings, fix_settings=fix_settings) # add missing mod_fix mods to mods_to_be_copied from odk_servermanager.modfix import register_fixes mod_fixes = register_fixes(fix_settings.enabled_fixes) for fix in mod_fixes: # Check that it's a required mod: if fix.name in self.settings.user_mods_list + self.settings.server_mods_list: copy_hooks = [ "hook_init_copy_pre", "hook_init_copy_replace", "hook_init_copy_post", "hook_update_copy_pre", "hook_update_copy_replace", "hook_update_copy_post" ] for copy_hook in copy_hooks: # Check if there's a copy hook enabled... if getattr( fix, copy_hook ) is not None and fix.name not in self.settings.mods_to_be_copied: # ... if so, add the mod to mods_to_be_copied self.settings.mods_to_be_copied.append(fix.name)
def test_should_be_able_to_create_a_config_ini_file_without_comments(self, reset_folder_structure, have_same_content): """A config ini should be able to create a config ini file without comments.""" data = { "config": { "hostname": "TEST SERVER", "password": "******", "password_admin": "p4ssw0rd!", "mission_template": "mission.name" }, "bat": { "server_title": "TEST SERVER", "server_port": "2202", "server_max_mem": "8192", "server_config_file_name": "ServerConfig.cfg", "server_cfg_file_name": "Arma3Training.cfg" }, "ODKSM": { "server_instance_name": "training", "server_mods_list": ["ODKMIN"], "mods_to_be_copied": ["ace"], "user_mods_list": ["ace", "CBA_A3"], "user_mods_preset": "tests/resources/preset.html", "arma_folder": "tests/resources/Arma", "skip_keys": "" }, "mod_fix_settings": { "enabled_fixes": ["cba_a3"], "cba_settings": "tests/resources/server.cfg" } } config_file = join(self.test_path, "config.ini") ConfigIni().create_file(config_file, data, add_comments=False, add_no_value_entry=False) assert isfile(config_file) control = join(test_resources, "config.ini") assert have_same_content(config_file, control)
def bootstrap(self, default_config_file: str = None) -> None: """Interactive UI to start building a new server instance.""" print("\n ======[ WELCOME TO ODKSM! ]======\n") try: # try and read the config data = ConfigIni.read_file(default_config_file, bootstrap=True) # check for needed fields if data["bootstrap"].get("instances_root", "") == "": raise ValueError( "'instances_root' field can't be empty in the [bootstrap] section!" ) if data["bootstrap"].get("odksm_folder_path", "") == "": raise ValueError( "'odksm_folder_path' field can't be empty in the [bootstrap] section!" ) except Exception as err: self._ui_abort( "\n [ERR] Error while reading the default config file!\n\n {}\n\n " "Check the documentation in the wiki, in the bootstrap.ini example file, in the README.md or" " in the odksm_servermanager/settings.py.\n Bye!\n".format( err)) try: # Check the instances_root exists instances_root = data["bootstrap"]["instances_root"] if not isdir(instances_root): raise Exception( "Could not find the instances_folder '{}'".format( instances_root)) print( "\n This utility will help you start creating your server instance.\n" ) instance_name = input(" Choose a unique name for the instance: ") # check if custom templates are needed custom_bat_template_needed = False custom_config_template_needed = False custom_templates_needed_string = input( " Will you need custom templates? (y/n) ") if custom_templates_needed_string == "y": print("\n OK! Now.. \n") custom_bat_template_needed_string = input( " ... will you need a custom BAT template? (y/n) ") if custom_bat_template_needed_string == "y": custom_bat_template_needed = True custom_config_template_needed_string = input( " ... will you need a custom CONFIG template? (y/n) ") if custom_config_template_needed_string == "y": custom_config_template_needed = True # create the folder instance_dir = join(instances_root, instance_name) mkdir(instance_dir) # copy templates if needed if custom_bat_template_needed: bat_template_file_name = "run_server_template.txt" bat_template_file = join(instance_dir, bat_template_file_name) template_file = self._get_resource_file( "templates/{}".format(bat_template_file_name)) copy(template_file, bat_template_file) data["bat"]["bat_template"] = abspath(bat_template_file) if custom_config_template_needed: config_template_file_name = "server_cfg_template.txt" config_template_file = join(instance_dir, config_template_file_name) template_file = self._get_resource_file( "templates/{}".format(config_template_file_name)) copy(template_file, config_template_file) data["config"]["config_template"] = abspath( config_template_file) # prepare some fields for the config file data["ODKSM"]["server_instance_name"] = instance_name data["ODKSM"]["server_instance_root"] = abspath(instance_dir) # generate the config.ini file ConfigIni().create_file(join(instance_dir, "config.ini"), data) # compile the ODKSM.bat file bat_template = pkg_resources.resource_string( "odk_servermanager", "templates/ODKSM_bat_template.txt").decode("UTF-8") bat_file = join(instance_dir, "ODKSM.bat") compile_from_template( bat_template, bat_file, {"odksm_folder_path": data["bootstrap"]["odksm_folder_path"]}) print( "\n Instance folder created!\n\n [WARNING] IMPORTANT! YOU ARE NOT DONE! You still need to edit the\n" " config.ini file in the folder and to run the actual ODKSM.bat tool.\n Bye!\n" ) except Exception as err: self._ui_abort( "\n [ERR] Error while bootstrapping the instance!\n\n {}\n\n " "Check the documentation in the wiki, in the bootstrap.ini example file, in the README.md or" " in the odksm_servermanager/settings.py.\n Bye!\n".format( err))
def test_should_be_able_to_get_the_config_ini_structure(self): """A config ini should be able to get the config.ini structure.""" data = ConfigIni._get_config_structure() assert isinstance(data, dict) assert len(data) == 2 assert len(data["sections"]) == 4
def test_should_be_able_to_read_a_bootstrap_ini_file(self, reset_folder_structure): """A config ini should be able to read a bootstrap.ini file.""" bootstrap_file = join(test_resources, "bootstrap.ini") data = ConfigIni.read_file(bootstrap_file, bootstrap=True) assert data["bootstrap"]["instances_root"] == "tests/resources/Arma"