+import json
import os
from cStringIO import StringIO
from . import ansible
from ..config import config as teuth_config
+from ..misc import get_scratch_devices
class CephAnsible(ansible.Ansible):
playbook: {playbook}
It always uses a dynamic inventory.
+
+ It will optionally do the following automatically based on ``vars`` that
+ are passed in:
+ * Set ``devices`` for each host if ``osd_auto_discovery`` is not True
""".format(
git_base=teuth_config.ceph_git_base_url,
playbook=_default_playbook,
want = lambda role: role.startswith(role_prefix)
for (remote, roles) in self.cluster.only(want).remotes.iteritems():
hostname = remote.hostname
+ host_vars = self.get_host_vars(remote)
if group not in hosts_dict:
- hosts_dict[group] = {hostname: None}
+ hosts_dict[group] = {hostname: host_vars}
elif hostname not in hosts_dict[group]:
- hosts_dict[group][hostname] = None
+ hosts_dict[group][hostname] = host_vars
- print hosts_dict
hosts_stringio = StringIO()
for group in sorted(hosts_dict.keys()):
hosts_stringio.write('[%s]\n' % group)
for hostname in sorted(hosts_dict[group].keys()):
vars = hosts_dict[group][hostname]
if vars:
- vars_list = ['%s=%s' % (k, v)
- for k, v in vars.iteritems()]
+ vars_list = []
+ for key in sorted(vars.keys()):
+ vars_list.append(
+ "%s='%s'" % (key, json.dumps(vars[key]).strip('"'))
+ )
host_line = "{hostname} {vars}".format(
hostname=hostname,
vars=' '.join(vars_list),
self.inventory = self._write_hosts_file(hosts_stringio.read().strip())
self.generated_inventory = True
+ def get_host_vars(self, remote):
+ extra_vars = self.config.get('vars', dict())
+ host_vars = dict()
+ if not extra_vars.get('osd_auto_discovery', False):
+ host_vars['devices'] = get_scratch_devices(remote)
+ return host_vars
+
task = CephAnsible
)
self.patcher_fetch_repo.start()
+ def fake_get_scratch_devices(remote):
+ return ['/dev/%s' % remote.shortname]
+
+ self.patcher_get_scratch_devices = patch(
+ 'teuthology.task.ceph_ansible.get_scratch_devices',
+ fake_get_scratch_devices,
+ )
+ self.patcher_get_scratch_devices.start()
+
def stop_patchers(self):
super(TestCephAnsibleTask, self).stop_patchers()
self.patcher_fetch_repo.stop()
+ self.patcher_get_scratch_devices.stop()
def test_playbook_none(self):
skip(SKIP_IRRELEVANT)
def test_generate_hosts_file(self):
self.task_config.update(dict(
- playbook=[]
+ playbook=[],
+ vars=dict(osd_auto_discovery=True),
))
task = self.klass(self.ctx, self.task_config)
hosts_file_path = '/my/hosts/file'
'[osds]',
'remote3',
])
+
+ def test_generate_hosts_file_with_devices(self):
+ self.task_config.update(dict(
+ playbook=[]
+ ))
+ task = self.klass(self.ctx, self.task_config)
+ hosts_file_path = '/my/hosts/file'
+ hosts_file_obj = StringIO()
+ hosts_file_obj.name = hosts_file_path
+ with patch.object(ansible, 'NamedTemporaryFile') as m_NTF:
+ m_NTF.return_value = hosts_file_obj
+ task.generate_hosts_file()
+ m_NTF.assert_called_once_with(prefix="teuth_ansible_hosts_",
+ delete=False)
+ assert task.generated_inventory is True
+ assert task.inventory == hosts_file_path
+ hosts_file_obj.seek(0)
+ assert hosts_file_obj.read() == '\n'.join([
+ '[mdss]',
+ 'remote2 devices=\'["/dev/remote2"]\'',
+ '',
+ '[mons]',
+ 'remote1 devices=\'["/dev/remote1"]\'',
+ '',
+ '[osds]',
+ 'remote3 devices=\'["/dev/remote3"]\'',
+ ])