def __init__(self): self.stdin_path = '/dev/null' self.stdout_path = '/dev/tty' self.stderr_path = '/dev/tty' self.pidfile_path = '/tmp/foo.pid' self.pidfile_timeout = 5 self.vmCreated={} #logDict = configReader.readConfigFile("logdetails") #LOG_FILENAME = logDict['deamonlog'] #take the filename of info from key.conf deamDict = configReader.readConfigFile("key.conf","infod") self.paramDict = configReader.readConfigFile(deamDict["file"],deamDict["package"]) print self.paramDict try : authDict = configReader.readConfigFile("key.conf","KeystoneAuth") #get the nova object self.nova=client.Client(authDict['versionnumber'],authDict['username'],authDict['password'],authDict['tennantname'],authDict['authurl']) except : print "Authentication failure, please check the credentials. Terminating Deamon..." sys.exit(1) try : self.image=self.nova.images.find(name=self.paramDict['osname']) #find te flavor self.flavor=self.nova.flavors.find(name=self.paramDict['flavorname']) except: print "Please check the key.conf file for "+ deamDict["file"] + " and "+ deamDict["package"] +"section for osname or flavor. Terminating deamon..." #print paramDict sys.exit(1) try : self.hourInput = int(self.paramDict['start-hour']); if self.hourInput > 23: print "hours should less than 23. Terminating Deamon..." sys.exit(1) self.minuteInput = int(self.paramDict['start-minute']); if self.minuteInput > 59: print "minutes should less than 60. Terminating Deamon..." sys.exit(1) self.intsanceCount = int(self.paramDict['intsancecount']); if self.intsanceCount > 35 | self.intsanceCount < 1: print "Instance count should less than 35. Terminating Deamon..." sys.exit(1) except : print "please enter hour and minute in integers. Terminating Deamon..." sys.exit(1) try : #validation needed self.vmname = self.paramDict['vmname']; if len(self.vmname) == 0: self.vmname = deamDict["package"] print "vmname is explicitly empty. taking section name by default : "+self.vmname except : print "vmname not found. Terminating Deamon..." sys.exit(1)
def dumpLog(logString): logDict = configReader.readConfigFile("key.conf","logdetails") LOG_FILENAME = logDict['createvmlog'] logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG) #print datetime.datetime.now() logging.debug( str(datetime.datetime.now()) + " " + logString) return 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
def InitializeDeamon(self): self.vmCreated={} #holds the key/value pair of vm created #bellow cade will not be demonized #take the filename to create log logDict = configReader.readConfigFile("key.conf", "logdetails") self.LOG_FILENAME = logDict['deamonlog'] #dump status log self.dumplogd("createvm : called initialization") #get file from key file deamDict = configReader.readConfigFile("key.conf", "infod") #retrieve the initialization details self.paramDict = configReader.readConfigFile(deamDict["file"],deamDict["package"]) #print self.paramDict # this will print in terminal #check each pareameter and validate try : authDict = configReader.readConfigFile("key.conf", "KeystoneAuth") #get the nova object self.nova=client.Client(authDict['versionnumber'],authDict['username'],authDict['password'],authDict['tennantname'],authDict['authurl']) except : print "Authentication failure, please check the credentials. Terminating Deamon..." sys.exit(1) try : self.image=self.nova.images.find(name=self.paramDict['osname']) #find te flavor self.flavor=self.nova.flavors.find(name=self.paramDict['flavorname']) except: print "Please check the key.conf file for " + deamDict["file"] + " and " + deamDict["package"] + "section for osname or flavor. Terminating deamon..." #print paramDict sys.exit(1) try : # boundry checking and type checking for hour minute and instance count self.startHour = int(self.paramDict['start-hour']); if self.startHour > 23 | self.startHour < 0 : print "hours should less than 23. Terminating Deamon..." sys.exit(1) self.startMinute = int(self.paramDict['start-minute']); if self.startMinute > 59 | self.startMinute < 0 : print "minutes should less than 60. Terminating Deamon..." sys.exit(1) self.endHour = int(self.paramDict['end-hour']); if self.endHour > 23 | self.endHour < 0 : print "hour should less than 23. Terminating Deamon..." sys.exit(1) self.endMinute = int(self.paramDict['end-minute']); if self.endMinute > 59 | self.endMinute < 0: print "minutes should less than 60. Terminating Deamon..." sys.exit(1) self.intsanceCount = int(self.paramDict['intsancecount']); if self.intsanceCount > 35 | self.intsanceCount < 1: print "Instance count should less than 35. Terminating Deamon..." sys.exit(1) except : print "please enter hour, minute, intsace count in integer in integers. Terminating Deamon..." sys.exit(1) try : #validation needed self.vmname = self.paramDict['vmname']; if len(self.vmname) == 0: self.vmname = deamDict["package"] print "vmname is explicitly empty. taking section name by default : " + self.vmname except : print "vmname not found. Terminating Deamon..." sys.exit(1) self.dumplogd("createvmd : end of initialization ") print "Deamon Initialized and running." print "Check "+self.LOG_FILENAME+" for loges dumped by deamon " print "to stop use: %s stop|restart" % sys.argv[0] pass
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
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
def createVm(argv): osname = "" flavor = "" vmname = "" authDict = {} numberOfVms = 1 vmCreated={} #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>' 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>' 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>' 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' return vmCreated authDict = configReader.readConfigFile("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): 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 else: print vmname+str(i) + " : Buid Failed .. " # return the dictionary {('vmname':'ID')} return vmCreated