Add tools for getting ansible inventory

Change-Id: I596057cc79a91bbc099a2721b5453058069e4b41
This commit is contained in:
Federico Ressi 2020-07-06 16:51:17 +02:00
parent 8c9f389692
commit 0d8b6d9430
6 changed files with 200 additions and 0 deletions

3
.gitignore vendored
View File

@ -46,3 +46,6 @@ tobiko.conf*
# Infrared things
.infrared
workspace.tgz
# Tripleo files
tripleo-hosts.yaml

View File

@ -0,0 +1,56 @@
# Copyright 2019 Red Hat
#
# 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.
from __future__ import absolute_import
import os
import testtools
import yaml
from tobiko import config
from tobiko import tripleo
CONF = config.CONF
@tripleo.skip_if_missing_undercloud
class InventoryFileTest(testtools.TestCase):
@tripleo.skip_if_missing_tripleo_ansible_inventory
def test_get_tripleo_ansible_inventory(self):
inventory = tripleo.get_tripleo_ansible_inventory()
self.assertIn('Undercloud', inventory)
self.assertIn('Controller', inventory)
self.assertIn('Compute', inventory)
@tripleo.skip_if_missing_tripleo_ansible_inventory
def test_get_tripleo_ansible_inventory_file(self):
inventory_file = tripleo.get_tripleo_ansible_inventory_file()
self.assertTrue(os.path.isfile(inventory_file))
def test_has_tripleo_ansible_inventory(self):
result = tripleo.has_tripleo_ansible_inventory()
inventory_file = tripleo.get_tripleo_ansible_inventory_file()
self.assertIs(inventory_file and os.path.isfile(inventory_file),
result)
def test_read_tripleo_ansible_inventory(self):
inventory_yaml = tripleo.read_tripleo_ansible_inventory()
self.assertIsInstance(inventory_yaml, str)
self.assertTrue(inventory_yaml)
inventory = yaml.safe_load(inventory_yaml)
self.assertIn('Undercloud', inventory)
self.assertIn('Controller', inventory)
self.assertIn('Compute', inventory)

View File

@ -0,0 +1,28 @@
# Copyright 2020 Red Hat
#
# 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.
from __future__ import absolute_import
from tobiko.tripleo import _ansible
from tobiko.tripleo import _undercloud as undercloud
get_tripleo_ansible_inventory = _ansible.get_tripleo_ansible_inventory
get_tripleo_ansible_inventory_file = \
_ansible.get_tripleo_ansible_inventory_file
has_tripleo_ansible_inventory = _ansible.has_tripleo_ansible_inventory
read_tripleo_ansible_inventory = _ansible.read_tripleo_ansible_inventory
skip_if_missing_tripleo_ansible_inventory = \
_ansible.skip_if_missing_tripleo_ansible_inventory
skip_if_missing_undercloud = undercloud.skip_if_missing_undercloud

View File

@ -0,0 +1,85 @@
# Copyright 2020 Red Hat
#
# 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.
from __future__ import absolute_import
import io
import os
import yaml
import tobiko
from tobiko.shell import sh
from tobiko.tripleo import _undercloud
from tobiko.tripleo import _config
def get_tripleo_ansible_inventory():
inventory_file = get_tripleo_ansible_inventory_file()
with io.open(inventory_file, 'rb') as fd:
return yaml.safe_load(fd)
def has_tripleo_ansible_inventory():
inventory_file = get_tripleo_ansible_inventory_file()
return inventory_file and os.path.isfile(inventory_file)
skip_if_missing_tripleo_ansible_inventory = \
tobiko.skip_unless("Can't read TripleO Ansible inventory",
has_tripleo_ansible_inventory)
def get_tripleo_ansible_inventory_file():
return tobiko.setup_fixture(TripleoAnsibleInventoryFixture).inventory_file
class TripleoAnsibleInventoryFixture(tobiko.SharedFixture):
inventory_file = None
def setup_fixture(self):
tripleo = _config.get_tripleo_config()
self.inventory_file = inventory_file = tobiko.tobiko_config_path(
tripleo.inventory_file)
if inventory_file and not os.path.isfile(inventory_file):
content = read_tripleo_ansible_inventory()
with io.open(inventory_file, 'w') as fd:
fd.write(content)
READ_TRIPLEO_ANSIBLE_INVENTORY_SCRIPT = """
source {undercloud_rcfile} || exit 1
set -x
INVENTORY_FILE=$(mktemp tripleo-hosts-XXXXXXXXXX.yaml)
tripleo-ansible-inventory --ansible_ssh_user "{overcloud_ssh_username}" \\
--static-yaml-inventory "$INVENTORY_FILE"
RC=$?
if [ $RC == 0 ]; then
cat "$INVENTORY_FILE"
fi
rm -fR "$INVENTORY_FILE"
exit $RC
"""
def read_tripleo_ansible_inventory():
tripleo = _config.get_tripleo_config()
ssh_client = _undercloud.undercloud_ssh_client()
script = READ_TRIPLEO_ANSIBLE_INVENTORY_SCRIPT.format(
undercloud_rcfile=tripleo.undercloud_rcfile,
overcloud_ssh_username=tripleo.overcloud_ssh_username)
return sh.execute('/bin/bash', stdin=script, ssh_client=ssh_client).stdout

28
tobiko/tripleo/_config.py Normal file
View File

@ -0,0 +1,28 @@
# Copyright 2020 Red Hat
#
# 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.
from __future__ import absolute_import
import tobiko
class TripleoConfigFixture(tobiko.SharedFixture):
tripleo = None
def setup_fixture(self):
self.tripleo = tobiko.tobiko_config().tripleo
def get_tripleo_config():
return tobiko.setup_fixture(TripleoConfigFixture).tripleo