def test_sanitize_container_name(container_name, expected_name): sanitize_container_name(str(container_name)) == expected_name
def prepare_env(self): """ Manages reading environment metadata files under ``private_data_dir`` and merging/updating with existing values so the :py:class:`ansible_runner.runner.Runner` object can read and use them easily """ try: passwords = self.loader.load_file('env/passwords', Mapping) self.expect_passwords = { re.compile(pattern, re.M): password for pattern, password in iteritems(passwords) } except ConfigurationError: output.debug('Not loading passwords') self.expect_passwords = dict() self.expect_passwords[pexpect.TIMEOUT] = None self.expect_passwords[pexpect.EOF] = None try: self.settings = self.loader.load_file('env/settings', Mapping) except ConfigurationError: output.debug("Not loading settings") self.settings = dict() self.process_isolation = self.settings.get('process_isolation', self.process_isolation) self.process_isolation_executable = self.settings.get('process_isolation_executable', self.process_isolation_executable) if self.containerized: self.container_name = "ansible_runner_{}".format(sanitize_container_name(self.ident)) self.env = {} # Special flags to convey info to entrypoint or process in container self.env['LAUNCHED_BY_RUNNER'] = '1' artifact_dir = os.path.join("/runner/artifacts", "{}".format(self.ident)) self.env['AWX_ISOLATED_DATA_DIR'] = artifact_dir if self.fact_cache_type == 'jsonfile': self.env['ANSIBLE_CACHE_PLUGIN_CONNECTION'] = os.path.join(artifact_dir, 'fact_cache') else: # seed env with existing shell env self.env = os.environ.copy() if self.envvars and isinstance(self.envvars, dict): self.env.update(self.envvars) try: envvars = self.loader.load_file('env/envvars', Mapping) if envvars: self.env.update({str(k):str(v) for k, v in envvars.items()}) except ConfigurationError: output.debug("Not loading environment vars") # Still need to pass default environment to pexpect try: if self.ssh_key_data is None: self.ssh_key_data = self.loader.load_file('env/ssh_key', string_types) except ConfigurationError: output.debug("Not loading ssh key") self.ssh_key_data = None self.idle_timeout = self.settings.get('idle_timeout', None) self.job_timeout = self.settings.get('job_timeout', None) self.pexpect_timeout = self.settings.get('pexpect_timeout', 5) self.process_isolation_path = self.settings.get('process_isolation_path', self.process_isolation_path) self.process_isolation_hide_paths = self.settings.get('process_isolation_hide_paths', self.process_isolation_hide_paths) self.process_isolation_show_paths = self.settings.get('process_isolation_show_paths', self.process_isolation_show_paths) self.process_isolation_ro_paths = self.settings.get('process_isolation_ro_paths', self.process_isolation_ro_paths) self.directory_isolation_cleanup = bool(self.settings.get('directory_isolation_cleanup', True)) self.container_image = self.settings.get('container_image', self.container_image) self.container_volume_mounts = self.settings.get('container_volume_mounts', self.container_volume_mounts) self.container_options = self.settings.get('container_options', self.container_options) self.resource_profiling = self.settings.get('resource_profiling', self.resource_profiling) self.resource_profiling_base_cgroup = self.settings.get('resource_profiling_base_cgroup', self.resource_profiling_base_cgroup) self.resource_profiling_cpu_poll_interval = self.settings.get('resource_profiling_cpu_poll_interval', self.resource_profiling_cpu_poll_interval) self.resource_profiling_memory_poll_interval = self.settings.get('resource_profiling_memory_poll_interval', self.resource_profiling_memory_poll_interval) self.resource_profiling_pid_poll_interval = self.settings.get('resource_profiling_pid_poll_interval', self.resource_profiling_pid_poll_interval) self.resource_profiling_results_dir = self.settings.get('resource_profiling_results_dir', self.resource_profiling_results_dir) self.pexpect_use_poll = self.settings.get('pexpect_use_poll', True) self.suppress_ansible_output = self.settings.get('suppress_ansible_output', self.quiet) if 'AD_HOC_COMMAND_ID' in self.env or not os.path.exists(self.project_dir): self.cwd = self.private_data_dir else: if self.directory_isolation_path is not None: self.cwd = self.directory_isolation_path else: self.cwd = self.project_dir if 'fact_cache' in self.settings: if 'fact_cache_type' in self.settings: if self.settings['fact_cache_type'] == 'jsonfile': self.fact_cache = os.path.join(self.artifact_dir, self.settings['fact_cache']) else: self.fact_cache = os.path.join(self.artifact_dir, self.settings['fact_cache'])
def _prepare_env(self, runner_mode='pexpect'): """ Manages reading environment metadata files under ``private_data_dir`` and merging/updating with existing values so the :py:class:`ansible_runner.runner.Runner` object can read and use them easily """ self.runner_mode = runner_mode try: if self.settings: self.settings = self.settings.update(self.loader.load_file('env/settings', Mapping)) else: self.settings = self.loader.load_file('env/settings', Mapping) except ConfigurationError: debug("Not loading settings") self.settings = dict() if self.runner_mode == 'pexpect': try: passwords = self.loader.load_file('env/passwords', Mapping) self.expect_passwords = { re.compile(pattern, re.M): password for pattern, password in iteritems(passwords) } except ConfigurationError: debug('Not loading passwords') self.expect_passwords = dict() self.expect_passwords[pexpect.TIMEOUT] = None self.expect_passwords[pexpect.EOF] = None if self.passwords: self.expect_passwords.update(self.passwords) self.pexpect_timeout = self.settings.get('pexpect_timeout', 5) self.pexpect_use_poll = self.settings.get('pexpect_use_poll', True) self.idle_timeout = self.settings.get('idle_timeout', None) self.job_timeout = self.settings.get('job_timeout', None) elif self.runner_mode == 'subprocess': self.subprocess_timeout = self.settings.get('subprocess_timeout', 300) self.process_isolation = self.settings.get('process_isolation', self.process_isolation) self.process_isolation_executable = self.settings.get('process_isolation_executable', self.process_isolation_executable) self.container_image = self.settings.get('container_image', self.container_image) self.container_volume_mounts = self.settings.get('container_volume_mounts', self.container_volume_mounts) self.container_options = self.settings.get('container_options', self.container_options) if self.containerized: self.container_name = "ansible_runner_{}".format(sanitize_container_name(self.ident)) self.env = {} # Special flags to convey info to entrypoint or process in container self.env['LAUNCHED_BY_RUNNER'] = '1' artifact_dir = os.path.join("/runner/artifacts", "{}".format(self.ident)) self.env['AWX_ISOLATED_DATA_DIR'] = artifact_dir if self.fact_cache_type == 'jsonfile': self.env['ANSIBLE_CACHE_PLUGIN_CONNECTION'] = os.path.join(artifact_dir, 'fact_cache') else: # seed env with existing shell env self.env = os.environ.copy() if self.envvars and isinstance(self.envvars, dict): self.env.update(self.envvars) try: envvars = self.loader.load_file('env/envvars', Mapping) if envvars: self.env.update({str(k):str(v) for k, v in envvars.items()}) except ConfigurationError: debug("Not loading environment vars") # Still need to pass default environment to pexpect try: if self.ssh_key_data is None: self.ssh_key_data = self.loader.load_file('env/ssh_key', string_types) except ConfigurationError: debug("Not loading ssh key") self.ssh_key_data = None # write the SSH key data into a fifo read by ssh-agent if self.ssh_key_data: self.ssh_key_path = os.path.join(self.artifact_dir, 'ssh_key_data') open_fifo_write(self.ssh_key_path, self.ssh_key_data) self.suppress_ansible_output = self.settings.get('suppress_ansible_output', self.quiet) if 'fact_cache' in self.settings: if 'fact_cache_type' in self.settings: if self.settings['fact_cache_type'] == 'jsonfile': self.fact_cache = os.path.join(self.artifact_dir, self.settings['fact_cache']) else: self.fact_cache = os.path.join(self.artifact_dir, self.settings['fact_cache']) # Use local callback directory if not self.containerized: callback_dir = self.env.get('AWX_LIB_DIRECTORY', os.getenv('AWX_LIB_DIRECTORY')) if callback_dir is None: callback_dir = os.path.join(os.path.split(os.path.abspath(__file__))[0], "..", "callbacks") python_path = self.env.get('PYTHONPATH', os.getenv('PYTHONPATH', '')) self.env['PYTHONPATH'] = ':'.join([python_path, callback_dir]) if python_path and not python_path.endswith(':'): python_path += ':' self.env['ANSIBLE_CALLBACK_PLUGINS'] = ':'.join(filter(None,(self.env.get('ANSIBLE_CALLBACK_PLUGINS'), callback_dir))) if 'AD_HOC_COMMAND_ID' in self.env: self.env['ANSIBLE_STDOUT_CALLBACK'] = 'minimal' else: self.env['ANSIBLE_STDOUT_CALLBACK'] = 'awx_display' self.env['ANSIBLE_RETRY_FILES_ENABLED'] = 'False' if 'ANSIBLE_HOST_KEY_CHECKING' not in self.env: self.env['ANSIBLE_HOST_KEY_CHECKING'] = 'False' if not self.containerized: self.env['AWX_ISOLATED_DATA_DIR'] = self.artifact_dir if self.fact_cache_type == 'jsonfile': self.env['ANSIBLE_CACHE_PLUGIN'] = 'jsonfile' if not self.containerized: self.env['ANSIBLE_CACHE_PLUGIN_CONNECTION'] = self.fact_cache debug('env:') for k,v in sorted(self.env.items()): debug(f' {k}: {v}')