Browse Source

Merge "Move inventory filters to tripleo-upgrade"

tags/1.4.0
Zuul 1 month ago
committed by Gerrit Code Review
parent
commit
239b432f33
2 changed files with 0 additions and 229 deletions
  1. +0
    -104
      tripleo_ansible/ansible_plugins/filter/tripleo_inventory.py
  2. +0
    -125
      tripleo_ansible/tests/plugins/filter/test_tripleo_inventory.py

+ 0
- 104
tripleo_ansible/ansible_plugins/filter/tripleo_inventory.py View File

@@ -1,104 +0,0 @@
#!/usr/bin/env python
# Copyright 2020 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import collections
import six
import sys
import yaml


def to_inventory_hostmap(data):
# Flattens inventory to a group->host mapping
if isinstance(data, six.string_types):
inventory = yaml.safe_load(data)
else:
inventory = data

group_host_map = {}

todo = collections.deque(inventory.keys())
while todo:
group = todo.popleft()
if 'hosts' in inventory[group]:
group_host_map[group] = list(inventory[group]['hosts'])
else:
if 'children' in inventory[group]:
for child in inventory[group]['children']:
# Children have not all been flattened yet
# so postpone flattening this group
if child in todo:
todo.append(group)
break
else:
group_host_map[group] = []
for child in inventory[group]['children']:
group_host_map[group] += group_host_map[child]
group_host_map[group].sort()
return group_host_map


def to_inventory_rolemap(data):
# Falttens inventory to a group->role mapping
if isinstance(data, six.string_types):
inventory = yaml.safe_load(data)
else:
inventory = data

group_role_map = {}

todo = collections.deque(inventory.keys())
while todo:
group = todo.popleft()
if 'tripleo_role_name' in inventory[group].get('vars', {}):
group_role_map[group] = [inventory[group]['vars']['tripleo_role_name']]
else:
if 'children' in inventory[group]:
for child in inventory[group]['children']:
# Children have not all been flattened yet
# so postpone flattening this group
if child in todo:
todo.append(group)
break
else:
group_role_map[group] = []
for child in inventory[group]['children']:
group_role_map[group] += group_role_map[child]
group_role_map[group].sort()
return group_role_map


def to_inventory_roles(data):
# Returns list of tripleo roles in inventory
if isinstance(data, six.string_types):
inventory = yaml.safe_load(data)
else:
inventory = data

roles = {}
for group, group_data in six.iteritems(inventory):
group_role = group_data.get('vars', {}).get('tripleo_role_name', None)
if group_role is not None:
roles[group_role] = True
return sorted(list(roles))


class FilterModule(object):
def filters(self):
return {
'to_inventory_hostmap': to_inventory_hostmap,
'to_inventory_rolemap': to_inventory_rolemap,
'to_inventory_roles': to_inventory_roles,
}

+ 0
- 125
tripleo_ansible/tests/plugins/filter/test_tripleo_inventory.py View File

@@ -1,125 +0,0 @@
# Copyright 2020 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os

from tripleo_ansible.ansible_plugins.filter import tripleo_inventory
from tripleo_ansible.tests import base as tests_base

static_inventory = """
overcloud:
children:
allovercloud: {}
allovercloud:
children:
Compute: {}
Controller: {}
vars: {}
Controller:
children:
overcloud_Controller: {}
overcloud_Controller:
hosts:
overcloud-controller-0: {foo_bar: baz}
vars:
tripleo_role_name: Controller
Compute:
children:
overcloud_Compute: {}
overcloud_Compute:
hosts:
overcloud-novacompute-0: {foo_bar: baz}
vars:
tripleo_role_name: Compute
"""

dynamic_inventory = """
{
"overcloud": {
"children": [ "allovercloud" ]
},
"allovercloud": {
"children": [ "Compute", "Controller" ],
"vars" : {}
},
"Controller" : {
"children": [ "overcloud_Controller" ]
},
"overcloud_Controller" : {
"hosts": [ "overcloud-controller-0" ],
"vars": { "tripleo_role_name": "Controller" }
},
"Compute" : {
"children": [ "overcloud_Compute" ]
},
"overcloud_Compute" : {
"hosts": ["overcloud-novacompute-0" ],
"vars": { "tripleo_role_name": "Compute" }
}
}
"""


class TestInventoryFilters(tests_base.TestCase):

def setUp(self):
super(TestInventoryFilters, self).setUp()

def _test_hostmap(self, inventory):
expected = {
"Compute": ["overcloud-novacompute-0"],
"overcloud_Compute": ["overcloud-novacompute-0"],
"Controller": ["overcloud-controller-0"],
"overcloud_Controller": ["overcloud-controller-0"],
"overcloud": ["overcloud-controller-0", "overcloud-novacompute-0"],
"allovercloud": ["overcloud-controller-0", "overcloud-novacompute-0"],
}
result = tripleo_inventory.to_inventory_hostmap(inventory)
self.assertEqual(expected, result)

def test_hostmap_static(self):
self._test_hostmap(static_inventory)

def test_hostmap_dynamic(self):
self._test_hostmap(dynamic_inventory)

def _test_rolemap(self, inventory):
expected = {
"overcloud": ["Compute", "Controller"],
"allovercloud": ["Compute", "Controller"],
"Controller": ["Controller"],
"overcloud_Controller": ["Controller"],
"Compute": ["Compute"],
"overcloud_Compute": ["Compute"]
}
result = tripleo_inventory.to_inventory_rolemap(inventory)
self.assertEqual(expected, result)

def test_rolemap_static(self):
self._test_rolemap(static_inventory)

def test_rolemap_dynamic(self):
self._test_rolemap(dynamic_inventory)

def _test_roles(self, inventory):
expected = ["Compute", "Controller"]
result = tripleo_inventory.to_inventory_roles(inventory)
self.assertEqual(expected, result)

def test_roles_static(self):
self._test_roles(static_inventory)

def test_roles_dynamic(self):
self._test_roles(dynamic_inventory)

Loading…
Cancel
Save