示例#1
0
def getVncUrl(argv):
	write_file_name = "filename.csv";
	LogDump.dumpLog("getVncUrl :  called...")
	
	authDict = {}
	KEY_FILE = "key.conf"
	urlDict={}
	# get the options from cammand line
	try:
		opts, args = getopt.getopt(argv, "h", ["help"])
	except getopt.GetoptError:
		print 'getVncUrl : will create a xls file which shows vnc console url for each instances'
		LogDump.dumpLog("getVncUrl : option exception throwed") 
		return 0
	
	
	# iterate the otion object and get each option and its value
	for opt,arg in opts:
		if opt in ("-h","--help","help") :
			print 'getVncUrl : will create a xls file which shows vnc console url for each instances'
			LogDump.dumpLog("getVncUrl : asked help ")
			return 0
		
	authDict = configReader.readConfigFile(KEY_FILE,"KeystoneAuth")
	#get the nova object
	nova=client.Client(authDict['versionnumber'], authDict['username'], authDict['password'], authDict['tennantname'], authDict['authurl'])
	instance = nova.servers.list()
	target = open(write_file_name, 'w')
	target.write("VM_NAME, STATUS , CONSOLE_URL\n");
	for each in instance:
		print each.get_vnc_console('novnc')['console']['url']
		target.write(each.name+","+each.status+","+each.get_vnc_console('novnc')['console']['url']+"\n")
	target.close()
		
	return 0
示例#2
0
def createVm(argv):
	LogDump.dumpLog("createvm :  called...")
	
	osname = ""
	flavor = ""
	vmname = ""
	authDict = {}
	numberOfVms = 1
	vmCreated={}
	KEY_FILE = "key.conf"
	#vmCreated['createVmStat'] = 'error'
	# get the options from cammand line
	try:
		opts, args = getopt.getopt(argv, "ho:f:n:c:", ["help", "osname=", "flavor=", "vmname=","count="])
	except getopt.GetoptError:
		print 'please use the bellow syntax'
		print 'createVM.py -o <os_name> -f <flavour> -n <vm_name> -c <number_of_instance>'
		print 'createVM.py --osname=<os_name> --flavor=<flavour> --vmname=<vm_name> --count=<number_of_instance>'
		LogDump.dumpLog("createVm : option exception throwed") 
		return vmCreated
	
	if len(opts) < 3:
		print 'please use the bellow syntax'
		print 'createVM.py -o <os_name> -f <flavour> -n <vm_name> -c <number_of_instance>'
		print 'createVM.py --osname=<os_name> --flavor=<flavour> --vmname=<vm_name> --count=<number_of_instance>'
		LogDump.dumpLog("createVm : missed syntax")
		return vmCreated
	# iterate the otion object and get each option and its value
	for opt,arg in opts:
		if opt == '-h':
			print 'please use the bellow syntax'
			print 'createVM.py -o <os_name> -f <flavour> -n <vm_name> -c <number_of_instance>'
			print 'createVM.py --osname=<os_name> --flavor=<flavour> --vmname=<vm_name> --count=<number_of_instance>'
			LogDump.dumpLog("createVm : asked help ")
			return vmCreated
		elif opt in ("-o","--osname"):
			osname = arg
		elif opt in ("-f","--flavor"):
			flavor = arg
		elif opt in ("-n","--vmname"):
			vmname = arg
		elif opt in ("-c","--count"):
			try:			
				if int(arg) > 0 :
					numberOfVms = int(arg)
			except: 
				print 'please use the bellow syntax'
				print 'createVM.py -o <os_name> -f <flavour> -n <vm_name> -c <number_of_instance>'
				print 'createVM.py --osname=<os_name> --flavor=<flavour> --vmname=<vm_name> --count=<number_of_instance>'
				print '-c & --count arguement should be INTEGERS'
				LogDump.dumpLog("createVm : accepted arguements is iteger but ecieved string "+opt + " "+ arg)
				return vmCreated	
	
	authDict = configReader.readConfigFile(KEY_FILE,"KeystoneAuth")
	#get the nova object
	nova=client.Client(authDict['versionnumber'],authDict['username'],authDict['password'],authDict['tennantname'],authDict['authurl'])
	# create a vm after getting the inputs
	#find the image object
	image=nova.images.find(name=osname)
	#find te flavor
	flavor=nova.flavors.find(name=flavor)
	#iterate --count/-c times to create --count/-c number of VMs ;each VM's name suffixed with i
	for i in range(0,numberOfVms):
		#if not nova.servers.find(name = vmname+str(i)):
		try:
			vm=nova.servers.find(name = vmname+str(i))
			vmCreated[vmname+str(i)] = vm.id
			LogDump.dumpLog("createvm : "+vmname+str(i)+" ID : "+ vm.id+ " already present !!")
			print "createvm : "+vmname+str(i)+" ID : "+ vm.id+ " already present !!"
		except :
			# if exception is created then vm with name is not present..so create a new one
			vm=nova.servers.create(name=vmname+str(i),image=image,flavor=flavor)
			print "creating VM named : "+vmname+str(i)
			if vm.status == "BUILD" :
				#print vm.networks['private'][0]
				print "ID : "+ vm.id
				print "Build Success !!"
				vmCreated[vmname+str(i)] = vm.id
				LogDump.dumpLog("createvm : "+vmname+str(i)+"ID : "+ vm.id+ "Build Success !!")
			else:
				print vmname+str(i) + " : Buid Failed .. "
				LogDump.dumpLog("createvm : "+vmname+str(i)+ " Buid Failed ..")
			
		#else:
		#	print "vm already exists"
			
	# return the dictionary {('vmname':'ID')}
	print vmCreated
	return vmCreated
