summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorKen Raffenetti <raffenet@mcs.anl.gov>2006-10-13 16:49:41 +0000
committerKen Raffenetti <raffenet@mcs.anl.gov>2006-10-13 16:49:41 +0000
commit6794db04a229153ceefdfb3d3b99ed6e691d5df6 (patch)
tree92d4652d6cc02dffd6691982621ae54086bee2dd /tools
parenta29930d4e75ddce4c0ae65ca890924f9672e3026 (diff)
downloadbcfg2-6794db04a229153ceefdfb3d3b99ed6e691d5df6.tar.gz
bcfg2-6794db04a229153ceefdfb3d3b99ed6e691d5df6.tar.bz2
bcfg2-6794db04a229153ceefdfb3d3b99ed6e691d5df6.zip
adding functionality to hostbase command line tools
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@2432 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'tools')
-rwxr-xr-xtools/batchadd.py230
-rwxr-xr-xtools/hostbase.py65
-rwxr-xr-xtools/hostinfo.py4
3 files changed, 178 insertions, 121 deletions
diff --git a/tools/batchadd.py b/tools/batchadd.py
index 728c5780d..77817f9f2 100755
--- a/tools/batchadd.py
+++ b/tools/batchadd.py
@@ -5,144 +5,136 @@ from datetime import date
os.environ['DJANGO_SETTINGS_MODULE'] = 'Hostbase.settings'
from Hostbase.hostbase.models import *
from Hostbase.settings import DEFAULT_MX, PRIORITY
+import Hostbase.regex
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])
+def handle_error(field):
+ if '-f' in sys.argv:
+ return
+ print "Error: %s is already defined in hostbase" % field
+ if '-s' in sys.argv:
+ sys.exit(1)
- lines = len(filelist)
+def checkformat(values, indices):
+ """Ensures file contains all necessary attributes in order """
+ filelist = [pair[0] for pair in values]
- while True:
- if filelist and not filelist[0:14] == host_attribs:
+# lines = len(filelist)
+
+ for index in indices:
+ filelist = filelist[index:]
+ if filelist[0:14] != host_attribs:
# figure out what to do here
- return 0
- sys.exit()
- elif not filelist:
- return 1
+ return False
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("->"))
+ # process rest of host attributes
+ try:
+ next = filelist[1:].index('hostname')
+ remaining = filelist[14:next+1]
+ except:
+ remaining = filelist[14:]
+ needfields = ['mac_addr', 'hdwr_type', 'ip_addr']
+ if [item for item in needfields if item not in remaining]:
+ return False
+ return True
+
+
+if __name__ == '__main__':
+
+ # 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()
+ # splits and strips lines into (attribute, value)
+ info = [[item.strip() for item in line.split("->")] for line in lines
+ if line.lstrip(' ')[0] != '#' and line != '\n']
+
+ 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])
+ info = info[2:]
-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"
+ mx, created = MX.objects.get_or_create(mx=DEFAULT_MX, priority=PRIORITY)
+ if created:
+ mx.save()
-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:
+ hostindices = [num for num in range(0, len(info)) if info[num][0] == 'hostname']
+
+ if not checkformat(info, hostindices):
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:
+
+#################
+
+ for host in hostindices:
try:
- pair = info.pop(0)
+ host = Host.objects.get(hostname=info[host][1])
+ handle_error(info[host][1])
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]
+ # do something here
+ pass
- 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:
+ macindices = [num for num in range(0, len(info)) if info[num][0] == 'mac_addr']
+ for mac_addr in macindices:
try:
- info[0]
+ host = Interface.objects.get(mac_addr=info[mac_addr][1])
+ handle_error(info[mac_addr][1])
except:
- sys.exit()
- if info[0][0] == 'mac_addr':
+ # do something here
+ pass
+
+ for host in hostindices:
+ blank = Host()
+ for attrib in host_attribs:
pair = info.pop(0)
- try:
- Interface.get(mac_addr=pair[1])
- print "Error: %s already exists" % inter.mac_addr
- sys.exit()
- except:
+ 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))
+ else:
+ blank.__dict__[pair[0]] = pair[1]
+ blank.status = 'active'
+ blank.save()
+ newhostname = blank.hostname.split(".")[0]
+ newdomain = blank.hostname.split(".", 1)[1]
+ while info and info[0][0] != 'hostname':
+ if info[0][0] == 'mac_addr':
+ pair = info.pop(0)
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
-
+ 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()
+
diff --git a/tools/hostbase.py b/tools/hostbase.py
new file mode 100755
index 000000000..c25be3370
--- /dev/null
+++ b/tools/hostbase.py
@@ -0,0 +1,65 @@
+#!/usr/bin/python
+import sys, os
+os.environ['DJANGO_SETTINGS_MODULE'] = 'Hostbase.settings'
+from Hostbase.hostbase.models import Host, Interface
+from getopt import getopt, GetoptError
+from re import split
+
+attribs = ['hostname', 'whatami', 'netgroup', 'security_class', 'support',
+ 'csi', 'printq', 'dhcp', 'outbound_smtp', 'primary_user',
+ 'administrator', 'location', 'expiration_date', 'comments',
+ 'status', 'last']
+
+already_exists = None
+#here's my attempt at making the command line idiot proof
+#you must supply and arugument and hostname for hostbase.py to run
+try:
+ (opts, args) = getopt(sys.argv[1:],'l:c:')
+ sys.argv[1]
+ if len(split("\.", opts[0][1])) == 1:
+ hosttouse = opts[0][1] + ".mcs.anl.gov"
+ else:
+ hosttouse = opts[0][1]
+except (GetoptError, IndexError):
+ print "\nUsage: hostbase.py -flag (hostname)\n"
+ print "Flags:"
+ print "\t-l look (hostname)\n"
+# print "\t-c copy (hostname)\n"
+ sys.exit()
+
+try:
+ host = Host.objects.get(hostname=hosttouse)
+except:
+ print "Error: host %s not in hostbase" % hosttouse
+ sys.exit(1)
+interfaces = []
+for interface in host.interface_set.all():
+ interfaces.append([interface, interface.ip_set.all()])
+hostinfo = "\n"
+for attrib in attribs:
+ if not (opts[0][0] == '-c' and attrib in ['status', 'last']):
+ if attrib == 'dhcp' or attrib == 'outbound_smtp':
+ if host.__dict__[attrib]:
+ hostinfo += "%-32s-> %s\n" % (attrib, 'y')
+ else:
+ hostinfo += "%-32s-> %s\n" % (attrib, 'n')
+ else:
+ hostinfo += "%-32s-> %s\n" % (attrib, host.__dict__[attrib])
+for interface in interfaces:
+ hostinfo += "\n%-32s-> %s\n" % ('mac_addr', interface[0].mac_addr)
+ hostinfo += "%-32s-> %s\n" % ('hdwr_type', interface[0].hdwr_type)
+ for ip in interface[1]:
+ hostinfo += "%-32s-> %s\n" % ('ip_addr', ip.ip_addr)
+
+if opts[0][0] == '-l':
+ """Displays general host information"""
+ print hostinfo
+
+if opts[0][0] == '-c':
+ """Provides pre-filled template to copy a host record"""
+ fd = open('/tmp/hostbase.%s.tmp' % host.id, 'w')
+ fd.write(hostinfo)
+ fd.close()
+ os.system('vi + /tmp/hostbase.%s.tmp' % host.id)
+ os.system('batchadd.py /tmp/hostbase.%s.tmp' % host.id)
+
diff --git a/tools/hostinfo.py b/tools/hostinfo.py
index 3418f0c76..d2b3628a3 100755
--- a/tools/hostinfo.py
+++ b/tools/hostinfo.py
@@ -1,10 +1,10 @@
#!/usr/bin/python
"""Hostinfo queries the hostbase database according to user-defined data"""
-__revision__ = '0.1'
+from os import system, environ
+environ['DJANGO_SETTINGS_MODULE'] = 'Hostbase.settings'
from getopt import gnu_getopt, GetoptError
from django.db import connection
-from os import system
import sys
logic_ops = ["and", "or"]