def imports(): return [ TaskDeclaration(CleanTask()), TaskDeclaration(PublishTask()), TaskDeclaration(BuildTask()), TaskDeclaration(InstallTask()), TaskDeclaration(UnitTestTask()) ]
def imports(): return [ TaskDeclaration(CooperativeSyncTask()), TaskDeclaration(CooperativeSnippetInstallTask()), TaskDeclaration(CooperativeInstallTask()), TaskDeclaration(CooperativeSnippetWizardTask()), TaskDeclaration(CooperativeSyncTask()), TaskDeclaration(GetEnvTask()), TaskDeclaration(SetEnvTask()), TaskDeclaration(FileRendererTask()), TaskDeclaration(RenderDirectoryTask()) ]
def mock_execution_context( task: TaskInterface, args: Dict[str, str] = None, env: Dict[str, str] = None, defined_args: Dict[str, dict] = None) -> ExecutionContext: """ Prepares a simplified rkd.api.contract.ExecutionContext instance :param task: :param args: :param env: :param defined_args: :return: """ if args is None: args = {} if env is None: env = {} if defined_args is None: defined_args = {} if args and not defined_args: for name, passed_value in args.items(): defined_args[name] = {'default': ''} return ExecutionContext(TaskDeclaration(task), parent=None, args=args, env=env, defined_args=defined_args)
def test_functional_hooks_are_executed_when_exists_and_files_with_extension_only_are_skipped( self): """Given we have an example hooks in pre-upgrade/whoami.sh and in post-upgrade/history.sh And we try to run those hooks using hooks_executed() Then we will see output produced by those scripts And .dotfiles will be ignored """ self._prepare_test_data() buffer = StringIO() hooks_capturing_io = IO() task = TestTask() task._io = BufferedSystemIO() ctx = ExecutionContext(TaskDeclaration(task), args={}, env={}) with hooks_capturing_io.capture_descriptors(stream=buffer, enable_standard_out=True): with task.hooks_executed(ctx, 'upgrade'): pass self.assertIn('>> This is a whoami.sh hook, test:', buffer.getvalue(), msg='Expected pre-upgrade hook to be ran') self.assertIn('25 June 1978 the rainbow flag was first flown', buffer.getvalue(), msg='Expected post-upgrade hook to be ran') self.assertIn('pre-upgrade/whoami.sh', task._io.get_value()) self.assertNotIn('.gitkeep', task._io.get_value())
def test_distinct_imports_separtes_lists(self): """A successful case for distinct_imports()""" imports, aliases = distinct_imports('hello', [ TaskDeclaration(TaskForTesting()), TaskAliasDeclaration(':hello', [':test']) ]) self.assertTrue(isinstance(imports[0], TaskDeclaration)) self.assertTrue(isinstance(aliases[0], TaskAliasDeclaration))
def execute_mocked_task_and_get_output(self, task: TaskInterface, args=None, env=None) -> str: """ Run a single task, capturing it's output in a simplified way. There is no whole RKD bootstrapped in this operation. :param TaskInterface task: :param dict args: :param dict env: :return: """ if args is None: args = {} if env is None: env = {} ctx = ApplicationContext([], [], '') ctx.io = BufferedSystemIO() task.internal_inject_dependencies( io=ctx.io, ctx=ctx, executor=OneByOneTaskExecutor(ctx=ctx), temp_manager=TempManager()) merged_env = deepcopy(os.environ) merged_env.update(env) r_io = IO() str_io = StringIO() defined_args = {} for arg, arg_value in args.items(): defined_args[arg] = {'default': ''} with r_io.capture_descriptors(enable_standard_out=True, stream=str_io): try: # noinspection PyTypeChecker result = task.execute( ExecutionContext(TaskDeclaration(task), args=args, env=merged_env, defined_args=defined_args)) except Exception: self._restore_standard_out() print(ctx.io.get_value() + "\n" + str_io.getvalue()) raise return ctx.io.get_value() + "\n" + str_io.getvalue( ) + "\nTASK_EXIT_RESULT=" + str(result)
def _get_prepared_compose_driver(self, args: dict = {}, env: dict = {}) -> ComposeDriver: merged_env = deepcopy(os.environ) merged_env.update(env) task = self.satisfy_task_dependencies(TestTask(), BufferedSystemIO()) declaration = TaskDeclaration(task) ctx = ExecutionContext(declaration, args=args, env=merged_env) return ComposeDriver(task, ctx, TEST_PROJECT_NAME)
def test_non_existing_dir_is_skipped(self): """Assert that non-existing directory does not cause exception, but will be skipped""" task = TestTask() task._io = BufferedSystemIO() task._io.set_log_level('debug') ctx = ExecutionContext(TaskDeclaration(task), args={}, env={}) task.execute_hooks(ctx, 'non-existing-directory') self.assertIn( 'Hooks dir "./hooks.d//non-existing-directory/" not present, skipping', task._io.get_value())
def test_context_resolves_recursively_task_aliases(self): ctx = ApplicationContext([TaskDeclaration(InitTask())], [ TaskAliasDeclaration(':deeper', [':init', ':init']), TaskAliasDeclaration(':deep', [':init', ':deeper']) ], directory='') ctx.compile() task = ctx.find_task_by_name(':deep') task: GroupDeclaration # :deeper = :init # :deep = :init :deeper = :init :init :init self.assertEqual(':init', task.get_declarations()[0].to_full_name()) self.assertEqual(':init', task.get_declarations()[1].to_full_name()) self.assertEqual(':init', task.get_declarations()[2].to_full_name())
def test_stop_task_executes_stop_task_multiple_times(self): """Test that StopTask will call Driver.stop() multiple times""" task = StopTask() recorded_calls = [] ctx = ExecutionContext(TaskDeclaration(task), args={}, env={}) task.containers( ctx ).stop = lambda service_name, args='', capture=False: recorded_calls.append( service_name) self.execute_mocked_task_and_get_output(task, args={'--profile': ''}) self.assertIn('gateway', recorded_calls) self.assertIn('gateway_letsencrypt', recorded_calls) self.assertIn('gateway_proxy_gen', recorded_calls)
def test_restart_calls_driver_restart_method_on_matched_services(self): """Test calls restart on expected services in expected order""" task = RestartTask() restarted_services = [] ctx = ExecutionContext(TaskDeclaration(task), args={}, env={}) task.containers( ctx ).restart = lambda service_name, args='': restarted_services.append( service_name) self.execute_mocked_task_and_get_output(task, args={ '--profile': 'profile1', '--with-image': False }) self.assertEqual(['gateway', 'website'], restarted_services)
def test_functional_execute_hooks_executes_post_upgrade_hooks(self): """Assert that post-upgrade/history.sh is executed""" self._prepare_test_data() buffer = StringIO() hooks_capturing_io = IO() task = TestTask() task._io = BufferedSystemIO() ctx = ExecutionContext(TaskDeclaration(task), args={}, env={}) with hooks_capturing_io.capture_descriptors(stream=buffer, enable_standard_out=True): task.execute_hooks(ctx, 'post-upgrade') self.assertIn('25 June 1978 the rainbow flag was first flown', buffer.getvalue(), msg='Expected post-upgrade hook to be ran')
def imports(): return [ TaskDeclaration(ListContainersTask()), TaskDeclaration(StartTask()), TaskDeclaration(UpgradeTask()), TaskDeclaration(StopTask()), TaskDeclaration(StopAndRemoveTask()), TaskDeclaration(ListDefinedServices()), TaskDeclaration(ServiceUpTask()), TaskDeclaration(ServiceStopTask()), TaskDeclaration(WaitForServiceTask()), TaskDeclaration(ServiceRemoveTask()), TaskDeclaration(GetContainerNameTask()), TaskDeclaration(ExecTask()), TaskDeclaration(LogsTask()), TaskDeclaration(AnalyzeServiceTask()), TaskDeclaration(InspectContainerTask()), TaskDeclaration(PullTask()), TaskDeclaration(RestartTask()), TaskDeclaration(ListConfigsTask()), TaskDeclaration(EnableConfigTask()), TaskDeclaration(DisableConfigTask()), TaskDeclaration(DumpComposeArguments()), TaskDeclaration(DumpComposeConfigTask()), # production-related TaskDeclaration(ReloadGatewayTask()), TaskDeclaration(ShowSSLStatusTask()), TaskDeclaration(ForceReloadSSLTask()), TaskDeclaration(MaintenanceOnTask()), TaskDeclaration(MaintenanceOffTask()), TaskDeclaration(DeploymentTask()), TaskDeclaration(SSHTask()), TaskDeclaration(UpdateFilesTask()), TaskDeclaration(CreateExampleDeploymentFileTask()), TaskDeclaration(ManageVagrantTask()), TaskDeclaration(EditVaultTask()), TaskDeclaration(EncryptVaultTask()), TaskDeclaration(EnvEncryptTask()), # git TaskDeclaration(FetchRepositoryTask()), TaskDeclaration(FetchAllRepositories()), TaskDeclaration(SetPermissionsForWritableDirectoriesTask()), TaskDeclaration(ListRepositoriesTask()), TaskDeclaration(CooperativeSyncTask()), TaskDeclaration(CooperativeInstallTask()), TaskDeclaration(CreateHarborStructureTask()), TaskDeclaration(CooperativeSnippetWizardTask()), TaskDeclaration(CooperativeSnippetInstallTask()), TaskDeclaration(GetEnvTask()), TaskDeclaration(SetEnvTask()), TaskDeclaration(FileRendererTask()), TaskDeclaration(RenderDirectoryTask()), # templates TaskAliasDeclaration( ':harbor:templates:render', [ ':j2:directory-to-directory', '--source=templates', '--target=./' ], description='Render templates stored in containers/templates ' + 'into root directory, with preserving the tree structure') ]
import os from rkd.api.syntax import TaskDeclaration from rkd.api.contract import ExecutionContext from rkd.standardlib import CallableTask def union_method(context: ExecutionContext) -> bool: os.system('xdg-open https://iwa-ait.org') return True IMPORTS = [TaskDeclaration(CallableTask(':create-union', union_method))] TASKS = []
from rkd_python import imports as PythonImports # group of imports (not all packages supports it, but most of them) from rkd.standardlib.jinja import FileRendererTask # single task from rkd.standardlib import CallableTask # Basic Python callable task for a little bit advanced usage # from .mypackage import MyTask # import your task from local package def example_method(ctx: ExecutionContext, task: CallableTask) -> bool: os.system('xdg-open https://twitter.com/wrkclasshistory') return True IMPORTS = [ # We declare that we will use this task. # Declaration can take some additional arguments like args= or env=, to always append environment and/or commandline switches # regardless of if user used it TaskDeclaration(FileRendererTask()), # remember about the "," between tasks, it's an array/list ;) # TaskDeclaration(MyTask()) TaskDeclaration( CallableTask(':read-real-history', example_method, description='Example task with simple Python code')) ] IMPORTS += PythonImports() TASKS = [ # declared task-aliases. A Task Alias is a shortcut eg. ":release" that will expands to ":py:build :py:publish --username= (...)" # the best feature in task-aliases is that you can append and overwrite last commandline arguments, those will be added # at the end of the command Task(
this.sh('docker tag {image} {image}-$(date "+%Y-%m-%d")'.format( image=image_name)) if should_push: this.sh('docker push {image}'.format(image=image_name)) this.sh( 'docker push {image}-$(date "+%Y-%m-%d")'.format(image=image_name)) return True # # Single tasks declarations # IMPORTS = [ TaskDeclaration(GenerateReadmeTask()), # # Builds a Docker image # TaskDeclaration( CallableTask(':image', build_image, description='Builds a docker image', argparse_options=[ ArgparseArgument( args=['--git-tag'], kwargs={ 'required': False, 'help':
# # Base RKD Makefile, contains basic commands such as :tasks, :sh or :version # from rkd.api.syntax import TaskDeclaration from rkd.standardlib.shell import ShellCommandTask, ExecProcessCommand from rkd.standardlib import InitTask, TasksListingTask, VersionTask, CreateStructureTask, LineInFileTask IMPORTS = [ TaskDeclaration(ShellCommandTask()), TaskDeclaration(ExecProcessCommand()), TaskDeclaration(InitTask()), TaskDeclaration(TasksListingTask()), TaskDeclaration(VersionTask()), TaskDeclaration(CreateStructureTask()), TaskDeclaration(LineInFileTask()) ] TASKS = [ # example: # TaskAliasDeclaration(':env:test', [':py:publish', '--username=...', '--password=...'], env={'DB_PASSWORD': '******'}) ]