You are viewing a plain text version of this content. The canonical link for it is here.
Posted to tashi-commits@incubator.apache.org by st...@apache.org on 2012/02/08 04:44:11 UTC
svn commit: r1241773 [2/3] - in /incubator/tashi/branches/oldstable: ./ etc/
src/ src/tashi/ src/tashi/agents/ src/tashi/client/
src/tashi/clustermanager/ src/tashi/clustermanager/data/
src/tashi/nodemanager/ src/tashi/nodemanager/vmcontrol/ src/tashi/...
Modified: incubator/tashi/branches/oldstable/src/tashi/rpycservices/rpycservices.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/oldstable/src/tashi/rpycservices/rpycservices.py?rev=1241773&r1=1241772&r2=1241773&view=diff
==============================================================================
--- incubator/tashi/branches/oldstable/src/tashi/rpycservices/rpycservices.py (original)
+++ incubator/tashi/branches/oldstable/src/tashi/rpycservices/rpycservices.py Wed Feb 8 04:44:09 2012
@@ -19,8 +19,8 @@ import rpyc
from tashi.rpycservices.rpyctypes import *
import cPickle
-clusterManagerRPCs = ['createVm', 'shutdownVm', 'destroyVm', 'suspendVm', 'resumeVm', 'migrateVm', 'pauseVm', 'unpauseVm', 'getHosts', 'getNetworks', 'getUsers', 'getInstances', 'vmmSpecificCall', 'registerNodeManager', 'vmUpdate', 'activateVm']
-nodeManagerRPCs = ['instantiateVm', 'shutdownVm', 'destroyVm', 'suspendVm', 'resumeVm', 'prepReceiveVm', 'prepSourceVm', 'migrateVm', 'receiveVm', 'pauseVm', 'unpauseVm', 'getVmInfo', 'listVms', 'vmmSpecificCall', 'getHostInfo']
+clusterManagerRPCs = ['createVm', 'shutdownVm', 'destroyVm', 'suspendVm', 'resumeVm', 'migrateVm', 'pauseVm', 'unpauseVm', 'getHosts', 'getNetworks', 'getUsers', 'getInstances', 'vmmSpecificCall', 'registerNodeManager', 'vmUpdate', 'activateVm', 'registerHost', 'getImages', 'copyImage']
+nodeManagerRPCs = ['instantiateVm', 'shutdownVm', 'destroyVm', 'suspendVm', 'resumeVm', 'prepReceiveVm', 'prepSourceVm', 'migrateVm', 'receiveVm', 'pauseVm', 'unpauseVm', 'getVmInfo', 'listVms', 'vmmSpecificCall', 'getHostInfo', 'liveCheck']
def clean(args):
"""Cleans the object so cPickle can be used."""
@@ -43,7 +43,7 @@ def clean(args):
class client:
def __init__(self, host, port, username=None, password=None):
- """Client for ManagerService. If username and password are provided, rpyc.tls_connect will be used to connect, else rpyc.connect will be used."""
+ """Client for ManagerService. If username and password are provided, rpyc.tlslite_connect will be used to connect, else rpyc.connect will be used."""
self.host = host
self.port = int(port)
self.username = username
@@ -53,7 +53,7 @@ class client:
def createConn(self):
"""Creates a rpyc connection."""
if self.username != None and self.password != None:
- return rpyc.tls_connect(host=self.host, port=self.port, username=self.username, password=self.password)
+ return rpyc.tlslite_connect(host=self.host, port=self.port, username=self.username, password=self.password)
else:
return rpyc.connect(host=self.host, port=self.port)
Modified: incubator/tashi/branches/oldstable/src/tashi/rpycservices/rpyctypes.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/oldstable/src/tashi/rpycservices/rpyctypes.py?rev=1241773&r1=1241772&r2=1241773&view=diff
==============================================================================
--- incubator/tashi/branches/oldstable/src/tashi/rpycservices/rpyctypes.py (original)
+++ incubator/tashi/branches/oldstable/src/tashi/rpycservices/rpyctypes.py Wed Feb 8 04:44:09 2012
@@ -143,6 +143,40 @@ class Network(object):
def __ne__(self, other):
return not (self == other)
+class LocalImages(object):
+ def __init__(self, d=None):
+ self.id = None
+ self.userId = None
+ self.imageName = None
+ self.imageSize = None
+ self.isPublic = None
+ self.explicitUserIds = None
+ if isinstance(d, dict):
+ if 'id' in d:
+ self.id = d['id']
+ if 'userId' in d:
+ self.userId = d['userId']
+ if 'imageName' in d:
+ self.imageName = d['imageName']
+ if 'imageSize' in d:
+ self.imageSize = d['imageSize']
+ if 'isPublic' in d:
+ self.isPublic = d['isPublic']
+ if 'explicitUserIds' in d:
+ self.explicitUserIds = d['explicitUserIds']
+
+ def __str__(self):
+ return str(self.__dict__)
+
+ def __repr__(self):
+ return repr(self.__dict__)
+
+ def __eq__(self, other):
+ return isinstance(other, self.__class__) and self.__dict__ == other.__dict__
+
+ def __ne__(self, other):
+ return not (self == other)
+
class User(object):
def __init__(self, d=None):
self.id = None
Modified: incubator/tashi/branches/oldstable/src/tashi/util.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/oldstable/src/tashi/util.py?rev=1241773&r1=1241772&r2=1241773&view=diff
==============================================================================
--- incubator/tashi/branches/oldstable/src/tashi/util.py (original)
+++ incubator/tashi/branches/oldstable/src/tashi/util.py Wed Feb 8 04:44:09 2012
@@ -289,5 +289,14 @@ def enumToStringDict(cls):
d[cls.__dict__[i]] = i
return d
+def humanReadable(size):
+ suffixes = [("B",2**10), ("K",2**20), ("M",2**30), ("G",2**40), ("T",2**50)]
+ for suf, lim in suffixes:
+ if size > lim:
+ continue
+ else:
+ return round(size/float(lim/2**10),2).__str__()+suf
+
+
vmStates = enumToStringDict(InstanceState)
hostStates = enumToStringDict(HostState)
Modified: incubator/tashi/branches/oldstable/src/tashi/version.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/oldstable/src/tashi/version.py?rev=1241773&r1=1241772&r2=1241773&view=diff
==============================================================================
--- incubator/tashi/branches/oldstable/src/tashi/version.py (original)
+++ incubator/tashi/branches/oldstable/src/tashi/version.py Wed Feb 8 04:44:09 2012
@@ -15,4 +15,4 @@
# specific language governing permissions and limitations
# under the License.
-version = "201104"
+version = "201111"
Propchange: incubator/tashi/branches/oldstable/src/zoni/Makefile
('svn:mergeinfo' removed)
Propchange: incubator/tashi/branches/oldstable/src/zoni/__init__.py
('svn:mergeinfo' removed)
Modified: incubator/tashi/branches/oldstable/src/zoni/agents/dhcpdns.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/oldstable/src/zoni/agents/dhcpdns.py?rev=1241773&r1=1241772&r2=1241773&view=diff
==============================================================================
--- incubator/tashi/branches/oldstable/src/zoni/agents/dhcpdns.py (original)
+++ incubator/tashi/branches/oldstable/src/zoni/agents/dhcpdns.py Wed Feb 8 04:44:09 2012
@@ -155,7 +155,8 @@ class DhcpDns():
stdin.write("update delete %s.%s A\n" % (name, self.dnsDomain))
stdin.write("\n")
if (self.reverseDns):
- ip = socket.gethostbyname(name)
+ hostInfo = socket.gethostbyaddr(name)
+ ip = hostInfo[2][0]
ipSegments = map(int, ip.split("."))
ipSegments.reverse()
reverseIpStr = ("%d.%d.%d.%d.in-addr.arpa" % (ipSegments[0], ipSegments[1], ipSegments[2], ipSegments[3]))
@@ -175,15 +176,20 @@ class DhcpDns():
(pid, status) = os.waitpid(child.pid, os.WNOHANG)
def addCname(self, name, origName):
+
cmd = "nsupdate"
child = subprocess.Popen(args=cmd.split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE)
try:
# Check for existance of hostname
- ip = socket.gethostbyname(origName)
+ #ip = socket.gethostbyname(origName)
+ # add this to make sure we always input the textual name instead of the ip address by mistake
+ hostInfo = socket.gethostbyaddr(origName)
+ hostName = hostInfo[0].split(".")[0]
+
(stdin, stdout) = (child.stdin, child.stdout)
stdin.write("server %s\n" % (self.dnsServer))
stdin.write("key %s %s\n" % (self.dnsKeyName, self.dnsSecretKey))
- stdin.write("update add %s.%s %d CNAME %s.%s\n" % (name, self.dnsDomain, self.dnsExpire, origName, self.dnsDomain))
+ stdin.write("update add %s.%s %d CNAME %s.%s\n" % (name, self.dnsDomain, self.dnsExpire, hostName, self.dnsDomain))
stdin.write("\n")
stdin.close()
output = stdout.read()
Propchange: incubator/tashi/branches/oldstable/src/zoni/agents/dhcpdns.py
('svn:mergeinfo' removed)
Propchange: incubator/tashi/branches/oldstable/src/zoni/bootstrap/bootstrapinterface.py
('svn:mergeinfo' removed)
Modified: incubator/tashi/branches/oldstable/src/zoni/bootstrap/pxe.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/oldstable/src/zoni/bootstrap/pxe.py?rev=1241773&r1=1241772&r2=1241773&view=diff
==============================================================================
--- incubator/tashi/branches/oldstable/src/zoni/bootstrap/pxe.py (original)
+++ incubator/tashi/branches/oldstable/src/zoni/bootstrap/pxe.py Wed Feb 8 04:44:09 2012
@@ -25,12 +25,13 @@ import datetime
import subprocess
import MySQLdb
import traceback
+import logging
-from zoni.extra.util import logit
+from zoni.extra.util import normalizeMac
from zoni.bootstrap.bootstrapinterface import BootStrapInterface
class Pxe(BootStrapInterface):
- def __init__(self, config, verbose=None):
+ def __init__(self, config, data, verbose=None):
self.verbose = verbose
self.host = config['dbHost']
self.user = config['dbUser']
@@ -43,19 +44,26 @@ class Pxe(BootStrapInterface):
self.tftpUpdateFile = config['tftpUpdateFile']
self.tftpBaseFile = config['tftpBaseFile']
self.tftpBaseMenuFile = config['tftpBaseMenuFile']
+ self.initrdRoot = config['initrdRoot']
+ self.kernelRoot = config['kernelRoot']
+
+ self.log = logging.getLogger(os.path.basename(__file__))
- 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']
+ self.vlan_max = config['vlanMax']
+
+ self.data = data
+ #self.vlan_reserved = config['vlanReserved']
+ #data = instantiateImplementation("zoni.data.resourcequerysql.ResourceQuerySql", configs, options.verbosity)
# Connect to DB
- self.conn = MySQLdb.connect(host = self.host, port = self.port, user = self.user, passwd = self.passwd, db = self.db)
+ #self.conn = MySQLdb.connect(host = self.ho st, 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;"
@@ -97,4 +105,74 @@ class Pxe(BootStrapInterface):
os.system(cmd)
except Exception:
traceback.print_exc(sys.exc_info())
+
+ def generateBootOptions(self, image):
+ name = self.tftpBootOptionsDir + "/" + image
+ tftpdir = os.path.basename(self.tftpImageDir)
+ bootdir = os.path.basename(self.tftpBootOptionsDir)
+ imagedir = os.path.join(tftpdir, bootdir, image)
+
+ # Write out boot image file
+ val = "DISPLAY boot-screens/boot.txt\n"
+ val += "DEFAULT vesamenu.c32\n"
+ val += "TIMEOUT 2\n"
+ val += "PROMPT 0\n"
+ val += "MENU BACKGROUND boot-screens/zoni_pxe.jpg\n"
+
+ val += "MENU COLOR border 49;37 #00FFFFFF #00FFFFFF none\n"
+ val += "MENU INCLUDE %s-menu\n" % imagedir
+ f = open(name, "w")
+ f.write(val)
+ f.close()
+
+
+ # Write out the menu file
+ # Eventually iterate over all images this machine should be able to select from
+ name = self.tftpBootOptionsDir + "/" + image + "-menu"
+ kOpt= self.data.getKernelOptions(image)
+ kernelPath = os.path.join(self.kernelRoot, kOpt['kernel_arch'], kOpt['kernel_name'])
+ initrdPath = os.path.join(self.initrdRoot, kOpt['kernel_arch'], kOpt['initrd_name'])
+ val = "DISPLAY boot-screens/boot.txt\n\n"
+ val += "LABEL %s\n" % kOpt['image_name']
+ val += " MENU DEFAULT\n"
+ val += " kernel %s\n" % kernelPath
+ val += " append initrd=%s %s\n" % (initrdPath, kOpt['initrd_options'])
+
+ f = open(name, "w")
+ f.write(val)
+ f.close()
+
+
+
+ def setBootImage(self, mac, image):
+ mac_addr = "01-" + string.replace(normalizeMac(mac), ":", "-")
+ maclink = self.tftpImageDir + "/" + mac_addr
+ self.generateBootOptions(image)
+ # Check if it exists first
+ if os.path.exists(maclink):
+ try:
+ os.unlink(maclink)
+ except Exception, e:
+ traceback.print_exc(sys.exc_info())
+ if OSError:
+ print OSError, e
+ mesg = "ERROR : %s\n" % e
+ self.log.error(mesg)
+ return 1
+ return 1
+ # Create the boot option file
+ # Relink
+ newlink = os.path.basename(self.tftpBootOptionsDir) + "/" + image
+ try:
+ os.symlink(newlink, maclink)
+ mesg = "Image assignment Successful %s -> %s " % (mac, image)
+ self.log.info(mesg)
+ except Exception, e:
+ if OSError:
+ #mesg = "Cannot modify file. Please use sudo\n"
+ mesg = "ERROR : %s\n" % e
+ self.log.info(mesg)
+ return 1
+ return 1
+
Propchange: incubator/tashi/branches/oldstable/src/zoni/bootstrap/pxe.py
('svn:mergeinfo' removed)
Modified: incubator/tashi/branches/oldstable/src/zoni/client/zoni-cli.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/oldstable/src/zoni/client/zoni-cli.py?rev=1241773&r1=1241772&r2=1241773&view=diff
==============================================================================
--- incubator/tashi/branches/oldstable/src/zoni/client/zoni-cli.py (original)
+++ incubator/tashi/branches/oldstable/src/zoni/client/zoni-cli.py Wed Feb 8 04:44:09 2012
@@ -1,4 +1,5 @@
#! /usr/bin/env python
+
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
@@ -19,7 +20,6 @@
#
# $Id$
#
-
import os
import sys
import optparse
@@ -27,6 +27,7 @@ import socket
import logging.config
import getpass
import re
+import subprocess
@@ -49,10 +50,13 @@ from zoni.hardware.raritanpdu import rar
from zoni.hardware.delldrac import dellDrac
from zoni.agents.dhcpdns import DhcpDns
+from zoni.hardware.systemmanagement import SystemManagement
+
from zoni.extra.util import *
from zoni.version import *
+from tashi.util import instantiateImplementation, signalHandler
#import zoni.data.usermanagement
#from usermanagement import UserManagement
@@ -91,6 +95,7 @@ def main():
group.add_option("--reboot", "--reboot", dest="REBOOTNODE", help="Reboot node (Soft)", action="store_true", default=False)
group.add_option("--powerCycle", "--powercycle", dest="POWERCYCLE", help="Power Cycle (Hard)", action="store_true", default=False)
group.add_option("--powerOff", "--poweroff", dest="POWEROFF", help="Power off node", action="store_true", default=False)
+ group.add_option("--powerOffSoft", "--poweroffsoft", dest="POWEROFFSOFT", help="Power off node (soft)", action="store_true", default=False)
group.add_option("--powerOn", "--poweron", dest="POWERON", help="Power on node", action="store_true", default=False)
group.add_option("--powerReset", "--powerreset", dest="POWERRESET", help="Power reset node", action="store_true", default=False)
group.add_option("--console", dest="CONSOLE", help="Console mode", action="store_true", default=False)
@@ -101,6 +106,7 @@ def main():
group.add_option("-R", "--showReservation", "--showreservation", dest="showReservation", help="Show current node reservations", action="store_true", default=False)
group.add_option("-A", "--showAllocation", "--showallocation", dest="showAllocation", help="Show current node allocation", action="store_true", default=False)
group.add_option("-S", "--showResources", dest="showResources", help="Show available resources to choose from", action="store_true", default=False)
+ group.add_option("--getResources", "--getresources", dest="getResources", help="Get available resources to choose from", action="store_true", default=False)
group.add_option("-p", "--procs", dest="numProcs", help="Set number of processors" )
group.add_option("-c", "--clock", dest="clockSpeed", help="Processor clock" )
group.add_option("--memory", dest="numMemory", help="Amount of memory (Bytes)" )
@@ -114,11 +120,12 @@ def main():
# Admin Interface
group = optparse.OptionGroup(parser, "Admin Interface", "Administration Interface:")
group.add_option("--admin", dest="ADMIN", help="Enter Admin mode", action="store_true", default=False)
+ group.add_option("--setPortMode", "--setportmode", dest="setPortMode", help="Set port mode to access, trunk, or general")
group.add_option("--enableHostPort", "--enablehostport", dest="enableHostPort", help="Enable a switch port", action="store_true", default=False)
group.add_option("--disableHostPort", "--disablehostport", dest="disableHostPort", help="Disable a switch port", action="store_true", default=False)
- group.add_option("--destroyVlan", "--destroyvlan", dest="removeVlanId", help="Remove vlan from all switches")
+ group.add_option("--destroyVlan", "--destroyvlan", dest="destroyVlanId", help="Remove vlan from all switches")
group.add_option("--createVlan", "--createvlan", dest="createVlanId", help="Create a vlan on all switches")
- group.add_option("--addNodeToVlan", "--addnodetovlan", dest="add2Vlan", help="Add node to a vlan")
+ group.add_option("--addNodeToVlan", "--addnodetovlan", dest="add2Vlan", help="Add node to a vlan (:tagged)")
group.add_option("--removeNodeFromVlan", "--removenodefromvlan", dest="removeFromVlan", help="Remove node from a vlan")
group.add_option("--setNativeVlan", "--setnativevlan", dest="setNative", help="Configure native vlan")
group.add_option("--restoreNativeVlan", "--restorenativevlan", dest="restoreNative", help="Restore native vlan", action="store_true", default=False)
@@ -127,6 +134,8 @@ def main():
group.add_option("--interactiveSwitchConfig", "--interactiveswitchconfig", dest="interactiveSwitchConfig", help="Interactively configure a switch. switchhname")
group.add_option("--showSwitchConfig", "--showswitchconfig", dest="showSwitchConfig", help="Show switch config for node", action="store_true", default=False)
group.add_option("--register", dest="register", help="Register hardware to Zoni", action="store_true", default=False)
+ group.add_option("--labelPort", dest="labelPort", help="Label switch port", action="store_true", default=False)
+ group.add_option("--saveConfig", dest="saveConfig", help="SWITCHNAME - Save Switch Config")
parser.add_option_group(group)
@@ -144,16 +153,18 @@ def main():
group.add_option("--addDomain", "--adddomain", dest="addDomain", help="Add new domain to Zoni", action="store_true", default=False)
group.add_option("--removeDomain", "--removedomain", dest="removeDomain", help="remove a domain from Zoni", action="store_true", default=False)
group.add_option("-V", "--showVlans", "--showvlans", dest="showVlans", help="Show an from Zoni", action="store_true", default=False)
- group.add_option("--addVlan", "--addvlan", dest="addVlan", help="Add new vlan to Zoni", action="store_true", default=False)
- group.add_option("--removeVlan", "--removevlan", dest="removeVlan", help="Remove an from Zoni", action="store_true", default=False)
+ #group.add_option("--addVlan", "--addvlan", dest="addVlan", help="Add new vlan to Zoni", action="store_true", default=False)
+ #group.add_option("--removeVlan", "--removevlan", dest="removeVlan", help="Remove an from Zoni", action="store_true", default=False)
group.add_option("--assignVlan", "--assignvlan", dest="assignVlan", help="Assign vlan to a domain")
parser.add_option_group(group)
# Allocation Interface
group = optparse.OptionGroup(parser, "Allocation Interface", "Change current systems allocations:")
+ #group.add_option("--addReservation", "--addreservation", dest="addReservation", help="Add a Reservation", action="store_true", default=False)
group.add_option("--addImage", "--addimage", dest="addImage", help="Add image to Zoni - amd64-image:dist:dist_ver")
group.add_option("--delImage", "--delimage", dest="delImage", help="Delete PXE image")
#group.add_option("--addPxeImage", "--addpxeimage", dest="imageName", help="Add PXE image to database", action="store_true", default=False)
+ group.add_option("--assignImage", "--assignimage", dest="assignImage", help="Assign image to resource")
group.add_option("--imageName", "--imagename", dest="imageName", help="Assign image to resource")
group.add_option("--allocateNode", "--allocatenode", dest="allocateNode", help="Assign node to a user", action="store_true", default=False)
@@ -165,12 +176,15 @@ def main():
group.add_option("--releaseNode", "--releasenode", dest="releaseNode", help="Release current node allocation", action="store_true", default=False)
group.add_option("--reservationDuration", "--reservationduration", dest="reservationDuration", help="Specify duration of node reservation - YYYYMMDD format")
group.add_option("-r", "--reservationId", "--reservationid", dest="reservationId", help="Reservation ID")
- group.add_option("--addReservation", "--addreservation", dest="addReservation", help="Add a Reservation", action="store_true", default=False)
group.add_option("--updateReservation", "--updatereservation", dest="updateReservation", help="Update Reservation", action="store_true", default=False)
group.add_option("--delReservation", "--delreservation", dest="delReservation", help="Delete Reservation")
group.add_option("--rgasstest", dest="rgasstest", help="Debug testing function", action="store_true", default=False)
parser.add_option_group(group)
+ group = optparse.OptionGroup(parser, "Reservation Interface", "Change current systems reservations:")
+ group.add_option("--createReservation", "--createreservation", dest="createReservation", help="Create a new Reservation", action="store_true", default=False)
+ parser.add_option_group(group)
+
# Zoni Helpers
group = optparse.OptionGroup(parser, "Zoni Helpers", "Helper functions:")
group.add_option("--addDns", dest="addDns", help="Add a DNS entry", action="store_true", default=False)
@@ -187,42 +201,42 @@ def main():
cmdargs = {}
# setup db connection
- query = zoni.data.resourcequerysql.ResourceQuerySql(configs, options.verbosity)
+ #print "starting tread"
+ #threading.Thread(daemon=True, target=self.dbKeepAlive()).start()
+ #print "after tread"
+
+ data = instantiateImplementation("zoni.data.resourcequerysql.ResourceQuerySql", configs, options.verbosity)
+ reservation = instantiateImplementation("zoni.data.reservation.reservationMysql", configs, data, options.verbosity)
+ #query = zoni.data.resourcequerysql.ResourceQuerySql(configs, options.verbosity)
# Get host info
host=None
if options.nodeName:
- host = query.getHostInfo(options.nodeName)
+ host = data.getHostInfo(options.nodeName)
#print host
# Hardware control
- if options.hardwareType:
-
- if (options.hardwareType) and options.hardwareType not in configs['hardware_control']:
- mesg = "Non support hardware type specified\n"
- mesg += "Supported types:\n"
- mesg += str(configs['hardware_control'])
- mesg += "\n\n"
- sys.stdout.write(mesg)
- exit()
-
- if (options.hardwareType) and options.nodeName:
- #host = query.getHostInfo(options.nodeName)
- if options.hardwareType == "ipmi":
- hw = Ipmi(options.nodeName, host["ipmi_user"], host["ipmi_password"])
-
- if options.hardwareType == "pdu":
- hw = raritanDominionPx(configs, host)
-
- if options.hardwareType == "drac":
- # Check if node has drac card
- if "drac_name" in host:
- hw = dellDrac(host)
- else:
- mesg = "Host (" + options.nodeName + ") does not have a DRAC card!!\n"
- sys.stdout.write(mesg)
- exit(1)
+ if options.hardwareType and options.nodeName:
+ host = data.getHostInfo(options.nodeName)
+ if options.hardwareType == "ipmi":
+ #hardware = zoni.hardware.systemmanagement.SystemManagement(configs,data)
+ hw = Ipmi(configs, options.nodeName, host)
+#
+ if options.hardwareType == "pdu":
+ hw = raritanDominionPx(configs, options.nodeName, host)
+#
+ if options.hardwareType == "drac":
+ ## Check if node has drac card
+ if "drac_name" in host:
+ hw= dellDrac(configs, options.nodeName, host)
+ else:
+ mesg = "Host (" + options.nodeName + ") does not have a DRAC card!!\n"
+ sys.stdout.write(mesg)
+ exit(1)
+ if (options.REBOOTNODE or options.POWERCYCLE or options.POWEROFF or options.POWEROFFSOFT or \
+ options.POWERON or options.POWERSTATUS or options.CONSOLE or \
+ options.POWERRESET) and options.nodeName:
if options.verbosity:
hw.setVerbose(True)
@@ -236,6 +250,9 @@ def main():
if options.POWEROFF:
hw.powerOff()
exit()
+ if options.POWEROFFSOFT:
+ hw.powerOffSoft()
+ exit()
if options.POWERON:
hw.powerOn()
exit()
@@ -249,20 +266,51 @@ def main():
hw.activateConsole()
exit()
hw.getPowerStatus()
+ exit()
+ else:
+ hw = zoni.hardware.systemmanagement.SystemManagement(configs,data)
+ if (options.REBOOTNODE or options.POWERCYCLE or options.POWEROFF or options.POWEROFFSOFT or \
+ options.POWERON or options.POWERSTATUS or options.CONSOLE or \
+ options.POWERRESET) and options.nodeName:
- if (options.hardwareType) and not options.nodeName:
- mesg = "\nMISSSING OPTION: Node name required -n or --nodeName\n"
- parser.print_help();
- sys.stderr.write(mesg)
+ if options.verbosity:
+ hw.setVerbose(True)
+
+ if options.REBOOTNODE:
+ hw.powerReset(options.nodeName)
+ exit()
+ if options.POWERCYCLE:
+ hw.powerCycle(options.nodeName)
+ exit()
+ if options.POWEROFFSOFT:
+ hw.powerOffSoft(options.nodeName)
+ exit()
+ if options.POWEROFF:
+ hw.powerOff(options.nodeName)
exit()
+ if options.POWERON:
+ hw.powerOn(options.nodeName)
+ exit()
+ if options.POWERRESET:
+ hw.powerReset(options.nodeName)
+ exit()
+ if options.POWERSTATUS:
+ hw.getPowerStatus(options.nodeName)
+ exit()
+ if options.CONSOLE:
+ hw.activateConsole(options.nodeName)
+ exit()
+ hw.getPowerStatus(options.nodeName)
+
+
if (options.REBOOTNODE or options.POWERCYCLE or options.POWEROFF or \
options.POWERON or options.POWERSTATUS or options.CONSOLE or \
- options.POWERRESET) and not options.hardwareType:
+ options.POWERRESET) and not options.nodeName:
parser.print_help()
- usage = "\nMISSING OPTION: When specifying hardware parameters, you need the --hw option\n"
- print usage
+ mesg = "\nMISSSING OPTION: Node name required -n or --nodeName\n"
+ print mesg
exit()
# Query Interface
@@ -285,6 +333,13 @@ def main():
parser.print_help()
exit()
+ if options.getResources:
+ print "ALL resources"
+ print data.getAvailableResources()
+ key = "3Lf7Qy1oJZue1q/e3ZQbfg=="
+ print "Tashi Resources"
+ print data.getMyResources(key)
+
# Show current allocations
if (options.showAllocation) or (options.show and re.match(".lloca.*", args[0])):
if options.uid:
@@ -292,7 +347,7 @@ def main():
else:
nameorid = options.userName
- query.showAllocation(nameorid)
+ data.showAllocation(nameorid)
exit()
# Show current reservation
@@ -302,20 +357,20 @@ def main():
else:
nameorid = options.userName
- query.showReservation(nameorid)
+ data.showReservation(nameorid)
# Print all Resources
if options.showResources or (options.show and re.match(".esour.*", args[0])):
- query.showResources(cmdargs)
+ data.showResources(cmdargs)
# Show PXE images
#if (options.show and re.match(".esour.*", args[0])):
if options.showPxeImages or (options.show and re.match("pxei.*", args[0])):
- query.showPxeImages()
+ data.showPxeImages()
# Show machine to PXE image mapping
if options.showPxeImageMap or (options.show and re.match("pxem.*", args[0])):
- query.showPxeImagesToSystemMap(cmdargs)
+ data.showPxeImagesToSystemMap(cmdargs)
if (len(sys.argv) == 1):
parser.print_help()
@@ -324,37 +379,46 @@ def main():
# Delete reservation
if (options.delReservation):
- query.removeReservation(options.delReservation)
+ data.removeReservation(options.delReservation)
exit()
# Specify usermanagement, ldap or files
- usermgt = usermanagement.ldap()
+ if configs['userManagement'] == "ldap":
+ usermgt = usermanagement.ldap()
+ elif configs['userManagement'] == "files":
+ usermgt = usermanagement.files()
+ else:
+ print "User management problem"
+ exit()
if (options.rgasstest):
+ #data.getHardwareCapabilities(options.nodeName)
#pdu = raritanDominionPx(host)
#print pdu
#bootit = pxe.Pxe(configs, options.verbosity)
- #bootit.createPxeUpdateFile(query.getPxeImages())
+ #bootit.createPxeUpdateFile(data.getPxeImages())
#bootit.updatePxe()
#print "host is ", host
#drac = dellDrac("drac-r2r1c3", 1)
+ drac = dellDrac(configs, options.nodeName, host)
#drac.getPowerStatus()
#drac.powerOff()
#drac.powerOn()
#drac.powerCycle()
- #drac.powerReset()
+ drac.setVerbose(options.verbosity)
+ drac.powerReset()
#drac.getPowerStatus()
- print "host is ", host
- pdu = raritanDominionPx(configs, host)
- print "pdu", pdu
- pdu.getPowerStatus()
- exit()
+ #print "host is ", host
+ #pdu = raritanDominionPx(configs, options.nodeName, host)
+ #print "pdu", pdu
+ #pdu.getPowerStatus()
+ #exit()
# Create a reservation for a user
- if (options.addReservation):
+ if (options.createReservation):
if not (options.userName or options.uid):
- mesg = "ERROR: AddReservation requires the following arguments...\n"
+ mesg = "ERROR: CreateReservation requires the following arguments...\n"
if not (options.userName or options.uid):
mesg += " Username: --userName=username or --uid 1000\n"
@@ -364,19 +428,23 @@ def main():
exit()
userId = options.uid
- if not options.uid:
+ if not userId:
userId = usermgt.getUserId(options.userName)
- reservationId = query.addReservation(userId, options.reservationDuration, options.myNotes + " " + str(string.join(args[0:len(args)])))
+ if userId:
+ reservationId = reservation.createReservation(userId, options.reservationDuration, options.myNotes + " " + str(string.join(args[0:len(args)])))
+ else:
+ print "user doesn't exist"
+ exit()
# Allocate node to user
if (options.allocateNode):
if options.reservationId and options.domain and options.vlanInfo and options.nodeName and options.imageName:
- host = query.getHostInfo(options.nodeName)
+ host = data.getHostInfo(options.nodeName)
# optparse does not pass multiple args that are quoted anymore?
# Used to work with python 2.5.2. Doesn't work with python 2.6.5.
- query.allocateNode(options.reservationId, options.domain, host['sys_id'], options.vlanInfo, options.imageName, options.myNotes + " " + str(string.join(args[0:len(args)])))
+ data.allocateNode(options.reservationId, options.domain, host['sys_id'], options.vlanInfo, options.imageName, options.hostName, options.myNotes + " " + str(string.join(args[0:len(args)])))
else:
mesg = "USAGE: %s --allocateNode --nodeName nodeName --domain domainname --reservationId ID --vlanInfo vlanNums:info, --imageName imagename [--notes]\n" % (sys.argv[0])
mesg += "Options\n"
@@ -389,7 +457,6 @@ def main():
sys.stdout.write(mesg)
exit()
- exit() #remove this
if not (options.reservationId) or not options.nodeName:
mesg = "ERROR: AllocateNode requires the following arguments...\n"
if not (options.nodeName):
@@ -404,8 +471,23 @@ def main():
sys.stderr.write(mesg)
exit()
- print host['sys_id']
- query.allocateNode(options.reservationId, host['sys_id'], options.hostName, vlanNum, options.ipAddr, options.Notes)
+ # Reconfigure switchports
+ memberMap = data.getDomainMembership(host['sys_id'])
+ HwSwitch = HwDellSwitch
+ hwswitch = HwSwitch(configs, host)
+ for vlan, tag in memberMap.iteritems():
+ hwswitch.addNodeToVlan(vlan, tag)
+ # Register node in DNS
+ if options.hostName:
+ # Add cname
+ cmd = "zoni --addCname %s %s" % (options.hostName, host['location'])
+ subprocess.Popen(string.split(cmd))
+
+
+
+
+ #data.allocateNode(options.reservationId, options.domain, host['sys_id'], options.vlanInfo, options.imageName, options.hostName, options.myNotes + " " + str(string.join(args[0:len(args)])))
+ #data.allocateNode(options.reservationId, host['sys_id'], options.hostName, vlanNum, options.ipAddr, options.myNotes)
exit()
# Update allocation
@@ -423,14 +505,13 @@ def main():
exit()
userId = None
- if options.uid and options.userName:
+ if options.uid or options.userName:
# Get the username from uid
userId = options.uid
if not options.uid:
userId = usermgt.getUserId(options.userName)
- print options.reservationId, userId, options.reservationDuration, options.vlanIsolate, options.Notes
- query.updateReservation(options.reservationId, userId, options.reservationDuration, options.vlanIsolate, options.Notes)
+ data.updateReservation(options.reservationId, userId, options.reservationDuration, options.vlanIsolate, options.myNotes)
# Release node allocation
if (options.releaseNode):
@@ -441,71 +522,74 @@ def main():
sys.stderr.write(mesg)
exit()
- query.releaseNode(options.nodeName)
+ data.releaseNode(options.nodeName)
# Assign image to host
- #if (options.assignImage):
- #if not options.nodeName:
- #usage = "Node not specified. Please specify a node with --nodeName or -n"
- #print usage
- #exit()
- #if query.assignImagetoHost(host, options.assignImage):
+ if (options.assignImage):
+ if not options.nodeName:
+ usage = "Node not specified. Please specify a node with --nodeName or -n"
+ print usage
+ exit()
+ # need to fix this later
+ #if data.assignImagetoHost(host, options.assignImage):
#print "ERROR"
#exit()
-#
+
# Update PXE
- #bootit = pxe.Pxe(configs, options.verbosity)
- #bootit.createPxeUpdateFile(query.getPxeImages())
+ bootit = Pxe(configs, data, options.verbosity)
+ bootit.setBootImage(host['mac_addr'], options.assignImage)
+ #bootit.createPxeUpdateFile(data.getPxeImages())
#bootit.updatePxe()
# Add image to database
if (options.addImage):
- query.addImage(options.addImage)
+ data.addImage(options.addImage)
# Delete PXE image
if (options.delImage):
- query.delImage(options.delImage)
+ data.delImage(options.delImage)
# Domain interface
if (options.showDomains):
- query.showDomains()
+ data.showDomains()
if (options.addDomain):
- if len(args) > 2 and options.reservationId:
- query.addDomain(args[0], string.join(args[1:len(args)]), options.reservationId)
+ if len(args) > 2 and options.vlanInfo:
+ data.addDomain(args[0], string.join(args[1:len(args)]), options.vlanInfo)
else:
- mesg = "USAGE: %s --addDomain domainname domaindesc --reservationId ID\n" % (sys.argv[0])
+ mesg = "USAGE: %s --addDomain domainname \"domain desc\" --vlanInfo vlan:type,vlan:type\n" % (sys.argv[0])
+ mesg += "Options\n\n --vlanInfo 999:native,1000:untagged,1001:tagged\n"
sys.stdout.write(mesg)
exit()
if (options.removeDomain):
if len(args) > 0:
- query.removeDomain(args[0])
+ data.removeDomain(args[0])
else:
mesg = "USAGE: %s --removeDomain domainname \n" % (sys.argv[0])
sys.stdout.write(mesg)
exit()
if (options.showVlans):
- query.showVlans()
- if (options.addVlan):
- print len(args)
- if len(args) > 0:
- query.addVlan(args[0], string.join(args[1:len(args)]))
- else:
- mesg = "USAGE: %s --addVlan vlanNumber [VlanDesc]\n" % (sys.argv[0])
- sys.stdout.write(mesg)
- exit()
- if (options.removeVlan):
- if len(args) > 1:
- query.removeVlan(args[0])
- else:
- mesg = "USAGE: %s --removeVlan VlanNumber\n" % (sys.argv[0])
- sys.stdout.write(mesg)
- exit()
+ data.showVlans()
+ #if (options.addVlan):
+ #print len(args)
+ #if len(args) > 0:
+ #data.addVlan(args[0], string.join(args[1:len(args)]))
+ #else:
+ #mesg = "USAGE: %s --addVlan vlanNumber [VlanDesc]\n" % (sys.argv[0])
+ #sys.stdout.write(mesg)
+ #exit()
+ #if (options.removeVlan):
+ #if len(args) > 0:
+ #data.removeVlan(args[0])
+ #else:
+ #mesg = "USAGE: %s --removeVlan VlanNumber\n" % (sys.argv[0])
+ #sys.stdout.write(mesg)
+ #exit()
if (options.assignVlan):
print len(args)
if len(args) > 0:
- query.assignVlan(options.assignVlan, args[0], options.forcefully)
+ data.assignVlan(options.assignVlan, args[0], options.forcefully)
else:
mesg = "USAGE: %s --assignVlan vlannum DomainName\n" % (sys.argv[0])
sys.stdout.write(mesg)
@@ -514,8 +598,9 @@ def main():
# Admin Interface
# snmpwalk -v2c -c zoni-domain sw0-r1r1 .1.3.6.1.2.1.17.7.1.4.3.1.5
if (options.ADMIN):
+
- if not options.nodeName and not options.createVlanId and not options.removeVlanId and not options.switchPort and not options.interactiveSwitchConfig:
+ if not options.nodeName and not options.createVlanId and not options.destroyVlanId and not options.switchPort and not options.interactiveSwitchConfig and not options.saveConfig:
mesg = "\nERROR: nodeName or switch not specified. Please specify nodename with -n or --nodeName or --switchport\n"
parser.print_help()
sys.stderr.write(mesg)
@@ -523,37 +608,63 @@ def main():
# We can specify port/switch combinations here
if options.switchPort:
- host = query.getSwitchInfo(options.switchPort.split(":")[0])
+ host = data.getSwitchInfo(options.switchPort.split(":")[0])
if len(options.switchPort.split(":")) > 1:
host['hw_port'] = options.switchPort.split(":")[1]
host['location'] = options.switchPort
if options.interactiveSwitchConfig:
- host = query.getSwitchInfo(options.interactiveSwitchConfig)
+ host = data.getSwitchInfo(options.interactiveSwitchConfig)
HwSwitch = HwDellSwitch
hwswitch = HwSwitch(configs, host)
if options.verbosity:
hwswitch.setVerbose(True)
- #print "create vlan", options.createVlanId
- if options.enableHostPort and options.nodeName:
+ if options.setPortMode:
+ hwswitch.setPortMode(options.setPortMode)
+
+ if options.saveConfig:
+ hwswitch.saveConfig(options.saveConfig, data)
+ if options.labelPort:
+ mydesc = None
+ if len(args) > 0:
+ mydesc = " ".join(["%s" % i for i in args])
+ hwswitch.labelPort(mydesc)
+
+ if options.enableHostPort and (options.nodeName or options.switchPort):
hwswitch.enableHostPort()
if options.disableHostPort and (options.nodeName or options.switchPort):
hwswitch.disableHostPort()
+ # Create a new vlan on all switches and add to db
if options.createVlanId:
- hwswitch.createVlans(options.createVlanId, query.getAllSwitches(), query)
- if options.removeVlanId:
- hwswitch.removeVlans(options.removeVlanId, query.getAllSwitches(), query)
+ print options.createVlanId
+ hwswitch.createVlans(options.createVlanId, data.getAllSwitches(), data)
+ data.addVlan(options.createVlanId, string.join(args[1:len(args)]))
+ # Remove vlan on all switches and remove from db
+ if options.destroyVlanId:
+ hwswitch.removeVlans(options.destroyVlanId, data.getAllSwitches(), data)
+ data.removeVlan(options.destroyVlanId)
if options.add2Vlan and (options.nodeName or options.switchPort):
- hwswitch.addNodeToVlan(options.add2Vlan)
+ tag="untagged"
+ vlan = options.add2Vlan
+ if ":" in options.add2Vlan:
+ print options.add2Vlan
+ vlan = options.add2Vlan.split(":")[0]
+ tag = options.add2Vlan.split(":")[1]
+
+ hwswitch.addNodeToVlan(vlan, tag)
+ data.addNodeToVlan(host['location'], vlan, tag)
+ exit()
- if options.removeFromVlan and options.nodeName:
+ if options.removeFromVlan and (options.nodeName or options.switchPort):
hwswitch.removeNodeFromVlan(options.removeFromVlan)
+ data.removeNodeFromVlan(options.nodeName, options.removeFromVlan)
if options.setNative and (options.nodeName or options.switchPort):
hwswitch.setNativeVlan(options.setNative)
+ data.addNodeToVlan(host['location'], options.setNative, "native")
if options.restoreNative and options.nodeName:
hwswitch.restoreNativeVlan()
if options.removeAllVlans and (options.nodeName or options.switchPort):
@@ -569,14 +680,14 @@ def main():
# Register hardware
if options.register:
- supported_hardware = ['dell', 'raritan']
+ supported_hardware = ['dellswitch', 'raritan']
if len(args) < 3:
mesg = "ERROR: Expecting username and ip address of hardware to be registered\n"
mesg += os.path.basename(sys.argv[0]) + " --register HARDWARE username ipaddr\n"
mesg += "Supported hardware " + str(supported_hardware) + "\n"
sys.stderr.write(mesg)
else:
- if string.lower(args[0]) == "dell":
+ if string.lower(args[0]) == "dellswitch":
HwSwitch = HwDellSwitch
hw = HwSwitch(configs)
elif string.lower(args[0]) == "raritan":
@@ -594,7 +705,7 @@ def main():
data = hw.registerToZoni(args[1], password, args[2])
# Register to DB
- #query.registerHardware(data)
+ #data.registerHardware(data)
# Zoni Helper
if options.addDns or options.removeDns or options.addDhcp or options.removeDhcp or options.addCname or options.removeCname:
@@ -697,7 +808,7 @@ def main():
if options.addCname:
if len(args) < 2:
mesg = "ERROR: Incorrect number of arguments\n"
- mesg += "Example: " + os.path.basename(sys.argv[0]) + " " + thisone + "cname existing_name"
+ mesg += "Example: %s %s cname existing_name" % (os.path.basename(sys.argv[0]), thisone)
print mesg
exit()
hostName = args[1]
@@ -707,10 +818,10 @@ def main():
dhcpdns = DhcpDns(configs, verbose=options.verbosity)
dhcpdns.addCname(cname, hostName)
if dhcpdns.error:
- mesg = "[FAIL] \n" + str(dhcpdns.error) + "\n"
+ mesg = "[FAIL] \n %s\n" % str(dhcpdns.error)
sys.stdout.write(mesg)
else:
- mesg = "[SUCCESS]" + "\n"
+ mesg = "[SUCCESS]\n"
sys.stdout.write(mesg)
if __name__ == "__main__":
Propchange: incubator/tashi/branches/oldstable/src/zoni/client/zoni-cli.py
('svn:mergeinfo' removed)
Modified: incubator/tashi/branches/oldstable/src/zoni/data/resourcequerysql.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/oldstable/src/zoni/data/resourcequerysql.py?rev=1241773&r1=1241772&r2=1241773&view=diff
==============================================================================
--- incubator/tashi/branches/oldstable/src/zoni/data/resourcequerysql.py (original)
+++ incubator/tashi/branches/oldstable/src/zoni/data/resourcequerysql.py Wed Feb 8 04:44:09 2012
@@ -25,10 +25,12 @@ import MySQLdb
import subprocess
import traceback
import logging
+import threading
+import time
import usermanagement
from zoni.data.infostore import InfoStore
-from zoni.extra.util import checkSuper
+from zoni.extra.util import checkSuper, createKey
from zoni.agents.dhcpdns import DhcpDns
class ResourceQuerySql(InfoStore):
@@ -39,84 +41,127 @@ class ResourceQuerySql(InfoStore):
self.user = config['dbUser']
self.passwd = config['dbPassword']
self.db = config['dbInst']
- #self.log = logging.getLogger(os.path.basename(__name__))
+ self.port = config['dbPort']
+ self._isDb = 1
+ self.vlan_max = config['vlanMax']
+ if self.port == "":
+ self.port = 3306
self.log = logging.getLogger(__name__)
- self.tftpRootDir = config['tftpRootDir']
- self.tftpImageDir = config['tftpImageDir']
- self.tftpBootOptionsDir = config['tftpBootOptionsDir']
-
-
- if config['dbPort'] == "":
- config['dbPort'] = 3306
-
- self.port = config['dbPort']
+ self.conn = self.createConn()
- self.vlan_max = config['vlan_max']
- #self.vlan_reserved = config['vlan_reserved']
+ def createConn(self):
# Connect to DB
try:
- self.conn = MySQLdb.connect(host = self.host, port = self.port, user = self.user, passwd = self.passwd, db = self.db)
+ return MySQLdb.connect(host = self.host, port = self.port, user = self.user, passwd = self.passwd, db = self.db)
+ #return MySQLdb.connect(host = self.config['dbHost'], port = self.config['dbPort'], user = self.config['dbUser'], passwd = self.config['dbPassword'], db = self.config['dbInst'])
+
except MySQLdb.OperationalError, e:
if e[0] == 2005:
print "ERROR :" + str(e[1])
- exit(1)
+ return
else:
print "ERROR : ", e
- exit(1)
+ return
+
+ def keepAlive(self):
+ while True:
+ if not self.conn.open:
+ self.log.info("Reinitializing DB connection")
+ self.conn = self.createConn()
+ time.sleep(10)
def getNote(self):
return "Created by Zoni"
- def addDomain(self, name, desc, reservationId):
- # Check if there is a reservation
- query = "select * from reservationinfo where reservation_id = %s" % (reservationId)
- result = self.__selectDb(query)
- if result.rowcount < 1:
- mesg = "Reservation does not exist : %s" % (reservationId)
- self.log.error(mesg)
- return -1
+ def addDomain(self, name, desc, vlanInfo):
+ # Check if vlans exist
+ vlans = []
+ for val in vlanInfo.split(","):
+ try:
+ ret = self.getVlanId(val.split(":")[0])
+ vlans.append(val)
+ except Exception, e:
+ print e
+ exit()
if desc == None:
desc = self.getNote()
- print name
if self.__checkDup("domaininfo", "domain_name", name):
self.log.error("Domain (%s) already exists" % (name))
return -1
- query = "insert into domaininfo (domain_name, domain_desc, reservation_id) values ('%s','%s', '%s')" % (name, desc, reservationId)
+ # Create a key for the reservation
+ domainKey = createKey(name)
+ query = "insert into domaininfo (domain_name, domain_desc, domain_key) values ('%s','%s', '%s')" % (name, desc, domainKey)
try:
- result = self.__insertDb(query)
+ result = self.insertDb(query)
mesg = "Adding domain %s(%s)" % (name, desc)
self.log.info(mesg)
except Exception, e:
mesg = "Adding domain %s(%s) failed : %s" % (name, desc, e)
self.log.error(mesg)
+
+ # Get the domain_id
+ domainId = int(self.getDomainIdFromKey(domainKey))
+
+ # Map domain to vlan
+ for i in vlans:
+ vlanId = int(i.split(":")[0])
+ vlanType = i.split(":")[1]
+ query = "insert into domainmembermap values (%d, %d, '%s')" % (domainId, vlanId, vlanType)
+ try:
+ result = self.insertDb(query)
+ except Exception, e:
+ print e
+ def getDomainMembership(self, sys_id):
+ query = "select v.vlan_num, q.vlan_type from allocationinfo a, vlanmembermap q, vlaninfo v where q.vlan_id = v.vlan_id and a.allocation_id = q.allocation_id and sys_id = '%s'" % (sys_id)
+ result = self.selectDb(query)
+ data = {}
+ if result.rowcount:
+ for i in result.fetchall():
+ data[int(i[0])] = i[1]
+ return data
+ else:
+ return -1
+
def removeDomain(self, name):
+ domainId = self.__getSomething("domain_id", "domaininfo", "domain_name", name)
mesg = "Removing domain %s" % (name)
self.log.info(mesg)
query = "delete from domaininfo where domain_name = '%s'" % (name)
result = self.__deleteDb(query)
# Need to remove any vlans attached to this domain
+ query = "delete from domainmembermap where domain_id = '%s'" % (domainId)
+ result = self.__deleteDb(query)
def showDomains(self):
- query = "select domain_name, domain_desc from domaininfo"
- result = self.__selectDb(query)
+ usermgt = eval("usermanagement.%s" % (self.config['userManagement']) + "()")
+ query = "select r.reservation_id, r.user_id, d.domain_name, d.domain_desc from domaininfo d, allocationinfo a, reservationinfo r where d.domain_id = a.domain_id and a.reservation_id = r.reservation_id"
+ result = self.selectDb(query)
desc = result.description
if result.rowcount > 0:
- print "%s\t%s\n-------------------------------------" % (result.description[0][0], result.description[1][0])
+ print "%s\t%s\t%s\t%s" % (result.description[0][0], result.description[1][0], result.description[2][0], result.description[3][0])
+ print "------------------------------------------------------------"
for row in result.fetchall():
- print "%s\t\t%s" % (row[0], row[1])
+ resName = usermgt.getUserName(row[1])
+ print "%s\t\t%s\t%s\t\t%s" % (row[0], resName, row[2], row[3])
return 0
else:
mesg = "No Domains exist"
self.log.info(mesg)
return -1
- def addVlan(self, vnumber, desc=None):
+ def addVlan(self, vnum, desc=None):
+ vnumber = vnum
+ if ":" in vnum:
+ vnumber = vnum.split(":")[0]
+ desc = vnum.split(":")[1:len(vnumber)][0]
+ print vnumber, desc
+
if desc == None:
desc = "Created by Zoni"
if int(vnumber) > self.vlan_max:
@@ -128,7 +173,7 @@ class ResourceQuerySql(InfoStore):
return -1
query = "insert into vlaninfo (vlan_num, vlan_desc) values ('%s','%s')" % (vnumber, desc)
try:
- result = self.__insertDb(query)
+ result = self.insertDb(query)
mesg = "Adding vlan %s(%s)" % (vnumber, desc)
self.log.info(mesg)
except Exception, e:
@@ -152,7 +197,7 @@ class ResourceQuerySql(InfoStore):
def showVlans (self):
query = "select vlan_num, vlan_desc from vlaninfo order by vlan_num"
try:
- result = self.__selectDb(query)
+ result = self.selectDb(query)
print "%s\t%s\n-------------------------------------" % (result.description[0][0], result.description[1][0])
for row in result.fetchall():
print "%s\t\t%s" % (row[0], row[1])
@@ -166,24 +211,29 @@ class ResourceQuerySql(InfoStore):
domainId = self.__getSomething("domain_id", "domaininfo", "domain_name", domain)
vlanId = self.__getSomething("vlan_id", "vlaninfo", "vlan_num", vlan)
query = "select * from domainmembermap m, vlaninfo v, domaininfo d where d.domain_id = '%s' and v.vlan_id = %s and v.vlan_id = m.vlan_id and m.domain_id = d.domain_id" % (int(domainId), int(vlanId))
- if self.__selectDb(query).rowcount > 0:
+ if self.selectDb(query).rowcount > 0:
self.log.warning("Vlan %s already assigned to domain %s" % (vlan, domain));
return 0
# warning if vlan already assigned to another domain
query = "select * from domainmembermap where vlan_id = %s" % (vlanId)
- if self.__selectDb(query).rowcount > 0:
+ if self.selectDb(query).rowcount > 0:
self.log.warning("Vlan %s already assigned to a domain" % (vlan));
if not force:
return -1
self.log.info("Assigning vlan %s to domain %s" % (vlan, domain))
query = "insert into domainmembermap (domain_id, vlan_id) values (%s, %s)" % (domainId, vlanId)
- self.__insertDb(query)
+ self.insertDb(query)
def __getSomething(self, fieldname, table, critField, crit):
query = "select %s from %s where %s = '%s'" % (fieldname, table, critField, crit)
- result = self.__selectDb(query)
+ result = self.selectDb(query)
+ if result.rowcount == 0:
+ mesg = "No entry for '%s' found" % (crit)
+ self.log.error(mesg)
+ #return []
+ return -1
return result.fetchall()[0][0]
@@ -192,7 +242,7 @@ class ResourceQuerySql(InfoStore):
if (colname2 != None and value2 != None):
cond += " and %s = '%s'" % (colname2, value2)
query = "select * from %s %s" % (table, cond)
- result = self.__selectDb(query)
+ result = self.selectDb(query)
if result.rowcount == 0:
return []
return result.fetchall()
@@ -248,63 +298,121 @@ class ResourceQuerySql(InfoStore):
queryopt = self.__create_queryopts(cmdargs)
#query = "show fields from sysinfo"
- #results = self.__selectDb(query)
+ #results = self.selectDb(query)
query = "select " + defaultFields + "from sysinfo " + queryopt
- result = self.__selectDb(query)
+ result = self.selectDb(query)
line = ""
for i in defaultFields.split(","):
#line += string.strip(str(i)) + "\t"
line += str(i.center(20))
+ # header
print line
+ sum = {}
for row in result.fetchall():
line = ""
+ sum['totProc'] = sum.get('totProc', 0)
+ sum['totProc'] += int(row[2])
+ sum['totCores'] = sum.get('totCores', 0)
+ sum['totCores'] += int(row[3])
+ sum['totMemory'] = sum.get('totMemory', 0)
+ sum['totMemory'] += int(row[5])
for val in row:
line += str(val).center(20)
+
print line
- print str(result.rowcount) + " systems returned"
+ print "\n%s systems registered - %d procs | %d cores | %d bytes RAM" % (str(result.rowcount), sum['totProc'], sum['totCores'], sum['totMemory'],)
+
+ def getAvailableResources(self):
+ # Maybe should add a status flag?
+
+ query = "select sys_id from allocationinfo"
+ result = self.selectDb(query)
+ allocatedNodes = []
+ if result.rowcount:
+ for i in result.fetchall()[0]:
+ allocatedNodes.append(i)
+
+ query = "select sys_id, location, num_procs, num_cores, mem_total, clock_speed, proc_model from sysinfo where state_id = 0 or state_id = 1"
+ result = self.selectDb(query)
+ desc = result.description
+ res = {}
+ for i in result.fetchall():
+ sys_id = int(i[0])
+ if sys_id not in allocatedNodes:
+ location = i[1]
+ res[location] = res.get(location, {})
+ res[location][desc[0][0]] = sys_id
+ res[location][desc[1][0]] = location
+ res[location][desc[2][0]] = int(i[2])
+ res[location][desc[3][0]] = int(i[3])
+ res[location][desc[4][0]] = int(i[4])
+ res[location][desc[5][0]] = int(i[5])
+ res[location][desc[6][0]] = i[6]
+ return res
+
+ def getMyResources(self, key):
+ query = "select s.sys_id, s.location, s.num_procs, s.num_cores, s.mem_total, s.clock_speed, s.proc_model from sysinfo s, allocationinfo a, domaininfo d where a.sys_id = s.sys_id and a.domain_id = d.domain_id and d.domain_key = '%s'" % key
+ result = self.selectDb(query)
+ desc = result.description
+ res = {}
+ for i in result.fetchall():
+ sys_id = int(i[0])
+ location = i[1]
+ res[location] = res.get(location, {})
+ res[location][desc[0][0]] = sys_id
+ res[location][desc[1][0]] = location
+ res[location][desc[2][0]] = int(i[2])
+ res[location][desc[3][0]] = int(i[3])
+ res[location][desc[4][0]] = int(i[4])
+ res[location][desc[5][0]] = int(i[5])
+ res[location][desc[6][0]] = i[6]
+
+ return res
- #mysql -h rodimus -u reader irp-cluster -e "select * from sysinfo where location like 'r1%' and num_procs = 1"
+ def getDomainFromKey(self, key):
+ return self.__getSomething("domain_name", "domaininfo", "domain_key", key)
+ def getDomainIdFromKey(self, key):
+ return self.__getSomething("domain_id", "domaininfo", "domain_key", key)
+
+ def getKeyFromDomain(self, domain):
+ return self.__getSomething("domain_key", "domaininfo", "domain_name", domain)
+
def getLocationFromSysId (self, nodeId):
query = "select location from sysinfo where sys_id = \"" + str(nodeId) + "\""
- result = self.__selectDb(query)
+ result = self.selectDb(query)
return result.fetchall()[0][0]
+ def getSysIdFromLocation(self, location):
+ return self.__getSomething("sys_id", "sysinfo", "location", location)
+
def getMacFromSysId(self, nodeId):
query = "select mac_addr from sysinfo where sys_id = \"" + str(nodeId) + "\""
- result = self.__selectDb(query)
+ result = self.selectDb(query)
return result.fetchall()[0][0]
def getIpFromSysId(self, nodeId):
query = "select ip_addr from sysinfo where sys_id = \"" + str(nodeId) + "\""
- result = self.__selectDb(query)
+ result = self.selectDb(query)
return result.fetchall()[0][0]
def getAllSwitches(self):
switchList = []
query = "select hw_name from hardwareinfo where hw_type = \"switch\""
- result = self.__selectDb(query)
+ result = self.selectDb(query)
for switch in result.fetchall():
switchList.append(switch[0])
- # Use static list until we get all switches installed
- #switchList = ['sw1-r1r2', 'sw0-r1r1', 'sw0-r1r2', 'sw0-r1r3', 'sw0-r1r4', 'sw0-r2r3', 'sw0-r3r3', 'sw0-r3r2', 'sw0-r2r1c3', 'sw2-r1r2']
- #switchList = ['sw2-r1r2']
- #switchList = ['sw1-r1r2']
-
-
- print switchList
-
return switchList
def getAvailableVlan(self):
# Get list of available vlans
query = "select vlan_num from vlaninfo where domain = 'private'"
- result = self.__selectDb(query)
+ result = self.selectDb(query)
for vlan in result.fetchall()[0]:
avail = self.isVlanAvailable(vlan)
if avail:
@@ -317,7 +425,7 @@ class ResourceQuerySql(InfoStore):
def isVlanAvailable(self, vlan):
query = "select a.vlan_id, v.vlan_num from allocationinfo a, vlaninfo v where a.vlan_id = v.vlan_id and v.vlan_num = " + str(vlan)
- result = self.__selectDb(query)
+ result = self.selectDb(query)
if result.rowcount > 1:
return 0
else:
@@ -325,19 +433,19 @@ class ResourceQuerySql(InfoStore):
def getVlanId(self, vlan):
query = "select vlan_id from vlaninfo where vlan_num = \"" + str(vlan) + "\""
- result = self.__selectDb(query)
+ result = self.selectDb(query)
#print result.rowcount
if result.rowcount > 0:
- return result.fetchall()[0][0]
+ return int(result.fetchall()[0][0])
else:
mesg = "VLAN does not exist: " + str(vlan)
self.log.error(mesg)
- return -1
+ raise Exception, mesg
def isIpAvailable(self, ip_addr, vlan_id):
query = "select * from allocationinfo where ip_addr = \"" + str(ip_addr) + "\" and vlan_id = \"" + str(vlan_id) + "\""
#print "query ", query
- result = self.__selectDb(query)
+ result = self.selectDb(query)
#print "select row count is ", result.rowcount
if result.rowcount > 0:
return 0
@@ -348,7 +456,7 @@ class ResourceQuerySql(InfoStore):
def getDomainIp(self, vlan):
ip_start = 30
query = "select ip_network from vlaninfo where vlan_num = " + str(vlan)
- result = self.__selectDb(query)
+ result = self.selectDb(query)
ip_network = result.fetchall()[0][0]
v = ip_network.split(".")
ip_base = v[0] + "." + v[1] + "." + v[2]
@@ -358,7 +466,7 @@ class ResourceQuerySql(InfoStore):
#print "ip is ", ip_network
query = "select a.ip_addr from allocationinfo a, vlaninfo v where a.vlan_id = v.vlan_id and v.vlan_num = " + str(vlan);
- result = self.__selectDb(query)
+ result = self.selectDb(query)
#print "row count is ", result.rowcount
if result.rowcount > 0:
for ip in xrange(ip_start, 255):
@@ -374,12 +482,6 @@ class ResourceQuerySql(InfoStore):
return ip_addr
- #def showArchive(self):
- #query = "select * from allocationarchive"
- #result = self.__selectDb(query)
- #for i in result:
- #print i
-
def showAllocation(self, userId=None):
#from IPython.Shell import IPShellEmbed
#shell = IPShellEmbed(argv="")
@@ -388,14 +490,11 @@ class ResourceQuerySql(InfoStore):
# specify usermanagement - ldap or files
usermgt = eval("usermanagement.%s" % (self.config['userManagement']) + "()")
- query = "select r.user_id, d.domain_name, s.location, s.num_cores, \
- s.mem_total, r.reservation_expiration, r.notes, r.reservation_id, a.hostname, \
- a.notes, i.image_name from \
- sysinfo s, imageinfo i, \
- allocationinfo a, domaininfo d, reservationinfo r, imagemap j where \
- i.image_id = j.image_id \
- and r.reservation_id = a.reservation_id \
- and d.domain_id = a.domain_id and s.sys_id = a.sys_id "
+ #select a.reservation_id, a.sys_id, r.user_id, s.location, s.num_cores, s.mem_total, a.hostname, ii.image_name
+ #from allocationinfo a, reservationinfo r, sysinfo s, imageinfo ii, imagemap im
+ #where r.reservation_id = a.reservation_id and a.sys_id = s.sys_id and im.image_id = ii.image_id and im.allocation_id = a.allocation_id;
+
+ query = "select r.user_id, d.domain_name, s.location, s.num_cores, s.mem_total, r.reservation_expiration, r.notes, r.reservation_id, a.hostname, a.notes, ii.image_name, a.allocation_id from sysinfo s, imageinfo ii, allocationinfo a, domaininfo d, reservationinfo r, imagemap im where im.image_id = ii.image_id and r.reservation_id = a.reservation_id and d.domain_id = a.domain_id and s.sys_id = a.sys_id and im.allocation_id = a.allocation_id"
if userId:
myid = userId
if type(userId) == str:
@@ -404,18 +503,16 @@ class ResourceQuerySql(InfoStore):
query += " and user_id = '%s' " % (myid)
- query += "order by r.reservation_id asc, s.location"
+ query += " order by r.reservation_id asc, s.location"
- print query
- result = self.__selectDb(query)
+ result = self.selectDb(query)
- print "NODE ALLOCATION"
- print "---------------------------------------------------------------------------------"
+ print "NODE ALLOCATION\n"
+ sum = {}
if self.verbose:
- #print "Res_id\tUser \tNode \tCores\tMemory \tExpiration\t\tVLAN\tHOSTNAME \tIPADDR \t\tReservationNotes|AllocationNotes"
- print "%-5s%-10s%-10s%-12s%-12s%-5s%-15s%-18s%-24s%s" % ("Res", "User", "Host", "Cores/Mem","Expiration", "Vlan", "Hostname", "IP Addr", "Boot Image Name", "Notes")
+ print "%-5s%-10s%-10s%-10s%-13s%-12s%-10s%-34s%-20s%s" % ("Res", "User", "Host", "Domain", "Cores/Mem","Expiration", "Hostname", "Boot Image Name", "Vlan Member", "Notes")
else:
- print "%-10s%-10s%-13s%-12s%s" % ("User", "Node", "Cores/Mem","Expiration", "Notes")
+ print "%-10s%-10s%-10s%-13s%-12s%s" % ("User", "Node", "Domain", "Cores/Mem","Expiration", "Notes")
for i in result.fetchall():
uid = i[0]
@@ -429,18 +526,31 @@ class ResourceQuerySql(InfoStore):
rnotes = i[6]
resId= i[7]
hostname = i[8]
+ if hostname == None:
+ hostname = host
anotes = i[9]
image_name = i[10]
+ allocation_id = i[11]
userName = usermgt.getUserName(uid)
combined_notes = str(rnotes) + "|" + str(anotes)
+ sum['totCores'] = sum.get('totCores', 0)
+ sum['totCores'] += cores
+ sum['totMemory'] = sum.get('totMemory', 0)
+ sum['totMemory'] += memory
if self.verbose:
- vlanId = 1000
- ip_addr = "10.0.0.10"
- print "%-5s%-10s%-10s%-2s/%-10s%-12s%-5s%-15s%-18s%-24s%s" % (resId, userName, host, cores, memory,expire, vlanId, hostname, ip_addr, image_name, combined_notes)
+ query = "select v.vlan_num, m.vlan_type from vlaninfo v, vlanmembermap m where v.vlan_id = m.vlan_id and allocation_id = '%d' order by vlan_num asc" % allocation_id
+ vlanRes = self.selectDb(query)
+ vlanList = []
+ for i in vlanRes.fetchall():
+ tag = string.upper(str(i[1][0]))
+ mytag = "%s(%s)" %(str(i[0]), tag )
+ vlanList.append(mytag)
+
+ vlanMember = string.join(vlanList, ",")
+ print "%-5s%-10s%-10s%-10s%-2s/%-10s%-12s%-10s%-34s%-20s%s" % (resId, userName, host, domain, cores, memory,expire, hostname, image_name, vlanMember,combined_notes)
else:
- print "%-10s%-10s%-2s/%-10s%-12s%s" % (userName, host, cores, memory,expire, combined_notes)
- print "---------------------------------------------------------------------------------"
- print str(result.rowcount) + " systems returned"
+ print "%-10s%-10s%-10s%-2s/%-10s%-12s%s" % (userName, host, domain, cores, memory,expire, combined_notes)
+ print "\n%s systems allocated - %d cores| %d bytes RAM" % (str(result.rowcount), sum['totCores'], sum['totMemory'])
def showReservation(self, userId=None):
#from IPython.Shell import IPShellEmbed
@@ -468,7 +578,7 @@ class ResourceQuerySql(InfoStore):
#query += "order by r.user_id, s.location"
- result = self.__selectDb(query)
+ result = self.selectDb(query)
print "RESERVATIONS"
print "---------------------------------------------------------------------------------"
@@ -499,11 +609,10 @@ class ResourceQuerySql(InfoStore):
def getPxeImages(self):
- cursor = self.conn.cursor ()
- line = "select image_name from imageinfo"
- cursor.execute (line)
- row = cursor.fetchall()
- desc = cursor.description
+ query = "select image_name from imageinfo"
+ result = self.selectDb(query)
+ row = result.fetchall()
+ desc = result.description
imagelist = []
for i in row:
@@ -513,24 +622,37 @@ class ResourceQuerySql(InfoStore):
def showPxeImages(self):
- cursor = self.conn.cursor ()
- line = "select image_name, dist, dist_ver from imageinfo"
- cursor.execute (line)
- row = cursor.fetchall()
- desc = cursor.description
+ query = "select image_name, dist, dist_ver from imageinfo"
+ result = self.selectDb(query)
+ row = result.fetchall()
+ desc = result.description
for i in row:
print i
- cursor.close ()
+ def getKernelOptions(self,image):
+ val = {}
+ query = "select i.image_name, k.kernel_name, k.kernel_arch, n.initrd_name, n.initrd_options from imageinfo i, kernelinfo k, initrdinfo n where k.kernel_id = i.kernel_id and i.initrd_id = n.initrd_id and i.image_name = '%s'" % image
+ result = self.selectDb(query)
+ row = result.fetchone()
+ desc = result.description
+ val[desc[0][0]] = row[0]
+ val[desc[1][0]] = row[1]
+ val[desc[2][0]] = row[2]
+ val[desc[3][0]] = row[3]
+ val[desc[4][0]] = row[4]
+ return val
+
+
+
def showPxeImagesToSystemMap(self, cmdargs):
- extra = "l.mac_addr = j.mac_addr and j.image_id = i.image_id"
+ extra = "j.image_id = i.image_id"
queryopt = self.__create_queryopts(cmdargs, extra=extra)
- query = "select l.location, j.mac_addr, i.image_name from sysinfo l , imageinfo i, imagemap j " + queryopt + " order by l.location"
+ query = "select s.location, s.mac_addr, i.image_name from sysinfo s , imageinfo i, imagemap j " + queryopt + " order by s.location"
#print query
- result = self.__selectDb(query)
+ result = self.selectDb(query)
for i in result.fetchall():
print i
@@ -548,13 +670,14 @@ class ResourceQuerySql(InfoStore):
def getHostInfo(self, node):
host = {}
query = "select sys_id, mac_addr, num_procs, num_cores, mem_total, clock_speed, sys_vendor, sys_model, proc_vendor, proc_model, proc_cache, cpu_flags, bios_rev, location, system_serial_number, ip_addr from sysinfo where location = \"" + node + "\""
- result = self.__selectDb(query)
+ result = self.selectDb(query)
if result.rowcount > 1:
- print "Multiple entries for system exist. Please correct"
- exit()
+ mesg = "Multiple entries for system exist. Please correct"
+ self.log.info(mesg)
+ exit
if result.rowcount < 1:
mesg = "node does not exist :" + str(node) + "\n"
- sys.stderr.write(mesg)
+ self.log.error(mesg)
exit()
for i in result.fetchall():
@@ -582,10 +705,10 @@ class ResourceQuerySql(InfoStore):
# Get IPMI info
query = "select h.hw_userid, h.hw_password, h.hw_ipaddr from hardwareinfo h, portmap p, sysinfo s where p.sys_id = s.sys_id and h.hw_id = p.hw_id and h.hw_type = 'ipmi' and s.sys_id = " + str(host['sys_id']) + ""
- result = self.__selectDb(query)
+ result = self.selectDb(query)
if result.rowcount> 1:
print "Multiple entries for system exist. Please correct"
- exit()
+ return
for i in result.fetchall():
host['ipmi_user'] = i[0]
host['ipmi_password'] = i[1]
@@ -593,7 +716,7 @@ class ResourceQuerySql(InfoStore):
# Get image info
query = "select image_name from imagemap i, imageinfo j where i.image_id = j.image_id"
- result = self.__selectDb(query)
+ result = self.selectDb(query)
if result.rowcount == 0:
host['pxe_image_name'] = "None"
else:
@@ -602,7 +725,7 @@ class ResourceQuerySql(InfoStore):
# Get switch info
query = "select h.hw_id, h.hw_name, h.hw_model, h.hw_ipaddr, h.hw_userid, h.hw_password, p.port_num from hardwareinfo h, portmap p where p.hw_id = h.hw_id and hw_type = 'switch' and sys_id = " + str(host['sys_id'])
- result = self.__selectDb(query)
+ result = self.selectDb(query)
for i in result.fetchall():
host['hw_id'] = int(i[0])
host['hw_name'] = i[1]
@@ -614,7 +737,7 @@ class ResourceQuerySql(InfoStore):
# Get drac info
query = "select h.hw_id, h.hw_name, h.hw_model, h.hw_ipaddr, h.hw_userid, h.hw_password, p.port_num from hardwareinfo h, portmap p where p.hw_id = h.hw_id and hw_type = 'drac' and sys_id = " + str(host['sys_id'])
- result = self.__selectDb(query)
+ result = self.selectDb(query)
if result.rowcount > 0:
for i in result.fetchall():
host['drac_id'] = int(i[0])
@@ -627,7 +750,7 @@ class ResourceQuerySql(InfoStore):
# Get PDU info
query = "select h.hw_id, h.hw_name, h.hw_model, h.hw_ipaddr, h.hw_userid, h.hw_password, p.port_num from hardwareinfo h, portmap p where p.hw_id = h.hw_id and h.hw_type = 'pdu' and p.sys_id = " + str(host['sys_id'])
- result = self.__selectDb(query)
+ result = self.selectDb(query)
for i in result.fetchall():
host['pdu_id'] = int(i[0])
host['pdu_name'] = i[1]
@@ -646,9 +769,7 @@ class ResourceQuerySql(InfoStore):
# Get switch info
#switchList = self.getAllSwitches()
query = "select h.hw_id, h.hw_name, h.hw_model, h.hw_ipaddr, h.hw_userid, h.hw_password from hardwareinfo h where h.hw_name = \"" + str(switchName) + "\""
- #print "query is ", query
- result = self.__selectDb(query)
- #desc = cursor.description
+ result = self.selectDb(query)
for i in result.fetchall():
host['hw_id'] = int(i[0])
host['hw_name'] = i[1]
@@ -659,6 +780,7 @@ class ResourceQuerySql(InfoStore):
return host
def __queryDb(self, query):
+ self.conn.ping(True)
cursor = self.conn.cursor()
try:
cursor.execute (query)
@@ -673,6 +795,7 @@ class ResourceQuerySql(InfoStore):
return row
def execQuery(self, query):
+ self.conn.ping(True)
cursor = self.conn.cursor()
try:
cursor.execute (query)
@@ -683,10 +806,11 @@ class ResourceQuerySql(InfoStore):
msg = "%s : %s" % (e[1], query)
self.log.error(msg)
#traceback.print_exc(sys.exc_info())
- exit()
+ return
return cursor
- def __selectDb(self, query):
+ def selectDb(self, query):
+ self.conn.ping(True)
cursor = self.conn.cursor()
try:
cursor.execute (query)
@@ -700,6 +824,7 @@ class ResourceQuerySql(InfoStore):
return cursor
def __deleteDb(self, query):
+ self.conn.ping(True)
cursor = self.conn.cursor()
try:
cursor.execute (query)
@@ -713,6 +838,7 @@ class ResourceQuerySql(InfoStore):
return cursor
def __updateDb(self, query):
+ self.conn.ping(True)
cursor = self.conn.cursor()
try:
cursor.execute (query)
@@ -725,7 +851,8 @@ class ResourceQuerySql(InfoStore):
return -1
return cursor
- def __insertDb(self, query):
+ def insertDb(self, query):
+ self.conn.ping(True)
cursor = self.conn.cursor()
try:
cursor.execute (query)
@@ -756,7 +883,7 @@ class ResourceQuerySql(InfoStore):
else:
mesg = "ERROR: Invalid reservation duration"
self.log.error(mesg)
- exit()
+ return
mesg = "Updating reservationDuration :" + resDuration
self.log.info(mesg)
@@ -772,7 +899,6 @@ class ResourceQuerySql(InfoStore):
mesg = "UPDATING Vlan: "
self.log.info(mesg)
query = "update reservationinfo set vlan_num = " + vlanIsolate + " where reservation_id = \"" + str(reservationId) + "\""
- print "query is ", query
self.__updateDb(query)
if userId:
mesg = "UPDATING USER:"
@@ -799,19 +925,18 @@ class ResourceQuerySql(InfoStore):
else:
mesg = "ERROR: Invalid reservation duration\n"
self.log.info(mesg)
- exit()
+ return
- # create reservation
# Create the reservation
print userId, expireDate,reservationNotes
- query = "insert into reservationinfo (user_id, reservation_expiration, notes) values (\"" + str(userId) + "\", " + str(expireDate) + ", \"" + str(reservationNotes) + "\")"
- mesg = "Creating new reservation : " + query
+ query = "insert into reservationinfo (user_id, reservation_expiration, notes) values ('%s', '%s', '%s')" % (str(userId), str(expireDate), str(reservationNotes))
+ mesg = "Creating new reservation : %s" % query
self.log.info(mesg)
- self.__insertDb(query)
+ self.insertDb(query)
# Get the res_id
query = "select max(reservation_id) from reservationinfo"
- res_id = self.__selectDb(query).fetchone()[0]
- mesg = " Reservation created - ID :" + str(res_id)
+ res_id = self.selectDb(query).fetchone()[0]
+ mesg = " Reservation created - ID : %s" % str(res_id)
self.log.info(mesg)
return res_id
@@ -827,9 +952,10 @@ class ResourceQuerySql(InfoStore):
#str(user_id) + "\", \"" + str(reservation_type) + "\", \"" + \
#str(combined_notes) + "\")"
#
- #self.__insertDb(query)
+ #self.insertDb(query)
- def allocateNode(self, reservationId, domain, sysId, vlanInfo, imageName, notes=None):
+
+ def allocateNode(self, reservationId, domain, sysId, vlanInfo, imageName, newHostName=None, notes=None):
print "reservationId", reservationId, domain, sysId, vlanInfo, imageName, notes
# Check if node is already allocated
@@ -854,107 +980,108 @@ class ResourceQuerySql(InfoStore):
mesg = "Domain does not exist: %s(%s)" % (domainId, domain)
self.log.error(mesg)
return -1
+
+ imageId = self.__getSomething("image_id", "imageinfo", "image_name", imageName)
+ if len(self.__checkDup("imageinfo", "image_id", imageId)) == 0:
+ mesg = "Image does not exist: %s(%s)" % (imageName, imageId)
+ self.log.error(mesg)
+ return -1
# Check that all the vlans exist
for i in vlanInfo.split(","):
v = i.split(":")[0]
- if self.getVlanId(v) < 0:
- return -1
+ try:
+ self.getVlanId(v)
+ except Exception, e:
+ print e
# Insert to allocationinfo
nodeName = self.getLocationFromSysId(sysId)
mesg = "allocateNode %s : domain %s : reservation %s(%s)" % (nodeName, domain, reservationId, resinfo[4])
self.log.info(mesg)
- query = "insert into allocationinfo (sys_id, reservation_id, domain_id, notes) values ('%s', '%s', '%s', '%s')" % (sysId, reservationId, domainId, notes)
- result = self.__insertDb(query)
+ query = "insert into allocationinfo (sys_id, reservation_id, domain_id, hostname, notes) values ('%s', '%s', '%s', '%s', '%s')" % (sysId, reservationId, domainId, newHostName, notes)
+ result = self.insertDb(query)
allocationId = result.lastrowid
# Parse vlan info and add to vlanmembermap
for i in vlanInfo.split(","):
- print i
v = i.split(":")[0]
- vId = self.getVlanId(v)
t = i.split(":")[1]
- query = "insert into vlanmembermap (allocation_id, vlan_id, vlan_type) values ('%s', '%s', '%s')" % (allocationId, vId, t)
- result = self.__insertDb(query)
- mesg = "Adding vlan %s to node %s" % (v, nodeName)
- self.log.info(mesg)
+ self.addNodeToVlan(nodeName, v, t)
# Insert into imagemap
- image_id = self.__getSomething("image_id", "imageinfo", "image_name", imageName)
- query = "insert into imagemap (allocation_id, image_id) values ('%s', '%s')" % (allocationId, image_id)
- result = self.__insertDb(query)
-
- def rgasstest(self, vlan_num):
- query = "select * from vlaninfo where vlan_num = " + vlan_num
- res = self.__selectDb(query).fetchall()
- print res
-
+ query = "insert into imagemap (allocation_id, image_id) values ('%s', '%s')" % (allocationId, imageId)
+ result = self.insertDb(query)
+
+ self.__updateSysState(sysId, 1)
+
+ def addNodeToVlan(self, nodeName, v, t):
+ sysId = self.getSysIdFromLocation(nodeName)
+ allocationId = self.__getSomething("allocation_id", "allocationinfo", "sys_id", sysId)
+ vId = self.getVlanId(v)
+ if t == "native":
+ query = "select vlan_id from vlanmembermap where allocation_id = %d and vlan_type = 'native'" % (allocationId)
+ result = self.selectDb(query)
+ if result.rowcount > 0:
+ query = "update vlanmembermap set vlan_type = 'untagged' where allocation_id = %d and vlan_id = %d" % (allocationId, result.fetchall()[0][0])
+ else:
+ query = "delete from vlanmembermap where allocation_id = %d and vlan_id = %d" % (allocationId, vId)
+ result = self.selectDb(query)
+
+ if self.__checkDup("vlanmembermap", "vlan_id", vId, "allocation_id", allocationId):
+ self.log.error("Vlan %s already exists" % (v))
+ return -1
+ query = "insert into vlanmembermap (allocation_id, vlan_id, vlan_type) values ('%s', '%s', '%s')" % (allocationId, vId, t)
+
+ result = self.insertDb(query)
+ mesg = "Adding vlan %s to node %s" % (v, nodeName)
+ self.log.info(mesg)
+
+ def removeNodeFromVlan(self, nodeName, v):
+ sysId = self.getSysIdFromLocation(nodeName)
+ allocationId = self.__getSomething("allocation_id", "allocationinfo", "sys_id", sysId)
+ vId = self.getVlanId(v)
+ query = "delete from vlanmembermap where allocation_id = '%s' and vlan_id = '%s'" % (allocationId, vId)
+
+ result = self.insertDb(query)
+ mesg = "Removing vlan %s from node %s" % (v, nodeName)
+ self.log.info(mesg)
+
+
+ def __updateSysState(self, sysId, stateId):
+ query = "update sysinfo set state_id = '%s' where sys_id = '%s'" % (stateId, sysId)
+ return self.__updateDb(query)
def removeReservation(self, res):
+ mesg = "Removing reservation (%s)" % str(res)
+ self.log.info(mesg)
query = "delete from reservationinfo where reservation_id = " + str(res)
self.__updateDb(query)
query = "delete from allocationinfo where reservation_id = " + str(res)
self.__updateDb(query)
+
- @checkSuper
- def releaseNode(self, nodeName):
+ def releaseNode(self, location):
# Get the nodeId
- query = "select sys_id, r.reservation_id, a.ip_addr, hostname, vlan_id, a.notes, r.notes,r.user_id from allocationinfo a, sysinfo s, reservationinfo r where a.sys_id = s.sys_id and a.reservation_id = r.reservation_id and location = \"" + nodeName + "\""
- print query
- result = self.__selectDb(query)
+ sysId = self.__getSomething("sys_id", "sysinfo", "location", location)
+ query = "select * from allocationinfo where sys_id = '%s'" % (sysId)
+ result = self.selectDb(query)
if result.rowcount == 0:
- mesg = "ERROR: Node not allocated\n"
- sys.stderr.write(mesg)
- exit(1)
+ mesg = "Node %s not allocated" % (location)
+ self.log.error(mesg)
+ return
if result.rowcount > 1:
- mesg = "WARNING: Node allocated multiple times (" + str(result.rowcount) + ")"
+ mesg = "Node %s allocated multiple times(%s)" % (location, str(result.rowcount))
self.log.warning(mesg)
+ return
- val = result.fetchone()
- nodeId = int(val[0])
- resId = int(val[1])
- ip_addr = val[2]
- hostName = val[3]
- vlan_id = int(val[4])
- allocation_notes = val[5]
- reservation_notes = val[6]
- user_id = val[7]
-
- print "hostname is ", hostName
- # Assign IP address to node
- dhcpdns = DhcpDns(self.config, verbose=1)
- dnscheck = dhcpdns.removeDns(hostName)
- dhcpdns.removeDhcp(hostName)
-
- '''
- query = "select reservation_id, notes from reservationinfo where sys_id = " + str(nodeId)
- result = self.__selectDb(query)
- for i in result:
- print i
- print result.rowcount
- if result.rowcount == 0:
- mesg = "No Reservation for this node.\n Please check"
- logit(self.logFile, mesg)
- exit(1)
- if result.rowcount > 1:
- mesg = "WARNING: Multiple reservations exist (" + str(result.rowcount) + ")"
- logit(self.logFile, mesg)
-
- resId = int(result.fetchone()[0])
- res_notes = int(result.fetchone()[1])
-
- print resId, res_notes
- '''
-
# Eventually should add count =1 so deletes do get out of control
- query = "delete from allocationinfo where reservation_id = " + str(resId) + " and sys_id = " + str(nodeId)
+ query = "delete from allocationinfo where sys_id = '%s'" % (sysId)
result = self.__deleteDb(query)
-
- # Archive node release
- #reservation_type = "release"
- #self.archiveAllocation(nodeId, ip_addr, hostName, vlan_id, user_id, reservation_type, reservation_notes, allocation_notes)
+ # Set state to available
+ self.__updateSysState(sysId, 0)
+ return
def addImage(self, imageName):
name = ""
@@ -969,11 +1096,11 @@ class ResourceQuerySql(InfoStore):
dist_ver = imageName.split(":")[2]
query = "select * from imageinfo where image_name = \"" + name + "\""
- result = self.__selectDb(query)
+ result = self.selectDb(query)
if result.rowcount > 0:
mesg = "ERROR: Image already exists\n"
sys.stderr.write(mesg)
- exit()
+ return
if name == "":
mesg = "ERROR: Image details not specified\n"
@@ -981,10 +1108,10 @@ class ResourceQuerySql(InfoStore):
mesg = "Example amd64-rgass-testing:Ubuntu:8.04\n"
mesg += "or amd64-rgass-testing::\n"
sys.stderr.write(mesg)
- exit()
+ return
query = "insert into imageinfo (image_name, dist, dist_ver) values(\"" + name + "\", \"" + dist + "\", \"" + dist_ver + "\")"
- self.__insertDb(query)
+ self.insertDb(query)
def delImage(self, imageName):
@@ -993,7 +1120,7 @@ class ResourceQuerySql(InfoStore):
if result.rowcount == 0:
mesg = "ERROR: No images match your entry\n"
sys.stderr.write(mesg)
- exit()
+ return
def assignImagetoHost(self, host, image):
# imagemap db should be sys_id instead of mac_addr
@@ -1006,12 +1133,12 @@ class ResourceQuerySql(InfoStore):
if len(row) < 1:
mesg = "ERROR: Image \"" + image + "\" does not exist"
self.log.error(mesg)
- exit()
+ return
new_image_id = str(row[0][0])
# check for entry and delete in exists
query = "select * from imagemap where mac_addr = \"" + host['mac_addr'] + "\""
- result = self.__selectDb(query)
+ result = self.selectDb(query)
if result.rowcount > 0:
query = "delete from imagemap where mac_addr = \"" + host['mac_addr'] + "\""
result = self.__deleteDb(query)
@@ -1019,10 +1146,11 @@ class ResourceQuerySql(InfoStore):
# update the database entry with the new image for the host
query = "insert into imagemap (mac_addr, image_id) values (\"" + host['mac_addr'] + "\", " + new_image_id + ")"
- self.__insertDb(query)
+ self.insertDb(query)
# Update tftp link
+ # XXX move to pxe.py
mac_addr = "01-" + string.lower(string.replace(host['mac_addr'], ":", "-"))
maclink = self.tftpImageDir + "/" + mac_addr
#print "mac link is ", maclink
@@ -1055,6 +1183,33 @@ class ResourceQuerySql(InfoStore):
return 0
+
+ def getHardwareCapabilities(self, nodeName):
+ ''' getHardwareCapabilities return a list of lists with
+ [0] = hw method
+ [1] = hw method userid
+ [2] = hw method password '''
+
+ sysId = self.getSysIdFromLocation(nodeName)
+ query = "select h.hw_type, h.hw_userid, h.hw_password from hardwareinfo h, portmap p where p.hw_id = h.hw_id and p.sys_id = '%s'" % sysId
+ result = self.selectDb(query)
+ cap = []
+ val = []
+ for i in result.fetchall():
+ if i[0] in self.config['hardwareControl']:
+ val.append(i)
+
+ # order the hardware capabilities according to order in config file
+ for i in [j for j in self.config['hardwareControl']]:
+ for j in val:
+ if i in j:
+ cap.append(j)
+ return cap
+
+ # print out data in a consistent format
+ def __showIt(data):
+ pass
+
def registerHardware(self, data):
@@ -1084,7 +1239,7 @@ class ResourceQuerySql(InfoStore):
statement += "'" + i + "') "
count += 1
try:
- self.__insertDb(statement)
+ self.insertDb(statement)
mesg = "Device (%s) registered successfully\n" % (data['hw_name'])
self.log.info(mesg)
except Exception, e:
Propchange: incubator/tashi/branches/oldstable/src/zoni/data/resourcequerysql.py
('svn:mergeinfo' removed)
Modified: incubator/tashi/branches/oldstable/src/zoni/data/usermanagement.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/oldstable/src/zoni/data/usermanagement.py?rev=1241773&r1=1241772&r2=1241773&view=diff
==============================================================================
--- incubator/tashi/branches/oldstable/src/zoni/data/usermanagement.py (original)
+++ incubator/tashi/branches/oldstable/src/zoni/data/usermanagement.py Wed Feb 8 04:44:09 2012
@@ -111,11 +111,31 @@ class ldap(UserManagementInterface):
class files(UserManagementInterface):
def __init__(self):
- raise NotImplementedError
+ pass
- def getUserId(self):
- raise NotImplementedError
+ def getUserId(self, userName=None):
+ if userName == None:
+ return os.getuid()
+ cmd = "cat /etc/passwd "
+ a = os.popen(cmd)
+ for line in a.readlines():
+ if userName in line :
+ return line.split(":")[2]
+ return 0
+
+
+ def getUserName(self, userId=None):
+ if userId == None:
+ return os.getenv('USERNAME')
+ cmd = "cat /etc/passwd "
+ a = os.popen(cmd)
+ for line in a.readlines():
+ if str(userId) in line :
+ return line.split(":")[0]
+
+ return 0
+
def getGroupId(self):
- raise NotImplementedError
+ pass
Propchange: incubator/tashi/branches/oldstable/src/zoni/data/usermanagement.py
('svn:mergeinfo' removed)
Propchange: incubator/tashi/branches/oldstable/src/zoni/data/usermanagementinterface.py
('svn:mergeinfo' removed)
Modified: incubator/tashi/branches/oldstable/src/zoni/extra/util.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/oldstable/src/zoni/extra/util.py?rev=1241773&r1=1241772&r2=1241773&view=diff
==============================================================================
--- incubator/tashi/branches/oldstable/src/zoni/extra/util.py (original)
+++ incubator/tashi/branches/oldstable/src/zoni/extra/util.py Wed Feb 8 04:44:09 2012
@@ -19,10 +19,14 @@
#
import os
+import string
import ConfigParser
import time
import shutil
import re
+import threading
+import subprocess
+import logging
def loadConfigFile(parser):
#parser = ConfigParser.ConfigParser()
@@ -36,7 +40,8 @@ def loadConfigFile(parser):
# Management
config['userManagement'] = parser.get("management", "USER_MANAGEMENT").split()[0]
- config['infoStore'] = parser.get("management", "INFO_STORE").split()[0]
+ #config['infoStore'] = parser.get("management", "INFO_STORE").split()[0]
+ config['pickleFile'] = parser.get("management", "PICKLE_FILE").split()[0]
# DB connection
config['dbUser'] = parser.get("dbConnection", "DB_USER").split()[0]
@@ -56,6 +61,7 @@ def loadConfigFile(parser):
config['tftpBaseMenuFile'] = parser.get("pxe", "TFTP_BASE_MENU_FILE").split()[0]
config['pxeServerIP'] = parser.get("pxe", "PXE_SERVER_IP").split()[0]
config['initrdRoot'] = parser.get("pxe", "INITRD_ROOT").split()[0]
+ config['kernelRoot'] = parser.get("pxe", "KERNEL_ROOT").split()[0]
# Image store
config['imageServerIP'] = parser.get("imageStore", "IMAGE_SERVER_IP").split()[0]
@@ -70,7 +76,7 @@ def loadConfigFile(parser):
# VLAN
#config['vlan_reserved'] = parser.get("vlan", "VLAN_RESERVED")
- config['vlan_max'] = int(parser.get("vlan", "VLAN_MAX"))
+ config['vlanMax'] = int(parser.get("vlan", "VLAN_MAX"))
# Domain
config['zoniHomeDomain'] = parser.get("domain", "ZONI_HOME_DOMAIN").split()[0]
@@ -79,7 +85,7 @@ def loadConfigFile(parser):
#config['vlan_max'] = parser.get("vlan", "VLAN_MAX")
# HARDWARE CONTROL
- config['hardware_control'] = parser.get("hardware", "HARDWARE_CONTROL")
+ config['hardwareControl'] = eval(parser.get("hardware", "HARDWARE_CONTROL"))
# DHCP/DNS
config['dnsEnabled'] = parser.get("DhcpDns", "dnsEnabled")
@@ -131,6 +137,7 @@ def checkSuper(f):
exit()
return res
myF.__name__ = f.__name__
+
return myF
@@ -144,6 +151,14 @@ def timeF(f):
myF.__name__ = f.__name__
return myF
+def log(f):
+ def myF(*args, **kw):
+ print "calling %s%s" % (f.__name__, str(args))
+ res = f(*args, **kw)
+ print "returning from %s -> %s" % (f.__name__, str(res))
+ return res
+ myF.__name__ = f.__name__
+ return myF
def createDir(dirName, checkexists=None):
if checkexists and os.path.exists(dirName):
@@ -154,10 +169,11 @@ def createDir(dirName, checkexists=None)
os.mkdir(dirName, 0755)
print " Creating directory " + dirName
except (OSError, Exception), e:
- if e.errno == 17:
- print " " + e.args[1] + ": " + dirName
- else:
- print " " + e.args[1] + ": " + dirName
+ print " " + e.args[1] + ": " + dirName
+ return 0
+
+ return 1
+
def validIp(ip):
@@ -172,6 +188,10 @@ def validIp(ip):
return 0
return 1
+def normalizeMac(mac):
+ rawmac = re.sub('[.:-]', '', mac)
+ return string.lower(":".join(["%s%s" % (rawmac[i], rawmac[i+1]) for i in range(0,12,2)]))
+
def validMac(mac):
reg = '([a-fA-F0-9]{2}[:|\\-]?){6}'
val = re.compile(reg).search(mac)
@@ -179,5 +199,50 @@ def validMac(mac):
return 1
return 0
+def createKey(name):
+ tmpdir = "/tmp"
+ def cleanIt(tmpdir, name):
+ check = "K%s" % name
+ for i in os.listdir(tmpdir):
+ if check in i:
+ keyName = os.path.join(tmpdir, i)
+ os.unlink(keyName)
+
+ cleanIt(tmpdir, name)
+ cmd = "dnssec-keygen -a HMAC-MD5 -r /dev/urandom -b 128 -K %s -n USER %s" % (tmpdir, name)
+ c = subprocess.Popen(args=cmd.split(), stdout=subprocess.PIPE)
+ keyName = os.path.join(tmpdir, string.strip(c.stdout.readline()) + ".key")
+ f = open(keyName, "r")
+ val = string.strip(string.split(f.readline(), " " , 6)[6])
+ f.close()
+ return val
+
+
+
+def debugConsole(globalDict):
+ """A debugging console that optionally uses pysh"""
+ def realDebugConsole(globalDict):
+ try :
+ import atexit
+ from IPython.Shell import IPShellEmbed
+ def resetConsole():
+# XXXpipe: make input window sane
+ (stdin, stdout) = os.popen2("reset")
+ stdout.read()
+ dbgshell = IPShellEmbed()
+ atexit.register(resetConsole)
+ dbgshell(local_ns=globalDict, global_ns=globalDict)
+ except Exception:
+ CONSOLE_TEXT=">>> "
+ input = " "
+ while (input != ""):
+ sys.stdout.write(CONSOLE_TEXT)
+ input = sys.stdin.readline()
+ try:
+ exec(input) in globalDict
+ except Exception, e:
+ sys.stdout.write(str(e) + "\n")
+ if (os.getenv("DEBUG", "0") == "1"):
+ threading.Thread(target=lambda: realDebugConsole(globalDict)).start()
Propchange: incubator/tashi/branches/oldstable/src/zoni/extra/util.py
('svn:mergeinfo' removed)