示例#3
0
def deleteVm(argv):
    LogDump.dumpLog("deletevm :  called...")

    vmname = ""
    authDict = {}
    KEY_FILE = "key.conf"
    option = ""
    vmDeleted = {}
    # get the options from cammand line
    try:
        opts, args = getopt.getopt(argv, "han:s:", ["help", "all", "name=", "series="])
    except getopt.GetoptError:
        print "please use the bellow syntax"
        print "deletevm.py -a -n <vm_name> -s <series_name>"
        print "deletevm.py --all --name=<vm_name> --series=<series_name> "
        LogDump.dumpLog("deletevm : option exception throwed")
        return 0

    if len(opts) < 1:
        print "please use the bellow syntax"
        print "deletevm.py -a -n <vm_name> -s <series_name>"
        print "deletevm.py --all --name=<vm_name> --series=<series_name> "
        LogDump.dumpLog("deletevm : missed syntax")
        return 0
        # iterate the otion object and get each option and its value
    for opt, arg in opts:
        if opt in ("-h", "--help", "help"):
            print "please use the bellow syntax"
            print "deletevm.py -a -n <vm_name> -s <series_name>"
            print "deletevm.py --all --name=<vm_name> --series=<series_name> "
            LogDump.dumpLog("deletevm : asked help ")
            return 0
        elif opt in ("-a", "--all"):
            option = "all"
            break
        elif opt in ("-n", "--name"):
            vmname = arg
            option = "specific"
            if 0 >= len(vmname):
                print "please enter the specific vm name"
                print "deletevm.py -a -n <vm_name> -s <series_name>"
                print "deletevm.py --all --name=<vm_name> --series=<series_name> "
                return 0
            break
        elif opt in ("-s", "--series"):
            vmname = arg
            option = "series"
            if 0 >= len(vmname):
                print "please enter the vm series name excluding suffixed index"
                print "deletevm.py -a -n <vm_name> -s <series_name>"
                print "deletevm.py --all --name=<vm_name> --series=<series_name> "
                return 0
            break

    authDict = configReader.readConfigFile(KEY_FILE, "KeystoneAuth")
    # get the nova object
    nova = client.Client(
        authDict["versionnumber"],
        authDict["username"],
        authDict["password"],
        authDict["tennantname"],
        authDict["authurl"],
    )

    if option == "specific":
        try:
            instance = nova.servers.find(name=vmname)
            instance.delete()
        except:
            print "no VM with the name %s" % vmname
            return 0
    if option == "all":
        try:
            instances = nova.servers.list()
            for instance in instances:
                instance.delete()
        except:
            print "no VM present"
            return 0

    if option == "series":
        i = 0
        try:
            while 1:
                instance = nova.servers.find(name=vmname + str(i))
                instance.delete()
                i = i + 1

        except:
            if i == 0:
                print "No series found with name " + vmname + str(i)
            else:
                print str(i) + " instance(s) deleted of series - " + vmname

    return 0