ansible-playbook-grapher/ansibleplaybookgrapher/cli.py

87 lines
2.7 KiB
Python
Raw Normal View History

2017-11-16 15:21:19 +00:00
import sys
2017-11-21 15:34:58 +00:00
import os
import ntpath
2017-11-16 15:21:19 +00:00
from ansible.cli import CLI
from ansible.errors import AnsibleOptionsError
2017-11-27 14:00:14 +00:00
from ansible.utils.display import Display
2017-11-16 15:21:19 +00:00
from ansibleplaybookgrapher.grapher import Grapher
from ansibleplaybookgrapher import __prog__, __version__
2017-11-27 14:00:14 +00:00
display = Display()
2017-11-16 15:21:19 +00:00
class PlaybookGrapherCLI(CLI):
"""
Playbook grapher CLI
"""
def run(self):
super(PlaybookGrapherCLI, self).run()
playbook = self.args[0]
2017-11-17 16:39:57 +00:00
loader, inventory, variable_manager = self._play_prereqs(self.options)
2017-11-16 15:21:19 +00:00
grapher = Grapher(data_loader=loader, inventory_manager=inventory, variable_manager=variable_manager,
2017-11-21 15:34:58 +00:00
playbook_filename=playbook, output_filename=self.options.output_filename)
2017-11-16 15:21:19 +00:00
grapher.make_graph(include_role_tasks=self.options.include_role_tasks, tags=self.options.tags,
skip_tags=self.options.skip_tags)
grapher.render_graph(save_dot_file=self.options.save_dot_file)
grapher.post_process_svg()
def parse(self):
# create parser for CLI options
parser = CLI.base_parser(
usage="%s [options] playbook.yml" % __prog__,
subset_opts=True,
2017-11-17 16:39:57 +00:00
vault_opts=True,
runtask_opts=True,
2017-11-16 15:21:19 +00:00
desc="Make graph from your Playbook.",
)
parser.add_option('-i', '--inventory', dest='inventory', action="append",
help="specify inventory host path (default=[%s]) or comma separated host list. ")
2017-11-17 10:41:28 +00:00
parser.add_option("--include-role-tasks", dest="include_role_tasks", action='store_true', default=False,
2017-11-16 15:21:19 +00:00
help="Include the tasks of the role in the graph.")
2017-11-21 15:34:58 +00:00
parser.add_option("-s", "--save-dot-file", dest="save_dot_file", action='store_true', default=False,
2017-11-16 15:21:19 +00:00
help="Save the dot file used to generate the graph.")
2017-11-21 15:34:58 +00:00
parser.add_option("-o", "--ouput-file-name", dest='output_filename',
2017-11-16 15:21:19 +00:00
help="Output filename without the '.svg' extension. Default: <playbook>.svg")
parser.version = "%s %s" % (__prog__, __version__)
self.parser = parser
super(PlaybookGrapherCLI, self).parse()
if len(self.args) == 0:
raise AnsibleOptionsError("You must specify a playbook file to graph.")
if len(self.args) > 1:
2017-11-21 15:34:58 +00:00
raise AnsibleOptionsError("You must specify only one playbook file to graph.")
2017-11-16 15:21:19 +00:00
display.verbosity = self.options.verbosity
2017-11-21 15:34:58 +00:00
if self.options.output_filename is None:
self.options.output_filename = os.path.splitext(ntpath.basename(self.args[0]))[0]
2017-11-16 15:21:19 +00:00
def main():
cli = PlaybookGrapherCLI(sys.argv)
cli.parse()
cli.run()
if __name__ == "__main__":
main()