Skip to content
Snippets Groups Projects
Commit a4f0dca3 authored by Shahaan Ayyub's avatar Shahaan Ayyub
Browse files

Creating dynamicInventory-mcc2 which prepares dynamic inventory across multiple tenancies...

parent cc419b34
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
import sys, os, string, socket, re
import shlex, multiprocessing, time, shutil, json
import novaclient.v1_1.client as nvclient
import novaclient.exceptions as nvexceptions
import keystoneclient.v2_0.client as ksclient
from joblib import Parallel, delayed
from multiprocessing import Process, Manager, Pool
def gatherInfo(tenantName, tenantID, userName, passwd, authUrl, inventoryList):
## Fetch the Nova Object
projectName = os.path.basename(sys.argv[0])
nc = nvclient.Client( auth_url=authUrl,
username=userName,
api_key=passwd,
project_id=tenantName,
tenant_id=tenantID,
service_type="compute"
)
inventory = {}
#inventory['_meta'] = { 'hostvars': {} }
for server in nc.servers.list():
if server.metadata and \
'ansible_host_groups' in server.metadata and \
server.metadata['project_name'] == projectName.strip():
ansible_groups = server.metadata['ansible_host_groups'].split(',')
hostname = socket.gethostbyaddr(server.networks.values()[0][0])[0]
closed=True
while closed:
hostSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if not hostSocket.connect_ex((hostname, 22)):
closed = False
break
time.sleep(5)
hostSocket.close()
# Set Ansible Host Group
for group in ansible_groups:
groupName = group.strip()
if groupName not in inventory: inventory[groupName] = []
inventory[groupName].append(hostname)
else:
continue
inventoryList.append(inventory)
#print json.dumps(inventory)
if __name__ == "__main__":
inventoryList = []
jsonInventory = {}
try:
authUrl = os.environ['OS_AUTH_URL']
userName = os.environ['OS_USERNAME']
passwd = os.environ['OS_PASSWORD']
except KeyError:
print "Env Variables not set, Please run: source <openstack rc file>"
sys.exit()
kc = ksclient.Client(auth_url=authUrl, username=userName, password=passwd)
tenancies = kc.tenants.list()
Parallel(n_jobs=len(tenancies), backend="threading")(delayed(gatherInfo)
(tenant.name, tenant.id, userName, passwd, authUrl, inventoryList)
for tenant in tenancies)
for inventory in inventoryList: jsonInventory.update(inventory)
if not jsonInventory:
print "I could not find any project called ", os.path.basename(sys.argv[0]), "in any of "
for tenancy in tenancies: print tenancy.name
print "\nYou can select a project by symlinking to it, for example if you have a project called myProject do ln -s dynamicInventory-mcc2 myProject\n and then run ./myProject"
sys.exit()
else:
print json.dumps(jsonInventory)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment