Skip to content

Instantly share code, notes, and snippets.

@Mudpuppy12
Created January 13, 2017 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mudpuppy12/7c5250e2639f8521d2eb072ca6a62600 to your computer and use it in GitHub Desktop.
Save Mudpuppy12/7c5250e2639f8521d2eb072ca6a62600 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
## Made to clean up duplicate entries for KiR
## Proof of concept as
##
##
## curl https://iad3.kir.kickstart.rackspace.com/dc/api/inventory/get_ids_by_server_number/605090 | python -m json.tool \
## | grep 'id' | awk '{print $2}' | tr -d '"' | tr -d ',' | while read object_id; do echo $object_id; sleep 1; \
## curl https://iad3.kir.kickstart.rackspace.com/dc/api/inventory/remove/$object_id; echo; echo; sleep 1; done
__author__ = 'denn8098'
import os, sys, warnings, argparse, requests
from distutils.util import strtobool
# HP Ilo loves to toss warnings
warnings.filterwarnings("ignore")
# Custom Libs
base_dir = os.path.abspath('..').split('rpc_lem')[0]
lib_path = os.path.abspath(os.path.join(base_dir, 'rpc_lem/lib'))
sys.path.append(lib_path)
LAB_DATA_DIR = "../dyml/yml"
if os.environ.has_key('LAB_NAME'):
LAB_NAME = os.environ['LAB_NAME'].lower()
else:
print "Need to set LAB_NAME"
sys.exit(1)
from lem import generic
lab = generic.cluster(LAB_DATA_DIR + "/" + LAB_NAME + ".yml")
lab_name = LAB_NAME.upper()
def user_yes_no_query(question):
sys.stdout.write('%s [y/n] ' % question)
while True:
try:
return strtobool(raw_input().lower())
except ValueError:
sys.stdout.write('Please respond with \'y\' or \'n\'.\n')
def args():
"""Setup argument Parsing."""
parser = argparse.ArgumentParser(
usage='%(prog)s',
description='LEM Fix KiR duplicate ID',
epilog='LEM is not Licensed "Apache 2.0"')
parser.add_argument(
'-p',
'--printdupes',
help='Print the # of duplicate KiR records',
required=False,
default=False,
action='store_true'
)
parser.add_argument(
'-s',
'--showkirid',
help='Show the kir id',
required=False,
default=False,
action='store_true'
)
parser.add_argument(
'-d',
'--deletedupes',
help='Remove duplicate / older KiR records',
required=False,
default=False,
action='store_true'
)
parser.add_argument(
'-x',
'--exclude',
help='Exclude devices',
required=False,
nargs="+",
default=[]
)
parser.add_argument(
'-i',
'--include',
help='Include only these devices, nothing else',
required=False,
nargs="+",
default=[]
)
return vars(parser.parse_args())
def return_kirid(dc,device):
kirAPI = requests.session()
if "IAD3" in dc: # IAD 3
kir_url_base = "https://iad3.kir.kickstart.rackspace.com"
if "ORD1" in dc: # ORD1
kir_url_base = "https://ord1.kir.kickstart.rackspace.com"
if "LON3" in dc: # LON3
kir_url_base = "https://lon3.kir.kickstart.rackspace.com"
if "HK1" in dc: # HK1
kir_url_base = "https://hkg1.kir.kickstart.rackspace.com"
if "SYD2" in dc: # SYD2
kir_url_base = "https://syd2.kir.kickstart.rackspace.com"
if "DFW" in dc: # DFW
kir_url_base = "https://dfw1.kir.kickstart.rackspace.com"
tmp = kirAPI.get('%s/dc/api/inventory/get_ids_by_server_number/%s' % (kir_url_base, device), verify=False).json()
return tmp
def return_dupes(dc, device):
kirAPI = requests.session()
if "IAD3" in dc: #IAD 3
kir_url_base = "https://iad3.kir.kickstart.rackspace.com"
if "ORD1" in dc: #ORD1
kir_url_base = "https://ord1.kir.kickstart.rackspace.com"
if "LON3" in dc: #LON3
kir_url_base = "https://lon3.kir.kickstart.rackspace.com"
if "HK1" in dc: #HK1
kir_url_base = "https://hkg1.kir.kickstart.rackspace.com"
if "SYD2" in dc: #SYD2
kir_url_base = "https://syd2.kir.kickstart.rackspace.com"
if "DFW" in dc: # DFW
kir_url_base = "https://dfw1.kir.kickstart.rackspace.com"
tmp = kirAPI.get('%s/dc/api/inventory/get_ids_by_server_number/%s' % (kir_url_base, device),verify=False).json()
if len(tmp['objects']) > 1:
# We have multiple objects, lets get the time created stamp for them.
# This requires another api call on the record to get this information
kir_timestamp = 0
for record in tmp['objects']:
tmp2 = kirAPI.get(record['url']).json()
record['time_created'] = tmp2['time_created']
# The valid_id would be the latest timestamp, or highest value
for record in tmp['objects']:
if record['time_created'] > kir_timestamp:
valid_id = record['id']
# Lets push everything not a valid_id into a duplicate array
duplicates = []
for record in tmp['objects']:
if record['id'] != valid_id:
duplicates.append(record)
return duplicates
else:
return []
def delete_dupes(duplicates):
# https://iad3.kir.kickstart.rackspace.com/dc/api/inventory/remove/
# https://iad3.kir.kickstart.rackspace.com/dc/api/inventory/index/5408cc77612b3563f2ebdff9
kirAPI = requests.session()
for record in duplicates:
delete_url = record['url'].split('index')[0] + 'remove/' + record['id']
result = kirAPI.get(delete_url).json()
print result
def main():
user_args = args()
if user_args['printdupes']:
for node in lab:
device = node.get('device')
if str(device) in user_args['exclude']:
continue
if user_args['include']:
if str(device) not in user_args['include']:
continue
print "%s: Has %s duplicate Kir Entries." % (node.get('device'),len(return_dupes(lab.get('dc'),node.get('device'))))
if user_args['deletedupes']:
for node in lab:
device = node.get('device')
if str(device) in user_args['exclude']:
continue
if user_args['include']:
if str(device) not in user_args['include']:
continue
duplicates = return_dupes(lab.get('dc'),node.get('device'))
if len(duplicates) > 0:
print "Deleting duplicates for node %s" % (node.get('device'))
delete_dupes(duplicates)
if user_args['showkirid']:
for node in lab:
device = node.get('device')
if str(device) in user_args['exclude']:
continue
if user_args['include']:
if str(device) not in user_args['include']:
continue
tmp = return_kirid(lab.get('dc'),node.get('device'))
print "Device #%s:" % (node.get('device'))
for record in tmp['objects']:
print "\t%s" % (record['url'])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment