Exemple #1
0
def build_collection_command():
    app_ctx = app_context.app_ctx.get()
    with tempfile.TemporaryDirectory() as working_dir:
        collection_dir = os.path.join(working_dir, 'community', 'ansible')

        sh.ansible_galaxy('collection', 'init', 'community.ansible', '--init-path', working_dir)
        # Copy the README.md file
        readme = get_antsibull_data('README_md.txt')
        with open(os.path.join(collection_dir, 'README.md'), 'wb') as f:
            f.write(readme)

        # Parse the deps file
        deps_file = DepsFile(os.path.join(app_ctx.extra['data_dir'], app_ctx.extra['deps_file']))
        dummy1_, dummy2_, deps = deps_file.parse()

        # Template the galaxy.yml file
        dep_string = json.dumps(deps)
        dep_string.replace(', ', ',\n    ')
        galaxy_yml = get_antsibull_data('galaxy_yml.j2').decode('utf-8')
        galaxy_yml_tmpl = Template(galaxy_yml)
        galaxy_yml_contents = galaxy_yml_tmpl.render(version=app_ctx.extra['ansible_version'],
                                                     dependencies=dep_string)

        with open(os.path.join(collection_dir, 'galaxy.yml'), 'w') as f:
            f.write(galaxy_yml_contents)

        sh.ansible_galaxy('collection', 'build',
                          '--output-path', app_ctx.extra['collection_dir'],
                          collection_dir)

    return 0
Exemple #2
0
    def execute(self):
        role = self.molecule._args['<role>']
        role_path = os.path.join(os.curdir, role)
        if not role:
            msg = '{}The init command requires a role name. Try:\n\n{}{} init <role>{}'
            print(msg.format(Fore.RED, Fore.YELLOW, os.path.basename(sys.argv[
                0]), Fore.RESET))
            sys.exit(1)

        if os.path.isdir(role):
            msg = '{}The directory {} already exists. Cannot create new role.{}'
            print(msg.format(Fore.RED, role_path, Fore.RESET))
            sys.exit(1)

        try:
            sh.ansible_galaxy('init', role)
        except (CalledProcessError, sh.ErrorReturnCode_1) as e:
            print('ERROR: {}'.format(e))
            sys.exit(e.returncode)

        self.clean_meta_main(role_path)

        env = Environment(loader=PackageLoader('molecule', 'templates'),
                          keep_trailing_newline=True)

        t_molecule = env.get_template(self.molecule._config.config['molecule'][
            'init']['templates']['molecule'])
        t_playbook = env.get_template(self.molecule._config.config['molecule'][
            'init']['templates']['playbook'])
        t_test_default = env.get_template(self.molecule._config.config[
            'molecule']['init']['templates']['test_default'])

        sanitized_role = re.sub('[._]', '-', role)
        with open(
                os.path.join(
                    role_path,
                    self.molecule._config.config['molecule']['molecule_file']),
                'w') as f:
            f.write(t_molecule.render(config=self.molecule._config.config,
                                      role=sanitized_role))

        with open(
                os.path.join(
                    role_path,
                    self.molecule._config.config['ansible']['playbook']),
                'w') as f:
            f.write(t_playbook.render(role=role))

        testinfra_path = os.path.join(
            role_path,
            self.molecule._config.config['molecule']['testinfra_dir'])
        os.makedirs(testinfra_path)

        with open(os.path.join(testinfra_path, 'test_default.py'), 'w') as f:
            f.write(t_test_default.render())

        msg = '{}Successfully initialized new role in {}{}'
        print(msg.format(Fore.GREEN, role_path, Fore.RESET))
        sys.exit(0)
Exemple #3
0
    def execute(self):
        """
        Creates the scaffolding for a new role intended for use with molecule

        :return: None
        """
        role = self.molecule._args['<role>']
        role_path = './' + role + '/'

        if not role:
            msg = '{}The init command requires a role name. Try:\n\n{}{} init <role>{}'
            print(msg.format(Fore.RED, Fore.YELLOW, os.path.basename(sys.argv[0]), Fore.RESET))
            sys.exit(1)

        if os.path.isdir(role):
            msg = '{}The directory {} already exists. Cannot create new role.{}'
            print(msg.format(Fore.RED, role_path, Fore.RESET))
            sys.exit(1)

        try:
            sh.ansible_galaxy('init', role)
        except (CalledProcessError, sh.ErrorReturnCode_1) as e:
            print('ERROR: {}'.format(e))
            sys.exit(e.returncode)

        env = Environment(loader=PackageLoader('molecule', 'templates'), keep_trailing_newline=True)

        t_molecule = env.get_template(self.molecule._config.config['molecule']['init']['templates']['molecule'])
        t_playbook = env.get_template(self.molecule._config.config['molecule']['init']['templates']['playbook'])
        t_default_spec = env.get_template(self.molecule._config.config['molecule']['init']['templates']['default_spec'])
        t_spec_helper = env.get_template(self.molecule._config.config['molecule']['init']['templates']['spec_helper'])

        sanitized_role = re.sub('[._]', '-', role)
        with open(role_path + self.molecule._config.config['molecule']['molecule_file'], 'w') as f:
            f.write(t_molecule.render(config=self.molecule._config.config, role=sanitized_role))

        with open(role_path + self.molecule._config.config['ansible']['playbook'], 'w') as f:
            f.write(t_playbook.render(role=role))

        serverspec_path = role_path + self.molecule._config.config['molecule']['serverspec_dir'] + '/'
        os.makedirs(serverspec_path)
        os.makedirs(serverspec_path + 'hosts')
        os.makedirs(serverspec_path + 'groups')

        with open(serverspec_path + 'default_spec.rb', 'w') as f:
            f.write(t_default_spec.render())

        with open(serverspec_path + 'spec_helper.rb', 'w') as f:
            f.write(t_spec_helper.render())

        msg = '{}Successfully initialized new role in {}{}'
        print(msg.format(Fore.GREEN, role_path, Fore.RESET))
        sys.exit(0)
    def execute(self):
        role = self.molecule._args['<role>']
        role_path = os.path.join(os.curdir, role)

        if not role:
            msg = '{}The init command requires a role name. Try:\n\n{}{} init <role>{}'
            print(msg.format(Fore.RED, Fore.YELLOW, os.path.basename(sys.argv[0]), Fore.RESET))
            sys.exit(1)

        if os.path.isdir(role):
            msg = '{}The directory {} already exists. Cannot create new role.{}'
            print(msg.format(Fore.RED, role_path, Fore.RESET))
            sys.exit(1)

        try:
            sh.ansible_galaxy('init', role)
        except (CalledProcessError, sh.ErrorReturnCode_1) as e:
            print('ERROR: {}'.format(e))
            sys.exit(e.returncode)

        env = Environment(loader=PackageLoader('molecule', 'templates'), keep_trailing_newline=True)

        t_molecule = env.get_template(self.molecule._config.config['molecule']['init']['templates']['molecule'])
        t_playbook = env.get_template(self.molecule._config.config['molecule']['init']['templates']['playbook'])
        t_default_spec = env.get_template(self.molecule._config.config['molecule']['init']['templates']['default_spec'])
        t_spec_helper = env.get_template(self.molecule._config.config['molecule']['init']['templates']['spec_helper'])

        sanitized_role = re.sub('[._]', '-', role)
        with open(os.path.join(role_path, self.molecule._config.config['molecule']['molecule_file']), 'w') as f:
            f.write(t_molecule.render(config=self.molecule._config.config, role=sanitized_role))

        with open(os.path.join(role_path, self.molecule._config.config['ansible']['playbook']), 'w') as f:
            f.write(t_playbook.render(role=role))

        serverspec_path = os.path.join(role_path, self.molecule._config.config['molecule']['serverspec_dir'])
        os.makedirs(serverspec_path)
        os.makedirs(os.path.join(serverspec_path, 'hosts'))
        os.makedirs(os.path.join(serverspec_path, 'groups'))

        with open(os.path.join(serverspec_path, 'default_spec.rb'), 'w') as f:
            f.write(t_default_spec.render())

        with open(os.path.join(serverspec_path, 'spec_helper.rb'), 'w') as f:
            f.write(t_spec_helper.render())

        msg = '{}Successfully initialized new role in {}{}'
        print(msg.format(Fore.GREEN, role_path, Fore.RESET))
        sys.exit(0)
Exemple #5
0
    def execute(self):
        role = self.molecule._args['<role>']

        if not role:
            role = os.getcwd().split(os.sep)[-1]
            role_path = os.getcwd()
            util.print_info("Initializing molecule in current directory...")

        else:

            if os.path.isdir(role):
                msg = 'The directory {} already exists. Cannot create new role.'
                LOG.error(msg.format(role))
                util.sysexit()

            role_path = os.path.join(os.curdir, role)

            util.print_info("Initializing role {}...".format(role))

            try:
                if self.molecule._args['--offline']:
                    sh.ansible_galaxy('init', '--offline', role)
                else:
                    sh.ansible_galaxy('init', role)
            except (subprocess.CalledProcessError, sh.ErrorReturnCode_1) as e:
                LOG.error('ERROR: {}'.format(e))
                util.sysexit(e.returncode)

            self.clean_meta_main(role_path)

        env = jinja2.Environment(loader=jinja2.PackageLoader(
            'molecule', 'template'),
                                 keep_trailing_newline=True)

        t_molecule = env.get_template(self.molecule.config.config['molecule']
                                      ['init']['templates']['molecule'])
        t_playbook = env.get_template(self.molecule.config.config['molecule']
                                      ['init']['templates']['playbook'])
        t_test_default = env.get_template(
            self.molecule.config.config['molecule']['init']['templates']
            ['test_default'])

        if (self.molecule._args['--docker']):
            t_molecule = env.get_template(
                self.molecule.config.config['molecule']['init']['templates']
                ['molecule_docker'])
        if (self.molecule._args['--openstack']):
            t_molecule = env.get_template(
                self.molecule.config.config['molecule']['init']['templates']
                ['molecule_openstack'])

        sanitized_role = re.sub('[._]', '-', role)
        with open(os.path.join(role_path, self.molecule.config.molecule_file),
                  'w') as f:
            f.write(
                t_molecule.render(config=self.molecule.config.config,
                                  role=sanitized_role))

        with open(
                os.path.join(
                    role_path,
                    self.molecule.config.config['ansible']['playbook']),
                'w') as f:
            f.write(t_playbook.render(role=role))

        testinfra_path = os.path.join(
            role_path,
            self.molecule.config.config['molecule']['testinfra_dir'])

        if not os.path.isdir(testinfra_path):
            os.mkdir(testinfra_path)

        with open(os.path.join(testinfra_path, 'test_default.py'), 'w') as f:
            f.write(t_test_default.render())

        msg = 'Successfully initialized new role in {}'
        util.print_success(msg.format(role_path))
        util.sysexit(0)
Exemple #6
0
    def execute(self):
        role = self.molecule._args['<role>']

        if not role:
            role = os.getcwd().split(os.sep)[-1]
            role_path = os.getcwd()
            utilities.print_info(
                "Initializing molecule in current directory...")

        else:

            if os.path.isdir(role):
                msg = 'The directory {} already exists. Cannot create new role.'
                utilities.logger.error(msg.format(role))
                utilities.sysexit()

            role_path = os.path.join(os.curdir, role)

            utilities.print_info("Initializing role {}...".format(role))

            try:
                if self.molecule._args['--offline']:
                    sh.ansible_galaxy('init', '--offline', role)
                else:
                    sh.ansible_galaxy('init', role)
            except (subprocess.CalledProcessError, sh.ErrorReturnCode_1) as e:
                utilities.logger.error('ERROR: {}'.format(e))
                utilities.sysexit(e.returncode)

            self.clean_meta_main(role_path)

        env = jinja2.Environment(
            loader=jinja2.PackageLoader('molecule', 'templates'),
            keep_trailing_newline=True)

        t_molecule = env.get_template(self.molecule._config.config['molecule'][
            'init']['templates']['molecule'])
        t_playbook = env.get_template(self.molecule._config.config['molecule'][
            'init']['templates']['playbook'])
        t_test_default = env.get_template(self.molecule._config.config[
            'molecule']['init']['templates']['test_default'])

        if (self.molecule._args['--docker']):
            t_molecule = env.get_template(self.molecule._config.config[
                'molecule']['init']['templates']['molecule_docker'])
        if (self.molecule._args['--openstack']):
            t_molecule = env.get_template(self.molecule._config.config[
                'molecule']['init']['templates']['molecule_openstack'])

        sanitized_role = re.sub('[._]', '-', role)
        with open(
                os.path.join(role_path, self.molecule._config.molecule_file),
                'w') as f:
            f.write(t_molecule.render(config=self.molecule._config.config,
                                      role=sanitized_role))

        with open(
                os.path.join(
                    role_path,
                    self.molecule._config.config['ansible']['playbook']),
                'w') as f:
            f.write(t_playbook.render(role=role))

        testinfra_path = os.path.join(
            role_path,
            self.molecule._config.config['molecule']['testinfra_dir'])

        if not os.path.isdir(testinfra_path):
            os.mkdir(testinfra_path)

        with open(os.path.join(testinfra_path, 'test_default.py'), 'w') as f:
            f.write(t_test_default.render())

        msg = 'Successfully initialized new role in {}'
        utilities.print_success(msg.format(role_path))
        utilities.sysexit(0)
Exemple #7
0
    def execute(self):
        role = self.molecule._args['<role>']
        role_path = os.path.join(os.curdir, role)
        if not role:
            msg = '{}The init command requires a role name. Try:\n\n{}{} init <role>{}'
            utilities.logger.error(
                msg.format(colorama.Fore.RED, colorama.Fore.YELLOW,
                           os.path.basename(sys.argv[0]), colorama.Fore.RESET))
            sys.exit(1)

        if os.path.isdir(role):
            msg = '{}The directory {} already exists. Cannot create new role.{}'
            utilities.logger.error(
                msg.format(colorama.Fore.RED, role_path, colorama.Fore.RESET))
            sys.exit(1)

        try:
            if self.molecule._args['--offline']:
                sh.ansible_galaxy('init', '--offline', role)
            else:
                sh.ansible_galaxy('init', role)
        except (CalledProcessError, sh.ErrorReturnCode_1) as e:
            utilities.logger.error('ERROR: {}'.format(e))
            sys.exit(e.returncode)

        self.clean_meta_main(role_path)

        env = jinja2.Environment(loader=jinja2.PackageLoader(
            'molecule', 'templates'),
                                 keep_trailing_newline=True)

        t_molecule = env.get_template(self.molecule._config.config['molecule']
                                      ['init']['templates']['molecule'])
        t_playbook = env.get_template(self.molecule._config.config['molecule']
                                      ['init']['templates']['playbook'])
        t_test_default = env.get_template(
            self.molecule._config.config['molecule']['init']['templates']
            ['test_default'])

        if (self.molecule._args['--docker']):
            t_molecule = env.get_template(
                self.molecule._config.config['molecule']['init']['templates']
                ['molecule_docker'])

        sanitized_role = re.sub('[._]', '-', role)
        with open(
                os.path.join(
                    role_path,
                    self.molecule._config.config['molecule']['molecule_file']),
                'w') as f:
            f.write(
                t_molecule.render(config=self.molecule._config.config,
                                  role=sanitized_role))

        with open(
                os.path.join(
                    role_path,
                    self.molecule._config.config['ansible']['playbook']),
                'w') as f:
            f.write(t_playbook.render(role=role))

        testinfra_path = os.path.join(
            role_path,
            self.molecule._config.config['molecule']['testinfra_dir'])

        with open(os.path.join(testinfra_path, 'test_default.py'), 'w') as f:
            f.write(t_test_default.render())

        msg = '{}Successfully initialized new role in {}{}'
        print(msg.format(colorama.Fore.GREEN, role_path, colorama.Fore.RESET))
        sys.exit(0)