2017-11-21 15:34:58 +00:00
|
|
|
import ntpath
|
2018-12-30 20:32:11 +00:00
|
|
|
import os
|
2020-06-26 22:57:06 +00:00
|
|
|
import sys
|
2020-11-17 19:32:51 +00:00
|
|
|
from abc import ABC
|
2020-06-18 12:44:29 +00:00
|
|
|
|
2017-11-16 15:21:19 +00:00
|
|
|
from ansible.cli import CLI
|
|
|
|
from ansible.errors import AnsibleOptionsError
|
2018-12-30 20:40:03 +00:00
|
|
|
from ansible.release import __version__ as ansible_version
|
2020-11-13 09:41:51 +00:00
|
|
|
from ansible.utils.display import Display
|
2019-06-09 23:00:49 +00:00
|
|
|
from packaging import version
|
2017-11-16 15:21:19 +00:00
|
|
|
|
|
|
|
from ansibleplaybookgrapher import __prog__, __version__
|
2020-11-13 09:41:51 +00:00
|
|
|
from ansibleplaybookgrapher.grapher import PlaybookGrapher
|
2020-11-15 15:50:56 +00:00
|
|
|
from ansibleplaybookgrapher.postprocessor import PostProcessor
|
|
|
|
|
2019-06-09 23:00:49 +00:00
|
|
|
# We need to know if we are using ansible 2.8 because the CLI has been refactored in
|
|
|
|
# https://github.com/ansible/ansible/pull/50069
|
2020-01-13 19:17:10 +00:00
|
|
|
IS_ANSIBLE_2_9_X = version.parse(ansible_version) >= version.parse("2.9")
|
2019-06-09 23:00:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_cli_class():
|
|
|
|
"""
|
|
|
|
Utility function to return the class to use as CLI depending on Ansible version
|
|
|
|
:return:
|
|
|
|
"""
|
2020-01-13 19:17:10 +00:00
|
|
|
if IS_ANSIBLE_2_9_X:
|
|
|
|
return PlaybookGrapherCLI29
|
2019-06-09 23:00:49 +00:00
|
|
|
else:
|
2020-01-13 19:17:10 +00:00
|
|
|
return PlaybookGrapherCLI28
|
2019-06-09 23:00:49 +00:00
|
|
|
|
|
|
|
|
2020-11-17 19:32:51 +00:00
|
|
|
class GrapherCLI(CLI, ABC):
|
2019-06-09 23:00:49 +00:00
|
|
|
"""
|
2020-11-17 19:32:51 +00:00
|
|
|
An abstract class to provide to be implemented by the different Grapher CLIs.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
super(GrapherCLI, self).run()
|
|
|
|
|
|
|
|
loader, inventory, variable_manager = CLI._play_prereqs()
|
|
|
|
# Looks like the display is a singleton. This instruction will NOT return a new instance.
|
|
|
|
# This is why we set the verbosity later because someone set it before us.
|
|
|
|
display = Display()
|
|
|
|
display.verbosity = self.options.verbosity
|
|
|
|
|
|
|
|
grapher = PlaybookGrapher(data_loader=loader, inventory_manager=inventory, variable_manager=variable_manager,
|
|
|
|
display=display, tags=self.options.tags, skip_tags=self.options.skip_tags,
|
|
|
|
playbook_filename=self.options.playbook_filename,
|
|
|
|
include_role_tasks=self.options.include_role_tasks)
|
|
|
|
|
|
|
|
grapher.make_graph()
|
|
|
|
|
|
|
|
svg_path = grapher.render_graph(self.options.output_filename, self.options.save_dot_file)
|
|
|
|
post_processor = PostProcessor(svg_path=svg_path)
|
|
|
|
post_processor.post_process(graph_representation=grapher.graph_representation)
|
|
|
|
post_processor.write()
|
|
|
|
|
2021-03-12 06:39:42 +00:00
|
|
|
display.display(f"The graph has been exported to {svg_path}")
|
2020-11-17 19:32:51 +00:00
|
|
|
|
|
|
|
return svg_path
|
|
|
|
|
|
|
|
|
|
|
|
class PlaybookGrapherCLI28(GrapherCLI):
|
|
|
|
"""
|
|
|
|
The dedicated playbook CLI for Ansible 2.8.
|
2019-06-09 23:00:49 +00:00
|
|
|
"""
|
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
def __init__(self, args, callback=None):
|
|
|
|
super(PlaybookGrapherCLI28, self).__init__(args=args, callback=callback)
|
|
|
|
# we keep the old options as instance attribute for backward compatibility for the grapher
|
|
|
|
# Ansible 2.8 has removed it and use a global context instead
|
|
|
|
self.options = None
|
2019-06-09 23:00:49 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
def _add_my_options(self):
|
|
|
|
"""
|
|
|
|
Method to add some options specific to the grapher
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
self.parser.add_option('-i', '--inventory', dest='inventory', action="append",
|
2020-06-26 22:57:06 +00:00
|
|
|
help="specify inventory host path or comma separated host list.")
|
2019-06-09 23:00:49 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
self.parser.add_option("--include-role-tasks", dest="include_role_tasks", action='store_true', default=False,
|
|
|
|
help="Include the tasks of the role in the graph.")
|
2019-06-09 23:00:49 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
self.parser.add_option("-s", "--save-dot-file", dest="save_dot_file", action='store_true', default=False,
|
|
|
|
help="Save the dot file used to generate the graph.")
|
2017-11-27 14:00:14 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
self.parser.add_option("-o", "--ouput-file-name", dest='output_filename',
|
|
|
|
help="Output filename without the '.svg' extension. Default: <playbook>.svg")
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
self.parser.version = "%s %s (with ansible %s)" % (__prog__, __version__, ansible_version)
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
def init_parser(self, usage="", desc=None, epilog=None):
|
|
|
|
super(PlaybookGrapherCLI28, self).init_parser(usage="%s [options] playbook.yml" % __prog__,
|
2020-06-26 22:57:06 +00:00
|
|
|
desc="Make graphs from your Ansible Playbooks.", epilog=epilog)
|
2020-01-13 19:17:10 +00:00
|
|
|
self._add_my_options()
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
from ansible.cli.arguments import optparse_helpers as opt_help
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
opt_help.add_subset_options(self.parser)
|
|
|
|
opt_help.add_vault_options(self.parser)
|
|
|
|
opt_help.add_runtask_options(self.parser)
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
def post_process_args(self, options, args):
|
|
|
|
options, args = super(PlaybookGrapherCLI28, self).post_process_args(options, args)
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
if len(args) == 0:
|
|
|
|
raise AnsibleOptionsError("You must specify a playbook file to graph.")
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
if len(args) > 1:
|
|
|
|
raise AnsibleOptionsError("You must specify only one playbook file to graph.")
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
# init the options
|
|
|
|
self.options = options
|
|
|
|
self.options.playbook_filename = args[0]
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
if self.options.output_filename is None:
|
|
|
|
# use the playbook name (without the extension) as output filename
|
|
|
|
self.options.output_filename = os.path.splitext(ntpath.basename(self.options.playbook_filename))[0]
|
|
|
|
|
|
|
|
return options, args
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2017-11-21 15:34:58 +00:00
|
|
|
|
2020-11-17 19:32:51 +00:00
|
|
|
class PlaybookGrapherCLI29(GrapherCLI):
|
2019-06-09 23:00:49 +00:00
|
|
|
"""
|
2020-06-18 12:44:29 +00:00
|
|
|
The dedicated playbook CLI for Ansible 2.9 and above.
|
2020-01-13 19:17:10 +00:00
|
|
|
Note: Use this class as the main CLI when we drop support for ansible < 2.9
|
2019-06-09 23:00:49 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, args, callback=None):
|
2020-01-13 19:17:10 +00:00
|
|
|
super(PlaybookGrapherCLI29, self).__init__(args=args, callback=callback)
|
2019-06-09 23:00:49 +00:00
|
|
|
# we keep the old options as instance attribute for backward compatibility for the grapher
|
|
|
|
# Ansible 2.8 has removed it and use a global context instead
|
|
|
|
self.options = None
|
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
def _add_my_options(self):
|
|
|
|
"""
|
|
|
|
Add some of my options to the parser
|
|
|
|
:param parser:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
self.parser.prog = __prog__
|
|
|
|
|
|
|
|
self.parser.add_argument('-i', '--inventory', dest='inventory', action="append",
|
2020-06-26 22:57:06 +00:00
|
|
|
help="specify inventory host path or comma separated host list.")
|
2020-01-13 19:17:10 +00:00
|
|
|
|
|
|
|
self.parser.add_argument("--include-role-tasks", dest="include_role_tasks", action='store_true', default=False,
|
|
|
|
help="Include the tasks of the role in the graph.")
|
|
|
|
|
|
|
|
self.parser.add_argument("-s", "--save-dot-file", dest="save_dot_file", action='store_true', default=False,
|
|
|
|
help="Save the dot file used to generate the graph.")
|
|
|
|
|
|
|
|
self.parser.add_argument("-o", "--ouput-file-name", dest='output_filename',
|
|
|
|
help="Output filename without the '.svg' extension. Default: <playbook>.svg")
|
|
|
|
|
|
|
|
self.parser.add_argument('--version', action='version',
|
|
|
|
version="%s %s (with ansible %s)" % (__prog__, __version__, ansible_version))
|
|
|
|
|
|
|
|
self.parser.add_argument('playbook_filename', help='Playbook to graph', metavar='playbook')
|
|
|
|
|
2019-06-09 23:00:49 +00:00
|
|
|
def init_parser(self, usage="", desc=None, epilog=None):
|
2020-01-13 19:17:10 +00:00
|
|
|
super(PlaybookGrapherCLI29, self).init_parser(usage="%s [options] playbook.yml" % __prog__,
|
2020-06-26 22:57:06 +00:00
|
|
|
desc="Make graphs from your Ansible Playbooks.", epilog=epilog)
|
2019-06-09 23:00:49 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
self._add_my_options()
|
2019-06-09 23:00:49 +00:00
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
# add ansible specific options
|
|
|
|
from ansible.cli.arguments import option_helpers as opt_help
|
2019-06-09 23:00:49 +00:00
|
|
|
opt_help.add_subset_options(self.parser)
|
|
|
|
opt_help.add_vault_options(self.parser)
|
|
|
|
opt_help.add_runtask_options(self.parser)
|
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
def post_process_args(self, options):
|
|
|
|
options = super(PlaybookGrapherCLI29, self).post_process_args(options)
|
2019-06-09 23:00:49 +00:00
|
|
|
|
|
|
|
# init the options
|
|
|
|
self.options = options
|
|
|
|
|
|
|
|
if self.options.output_filename is None:
|
|
|
|
# use the playbook name (without the extension) as output filename
|
|
|
|
self.options.output_filename = os.path.splitext(ntpath.basename(self.options.playbook_filename))[0]
|
|
|
|
|
2020-01-13 19:17:10 +00:00
|
|
|
return options
|
2019-06-09 23:00:49 +00:00
|
|
|
|
|
|
|
|
2019-02-18 22:42:54 +00:00
|
|
|
def main(args=None):
|
|
|
|
args = args or sys.argv
|
2019-06-09 23:00:49 +00:00
|
|
|
cli = get_cli_class()(args)
|
2017-11-16 15:21:19 +00:00
|
|
|
|
2019-02-18 23:28:51 +00:00
|
|
|
cli.run()
|
2017-11-16 15:21:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-12-31 14:16:23 +00:00
|
|
|
main(sys.argv)
|