Ejemplo n.º 1
0
    def test_parser_playbook(self):
        localdir = os.path.dirname(os.path.realpath(__file__))
        testfile = os.path.join(localdir, ".output/testplaybook.yml")
        folder_testfile = os.path.join(localdir, ".output")
        with codecs.open(testfile, "w", encoding="utf-8") as f:
            f.write("---\n")
            f.write(u"# Author: Me شيشه ب\n")
            f.write(u"# description: this is a test شيشه ب\n")
            f.write(u"- hosts: testhoشيشهsts\n")
            f.write("  tasks:\n")
            f.write(u"     - name: 'Install Apache شيشه ب'\n")
            f.write(u"       yum: name=httpd state=inشtalled\n")
            f.write(u"       tags:\n")
            f.write(u"           - apache\n")

        playbook = PlaybookParser([testfile], is_role=False)

        playbook.parse_playbooks()

        # One Element in Array
        assert (len(playbook.parserdata) == 1)
        assert ("author" in playbook.parserdata[folder_testfile][0])

        assert ("description" in playbook.parserdata[folder_testfile][0])

        assert ("task_info" in playbook.parserdata[folder_testfile][0])

        assert ("task_name"
                in playbook.parserdata[folder_testfile][0]['task_info'][0])

        assert ("task_tags"
                in playbook.parserdata[folder_testfile][0]['task_info'][0])

        assert (playbook.parserdata[folder_testfile][0]["is_role"] is False)
Ejemplo n.º 2
0
class DirParser(object):
    """ Parses an Ansible Project Directory Structure """
    def __init__(self, project):
        """ Setup Parser Modules
            @param project: is a relative or absolute path to an Ansible Project
        """
        self.ansiblecfg = AnsibleCfg(project)
        print("Parsing roles")
        self.roleparser = RoleParser(self.ansiblecfg.get_role_paths())
        print("Parsing playbooks")
        self.playbookparser = PlaybookParser(
            self.ansiblecfg.get_playbook_paths())
        print("Parsing hosts")
        self.hostVarsParser = HostVarsParser(self.ansiblecfg.get_hosts_paths())

        # Parse all playbooks
        self.hostVarsParser.parse_hosts_vars()
        self.playbookparser.parse_playbooks()

    def get_paths(self):
        return {
            'role': self.ansiblecfg.get_role_paths(),
            'playbooks': [self.ansiblecfg.get_playbook_paths()],
            'host': self.ansiblecfg.get_hosts_paths()
        }

    def get_parserdata(self):
        """ This function returns a datastructure
        that can be passed to a formatter """
        return {
            'playbooks': self.playbookparser.parserdata,
            'roles': self.roleparser.parserdata,
            'host_vars': self.hostVarsParser.parserdata
        }
Ejemplo n.º 3
0
class RoleParser(object):
    """ Parse Roles in Project """

    def __init__(self, role_paths):
        self.role_paths = role_paths
        self.playbooks = []
        self.main_tasks = []
        self.parserdata = []

        self.find_main_tasks()
        self.parse_main_tasks()

    def find_main_tasks(self):
        """ Find Entry Point to Each Role and Parse """
        for role_path in self.role_paths:
            for root, dirnames, filenames in os.walk(role_path):
                # WHAT OTHER DIRECTORIES SHOULD I LOOK IN???
                # handlers/main.yml??
                for filename in fnmatch.filter(filenames, '*.yml'):
                    # Absolute path to file
                    fullpath = os.path.join(root, filename)
                    m = re.match("^.*?/tasks/main.yml$", fullpath)
                    if m:
                        self.main_tasks.append(fullpath)

    def parse_main_tasks(self):
        """ Parse All Tasks Found In a Role """
        # Need to determine the rolename per task somehow
        self.playbookparser = PlaybookParser(self.main_tasks, is_role=True)

        # Parse all main tasks
        self.playbookparser.parse_playbooks()
        self.parserdata = self.playbookparser.parserdata
Ejemplo n.º 4
0
class RoleParser(object):
    """ Parse Roles in Project """
    def __init__(self, role_paths):
        self.role_paths = role_paths
        self.playbooks = []
        self.main_tasks = []
        self.parserdata = []

        self.find_main_tasks()
        self.parse_main_tasks()

    def find_main_tasks(self):
        """ Find Entry Point to Each Role and Parse """
        for role_path in self.role_paths:
            for root, dirnames, filenames in os.walk(role_path):
                # WHAT OTHER DIRECTORIES SHOULD I LOOK IN???
                # handlers/main.yml??
                for filename in fnmatch.filter(filenames, '*.yml'):
                    # Absolute path to file
                    fullpath = os.path.join(root, filename)
                    m = re.match("^.*?/tasks/main.yml$", fullpath)
                    if m:
                        self.main_tasks.append(fullpath)

    def parse_main_tasks(self):
        """ Parse All Tasks Found In a Role """
        # Need to determine the rolename per task somehow
        self.playbookparser = PlaybookParser(self.main_tasks, is_role=True)

        # Parse all main tasks
        self.playbookparser.parse_playbooks()
        self.parserdata = self.playbookparser.parserdata
Ejemplo n.º 5
0
    def parse_main_tasks(self):
        """ Parse All Tasks Found In a Role """
        # Need to determine the rolename per task somehow
        self.playbookparser = PlaybookParser(self.main_tasks, is_role=True)

        # Parse all main tasks
        self.playbookparser.parse_playbooks()
        self.parserdata = self.playbookparser.parserdata
Ejemplo n.º 6
0
    def __init__(self, project):
        """ Setup Parser Modules
            @project is a relative or absolute path to an Ansible Project
        """
        self.ansiblecfg = AnsibleCfg(project)
        self.roleparser = RoleParser(self.ansiblecfg.get_role_paths())
        self.playbookparser = PlaybookParser(
            self.ansiblecfg.get_playbook_paths())

        # Parse all playbooks
        self.playbookparser.parse_playbooks()
Ejemplo n.º 7
0
    def __init__(self, project):
        """ Setup Parser Modules
            @param project: is a relative or absolute path to an Ansible Project
        """
        self.ansiblecfg = AnsibleCfg(project)
        print("Parsing roles")
        self.roleparser = RoleParser(self.ansiblecfg.get_role_paths())
        print("Parsing playbooks")
        self.playbookparser = PlaybookParser(
            self.ansiblecfg.get_playbook_paths())
        print("Parsing hosts")
        self.hostVarsParser = HostVarsParser(self.ansiblecfg.get_hosts_paths())

        # Parse all playbooks
        self.hostVarsParser.parse_hosts_vars()
        self.playbookparser.parse_playbooks()
Ejemplo n.º 8
0
    def parse_main_tasks(self):
        """ Parse All Tasks Found In a Role """
        # Need to determine the rolename per task somehow
        self.playbookparser = PlaybookParser(self.main_tasks, is_role=True)

        # Parse all main tasks
        self.playbookparser.parse_playbooks()
        self.parserdata = self.playbookparser.parserdata
Ejemplo n.º 9
0
class DirParser(object):
    """ Parses an Ansible Project Directory Structure """
    def __init__(self, project):
        """ Setup Parser Modules
            @project is a relative or absolute path to an Ansible Project
        """
        self.ansiblecfg = AnsibleCfg(project)
        self.roleparser = RoleParser(self.ansiblecfg.get_role_paths())
        self.playbookparser = PlaybookParser(
            self.ansiblecfg.get_playbook_paths())

        # Parse all playbooks
        self.playbookparser.parse_playbooks()

    def get_parserdata(self):
        """ This function returns a datastructure
        that can be passed to a formatter """
        return self.playbookparser.parserdata + self.roleparser.parserdata
