#!/usr/bin/python

# Author: Robert Schweikert
# License: GPL v2
# Copyright: SUSE

import glob
import os
import sys
import syslog

# Only one interface should be configured at this point
# check that this is indeed the case
confFiles = glob.glob('/etc/sysconfig/network/ifcfg-eth*')
numConfInterf = len(confFiles)
if numConfInterf > 1:
	syslog.openlog('[BR_SETUP]', syslog.LOG_PID, syslog.LOG_DAEMON)
	syslog.syslog(syslog.LOG_INFO, 'Bridge setup\n')
	msg = 'Multiple configured eth devices found, cannot resolve ambiguity\n'
	syslog.syslog(syslog.LOG_ERR, msg)
	syslog.closelog()
	sys.exit(1)

if numConfInterf < 1:
	syslog.openlog('[BR_SETUP]', syslog.LOG_PID, syslog.LOG_DAEMON)
	syslog.syslog(syslog.LOG_INFO, 'Bridge setup\n')
	msg = 'No eth interface configured, cannot setup bridge\n'
	syslog.syslog(syslog.LOG_ERR, msg)
	syslog.closelog()
	sys.exit(1)

# Get the configured IP address (includes the subnet mask)
interfConf = confFiles[0]
interfaceName = interfConf.split('-')[-1]
lines = open(interfConf, 'r').readlines()
ipAddr = None
for ln in lines:
	if ln.find('IPADDR') != -1:
		ipAddr = ln.strip().split('=')[-1]
		break

# Stop the running network services
os.system('/etc/init.d/dhcpd stop >& /dev/null')
os.system('/etc/init.d/nfsserver stop >& /dev/null')
os.system('/sbin/ifdown %s >& /dev/null' %interfaceName)

# Write the bridge configuration
brCfg = open('/etc/sysconfig/network/ifcfg-br0', 'w')
brCfg.write("BOOTPROTO='static'\n")
brCfg.write("BRIDGE='yes'\n")
brCfg.write("BRIDGE_PORTS='%s'\n" %interfaceName)
brCfg.write("BRIDGE_STP='off'\n")
brCfg.write("BRIDGE_FORWARDDELAY=0\n")
brCfg.write("BROADCAST=''\n")
brCfg.write("ETHTOOL_OPTIONS=''\n")
brCfg.write("IPADDR=%s\n" %ipAddr)
brCfg.write("MTU=''\n")
brCfg.write("NAME=''\n")
brCfg.write("NETWORK=''\n")
brCfg.write("REMOTE_IPADDR=''\n")
brCfg.write("STARTMODE='auto'\n")
brCfg.write("USERCONTROL='no'\n")
brCfg.write("IPADDR_0='169.254.1.1/24'\n")
brCfg.write("LABEL_0='serviceport'\n")
brCfg.close()

# Server interface update
lines = open('/etc/sysconfig/dhcpd', 'r').readlines()
conf = open('/etc/sysconfig/dhcpd', 'w')
for ln in lines:
	if ln.find('DHCPD_INTERFACE') != -1:
		conf.write('DHCPD_INTERFACE="br0"\n')
		continue
	conf.write(ln)
conf.close()

# Remove the old network configuration file
os.remove(interfConf)

# Fix up the IP address in the sunstone configuration file
ctx = open('/etc/one/sunstone-server.conf', 'r').read()
os.rename('/etc/one/sunstone-server.conf', '/etc/one/sunstone-server.conf.save')
fout = open('/etc/one/sunstone-server.conf', 'w')
fout.write(ctx.replace('127.0.0.1', ipAddr.split("'")[1].split("/")[0]))
fout.close()

# Run the configuration setup
os.system('/sbin/SuSEconfig >& /dev/null')

# Insert the cloud information service and start it
os.system('/sbin/insserv cloudinfo')
# Insert the registration service and start it
os.system('/sbin/insserv noderegistrar')
# Proper permissions for the authentication file
os.system('chown oneadmin:cloud /var/lib/one/.one/one_auth')

if not os.path.exists('/var/lock/one'):
    os.mkdir('/var/lock/one')

os.system('/bin/chown -R oneadmin:cloud /var/lock/one')

# Start the cloud service
# oned must be started with special environment setting the first time
os.system('env ONE_AUTH=/var/lib/one/.one/one_auth /etc/init.d/one start >& /dev/null')
os.system('/sbin/insserv one')
os.system('/etc/init.d/one stop >& /dev/null')
# Insert the Web-UI server
os.system('/sbin/insserv sunstone')

