incubator-tashi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strou...@apache.org
Subject svn commit: r1366273 - in /incubator/tashi/trunk/src/tashi/client: tashi-admin.py test.py
Date Fri, 27 Jul 2012 04:17:24 GMT
Author: stroucki
Date: Fri Jul 27 04:17:23 2012
New Revision: 1366273

URL: http://svn.apache.org/viewvc?rev=1366273&view=rev
Log:
tashi-admin: cut down on non-admin helper functions
test: play to understand optparse

Added:
    incubator/tashi/trunk/src/tashi/client/test.py   (with props)
Modified:
    incubator/tashi/trunk/src/tashi/client/tashi-admin.py

Modified: incubator/tashi/trunk/src/tashi/client/tashi-admin.py
URL: http://svn.apache.org/viewvc/incubator/tashi/trunk/src/tashi/client/tashi-admin.py?rev=1366273&r1=1366272&r2=1366273&view=diff
==============================================================================
--- incubator/tashi/trunk/src/tashi/client/tashi-admin.py (original)
+++ incubator/tashi/trunk/src/tashi/client/tashi-admin.py Fri Jul 27 04:17:23 2012
@@ -18,6 +18,7 @@
 # under the License.    
 
 import os.path
+import optparse
 import random
 import sys
 import types
@@ -109,49 +110,6 @@ def getDefaultNetwork():
 			break
 	return networkId
 
-def randomNetwork():
-	return [NetworkConfiguration(d={'mac':randomMac(), 'network':getDefaultNetwork()})]
-
-def parseDisks(arg):
-	try:
-		strDisks = arg.split(",")
-		disks = []
-		for strDisk in strDisks:
-			strDisk = strDisk.strip()
-			(l, __s, r) = stringPartition(strDisk, ":")
-			if (r == ""):
-				r = "False"
-			r = boolean(r)
-			disk = DiskConfiguration(d={'uri':l, 'persistent':r})
-			disks.append(disk)
-		return disks
-	except:
-		raise ValueError("Incorrect format for disks argument")
-
-def parseNics(arg):
-	try:
-		strNics = arg.split(",")
-		nics = []
-		for strNic in strNics:
-			strNic = strNic.strip()
-			(l, __s, r) = stringPartition(strNic, ":")
-			n = l
-			if (n == ''):
-				n = getDefaultNetwork()
-			n = int(n)
-			(l, __s, r) = stringPartition(r, ":")
-			ip = l
-			if (ip == ''):
-				ip = None
-			m = r
-			if (m == ''):
-				m = randomMac()
-			nic = NetworkConfiguration(d={'mac':m, 'network':n, 'ip':ip})
-			nics.append(nic)
-		return nics
-	except:
-		raise ValueError("Incorrect format for nics argument")
-
 def parseHints(arg):
 	try:
 		strHints = arg.split(",")
@@ -164,98 +122,6 @@ def parseHints(arg):
 	except:
 		raise ValueError("Incorrect format for hints argument")
 
