Skip to content

Commit

Permalink
Add lxc_host dynamic group to inventory.
Browse files Browse the repository at this point in the history
Previously, it was difficult to target only the hosts holding LXC
containers. This change will add a host to the implicit 'lxc_hosts'
group any time a container is created in association with that host.

Users cannot define this group in their configuration. Doing so will
result in an runtime error.

This approach allows for specific targeting without using complicated
Jinja calculations as seen in commit
7669501. As such, the work in that
review has been replaced with this change.

Change-Id: I10faf5880a61abcc77f017573e0fd76d033224eb
  • Loading branch information
Nolan Brubaker authored and cloudnull committed Sep 16, 2016
1 parent 5c2f0b4 commit 518fb38
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 39 deletions.
9 changes: 9 additions & 0 deletions doc/source/developer-docs/inventory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ Use the host's name as an argument.

.. _`dynamic inventory functionality`: http://docs.ansible.com/ansible/intro_dynamic_inventory.html

The lxc_hosts Group
-------------------

When a container name is created by the dynamic inventory script, the host on
which the container resides is added to the ``lxc_hosts`` inventory group.

Using this name for a group in the configuration will result in a runtime
error.

Dynamic Inventory API documentation
-----------------------------------

Expand Down
36 changes: 0 additions & 36 deletions playbooks/common-plays/generate-lxc-container-hosts.yml

This file was deleted.

17 changes: 17 additions & 0 deletions playbooks/inventory/dynamic_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ def __str__(self):
return self.message


class LxcHostsDefined(Exception):
def __init__(self):
self.message = ("The group 'lxc_hosts' must not be defined in config;"
" it will be dynamically generated.")


def args(arg_list):
"""Setup argument Parsing."""
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -371,6 +377,8 @@ def _add_container_hosts(assignment, config, container_name, container_type,
# If host_type is not in config do not append containers to it
if host_type not in config[physical_host_type]:
continue
append_if(array=inventory['lxc_hosts']['hosts'],
item=host_type)

# Get any set host options
host_options = config[physical_host_type][host_type]
Expand Down Expand Up @@ -694,6 +702,8 @@ def container_skel_load(container_skel, inventory, config):
:param inventory: ``dict`` Living dictionary of inventory
:param config: ``dict`` User defined information
"""
if 'lxc_hosts' not in inventory.keys():
inventory['lxc_hosts'] = {'hosts': []}
for key, value in container_skel.iteritems():
contains_in = value.get('contains', False)
belongs_to_in = value.get('belongs_to', False)
Expand Down Expand Up @@ -960,6 +970,11 @@ def _check_multiple_ips_to_host(config):
return True


def _check_lxc_hosts(config):
if 'lxc_hosts' in config.keys():
raise LxcHostsDefined()


def _check_config_settings(cidr_networks, config, container_skel):
"""check preciseness of config settings
Expand Down Expand Up @@ -1012,6 +1027,8 @@ def _check_config_settings(cidr_networks, config, container_skel):

_check_multiple_ips_to_host(config)

_check_lxc_hosts(config)


def load_environment(config_path, environment):
"""Create an environment dictionary from config files
Expand Down
4 changes: 1 addition & 3 deletions playbooks/lxc-hosts-setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

- include: common-plays/generate-lxc-container-hosts.yml

- name: Basic lxc host setup
hosts: "{{ lxc_host_group | default('known_container_hosts') }}"
hosts: "{{ lxc_host_group | default('lxc_hosts')}}"
gather_facts: "{{ gather_facts | default(True) }}"
max_fail_percentage: 20
user: root
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/lxc_hosts-group-a5643e7010d6b151.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- The inventory script will now dynamically populate the ``lxc_hosts`` group
dynamically based on which machines have container affinities defined.

This group is not allowed in user-defined configuration.
32 changes: 32 additions & 0 deletions tests/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class TestAnsibleInventoryFormatConstraints(unittest.TestCase):
'log_all',
'log_containers',
'log_hosts',
'lxc_hosts',
'magnum',
'magnum_all',
'magnum_container',
Expand Down Expand Up @@ -403,6 +404,11 @@ def delete_config_key(self, user_defined_config, key):
finally:
self.write_config()

def add_config_key(self, key, value):
self.config_changed = True
self.user_defined_config[key] = value
self.write_config()

def delete_provider_network(self, net_name):
del self.user_defined_config['cidr_networks'][net_name]
self.write_config()
Expand Down Expand Up @@ -442,6 +448,10 @@ def set_new_ip(self, user_defined_config, group, hostname, ip):
user_defined_config[group][hostname]['ip'] = ip
self.write_config()

def add_host(self, group, host_name, ip):
self.user_defined_config[group][host_name] = {'ip': ip}
self.write_config()

def tearDown(self):
if self.config_changed:
self.restore_config()
Expand Down Expand Up @@ -1054,5 +1064,27 @@ def test_container_dict(self):
self.assertEqual(entry['interface'], 'eth1')


class TestLxcHosts(TestConfigCheckBase):

def test_lxc_hosts_group_present(self):
inventory = get_inventory()
self.assertIn('lxc_hosts', inventory)

def test_lxc_hosts_only_inserted_once(self):
inventory = get_inventory()
self.assertEqual(1, len(inventory['lxc_hosts']['hosts']))

def test_lxc_hosts_members(self):
self.add_host('shared-infra_hosts', 'aio2', '172.29.236.101')
inventory = get_inventory()
self.assertIn('aio2', inventory['lxc_hosts']['hosts'])
self.assertIn('aio1', inventory['lxc_hosts']['hosts'])

def test_lxc_hosts_in_config_raises_error(self):
self.add_config_key('lxc_hosts', {})
with self.assertRaises(di.LxcHostsDefined):
get_inventory()


if __name__ == '__main__':
unittest.main()

0 comments on commit 518fb38

Please sign in to comment.