Created
December 7, 2016 00:14
-
-
Save djtecha/20710057e138d476620c94473b4cb3f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import subprocess | |
import urllib2, urllib | |
import json | |
import os | |
import sys | |
VER = {} | |
APIKEY = '<KEY>' | |
LIBRENMS = 'http://<HOST>/api/v0/devices' | |
os.environ['FACTERLIB'] = "/etc/puppetlabs/code/modules/facter/files/facter_addons" | |
FACTS = json.loads(subprocess.Popen("facter -j", stdout=subprocess.PIPE, shell=True).communicate()[0].strip()) | |
CHECK = ["apache","awscli","jdk","postfix","puppet","redfin","postgresql","ship","warranty","bios","jgit","notes","owner"] | |
HOSTNAME = FACTS['networking']['hostname'] | |
for fact in CHECK: | |
try: | |
VER[fact] = FACTS[fact] | |
except: | |
VER[fact] = "NA" | |
ENDPOINT = "%s" % HOSTNAME | |
URL = "%s/%s" % (LIBRENMS, ENDPOINT) | |
req = urllib2.Request(URL) | |
req.add_header('X-Auth-Token', APIKEY) | |
res = json.loads(urllib2.urlopen(req).read()) | |
try: | |
ID = res['devices'][0]['device_id'] | |
except: | |
print "No ID Found for %s" % HOSTNAME | |
sys.exit(1) | |
#Update Notes | |
SW = "" | |
for i in VER: | |
SW+=i + ": " + VER[i] + " " | |
NOTES = '{"field": "notes", "data": "%s"}' % SW | |
try: | |
URL = "%s/%s" % (LIBRENMS, HOSTNAME) | |
headers = {'X-Auth-Token': APIKEY } | |
patch = urllib2.Request(URL, NOTES, headers) | |
patch.get_method = lambda: 'PATCH' | |
reponse = urllib2.urlopen(patch) | |
except: | |
print "Device is Probably Not Found" | |
#Update Components | |
try: | |
ENDPOINT = "%s/components" % HOSTNAME | |
URL = "%s/%s" % (LIBRENMS, ENDPOINT) | |
req = urllib2.Request(URL) | |
req.add_header('X-Auth-Token', APIKEY) | |
res = json.loads(urllib2.urlopen(req).read()) | |
EXISTS = [] | |
for y in res['components'].keys(): | |
EXISTS.append(res['components'][y]['type']) | |
UPDATE = VER.keys() | |
for x in UPDATE: | |
if x in EXISTS: | |
for y in res['components'].keys(): | |
if res['components'][y]['type'] == x: | |
ID = y | |
BLOB = x | |
DATA = '{ "%s": { "type": "%s", "label": "%s"}}' % (ID, BLOB, VER[x]) | |
URL = "%s/%s/components" % (LIBRENMS, HOSTNAME) | |
headers = {'X-Auth-Token': APIKEY } | |
put = urllib2.Request(URL, DATA, headers) | |
put.get_method = lambda: 'PUT' | |
reponse = urllib2.urlopen(put) | |
else: | |
DATA = '' | |
URL = "%s/%s/components/%s" % (LIBRENMS, HOSTNAME, x) | |
headers = {'X-Auth-Token': APIKEY } | |
post = urllib2.Request(URL, DATA, headers) | |
post.get_method = lambda: "POST" | |
reponse = urllib2.urlopen(post) | |
except: | |
UPDATE = VER.keys() | |
for x in UPDATE: | |
DATA = '' | |
URL = "%s/%s/components/%s" % (LIBRENMS, HOSTNAME, x) | |
headers = {'X-Auth-Token': APIKEY } | |
post = urllib2.Request(URL, DATA, headers) | |
post.get_method = lambda: "POST" | |
reponse = urllib2.urlopen(post) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment