def render_expected_profiles(): user = get_user() dict = { 'user': get_user(), 'home': get_user_environment_variable('HOME'), 'target_home': '/home/{}'.format(user) } expected_profiles = [] for boilerplate in expected_profile_boilerplates: template = Template(boilerplate) expected_profiles.append(template.render(dict)) return expected_profiles
def get_base_dictionary(): base_dict = {} current_user_name = get_user() base_dict["edi_current_user_name"] = current_user_name base_dict["edi_current_user_group_name"] = get_user_group() base_dict["edi_current_user_ssh_pub_keys"] = get_user_ssh_pub_keys() base_dict["edi_current_user_uid"] = get_user_uid() base_dict["edi_current_user_gid"] = get_user_gid() base_dict[ "edi_current_user_host_home_directory"] = get_user_home_directory( current_user_name) base_dict["edi_current_user_target_home_directory"] = "/home/{}".format( current_user_name) base_dict["edi_host_hostname"] = get_hostname() base_dict["edi_edi_plugin_directory"] = get_edi_plugin_directory() proxy_setup = ProxySetup() base_dict["edi_host_http_proxy"] = proxy_setup.get('http_proxy', default='') base_dict["edi_host_https_proxy"] = proxy_setup.get('https_proxy', default='') base_dict["edi_host_ftp_proxy"] = proxy_setup.get('ftp_proxy', default='') base_dict["edi_host_socks_proxy"] = proxy_setup.get('all_proxy', default='') base_dict["edi_host_no_proxy"] = proxy_setup.get('no_proxy', default='') base_dict["edi_edi_version"] = get_edi_version() base_dict["edi_lxd_version"] = get_lxd_version() base_dict["edi_current_display"] = get_current_display() return base_dict
def __init__(self, base_config_file): self.base_config_file = base_config_file self.project_directory = dirname(abspath(base_config_file.name)) self.config_id = splitext(basename(base_config_file.name))[0] if not ConfigurationParser._configurations.get(self.config_id): logging.info(("Load time dictionary:\n{}").format( yaml.dump(remove_passwords(self._get_load_time_dictionary()), default_flow_style=False))) logging.info(("Using base configuration file '{0}'").format( base_config_file.name)) base_config = self._get_base_config(base_config_file) global_config = self._get_overlay_config(base_config_file, "global") hostname = get_hostname() system_config = self._get_overlay_config(base_config_file, hostname) user = get_user() if user == hostname: user = '******'.format(user) logging.warning(( "User name and host name are equal! Going to search user overlay file " "with '.user' postfix.")) user_config = self._get_overlay_config(base_config_file, user) merge_1 = self._merge_configurations(base_config, global_config) merge_2 = self._merge_configurations(merge_1, system_config) merged_config = self._merge_configurations(merge_2, user_config) ConfigurationParser._configurations[self.config_id] = merged_config logging.info("Merged configuration:\n{0}".format(self.dump())) self._verify_version_compatibility()
def __init__(self, base_config_file): self.config_directory = dirname(abspath(base_config_file.name)) self.config_id = splitext(basename(base_config_file.name))[0] if not ConfigurationParser._configurations.get(self.config_id): logging.info(("Load time dictionary:\n{}").format( yaml.dump(self._get_load_time_dictionary(), default_flow_style=False))) logging.info(("Using base configuration file '{0}'").format( base_config_file.name)) base_config = self._get_base_config(base_config_file) global_config = self._get_overlay_config(base_config_file, "global") system_config = self._get_overlay_config(base_config_file, get_hostname()) user_config = self._get_overlay_config(base_config_file, get_user()) merge_1 = self._merge_configurations(base_config, global_config) merge_2 = self._merge_configurations(merge_1, system_config) merged_config = self._merge_configurations(merge_2, user_config) ConfigurationParser._configurations[self.config_id] = merged_config logging.info("Merged configuration:\n{0}".format(self.dump())) self._verify_version_compatibility()
def config_files(tmpdir_factory): dir_name = tmpdir_factory.mktemp('configuration') main_file = "{0}.yml".format(_config_name) with open(str(dir_name.join(main_file)), "w") as file: file.write(sample_file) overlay_dir = dir_name.join("configuration", "overlay") os.makedirs(str(overlay_dir)) overlays = [("global", sample_global_file), (get_user(), sample_user_file), (get_hostname(), sample_system_file)] for overlay in overlays: o_type, o_content = overlay o_file_name = "{0}.{1}.yml".format(_config_name, o_type) with open(str(overlay_dir.join(o_file_name)), "w") as o_file: o_file.write(o_content) for subdir in ['playbooks', 'templates', 'profiles']: directory = dir_name.join("plugins", subdir) os.makedirs(str(directory)) with open(str(directory.join("foo.yml")), "w") as file: file.write("baz") commands_dir = dir_name.join("plugins", "commands") os.makedirs(str(commands_dir)) commands = [ ('first', first_command), ('second', second_command), ('last', last_command), ] for name, content in commands: with open(str(commands_dir.join(name)), "w") as file: file.write(content) return str(dir_name.join(main_file))
def get_base_dictionary(): base_dict = {} current_user_name = get_user() base_dict["edi_current_user_name"] = current_user_name base_dict["edi_current_user_uid"] = get_user_uid() base_dict["edi_current_user_gid"] = get_user_gid() base_dict[ "edi_current_user_host_home_directory"] = get_user_environment_variable( "HOME") base_dict["edi_current_user_target_home_directory"] = "/home/{}".format( current_user_name) base_dict["edi_host_hostname"] = get_hostname() base_dict["edi_edi_plugin_directory"] = get_edi_plugin_directory() base_dict["edi_host_http_proxy"] = get_user_environment_variable( 'http_proxy', '') base_dict["edi_host_https_proxy"] = get_user_environment_variable( 'https_proxy', '') base_dict["edi_host_ftp_proxy"] = get_user_environment_variable( 'ftp_proxy', '') base_dict["edi_host_socks_proxy"] = get_user_environment_variable( 'all_proxy', '') base_dict["edi_host_no_proxy"] = get_user_environment_variable( 'no_proxy', '') base_dict["edi_lxd_version"] = get_lxd_version() return base_dict
def render_expected_profiles(): user = get_user() dictionary = {'user': user, 'home': get_user_home_directory(user), 'target_home': '/home/{}'.format(user)} expected_profiles = [] for boilerplate in expected_profile_boilerplates: template = Template(boilerplate) expected_profiles.append(normalize_yaml(template.render(dictionary))) return expected_profiles
def run(popenargs, sudo=False, input=None, timeout=None, check=True, universal_newlines=True, stdout=_ADAPTIVE, log_threshold=logging.DEBUG, **kwargs): """ Small wrapper around subprocess.run(). """ assert type(popenargs) is list subprocess_stdout = stdout if subprocess_stdout == _ADAPTIVE: if logging.getLogger().isEnabledFor(log_threshold): subprocess_stdout = None else: subprocess_stdout = subprocess.PIPE all_args = list() if not sudo and os.getuid() == 0: current_user = get_user() if current_user != 'root': # drop privileges all_args.extend(['sudo', '-u', current_user]) elif sudo and os.getuid() != 0: all_args.append('sudo') all_args.extend(popenargs) logging.log(log_threshold, "Running command: {0}".format(all_args)) result = mockablerun.run_mockable(all_args, input=input, timeout=timeout, check=check, universal_newlines=universal_newlines, stdout=subprocess_stdout, **kwargs) if (logging.getLogger().isEnabledFor(log_threshold) and subprocess_stdout is subprocess.PIPE): logging.log(log_threshold, result.stdout) return result
def run(popenargs, sudo=False, input=None, timeout=None, check=True, universal_newlines=True, stdout=_ADAPTIVE, **kwargs): """ Small wrapper around subprocess.run(). """ assert type(popenargs) is list subprocess_stdout = stdout if subprocess_stdout == _ADAPTIVE: if logging.getLogger().isEnabledFor(logging.INFO): subprocess_stdout = None else: subprocess_stdout = subprocess.PIPE myargs = popenargs.copy() if not sudo and os.getuid() == 0: current_user = get_user() if current_user != 'root': # drop privileges myargs = ["sudo", "-u", current_user] + myargs elif sudo and os.getuid() != 0: myargs.insert(0, "sudo") logging.info("Running command: {0}".format(myargs)) result = mockablerun.run_mockable(myargs, input=input, timeout=timeout, check=check, universal_newlines=universal_newlines, stdout=subprocess_stdout, **kwargs) if (logging.getLogger().isEnabledFor(logging.INFO) and subprocess_stdout is subprocess.PIPE): logging.info(result.stdout) return result
def _run_playbook(self, playbook, inventory, extra_vars, ansible_user): require_executable("ansible-playbook", "sudo apt install ansible") cmd = [] cmd.append("ansible-playbook") cmd.extend(["--connection", self.connection]) cmd.extend(["--inventory", inventory]) cmd.extend(["--extra-vars", "@{}".format(extra_vars)]) if self.connection == "ssh": cmd.extend(["--user", ansible_user]) cmd.append(playbook) if logging.getLogger().isEnabledFor(logging.DEBUG): cmd.append("-vvvv") ansible_env = os.environ.copy() ansible_env['ANSIBLE_REMOTE_TEMP'] = '/tmp/ansible-{}'.format(get_user()) run(cmd, env=ansible_env, log_threshold=logging.INFO)
def config_files(tmpdir_factory): dir_name = tmpdir_factory.mktemp('configuration') main_file = "{0}.yml".format(config_name) with open(str(dir_name.join(main_file)), "w") as file: file.write(sample_file) overlay_dir = dir_name.join("configuration", "overlay") os.makedirs(str(overlay_dir)) overlays = [("global", sample_global_file), (get_user(), sample_user_file), (get_hostname(), sample_system_file)] for overlay in overlays: o_type, o_content = overlay o_file_name = "{0}.{1}.yml".format(config_name, o_type) with open(str(overlay_dir.join(o_file_name)), "w") as o_file: o_file.write(o_content) playbook_dir = dir_name.join("plugins", "playbooks") os.makedirs(str(playbook_dir)) with open(str(playbook_dir.join("foo.yml")), "w") as file: file.write("baz") return str(dir_name.join(main_file))
def _run_playbook(self, playbook, inventory, extra_vars, ansible_user): snap_path = '/snap/bin' cmd = list() # on a Debian system the snap path might not be found in the PATH variable if snap_path not in os.environ['PATH']: cmd.extend( ["env", "PATH={}:{}".format(os.environ['PATH'], snap_path)]) cmd.append("ansible-playbook") cmd.extend(["--connection", self.connection]) cmd.extend(["--inventory", inventory]) cmd.extend(["--extra-vars", "@{}".format(extra_vars)]) if self.connection == "ssh": cmd.extend(["--user", ansible_user]) cmd.append(playbook) if logging.getLogger().isEnabledFor(logging.DEBUG): cmd.append("-vvvv") ansible_env = os.environ.copy() ansible_env['ANSIBLE_REMOTE_TEMP'] = '/tmp/ansible-{}'.format( get_user()) run(cmd, env=ansible_env, log_threshold=logging.INFO)
def _run_playbook(self, playbook, inventory, extra_vars, ansible_user): snap_path = '/snap/bin' cmd = list() # on a Debian system the snap path might not be found in the PATH variable if snap_path not in os.environ['PATH']: cmd.extend(["env", "PATH={}:{}".format(os.environ['PATH'], snap_path)]) cmd.append("ansible-playbook") cmd.extend(["--connection", self.connection]) cmd.extend(["--inventory", inventory]) cmd.extend(["--extra-vars", "@{}".format(extra_vars)]) if self.connection == "ssh": cmd.extend(["--user", ansible_user]) cmd.append(playbook) if logging.getLogger().isEnabledFor(logging.DEBUG): cmd.append("-vvvv") ansible_env = os.environ.copy() ansible_env['ANSIBLE_REMOTE_TEMP'] = '/tmp/ansible-{}'.format(get_user()) while True: try: run(cmd, env=ansible_env, log_threshold=logging.INFO) break except subprocess.CalledProcessError as error: if self.config.debug_mode(): print_error("{}\nFor more information increase the log level.".format(error)) click.echo('You can now fix the playbook and press "r" to retry. Hit any other key to abort: ', nl=False) input_char = click.getchar() click.echo() if input_char.lower() == 'r': pass else: raise KeyboardInterrupt else: raise error
def suppress_chown_during_debuild(monkeypatch): def fakechown(*_): pass if get_user() == 'root': # debuild case monkeypatch.setattr(shutil, 'chown', fakechown)