diff options
author | Ken Raffenetti <raffenet@mcs.anl.gov> | 2006-10-10 15:06:52 +0000 |
---|---|---|
committer | Ken Raffenetti <raffenet@mcs.anl.gov> | 2006-10-10 15:06:52 +0000 |
commit | 63c1fd4b9076538d77137e159a4c07460dd635e2 (patch) | |
tree | a5283e497e5f12370bc08d5578d7a0f3a262670d | |
parent | 9c4c7093ffabcfc0f41c5da6f1d68ef134d5f747 (diff) | |
download | bcfg2-63c1fd4b9076538d77137e159a4c07460dd635e2.tar.gz bcfg2-63c1fd4b9076538d77137e159a4c07460dd635e2.tar.bz2 bcfg2-63c1fd4b9076538d77137e159a4c07460dd635e2.zip |
added batch add script
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@2424 ce84e21b-d406-0410-9b95-82705330c041
-rwxr-xr-x | tools/batchadd.py | 148 | ||||
-rwxr-xr-x | tools/hostinfo.py | 43 |
2 files changed, 169 insertions, 22 deletions
diff --git a/tools/batchadd.py b/tools/batchadd.py new file mode 100755 index 000000000..728c5780d --- /dev/null +++ b/tools/batchadd.py @@ -0,0 +1,148 @@ +#!/usr/bin/python + +import sys, os +from datetime import date +os.environ['DJANGO_SETTINGS_MODULE'] = 'Hostbase.settings' +from Hostbase.hostbase.models import * +from Hostbase.settings import DEFAULT_MX, PRIORITY + +host_attribs = ['hostname', 'whatami', 'netgroup', 'security_class', 'support', + 'csi', 'printq', 'dhcp', 'outbound_smtp', 'primary_user', + 'administrator', 'location', 'expiration_date', 'comments'] + +def checkformat(values): + filelist = [] + for pair in values: + filelist.append(pair[0]) + + lines = len(filelist) + + while True: + if filelist and not filelist[0:14] == host_attribs: + # figure out what to do here + return 0 + sys.exit() + elif not filelist: + return 1 + else: + filelist = filelist[14:] + while True: + if filelist and filelist[0] == 'mac_addr': + filelist.pop(0) + if filelist and filelist[0] == 'hdwr_type': + filelist.pop(0) + while filelist and filelist[0] == 'ip_addr': + filelist.pop(0) + while filelist and filelist[0] == 'cname': + filelist.pop(0) + + if (filelist and filelist[0] == 'hostname') or not filelist: + break + +# argument handling for batchadd +try: + fd = open(sys.argv[1], 'r') +except (IndexError, IOError): + print "\nUsage: batchadd.py filename\n" + sys.exit() + +lines = fd.readlines() +info = [] +for line in lines: + if not line.lstrip(' ')[0] == '#' and not line == '\n': + info.append(line.split("->")) + +for x in range(0,len(info)): + if len(info[x]) > 1: + info[x][0] = info[x][0].strip() + info[x][1] = info[x][1].strip() + else: + print "Error: file format" + +if info[0][0] == 'mx' and info[1][0] == 'priority': + mx, created = MX.objects.get_or_create(mx=info[0][1], priority=info[1][1]) + try: + info.pop(0) + info.pop(0) + except: + print "Error: file format" + sys.exit() +else: + mx, created = MX.objects.get_or_create(mx=DEFAULT_MX, priority=PRIORITY) +if created: + mx.save() + +if not checkformat(info): + print "Error: file format" + sys.exit() + +while True: + blank = Host() + for attrib in host_attribs: + try: + pair = info.pop(0) + except: + sys.exit() + if pair[0] == 'dhcp' or pair[0] == 'outbound_smtp': + if pair[1] == 'y': + blank.__dict__[pair[0]] = 1 + else: + blank.__dict__[pair[0]] = 0 + elif pair[0] == 'expiration_date': + (year, month, day) = pair[1].split("-") + blank.expiration_date = date(int(year), int(month), int(day)) + elif pair[0] in host_attribs: + blank.__dict__[pair[0]] = pair[1] + + try: + host = Host.objects.get(hostname=blank.hostname) + print "Error: %s already exists in hostbase" % blank.hostname + sys.exit() + except: + pass + # do something here + blank.status = 'active' + blank.save() + newhostname = blank.hostname.split(".")[0] + newdomain = blank.hostname.split(".", 1)[1] + while True: + try: + info[0] + except: + sys.exit() + if info[0][0] == 'mac_addr': + pair = info.pop(0) + try: + Interface.get(mac_addr=pair[1]) + print "Error: %s already exists" % inter.mac_addr + sys.exit() + except: + inter = Interface.objects.create(host=blank, mac_addr=pair[1], hdwr_type='eth') + inter.save() + elif info[0][0] == 'hdwr_type': + pair = info.pop(0) + inter.hdwr_type = pair[1] + inter.save() + elif info[0][0] == 'ip_addr': + pair = info.pop(0) + ip = IP.objects.create(interface=inter, ip_addr=pair[1], num=1) + hostnamenode = Name(ip=ip, name=blank.hostname, dns_view='global', only=False) + hostnamenode.save() + namenode = Name(ip=ip, name=".".join([newhostname + "-" + inter.hdwr_type, + newdomain]), + dns_view="global", only=False) + namenode.save() + subnetnode = Name(ip=ip, name=newhostname + "-" + + ip.ip_addr.split(".")[2] + "." + + newdomain, dns_view="global", only=False) + subnetnode.save() + hostnamenode.mxs.add(mx) + namenode.mxs.add(mx) + subnetnode.mxs.add(mx) + elif info[0][0] == 'cname': + pair = info.pop(0) + cname = CName.objects.create(name=hostnamenode, cname=pair[1]) + cname.save() + else: + break + diff --git a/tools/hostinfo.py b/tools/hostinfo.py index d960354d9..3418f0c76 100755 --- a/tools/hostinfo.py +++ b/tools/hostinfo.py @@ -7,7 +7,6 @@ from django.db import connection from os import system import sys -xmldirectory = "./xmldata/" logic_ops = ["and", "or"] host_attribs = ["hostname", "whatami", "netgroup", "security_class", "support", "csi", "memory", "printq", "dhcp", "outbound_smtp", @@ -102,13 +101,13 @@ try: queryoptions += result[0] if ('--summary', '') in opts: fields = "h.hostname, h.whatami, h.location, h.primary_user" - query = """SELECT DISTINCT %s FROM (((((dbconvert_host h - INNER JOIN dbconvert_interface i ON h.id = i.host_id) - INNER JOIN dbconvert_ip p ON i.id = p.interface_id) - INNER JOIN dbconvert_name n ON p.id = n.ip_id) - INNER JOIN dbconvert_name_mxs x ON x.name_id = n.id) - INNER JOIN dbconvert_mx m ON m.id = x.mx_id) - LEFT JOIN dbconvert_cname c ON n.id = c.name_id + query = """SELECT DISTINCT %s FROM (((((hostbase_host h + INNER JOIN hostbase_interface i ON h.id = i.host_id) + INNER JOIN hostbase_ip p ON i.id = p.interface_id) + INNER JOIN hostbase_name n ON p.id = n.ip_id) + INNER JOIN hostbase_name_mxs x ON x.name_id = n.id) + INNER JOIN hostbase_mx m ON m.id = x.mx_id) + LEFT JOIN hostbase_cname c ON n.id = c.name_id WHERE %s ORDER BY h.hostname """ % (fields, queryoptions) cursor.execute(query) @@ -128,13 +127,13 @@ try: if not fields: print "No valid fields were entered. exiting..." sys.exit() - query = """SELECT DISTINCT %s FROM (((((dbconvert_host h - INNER JOIN dbconvert_interface i ON h.id = i.host_id) - INNER JOIN dbconvert_ip p ON i.id = p.interface_id) - INNER JOIN dbconvert_name n ON p.id = n.ip_id) - INNER JOIN dbconvert_name_mxs x ON x.name_id = n.id) - INNER JOIN dbconvert_mx m ON m.id = x.mx_id) - LEFT JOIN dbconvert_cname c ON n.id = c.name_id + query = """SELECT DISTINCT %s FROM (((((hostbase_host h + INNER JOIN hostbase_interface i ON h.id = i.host_id) + INNER JOIN hostbase_ip p ON i.id = p.interface_id) + INNER JOIN hostbase_name n ON p.id = n.ip_id) + INNER JOIN hostbase_name_mxs x ON x.name_id = n.id) + INNER JOIN hostbase_mx m ON m.id = x.mx_id) + LEFT JOIN hostbase_cname c ON n.id = c.name_id WHERE %s ORDER BY h.hostname """ % (fields, queryoptions) @@ -152,13 +151,13 @@ try: last = host print '' else: - basequery = """SELECT DISTINCT h.hostname FROM (((((dbconvert_host h - INNER JOIN dbconvert_interface i ON h.id = i.host_id) - INNER JOIN dbconvert_ip p ON i.id = p.interface_id) - INNER JOIN dbconvert_name n ON p.id = n.ip_id) - INNER JOIN dbconvert_name_mxs x ON x.name_id = n.id) - INNER JOIN dbconvert_mx m ON m.id = x.mx_id) - LEFT JOIN dbconvert_cname c ON n.id = c.name_id + basequery = """SELECT DISTINCT h.hostname FROM (((((hostbase_host h + INNER JOIN hostbase_interface i ON h.id = i.host_id) + INNER JOIN hostbase_ip p ON i.id = p.interface_id) + INNER JOIN hostbase_name n ON p.id = n.ip_id) + INNER JOIN hostbase_name_mxs x ON x.name_id = n.id) + INNER JOIN hostbase_mx m ON m.id = x.mx_id) + LEFT JOIN hostbase_cname c ON n.id = c.name_id WHERE """ cursor.execute(basequery + queryoptions + " ORDER BY h.hostname") |