Exemplo n.º 1
0
    def _create_inventory_file(self):
        """
        Creates the inventory file used by molecule and later passed to ansible-playbook.

        :return: None
        """
        inventory = ''

        for instance in self._provisioner.instances:
            inventory += self._provisioner.inventory_entry(instance)

        # get a list of all groups and hosts in those groups
        groups = {}
        for instance in self._provisioner.instances:
            if 'ansible_groups' in instance:
                for group in instance['ansible_groups']:
                    if group not in groups:
                        groups[group] = []
                    groups[group].append(instance['name'])

        for group, instances in groups.iteritems():
            inventory += '\n[{}]\n'.format(group)
            for instance in instances:
                inventory += '{}\n'.format(utilities.format_instance_name(
                    instance, self._env['MOLECULE_PLATFORM'],
                    self._provisioner.instances))

        inventory_file = self._config.config['molecule']['inventory_file']
        try:
            utilities.write_file(inventory_file, inventory)
        except IOError:
            utilities.logger.warning(
                '{}WARNING: could not write inventory file {}{}'.format(
                    colorama.Fore.YELLOW, inventory_file, colorama.Fore.RESET))
Exemplo n.º 2
0
    def _create_inventory_file(self):
        """
        Creates the inventory file used by molecule and later passed to ansible-playbook.

        :return: None
        """
        inventory = ''

        for instance in self._provisioner.instances:
            inventory += self._provisioner.inventory_entry(instance)

        # get a list of all groups and hosts in those groups
        groups = {}
        for instance in self._provisioner.instances:
            if 'ansible_groups' in instance:
                for group in instance['ansible_groups']:
                    if group not in groups:
                        groups[group] = []
                    groups[group].append(instance['name'])

        for group, instances in groups.iteritems():
            inventory += '\n[{}]\n'.format(group)
            for instance in instances:
                inventory += '{}\n'.format(
                    utilities.format_instance_name(
                        instance, self._env['MOLECULE_PLATFORM'],
                        self._provisioner.instances))

        inventory_file = self._config.config['molecule']['inventory_file']
        try:
            utilities.write_file(inventory_file, inventory)
        except IOError:
            utilities.logger.warning(
                '{}WARNING: could not write inventory file {}{}'.format(
                    colorama.Fore.YELLOW, inventory_file, colorama.Fore.RESET))
Exemplo n.º 3
0
    def test_write_file(self):
        tmp_file = "/tmp/test_utilities_write_file.tmp"
        contents = binascii.b2a_hex(os.urandom(15))
        utilities.write_file(tmp_file, contents)
        with open(tmp_file, "r") as f:
            data = f.read()
        os.remove(tmp_file)

        self.assertEqual(data, contents)
Exemplo n.º 4
0
 def _write_ssh_config(self):
     try:
         out = self._vagrant.ssh_config()
         ssh_config = self._get_vagrant_ssh_config()
     except CalledProcessError as e:
         print('ERROR: {}'.format(e))
         print("Does your vagrant VM exist?")
         sys.exit(e.returncode)
     utilities.write_file(ssh_config, out)
Exemplo n.º 5
0
 def _write_ssh_config(self):
     try:
         out = self._provisioner.conf(ssh_config=True)
         ssh_config = self._provisioner.ssh_config_file
     except CalledProcessError as e:
         print('ERROR: {}'.format(e))
         print("Does your vagrant VM exist?")
         sys.exit(e.returncode)
     utilities.write_file(ssh_config, out)
Exemplo n.º 6
0
    def test_write_file(self):
        tmp_file = '/tmp/test_utilities_write_file.tmp'
        contents = binascii.b2a_hex(os.urandom(15))
        utilities.write_file(tmp_file, contents)
        with open(tmp_file, 'r') as f:
            data = f.read()
        os.remove(tmp_file)

        self.assertEqual(data, contents)
Exemplo n.º 7
0
 def _write_ssh_config(self):
     try:
         out = self._provisioner.conf(ssh_config=True)
         ssh_config = self._provisioner.ssh_config_file
     except CalledProcessError as e:
         print('ERROR: {}'.format(e))
         print("Does your vagrant VM exist?")
         sys.exit(e.returncode)
     utilities.write_file(ssh_config, out)
Exemplo n.º 8
0
def test_write_file():
    tmp_file = '/tmp/test_utilities_write_file.tmp'
    contents = binascii.b2a_hex(os.urandom(15))
    utilities.write_file(tmp_file, contents)
    with open(tmp_file, 'r') as f:
        data = f.read()
    os.remove(tmp_file)

    assert data == contents
Exemplo n.º 9
0
 def _write_ssh_config(self):
     try:
         out = self._provisioner.conf(ssh_config=True)
         ssh_config = self._provisioner.ssh_config_file
         if ssh_config is None:
             return
     except subprocess.CalledProcessError as e:
         LOG.error('ERROR: {}'.format(e))
         LOG.error("Does your vagrant VM exist?")
         utilities.sysexit(e.returncode)
     utilities.write_file(ssh_config, out)
Exemplo n.º 10
0
    def _create_inventory_file(self):
        """
        Creates the inventory file used by molecule and later passed to ansible-playbook.

        :return: None
        """
        inventory = ''
        # TODO: for Ansiblev2, the following line must have s/ssh_//
        host_template = \
            '{} ansible_ssh_host={} ansible_ssh_port={} ansible_ssh_private_key_file={} ansible_ssh_user={}\n'
        for instance in self._provisioner.instances:
            ssh = self._provisioner.conf(
                vm_name=utilities.format_instance_name(
                    instance['name'], self._env['MOLECULE_PLATFORM'],
                    self._provisioner.instances))
            inventory += host_template.format(ssh['Host'], ssh['HostName'],
                                              ssh['Port'], ssh['IdentityFile'],
                                              ssh['User'])

        # get a list of all groups and hosts in those groups
        groups = {}
        for instance in self._provisioner.instances:
            if 'ansible_groups' in instance:
                for group in instance['ansible_groups']:
                    if group not in groups:
                        groups[group] = []
                    groups[group].append(instance['name'])

        for group, instances in groups.iteritems():
            inventory += '\n[{}]\n'.format(group)
            for instance in instances:
                inventory += '{}\n'.format(
                    utilities.format_instance_name(
                        instance, self._env['MOLECULE_PLATFORM'],
                        self._provisioner.instances))

        inventory_file = self._config.config['molecule']['inventory_file']
        try:
            utilities.write_file(inventory_file, inventory)
        except IOError:
            print('{}WARNING: could not write inventory file {}{}'.format(
                Fore.YELLOW, inventory_file, Fore.RESET))
Exemplo n.º 11
0
    def _create_inventory_file(self):
        """
        Creates the inventory file used by molecule and later passed to ansible-playbook.

        :return: None
        """
        inventory = ''
        # TODO: for Ansiblev2, the following line must have s/ssh_//
        host_template = \
            '{} ansible_ssh_host={} ansible_ssh_port={} ansible_ssh_private_key_file={} ansible_ssh_user={}\n'
        for instance in self._provisioner.instances:
            ssh = self._provisioner.conf(
                vm_name=utilities.format_instance_name(
                    instance['name'], self._env[
                        'MOLECULE_PLATFORM'], self._provisioner.instances))
            inventory += host_template.format(ssh['Host'], ssh['HostName'],
                                              ssh['Port'], ssh['IdentityFile'],
                                              ssh['User'])

        # get a list of all groups and hosts in those groups
        groups = {}
        for instance in self._provisioner.instances:
            if 'ansible_groups' in instance:
                for group in instance['ansible_groups']:
                    if group not in groups:
                        groups[group] = []
                    groups[group].append(instance['name'])

        for group, instances in groups.iteritems():
            inventory += '\n[{}]\n'.format(group)
            for instance in instances:
                inventory += '{}\n'.format(utilities.format_instance_name(
                    instance, self._env[
                        'MOLECULE_PLATFORM'], self._provisioner.instances))

        inventory_file = self._config.config['molecule']['inventory_file']
        try:
            utilities.write_file(inventory_file, inventory)
        except IOError:
            print('{}WARNING: could not write inventory file {}{}'.format(
                Fore.YELLOW, inventory_file, Fore.RESET))
Exemplo n.º 12
0
    def _add_or_update_vars(self, target):
        """Creates or updates to host/group variables if needed."""

        if target in self.config.config['ansible']:
            vars_target = self.config.config['ansible'][target]
        else:
            return

        molecule_dir = self.config.config['molecule']['molecule_dir']
        target_vars_path = os.path.join(molecule_dir, target)

        if not os.path.exists(os.path.abspath(target_vars_path)):
            os.mkdir(os.path.abspath(target_vars_path))

        for target in vars_target.keys():
            target_var_content = vars_target[target][0]

            utilities.write_file(
                os.path.join(
                    os.path.abspath(target_vars_path), target),
                "---\n" + yaml.dump(target_var_content,
                                    default_flow_style=False))
Exemplo n.º 13
0
    def _create_inventory_file(self):
        """
        Creates the inventory file used by molecule and later passed to
        ansible-playbook.

        :return: None
        """

        inventory = ''
        for instance in self._provisioner.instances:
            inventory += self._provisioner.inventory_entry(instance)

        # get a list of all groups and hosts in those groups
        groups = {}
        for instance in self._provisioner.instances:
            if 'ansible_groups' in instance:
                for group in instance['ansible_groups']:
                    if group not in groups:
                        groups[group] = []
                    groups[group].append(instance['name'])

        if self._args.get('--platform') == 'all':
            self._provisioner.platform = 'all'

        for group, instances in groups.iteritems():
            inventory += '\n[{}]\n'.format(group)
            for instance in instances:
                inventory += '{}\n'.format(utilities.format_instance_name(
                    instance, self._provisioner.platform,
                    self._provisioner.instances))

        inventory_file = self.config.config['ansible']['inventory_file']
        try:
            utilities.write_file(inventory_file, inventory)
        except IOError:
            LOG.warning('WARNING: could not write inventory file {}'.format(
                inventory_file))
Exemplo n.º 14
0
 def _write_state_file(self):
     utilities.write_file(self._config.config['molecule']['state_file'],
                          yaml.dump(self._state, default_flow_style=False))
Exemplo n.º 15
0
 def _write_state_file(self):
     utilities.write_file(self._config.config['molecule']['state_file'],
                          yaml.dump(self._state,
                                    default_flow_style=False))
Exemplo n.º 16
0
 def _write_ssh_config(self):
     ssh_config = self._get_ssh_config()
     if ssh_config is None:
         return
     out = self._provisioner.conf(ssh_config=True)
     utilities.write_file(ssh_config, out)
Exemplo n.º 17
0
 def _write_state_file(self):
     utilities.write_file(self._state_file,
                          yaml.safe_dump(self._data,
                                         default_flow_style=False,
                                         explicit_start=True,
                                         encoding='utf-8'))