def task_list(request): """List available fabric tasks.""" fabfile_path = settings.FABADMIN_FABFILE filename = os.path.split(fabfile_path)[1] module = os.path.splitext(filename)[0] fabfile = __import__(module) _, new_style, classic, default = load_tasks_from_module(fabfile) tasks = [] for task_group in [new_style, classic, default]: if task_group: for task in task_group.values(): tasks.append(Task(task)) task_choices = [NullTask()] + tasks output = "" if request.method == "POST": fabfile_form = FabfileForm(request.POST, tasks=task_choices) if fabfile_form.is_valid(): cleaned_data = fabfile_form.cleaned_data task = cleaned_data['task'] arguments = cleaned_data['arguments'] args = ['fab', '-f', fabfile_path, task + ":" + arguments] process = subprocess.Popen( args, shell=False, stdout=subprocess.PIPE ) converter = Ansi2HTMLConverter() shell_output = process.communicate()[0] output = converter.convert(shell_output, False) else: fabfile_form = FabfileForm(tasks=task_choices) return render_to_response("fabadmin/task_list.html", { 'tasks': tasks, 'fabfile_form': fabfile_form, 'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX, 'output': output }, RequestContext(request, {}))
def load(self, config_file): """Load the workflow rules from a Mamba .dc Python file :param config_file: The file where to load the configuration from :type config_file: str """ module_name = filepath.splitext(filepath.basename(config_file))[0] if self.deployer_object and self.deployer_object.loaded: raise deployer.DeployerError( "Tried to load {module} deployer that is " "already loaded!".format(module=module_name) ) self.deployer_name = module_name self.deployer_module = deployer.deployer_import(self.deployer_name, config_file) # load tasks docstring, new_style, classic, default = fabric_main.load_tasks_from_module(self.deployer_module) self.tasks = { "docstring": docstring, "functions": new_style if state.env.new_style_tasks else classic, "default": default, } state.commands.update(self.tasks.get("functions", {})) # abort if no commands found if not state.commands: log.err("No commands found ...aborting") else: for name in state.commands: execute(name)
def load_blueprints(packages=None): """ Load blueprints/tasks from fabric env """ callables = {} executables = {} # Fallback on blueprints from env packages = packages or fabric.state.env.get('blueprints') or [] #with python_path(): for package in packages: # Import blueprint module package = str(package) blueprint = package.rsplit('.', 1)[-1] imported = __import__(package) # Load tasks _, new_style, classic, _ = load_tasks_from_module(imported) tasks = new_style if fabric.state.env.new_style_tasks else classic if tasks: # Prefix top level imports with module/blueprint name if not package: tasks = {blueprint: tasks} callables.update(tasks) executables[blueprint] = dict((name.split('.', 1)[-1], name) for name in _task_names(tasks)) # Update available tasks for fabric fabric.state.commands.update(callables) # Update available blueprints blueprints.update(executables)
def test_tasks(self): class TestTasks(Tasks): @fab.task(default=True, aliases=['foo', 'bar']) def default(self): pass @fab.task(name='name', alias='alias') def task(self): pass roles = ['role_1', 'role_2'] hosts = ['host_1', 'host_2'] tasks = TestTasks(roles=roles, hosts=hosts) self.assertTrue(is_task_module(tasks)) self.assertTrue(tasks.default.is_default) self.assertListEqual(['foo', 'bar'], tasks.default.aliases) self.assertEqual('name', tasks.task.name) self.assertListEqual(['alias'], tasks.task.aliases) for task in tasks: self.assertListEqual(roles, task.roles) self.assertListEqual(hosts, task.hosts) docstring, new_style, classic, default = load_tasks_from_module(tasks) self.assertIsNone(docstring) self.assertIn('default', new_style) self.assertIn('alias', new_style) self.assertIn('foo', new_style) self.assertIn('bar', new_style) self.assertIn('name', new_style) self.assertDictEqual({}, classic) self.assertIs(tasks.default, default)
def test_commands_list(self): cases = dict( default=dict( init_kwargs=dict(container='container'), expected_commands_list=['revert', 'pull', 'rollback', 'update', 'deploy'], unexpected_commands_list=['migrate', 'migrate_back', 'backup', 'restore'], ), migrate_tasks=dict( init_kwargs=dict(container='container', migrate_commands=True), expected_commands_list=['revert', 'pull', 'rollback', 'update', 'deploy', 'migrate', 'migrate_back'], unexpected_commands_list=['backup', 'restore'], ), backup_tasks=dict( init_kwargs=dict(container='container', backup_commands=True), expected_commands_list=['revert', 'pull', 'rollback', 'update', 'deploy', 'backup', 'restore'], unexpected_commands_list=['migrate', 'migrate_back'], ), all_tasks=dict( init_kwargs=dict(container='container', backup_commands=True, migrate_commands=True), expected_commands_list=['revert', 'pull', 'rollback', 'update', 'deploy', 'backup', 'restore', 'migrate', 'migrate_back'], unexpected_commands_list=[], ), ) for case, data in cases.items(): with self.subTest(case=case): tasks = DockerTasks(**data['init_kwargs']) docstring, new_style, classic, default = load_tasks_from_module(tasks) for expected_command in data['expected_commands_list']: self.assertIn(expected_command, new_style) for unexpected_command in data['unexpected_commands_list']: self.assertNotIn(unexpected_command, new_style)
def initialize_fabric(): from fabric import state from fabric.main import load_tasks_from_module import fabtasks docstring, new_style, classic, default = load_tasks_from_module(fabtasks) tasks = new_style if state.env.new_style_tasks else classic state.commands.update(tasks) state.env['key_filename'] = 'key/id_rsa' state.env['abort_exception'] = RuntimeError # Use exception to abort failed commands
def initialize_fabric(): from fabric import state from fabric.main import load_tasks_from_module import fabtasks docstring, new_style, classic, default = load_tasks_from_module(fabtasks) tasks = new_style if state.env.new_style_tasks else classic state.commands.update(tasks) state.env['key_filename'] = 'key/id_rsa' state.env[ 'abort_exception'] = RuntimeError # Use exception to abort failed commands
def task_list(request): """List available fabric tasks.""" fabfile_path = settings.FABADMIN_FABFILE filename = os.path.split(fabfile_path)[1] module = os.path.splitext(filename)[0] fabfile = __import__(module) _, new_style, classic, default = load_tasks_from_module(fabfile) tasks = {} def _add_group(task_group, group_name): if task_group: for name, task in task_group.items(): if isinstance(task, dict): if group_name: name = "%s.%s" % (group_name, name) _add_group(task, name) else: t = Task(task, group_name) if not t.name in tasks: print "adding task: %s" % t.name tasks[t.name] = t else: print "skipping task: %s" % t.name for task_group in [new_style, classic, default]: _add_group(new_style, None) task_names = tasks.keys() task_names.sort() tasks = [tasks[n] for n in task_names] task_choices = [NullTask()] + tasks output = "" if request.method == "POST": fabfile_form = FabfileForm(request.POST, tasks=task_choices) if fabfile_form.is_valid(): cleaned_data = fabfile_form.cleaned_data task_str = cleaned_data['task'] arguments = cleaned_data['arguments'] if arguments: task_str = "%s:%s" % (task_str, arguments) args = ['fab', '-f', fabfile_path, task_str] process = subprocess.Popen( args, shell=False, stdout=subprocess.PIPE ) converter = Ansi2HTMLConverter() shell_output = process.communicate()[0] output = converter.convert(shell_output, False) else: fabfile_form = FabfileForm(tasks=task_choices) return render_to_response("fabadmin/task_list.html", { 'tasks': tasks, 'fabfile_form': fabfile_form, 'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX, 'output': output }, RequestContext(request, {}))
def set_alias(modules={}, seen_clear=False): from fabric.main import load_tasks_from_module, is_task_module, _seen from fabric.task_utils import _Dict from fabric import state new_style_tasks = _Dict() for name, module in modules.items(): if is_task_module(module): docs, newstyle, classic, default = load_tasks_from_module(module) for task_name, task in newstyle.items(): if name not in new_style_tasks: new_style_tasks[name] = _Dict() new_style_tasks[name][task_name] = task if default is not None: new_style_tasks[name].default = default state.commands.update(new_style_tasks) if seen_clear: _seen.clear()
def load(self, config_file): """Load the workflow rules from a Mamba .dc Python file :param config_file: The file where to load the configuration from :type config_file: str """ module_name = filepath.splitext(filepath.basename(config_file))[0] if self.deployer_object and self.deployer_object.loaded: raise deployer.DeployerError( 'Tried to load {module} deployer that is ' 'already loaded!'.format(module=module_name) ) self.deployer_name = module_name self.deployer_module = deployer.deployer_import( self.deployer_name, config_file ) # load tasks docstring, new_style, classic, default = ( fabric_main.load_tasks_from_module(self.deployer_module) ) self.tasks = { 'docstring': docstring, 'functions': new_style if state.env.new_style_tasks else classic, 'default': default } state.commands.update(self.tasks.get('functions', {})) # abort if no commands found if not state.commands: log.err('No commands found ...aborting') else: for name in state.commands: execute(name)
def main(fabfile_locations=None, file_paths=None): # type: (list, list) -> None fabfile_local = find_fabfile() if fabfile_locations is None and fabfile_local is None: fabfile_locations = ['~/fabfile.py'] print_info('Added $HOME to fabfile locations') docstring, new_style, classic, default = load_tasks_from_module(api) tasks = new_style if state.env.new_style_tasks else classic state.commands.update(tasks) print_info('Forwarding execution to Fabric') _load_settings_original = fabric_main.load_settings def hemp_load_settings(path): print_info('Loading hempfiles') load_hempfiles(file_paths) return _load_settings_original(path) fabric_main.load_settings = hemp_load_settings fabric_main.main(fabfile_locations)
:param name: recipes file :type name: string :param namespace: namespace to module :type namespace: string :param config_params: custom recipe params :type config_params: dict """ try: if isinstance(name, ModuleType): m = name name = m.__name__ else: m = import_module("{0}.{1}".format(NAME_LIB, name)) except Exception, e: abort("Can't import recipe {0}: {1}".format(name, e)) docstring, new_style, classic, default = load_tasks_from_module(m) tasks = new_style if env.new_style_tasks else classic _configure_module_config(m, _namespace_from(m), config_params) commands.update(_make_recipe_dict(tasks, _namespace_from(namespace))) puts("{0} recipe configured".format(name)) return True def _recipe_repr(recipe): """Configure recipe :param recipe: recipe item (list, string) :type param: string, tuple, list """