Ejemplo n.º 10
0
class DirParser(object):
    """ Parses an Ansible Project Directory Structure """

    def __init__(self, project):
        """ Setup Parser Modules
            @project is a relative or absolute path to an Ansible Project
        """
        self.ansiblecfg = AnsibleCfg(project)
        self.roleparser = RoleParser(self.ansiblecfg.get_role_paths())
        self.playbookparser = PlaybookParser(
            self.ansiblecfg.get_playbook_paths())

        # Parse all playbooks
        self.playbookparser.parse_playbooks()

    def get_parserdata(self):
        """ This function returns a datastructure
        that can be passed to a formatter """
        return self.playbookparser.parserdata + self.roleparser.parserdata
Ejemplo n.º 11
0
    def __init__(self, project):
        """ Setup Parser Modules
            @project is a relative or absolute path to an Ansible Project
        """
        self.ansiblecfg = AnsibleCfg(project)
        self.roleparser = RoleParser(self.ansiblecfg.get_role_paths())
        self.playbookparser = PlaybookParser(
            self.ansiblecfg.get_playbook_paths())

        # Parse all playbooks
        self.playbookparser.parse_playbooks()
Ejemplo n.º 12
0
    def test_parser_role_playbook(self):
        # Find Directory Of This Test
        localdir = os.path.dirname(os.path.realpath(__file__))
        # Create Tasks Directory
        if not os.path.exists(os.path.join(localdir, ".output/tasks/")):
            os.makedirs(os.path.join(localdir, ".output/tasks/"))
        # Set Output File
        testfile = os.path.join(localdir, ".output/tasks/main.yml")
        folder_testfile = localdir
        with open(testfile, "w") as f:
            f.write("---\n")
            f.write("# Author: Me\n")
            f.write("# description: this is a test\n")
            f.write("- name: 'Install Apache'\n")
            f.write("  yum: name=httpd state=installed\n")
            f.write("  tags:\n")
            f.write("    - apache\n")

        playbook = PlaybookParser([testfile], is_role=True)
        playbook.parse_playbooks()

        # One Element in Array
        assert (len(playbook.parserdata) == 1)

        assert ("author" in playbook.parserdata[folder_testfile][0])

        assert ("description" in playbook.parserdata[folder_testfile][0])

        assert ("task_info" in playbook.parserdata[folder_testfile][0])

        assert ("task_name"
                in playbook.parserdata[folder_testfile][0]['task_info'][0])

        assert ("task_tags"
                in playbook.parserdata[folder_testfile][0]['task_info'][0])

        assert (playbook.parserdata[folder_testfile][0]["name"] == ".output")
Ejemplo n.º 13
0
    def test_parser_playbook(self):
        localdir = os.path.dirname(os.path.realpath(__file__))
        testfile = os.path.join(localdir, ".output/testplaybook.yml")
        with open(testfile, "w") as f:
            f.write("---\n")
            f.write("# Author: Me\n")
            f.write("# description: this is a test\n")
            f.write("- name: 'Install Apache'\n")
            f.write("  yum: name=httpd state=installed\n")

        playbook = PlaybookParser([testfile], is_role=True)

        playbook.parse_playbooks()

        # One Element in Array
        assert (len(playbook.parserdata) == 1)

        assert ("author" in playbook.parserdata[0])

        assert ("description" in playbook.parserdata[0])

        assert ("task_names" in playbook.parserdata[0])

        assert (playbook.parserdata[0]["rolename"] is None)
Ejemplo n.º 14
0
    def test_parser_playbook(self):
        localdir = os.path.dirname(os.path.realpath(__file__))
        testfile = os.path.join(localdir, ".output/testplaybook.yml")
        with open(testfile, "w") as f:
             f.write("---\n")
             f.write("# Author: Me\n")
             f.write("# description: this is a test\n")
             f.write("- name: 'Install Apache'\n")
             f.write("  yum: name=httpd state=installed\n")

        playbook = PlaybookParser([testfile], is_role=True)

        playbook.parse_playbooks()

        # One Element in Array
        assert( len(playbook.parserdata) == 1 )

        assert( "author" in playbook.parserdata[0] )

        assert( "description" in playbook.parserdata[0] )

        assert( "task_names" in playbook.parserdata[0] )

        assert( playbook.parserdata[0]["rolename"] is None)