def checkout(ctx, background, nick, count, args_file, **kwargs): """Checkout or "create" a Virtual Machine broker instance COMMAND: broker checkout --workflow "workflow-name" --workflow-arg1 something or COMMAND: broker checkout --nick "nickname" :param ctx: clicks context object :param background: run a new broker subprocess to carry out command :param nick: shortcut for arguments saved in settings.yaml, passed in as a string :param args_file: this broker argument wil be replaced with the contents of the file passed in """ broker_args = helpers.clean_dict(kwargs) if nick: broker_args["nick"] = nick if count: broker_args["_count"] = count if args_file: broker_args["args_file"] = args_file # if additional arguments were passed, include them in the broker args # strip leading -- characters broker_args.update({(key[2:] if key.startswith("--") else key): val for key, val in zip(ctx.args[::2], ctx.args[1::2])}) broker_args = helpers.resolve_file_args(broker_args) if background: helpers.fork_broker() broker_inst = VMBroker(**broker_args) broker_inst.checkout()
def test_resolve_file_args(): broker_args = { "test_arg": "test_val", "args_file": "tests/data/broker_args.json", "complex_args": "tests/data/args_file.yaml", } new_args = helpers.resolve_file_args(broker_args) assert "args_file" not in new_args assert new_args["my_second_arg"] == "foo" assert new_args["complex_args"] == BROKER_ARGS_DATA["myarg"] assert new_args["test_arg"] == "test_val"
def execute(ctx, background, workflow, job_template, nick, output_format, artifacts, args_file): """Execute an arbitrary provider action COMMAND: broker execute --workflow "workflow-name" --workflow-arg1 something or COMMAND: broker execute --nick "nickname" :param ctx: clicks context object :param background: run a new broker subprocess to carry out command :param workflow: workflow template stored in Ansible Tower, passed in as a string :param job-template: job template stored in Ansible Tower, passed in as a string :param nick: shortcut for arguments saved in settings.yaml, passed in as a string :param output_format: change the format of the output to one of the choice options :param artifacts: AnsibleTower provider specific option for choosing what to return :param args_file: this broker argument wil be replaced with the contents of the file passed in """ broker_args = {} if nick: broker_args["nick"] = nick if workflow: broker_args["workflow"] = workflow if job_template: broker_args["job_template"] = job_template if artifacts: broker_args["artifacts"] = artifacts if args_file: broker_args["args_file"] = args_file # if additional arguments were passed, include them in the broker args # strip leading -- characters broker_args.update({(key[2:] if key.startswith("--") else key): val for key, val in zip(ctx.args[::2], ctx.args[1::2])}) broker_args = helpers.resolve_file_args(broker_args) if background: fork_broker() broker_inst = VMBroker(**broker_args) result = broker_inst.execute() if output_format == "raw": print(result) elif output_format == "log": logger.info(result) elif output_format == "yaml": print(helpers.yaml_format(result))
def __init__(self, **kwargs): kwargs = helpers.resolve_file_args(kwargs) logger.debug(f"Broker instantiated with {kwargs=}") self._hosts = kwargs.pop("hosts", []) self.host_classes = {"host": Host} # if a nick was specified, pull in the resolved arguments if "nick" in kwargs: nick = kwargs.pop("nick") kwargs = helpers.merge_dicts(kwargs, helpers.resolve_nick(nick)) logger.debug(f"kwargs after nick resolution {kwargs=}") if "host_classes" in kwargs: self.host_classes.update(kwargs.pop("host_classes")) # determine the provider actions based on kwarg parameters self._provider_actions = {} for key, action in PROVIDER_ACTIONS.items(): if key in kwargs: self._provider_actions[key] = action self._kwargs = kwargs