def __init__(self, cli_args=None): """ The Application implementation. """ self._execution_dir_path = os.getcwd() self._working_dir_path = os.path.join( self._execution_dir_path, '{0}-behave-working-dir'.format( os.path.basename(self._execution_dir_path))) self._working_dir = None self._behave_runner = None if not cli_args.cli_config_path: cli_args.cli_config_path = CTFCliConfig.find_cli_config( self._execution_dir_path) self._cli_conf = CTFCliConfig(cli_args) # If no Dockerfile passed on the cli, try to use one from the execution directory if not self._cli_conf.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_DOCKERFILE): local_file = os.path.join(self._execution_dir_path, 'Dockerfile') if not os.path.isfile(local_file): raise CTFCliError( "No Dockerfile passed on the cli and no Dockerfile " "is present in the current directory!") logger.debug("Using Dockerfile from the current directory.") self._cli_conf.set(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_DOCKERFILE, local_file) # TODO: Remove this or rework, once more types are implemented if self._cli_conf.get(CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_EXEC_TYPE) != 'ansible': raise CTFCliError( "Wrong ExecType configured. Currently only 'ansible' is supported!" )
def _create_ansible_config(self): """ Create ansible configuration file :return: None """ try: method = self._cli_conf.get(CTFCliConfig.ANSIBLE_SECTION_NAME, CTFCliConfig.CONFIG_ANSIBLE_METHOD) host = self._cli_conf.get(CTFCliConfig.ANSIBLE_SECTION_NAME, CTFCliConfig.CONFIG_ANSIBLE_HOST) user = self._cli_conf.get(CTFCliConfig.ANSIBLE_SECTION_NAME, CTFCliConfig.CONFIG_ANSIBLE_USER) sudo = self._cli_conf.get(CTFCliConfig.ANSIBLE_SECTION_NAME, CTFCliConfig.CONFIG_ANSIBLE_SUDO) except NoSectionError as e: raise CTFCliError("No configuration for 'ansible' provided!") except NoOptionError as e: raise CTFCliError("Wrong ansible configuration: {0}".format(str(e))) ansible_conf_path = os.path.join(self._working_dir, 'ansible.conf') ansible_conf_content = "[ctf]\n{host} ansible_connection={method} ansible_ssh_user={user} ansible_sudo={sudo}\n".format( host=host, method=method, user=user, sudo=sudo ) logger.debug("Writing ansible configuration to '%s'\n%s", ansible_conf_path, ansible_conf_content) with open(ansible_conf_path, 'w') as f: f.write(ansible_conf_content) self._exec_type_conf_path = ansible_conf_path
def run(self): """ The main application execution method """ logger.info("Running Containers Testing Framework cli") try: check_call("git submodule update --init", shell=True) except: pass # TODO: Remove this or rework, once more types are implemented if self._cli_conf.get( CTFCliConfig.GLOBAL_SECTION_NAME, CTFCliConfig.CONFIG_EXEC_TYPE) != 'ansible': raise CTFCliError("Wrong ExecType configured. Currently only 'ansible' is supported!") self._working_dir = BehaveWorkingDirectory(self._working_dir_path, self._cli_conf) # Setup Behave structure inside working directory # Clone common Features and steps into the working dir # Add the project specific Features and steps # Prepare the steps.py in the Steps dir that combines all the other self._working_dir.setup() # Execute Behave self._behave_runner = BehaveRunner(self._working_dir, self._cli_conf) sys.exit(self._behave_runner.run())
def _create_ansible_config(self): """ Create ansible configuration file :return: None """ script = None method = None host = None user = None try: script = self._cli_conf.get(CTFCliConfig.ANSIBLE_SECTION_NAME, CTFCliConfig.CONFIG_ANSIBLE_DYNAMIC_SCRIPT) except NoSectionError: raise CTFCliError("No configuration for 'ansible' provided!") except NoOptionError: logger.debug("No dynamic provision script found") script = None if not script: try: method = self._cli_conf.get(CTFCliConfig.ANSIBLE_SECTION_NAME, CTFCliConfig.CONFIG_ANSIBLE_METHOD) host = self._cli_conf.get(CTFCliConfig.ANSIBLE_SECTION_NAME, CTFCliConfig.CONFIG_ANSIBLE_HOST) user = self._cli_conf.get(CTFCliConfig.ANSIBLE_SECTION_NAME, CTFCliConfig.CONFIG_ANSIBLE_USER) except NoOptionError: logger.debug("No dynamic provision script found") # Optional parameters try: sudo = self._cli_conf.get(CTFCliConfig.ANSIBLE_SECTION_NAME, CTFCliConfig.CONFIG_ANSIBLE_SUDO) except NoOptionError: sudo = False ansible_conf_path = None if script: ansible_conf_path = os.path.join(self._working_dir, script) shutil.copy(os.path.abspath(script), self._working_dir) logger.debug("Using ansible dynamic configuration from '%s'", ansible_conf_path) else: ansible_conf_path = os.path.join(self._working_dir, 'ansible.conf') ansible_conf_content = "[ctf]\n{host} ansible_connection={method} ansible_ssh_user={user} ansible_sudo={sudo}\n".format( host=host, method=method, user=user, sudo=sudo ) logger.debug("Writing ansible configuration to '%s'\n%s", ansible_conf_path, ansible_conf_content) with open(ansible_conf_path, 'w') as f: f.write(ansible_conf_content) self._exec_type_conf_path = ansible_conf_path
def _add_remote_features(self): """ Add all remote features :return: """ for test in self._tests_conf.get_tests(): remote_repo = self._tests_conf.get_test_features(test) local_dir = os.path.join(self._features_dir, '{0}_features'.format(test).replace('-', '_')) logger.info("Using remote Features from '%s'", remote_repo) logger.debug("Cloning remote test Features from '%s' to '%s'", remote_repo, local_dir) try: check_call(['git', 'clone', '-q', remote_repo, local_dir]) except CalledProcessError as e: raise CTFCliError("Cloning of {0} failed!\n{1}".format(remote_repo, str(e)))