172 lines
4.8 KiB
Python
Raw Normal View History

2014-08-26 18:08:15 -05:00
#!/usr/bin/env python
# Copyright 2014, Rackspace US, Inc.
#
# 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.
#
# (c) 2014, Kevin Carter <kevin.carter@rackspace.com>
import argparse
import os
import json
import prettytable
def file_find(filename, user_file=None, pass_exception=False):
"""Return the path to a file.
If no file is found the system will exit.
The file lookup will be done in the following directories:
Remove and/or rename Rackspace related bits This patch removes and/or renames anything that is Rackspace specific from the playbooks, roles and variables. It also removes items which appear to be orphaned/unused and flattens the playbooks into a single directory in order to better match ansible best practise (and remove some horrible fiddles we were doing). The following have been removed due to RAX/RPC naming or RAX/RPC specific usage: - playbooks/monitoring - playbooks/rax* - playbooks/rpc* - roles/maas* - roles/rax* - roles/rpc* - scripts/f5-* - scripts/maas* - scripts/rpc* - scripts/*lab* - vars/repo_packages/rackspace* - vars/repo_packages/rax* - vars/repo_packages/rpc* - vars/repo_packages/holland.yml The following have been removed as they are unused: - playbooks/setup/host-network-setup.yml - roles/openssl_pem_request - roles/host_interfaces - scripts/elsa* - ssh/ - vars/repo_packages/turbolift.yml The following directories have been renamed: - etc/rpc_deploy > etc/openstack_deploy - rpc_deployment > playbooks The playbooks have all been moved into a single directory: - rpc_deployment/playbooks/infrastructure/* > playbooks/ - rpc_deployment/playbooks/openstack/* > playbooks/ - rpc_deployment/playbooks/setup/* > playbooks/ The following files have been renamed: - lxc-rpc > lxc-openstack - lxc-rpc.conf > lxc-openstack.conf - rpc_environment > openstack_environment - rpc_release > openstack_release (etc and pip) - rpc_tempest_gate.sh > openstack_tempest_gate.sh - rpc_user_config > openstack_user_config The following variables have been renamed: - rpc_release > openstack_release - rpc_repo_url > openstack_repo_url The following variables have been introduced: - openstack_code_name: The code name of the upstream OpenStack release (eg: Juno) Notable variable/template value changes: - rabbit_cluster_name: rpc > openstack - wsrep_cluster_name: rpc_galera_cluster > openstack_galera_cluster DocImpact Closes-Bug: #1403676 Implements: blueprint rackspace-namesake Change-Id: Ib480fdad500b03c7cb90684aa444da9946ba8032
2015-02-11 16:26:22 +00:00
/etc/openstack_deploy/
$(pwd)/openstack_deploy/
2014-08-26 18:08:15 -05:00
:param filename: ``str`` Name of the file to find
:param user_file: ``str`` Additional localtion to look in FIRST for a file
"""
file_check = [
os.path.join(
Remove and/or rename Rackspace related bits This patch removes and/or renames anything that is Rackspace specific from the playbooks, roles and variables. It also removes items which appear to be orphaned/unused and flattens the playbooks into a single directory in order to better match ansible best practise (and remove some horrible fiddles we were doing). The following have been removed due to RAX/RPC naming or RAX/RPC specific usage: - playbooks/monitoring - playbooks/rax* - playbooks/rpc* - roles/maas* - roles/rax* - roles/rpc* - scripts/f5-* - scripts/maas* - scripts/rpc* - scripts/*lab* - vars/repo_packages/rackspace* - vars/repo_packages/rax* - vars/repo_packages/rpc* - vars/repo_packages/holland.yml The following have been removed as they are unused: - playbooks/setup/host-network-setup.yml - roles/openssl_pem_request - roles/host_interfaces - scripts/elsa* - ssh/ - vars/repo_packages/turbolift.yml The following directories have been renamed: - etc/rpc_deploy > etc/openstack_deploy - rpc_deployment > playbooks The playbooks have all been moved into a single directory: - rpc_deployment/playbooks/infrastructure/* > playbooks/ - rpc_deployment/playbooks/openstack/* > playbooks/ - rpc_deployment/playbooks/setup/* > playbooks/ The following files have been renamed: - lxc-rpc > lxc-openstack - lxc-rpc.conf > lxc-openstack.conf - rpc_environment > openstack_environment - rpc_release > openstack_release (etc and pip) - rpc_tempest_gate.sh > openstack_tempest_gate.sh - rpc_user_config > openstack_user_config The following variables have been renamed: - rpc_release > openstack_release - rpc_repo_url > openstack_repo_url The following variables have been introduced: - openstack_code_name: The code name of the upstream OpenStack release (eg: Juno) Notable variable/template value changes: - rabbit_cluster_name: rpc > openstack - wsrep_cluster_name: rpc_galera_cluster > openstack_galera_cluster DocImpact Closes-Bug: #1403676 Implements: blueprint rackspace-namesake Change-Id: Ib480fdad500b03c7cb90684aa444da9946ba8032
2015-02-11 16:26:22 +00:00
'/etc', 'openstack_deploy', filename
2014-08-26 18:08:15 -05:00
),
os.path.join(
os.getcwd(), filename
)
]
if user_file is not None:
file_check.insert(0, os.path.expanduser(user_file))
for f in file_check:
if os.path.isfile(f):
return f
else:
if pass_exception is False:
raise SystemExit('No file found at: %s' % file_check)
else:
return False
def recursive_list_removal(inventory, purge_list):
for item in purge_list:
for _item in inventory:
if item == _item:
inventory.pop(inventory.index(item))
def recursive_dict_removal(inventory, purge_list):
for key, value in inventory.iteritems():
if isinstance(value, dict):
for _key, _value in value.iteritems():
if isinstance(_value, dict):
for item in purge_list:
if item in _value:
del(_value[item])
elif isinstance(_value, list):
recursive_list_removal(_value, purge_list)
elif isinstance(value, list):
recursive_list_removal(value, purge_list)
def args():
"""Setup argument Parsing."""
parser = argparse.ArgumentParser(
usage='%(prog)s',
Remove and/or rename Rackspace related bits This patch removes and/or renames anything that is Rackspace specific from the playbooks, roles and variables. It also removes items which appear to be orphaned/unused and flattens the playbooks into a single directory in order to better match ansible best practise (and remove some horrible fiddles we were doing). The following have been removed due to RAX/RPC naming or RAX/RPC specific usage: - playbooks/monitoring - playbooks/rax* - playbooks/rpc* - roles/maas* - roles/rax* - roles/rpc* - scripts/f5-* - scripts/maas* - scripts/rpc* - scripts/*lab* - vars/repo_packages/rackspace* - vars/repo_packages/rax* - vars/repo_packages/rpc* - vars/repo_packages/holland.yml The following have been removed as they are unused: - playbooks/setup/host-network-setup.yml - roles/openssl_pem_request - roles/host_interfaces - scripts/elsa* - ssh/ - vars/repo_packages/turbolift.yml The following directories have been renamed: - etc/rpc_deploy > etc/openstack_deploy - rpc_deployment > playbooks The playbooks have all been moved into a single directory: - rpc_deployment/playbooks/infrastructure/* > playbooks/ - rpc_deployment/playbooks/openstack/* > playbooks/ - rpc_deployment/playbooks/setup/* > playbooks/ The following files have been renamed: - lxc-rpc > lxc-openstack - lxc-rpc.conf > lxc-openstack.conf - rpc_environment > openstack_environment - rpc_release > openstack_release (etc and pip) - rpc_tempest_gate.sh > openstack_tempest_gate.sh - rpc_user_config > openstack_user_config The following variables have been renamed: - rpc_release > openstack_release - rpc_repo_url > openstack_repo_url The following variables have been introduced: - openstack_code_name: The code name of the upstream OpenStack release (eg: Juno) Notable variable/template value changes: - rabbit_cluster_name: rpc > openstack - wsrep_cluster_name: rpc_galera_cluster > openstack_galera_cluster DocImpact Closes-Bug: #1403676 Implements: blueprint rackspace-namesake Change-Id: Ib480fdad500b03c7cb90684aa444da9946ba8032
2015-02-11 16:26:22 +00:00
description='OpenStack Inventory Generator',
2014-08-26 18:08:15 -05:00
epilog='Inventory Generator Licensed "Apache 2.0"')
parser.add_argument(
'-f',
'--file',
help='Inventory file.',
required=True,
default=None
)
parser.add_argument(
'-s',
'--sort',
help='Sort items based on given key i.e. physical_host',
required=False,
default='component'
)
2014-08-26 18:08:15 -05:00
exclusive_action = parser.add_mutually_exclusive_group(required=True)
exclusive_action.add_argument(
'-r',
'--remove-item',
help='host name to remove from inventory, this can be used multiple'
' times.',
action='append',
default=[]
)
exclusive_action.add_argument(
'-l',
'--list-host',
help='',
action='store_true',
default=False
)
return vars(parser.parse_args())
def print_inventory(inventory, sort_key):
2014-08-26 18:08:15 -05:00
_meta_data = inventory['_meta']['hostvars']
required_list = [
'container_name',
'is_metal',
'component',
'physical_host',
'tunnel_address',
'ansible_ssh_host',
'container_types'
]
table = prettytable.PrettyTable(required_list)
for key, values in _meta_data.iteritems():
for rl in required_list:
if rl not in values:
values[rl] = None
else:
row = []
for _rl in required_list:
if _rl == 'container_name':
if values.get(_rl) is None:
values[_rl] = key
row.append(values.get(_rl))
else:
table.add_row(row)
for tbl in table.align.keys():
table.align[tbl] = 'l'
table.sortby = sort_key
2014-08-26 18:08:15 -05:00
return table
def main():
"""Run the main application."""
# Parse user args
user_args = args()
# Get the contents of the system environment json
environment_file = file_find(filename=user_args['file'])
with open(environment_file, 'rb') as f:
inventory = json.loads(f.read())
if user_args['list_host'] is True:
print(print_inventory(inventory, user_args['sort']))
2014-08-26 18:08:15 -05:00
else:
recursive_dict_removal(inventory, user_args['remove_item'])
with open(environment_file, 'wb') as f:
f.write(json.dumps(inventory, indent=2))
print('Success. . .')
if __name__ == "__main__":
main()