#!/usr/bin/env python

#####################################################
#
# initialize.py: initializes the database and
# dhcp config based on the network config 
# file in ../etc/network.conf.  If this file
# is not present, please create it with the  
# following format:
#
# #hostname ipaddress macaddress
# vm001 192.168.0.1 AA:AA:AA:BB:CC:DD
#
# make sure there are no jobs running when you run
# this script,  as it could cause them to fail.
################################################# 

import os
import ConfigParser
try:
    import sqlite3
except:
    from pysqlite2 import dbapi2 as sqlite3

config = ConfigParser.ConfigParser()
config.read('/etc/torque-kvm.conf')

#some parameters for your network:
subnetmask=config.get("net","subnetmask")
gateway=config.get("net","gateway")
broadcast=config.get("net","broadcast")
dnsserver=config.get("net","dnsserver")

# other parameters
torque_home=config.get("torque","torque_home")
dbfile=torque_home + "/net/network.db"
netfile=torque_home + "/net/network.conf"
dhcpfile=torque_home + "/net/dhcpd.entries"




#delete old files
try:
	os.remove(dbfile)
	os.remove(dhcpfile)
except OSError:
	pass

#create the database
conn = sqlite3.connect(dbfile)
c = conn.cursor()
c.execute('''create table reservations
(hostname text, ipaddr text, macaddr text, inuse integer)''')

outfile = open(dhcpfile,'w')

lines = open(netfile, 'r').readlines()
for line in lines:
	if line.startswith('#'):
		continue
	(hostname, ipaddr, macaddr) = line.rstrip().split(' ')

	#database insertion
	c.execute('insert into reservations values (?,?,?,?)', (hostname, ipaddr, macaddr, 0) )

	#dhcpd.conf include file 
	outfile.write('host '+hostname+" {\n")
	outfile.write(' hardware ethernet '+macaddr+";\n")
	outfile.write(' fixed-address '+ipaddr+";\n")
	outfile.write(' option host-name "'+hostname+"\";\n")
	outfile.write(' option subnet-mask '+subnetmask+";\n")
	outfile.write(' option routers '+gateway+";\n");
	outfile.write(' option broadcast-address '+broadcast+";\n")
	outfile.write(' option domain-name-servers '+dnsserver+";\n")
	outfile.write("}\n")	

conn.commit()
c.close()
outfile.close()

print "database initialized."