-def getVmLayout():
-	_hosts = client.getHosts()
-	_instances = client.getInstances()
-	hosts = {}
-	for h in _hosts:
-		h.instances = []
-		h.instanceIds = []
-		h.usedMemory = 0
-		h.usedCores = 0
-		hosts[h.id] = h
-	for i in _instances:
-		if (i.hostId in hosts):
-			hosts[i.hostId].instanceIds.append(i.id)
-			hosts[i.hostId].instances.append(i.name)
-			hosts[i.hostId].usedMemory += i.memory
-			hosts[i.hostId].usedCores += i.cores
-	return hosts.values()
-
-def getSlots(cores, memory):
-	hosts = getVmLayout()
-	count = 0
-
-	if cores < 1:
-		print "Argument to cores must be 1 or greater."
-		return
-
-	if memory <= 0:
-		print "Argument to memory must be greater than 0."
-		return
-
-	for h in hosts:
-		if h.up is False or h.state != HostState.Normal:
-			continue
-		countbycores = int((h.cores - h.usedCores) / cores)
-		countbymemory = int((h.memory - h.usedMemory) / memory)
-		count += max(0, min(countbycores, countbymemory))
-
-	print "%d" % (count), "available slots for",
-	print ("instances", "instance")[count == 1],
-	print "with %d" % (cores),
-	print ("cores", "core")[cores == 1],
-	print "and %d MB memory." % (memory)
-	
-def createMany(instance, count):
-	# will create instances from 0 to count-1
-	l = len(str(count - 1))
-	basename = instance.name
-	instances = []
-	for i in range(0, count):
-		for nic in instance.nics:
-			nic.mac = randomMac()
-		instance.name = basename + (("-%" + str(l) + "." + str(l) + "d") % (i))
-		instances.append(client.createVm(instance))
-	return instances
-
-def shutdownMany(basename):
-	return __shutdownOrDestroyMany("shutdown", basename)
-
-def destroyMany(basename):
-	return __shutdownOrDestroyMany("destroy", basename)
-
-def __shutdownOrDestroyMany(method, basename):
-	instances = client.getInstances()
-	count = 0
-	for i in instances:
-		if (i.name.startswith(basename + "-") and i.name[len(basename)+1].isdigit()):
-			# checking permissions here
-			checkIid(i.name)
-
-			if method == "shutdown":
-				client.shutdownVm(i.id)
-
-			elif method == "destroy":
-				client.destroyVm(i.id)
-
-			else:
-				raise ValueError("Unknown method")
-
-			count = count + 1
-	if (count == 0):
-		raise TashiException({'msg':"%s is an unused basename" % basename})
-	return None
-
-def getMyInstances():
-	userId = getUser()
-	_instances = client.getInstances()
-	instances = []
-	for i in _instances:
-		if (i.userId == userId):
-			instances.append(i)
-	return instances
-
 # Used to define default views on functions and to provide extra functionality (getVmLayout)
 extraViews = {
 'getSlots': (getSlots, None),
@@ -534,9 +400,12 @@ def main():
 	INDENT = (os.getenv("INDENT", 4))
 	if (len(sys.argv) < 2):
 		usage()
-	function = matchFunction(sys.argv[1])
+
 	config = Config(["Client"])
 
+	# get command name
+	function = matchFunction(sys.argv[1])
+
 	# build a structure of possible arguments
 	possibleArgs = {}
 	argList = argLists[function]
@@ -564,14 +433,6 @@ def main():
 				usage(function)
 				# this exits
 
-			if (arg.startswith("--hide-")):
-				show_hide.append((False, arg[7:]))
-				continue
-
-			if (arg.startswith("--show-")):
-				show_hide.append((True, arg[7:]))
-				continue
-
 			if (arg.startswith("--")):
 				if (arg[2:] in possibleArgs):
 					(parg, conv, default, required) = possibleArgs[arg[2:]]

Added: incubator/tashi/trunk/src/tashi/client/test.py
URL: http://svn.apache.org/viewvc/incubator/tashi/trunk/src/tashi/client/test.py?rev=1366273&view=auto
==============================================================================
--- incubator/tashi/trunk/src/tashi/client/test.py (added)
+++ incubator/tashi/trunk/src/tashi/client/test.py Fri Jul 27 04:17:23 2012
@@ -0,0 +1,52 @@
+#!/usr/bin/python
+
+import optparse
+import sys
+
+def setHostState(args):
+	parser = optparse.OptionParser()
+	parser.add_option("--host")
+	return 0
+
+def setHostNotes(args):
+	parser = optparse.OptionParser()
+	return 0
+
+cmdsdesc = (
+("setHostState", "Sets host state"),
+("setHostNotes", "Annotates a host"),
+)
+
+cmds = {
+'setHostState': setHostState,
+'setHostNotes': setHostNotes,
+}
+
+def main():
+	if len(sys.argv) < 2:
+		print "too few args"
+		sys.exit(-1)
+
+	scriptname = sys.argv[0]
+	cmd = sys.argv[1]
+	args = sys.argv[2:]
+
+	print "scriptname: %s" % (scriptname)
+	print "cmd: %s" % (cmd)
+
+	# allow use of lower case for commands
+	lccmddict = dict((foo.lower(), foo) for foo in cmds.keys())
+
+	if cmd.lower() not in lccmddict:
+		print "not a valid command"
+		sys.exit(-1)
+
+	handler = cmds[lccmddict[cmd]]
+
+	rv = handler(args)
+	return rv
+
+
+if __name__ == "__main__":
+	rv = main()
+	sys.exit(rv)

Propchange: incubator/tashi/trunk/src/tashi/client/test.py
------------------------------------------------------------------------------
    svn:executable = *



Mime
View raw message