# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.    
#
#  $Id$
#

import os 
import sys
import string
import datetime
import subprocess
import MySQLdb
import traceback

from util import logit
from bootmanagementinterface import BootManagementInterface

class Pxe(BootManagementInterface):
	def __init__(self, config, verbose=None):
		self.verbose  = verbose
		self.host = config['dbHost']
		self.user = config['dbUser']
		self.passwd = config['dbPassword']
		self.db = config['dbInst']
		
		self.tftpRootDir = config['tftpRootDir']
		self.tftpImageDir = config['tftpImageDir']
		self.tftpBootOptionsDir = config['tftpBootOptionsDir']
		self.tftpUpdateFile = config['tftpUpdateFile']
		self.tftpBaseFile = config['tftpBaseFile']
		self.tftpBaseMenuFile = config['tftpBaseMenuFile']

		self.logFile = config['logFile']

		if config['dbPort'] == "":
			config['dbPort'] = 3306

		self.port = config['dbPort']

		self.vlan_max = config['vlan_max']
		self.vlan_reserved = config['vlan_reserved']
		
		#  Connect to DB
		self.conn = MySQLdb.connect(host = self.host, port = self.port, user = self.user, passwd = self.passwd, db = self.db)
		#cursor.execute ("SELECT VERSION()")
		#print "server version:", row[0]
		#mysql -Dirp-cluster -hrodimus -u reader -e "select * from hostinfo;"


		

	'''  This will create the update file tftpUpdateFile used to generate all the pxe boot files
		 pass in a list of available images	
	'''
	def createPxeUpdateFile (self, images):
		try: 
			f = open(self.tftpUpdateFile, "w")
		except Exception:
			traceback.print_exc(sys.exc_info())

		dadate = datetime.datetime.now().strftime("%Y%m%d.%H%M.%S")
		val = "#  Generated by PRS : " + dadate
		f.write(val)
		for image in images:
			val = "\n#  IMAGE " + image + "\n"
			f.write(val)
			base = "cat " + self.tftpBaseFile + " | sed 's/MAGIC1/" + image + "/' > " + self.tftpBootOptionsDir + "/" + image + "\n"
			basemenu= "cat " + self.tftpBaseMenuFile + " | sed 's/LABEL " + image + "/LABEL " + image + "\\n\\tMENU DEFAULT/' > " + self.tftpBootOptionsDir + "/" + image + "-menu\n" 
			f.write(base)
			f.write(basemenu)
		f.close()
		

	def updatePxe(self):
		cmd = "chmod 755 " + self.tftpUpdateFile
		try:
			os.system(cmd)
		except Exception:
			traceback.print_exc(sys.exc_info())

		cmd = self.tftpUpdateFile
		try:
			os.system(cmd)
		except Exception:
			traceback.print_exc(sys.exc_info())
		
