def vmware_entry_point(_VirtualMachine): _Thread = heavy_thread(vmware_run, (_VirtualMachine, _MachineCount)) _ThreadList.append(_Thread) _VirtualMachine._m_thread = _Thread # pre run code here _VirtualMachine.pre_run_function() _Thread.set_active(True) _Thread.start()
def clone_vm(_VirtualMachine, _ClonedMachine): # cloning a virtual machine is defined to be copying the vmdk file or virtual # hard drive and the .vmx file _CloneLock.acquire() # extremely ram intensive process therefore cloning will only be allowed to one at a time def _os_clone_and_copy(_StartArg): print "Copying the virtual disk (this operation may take some time)..." print "Forking this operation to a seperate thread..." _Thread.set_active(True) os_copy(_StartArg) _ClonedMachine.set_disk_created(True) if _ClonedMachine.disk_created(): print "Disk Cloned" _Command = "sudo vmrun -T ws snapshot " + "\"" + _ClonedMachine._m_directory + _ClonedMachine._m_config_file + "\"" + " NewMachineSnapShot" print "Creating Snapshot" os.system(_Command) _Thread.sleep(20) # make sure the snapshot finishes before starting _ClonedMachine._m_disk_file = find_snapshot(_ClonedMachine) print "Snapshot Created continuing..." print "Completed copying the virtual disk, joining back to main thread..." _Thread.set_active(False) _ThreadList.remove(_Thread) _CloneLock.release() _ClonedMachine() # callback function is nested inside the virtual machine class _CopyDiskCommand = "vmware-vdiskmanager -r " + "\"" + _VirtualMachine._m_directory + _VirtualMachine._m_disk_file + "\"" + " -t 0 " + "\"" + _ClonedMachine._m_directory + _ClonedMachine._m_disk_file + "\"" # long operation, fork to a seperate thread _Thread = heavy_thread(_os_clone_and_copy, (_CopyDiskCommand,)) _ThreadList.append(_Thread) _Url = get_url() if _Url is None: return _DirUrl = _Url[7:] if (_Shelve.has_key(_DirUrl)): while(_Shelve.has_key(_DirUrl)): _Url = get_url() _DirUrl = _Url[7:] _SavedUrl = _DirUrl _DirUrl = _DirUrl.replace("/", "_") _DirUrl = _DirUrl.replace(".", "_") _Shelve[_SavedUrl] = _DirUrl _DirCommand = "sudo mkdir " + "\"" + "/media/hdd0/Virtual Machines/collected/" + _DirUrl + "\"" _ClonedMachine._m_collection = "/media/hdd0/Virtual Machines/collected/" + _DirUrl _ClonedMachine._m_url = _Url print _DirCommand os.system(_DirCommand) _Thread.start() _File = open(_VirtualMachine._m_directory + _VirtualMachine._m_config_file) # default open to read only # Check to see if the .vmx file that is being copied will automatically ignore copying the .vmdk file. # If not then add in two lines that will auto answer the prompted message to be true _Matched = False _ReWriteFile = True for _Line in _File: if re.match("uuid.action = *", _Line): _Matched = True elif re.match("scsi0:0.fileName = *", _Line): if re.match("scsi0:0.fileName = " + _ClonedMachine._m_disk_file, _Line): _ReWriteFile = False _File.close() _File = open(_VirtualMachine._m_directory + _VirtualMachine._m_config_file) # default read only _Lines = _File.readlines() if _ReWriteFile: _File = open(_ClonedMachine._m_directory + _ClonedMachine._m_config_file, 'w') for _Line in _Lines: if not re.match("scsi0:0.fileName = *", _Line): _File.write(_Line) _File.write("scsi0:0.fileName = " + "\"" + _ClonedMachine._m_disk_file + "\"") _File.write("\n") _File.close() _ClonedMachine.set_config_created(True) if _ClonedMachine.config_created(): print "VMX file cloned" if not _Matched: _File = open(_ClonedMachine._m_directory + _ClonedMachine._m_config_file, 'a') # open the new file to append _File.write("uuid.action = \"create\"") _File.write("\n") # new line _File.write("msg.autoAnswer = \"TRUE\"") _File.close() return _ClonedMachine, _Thread
def vmware_create(_WorkingDirectory, _DestinationDirectory, _StartUpFile = None, _NewVmx = "default.vmx", _NewVmdk = "default.vmdk", _CloneMachine = True, _ContinueNumber = 0, _Gui = True): _Vmx, _Vmdk = find_files(_WorkingDirectory) _VirtualMachine = vm(_WorkingDirectory, _Vmx, _Vmdk, _StartUpFile, True, True, _ContinueNumber, _Gui) _Vmx = _NewVmx _Vmdk = _NewVmdk if _CloneMachine: _ClonedMachine = vm(_DestinationDirectory, _Vmx, _Vmdk, _StartUpFile, False, False, _VirtualMachine._m_continue, _Gui) else: _ClonedMachine = vm(_DestinationDirectory, _Vmx, _Vmdk, _StartUpFile, True, True, _VirtualMachine._m_continue, _Gui) _ClonedMachine.set_function(vmware_entry_point) # increasing machine count ** need to lock ** _Lock.acquire() _MachineCount.value = _MachineCount.value + 1 _Lock.release() # synchronize finished if _CloneMachine: _ContinueThread = heavy_thread(continue_execution, (_ClonedMachine, None, True, _VirtualMachine,)) _ThreadList.append(_ContinueThread) _ContinueThread.start() else: _ClonedMachine._m_disk_file = find_snapshot(_ClonedMachine) _Url = get_url() if _Url is None: return _DirUrl = _Url[7:] if (_Shelve.has_key(_DirUrl)): while(_Shelve.has_key(_DirUrl)): _Url = get_url() _DirUrl = _Url[7:] _SavedUrl = _DirUrl _DirUrl = _DirUrl.replace("/", "_") _DirUrl = _DirUrl.replace(".", "_") _Shelve[_SavedUrl] = _DirUrl _DirCommand = "sudo mkdir " + "\"" + "/media/hdd0/Virtual Machines/collected/" + _DirUrl + "\"" _ClonedMachine._m_collection = "/media/hdd0/Virtual Machines/collected/" + _DirUrl _ClonedMachine._m_url = _Url print _DirCommand os.system(_DirCommand) _Thread = heavy_thread(continue_execution, (_ClonedMachine, None, False,)) _ThreadList.append(_Thread) _Thread.start() return _ClonedMachine
def clone_vm(_VirtualMachine, _ClonedMachine): # cloning a virtual machine is defined to be copying the vmdk file or virtual # hard drive and the .vmx file _CloneLock.acquire() # extremely ram intensive process therefore cloning will only be allowed to one at a time def _os_clone_and_copy(_StartArg): print "Copying the virtual disk (this operation may take some time)..." print "Forking this operation to a seperate thread..." _Thread.set_active(True) os_copy(_StartArg) _ClonedMachine.set_disk_created(True) if _ClonedMachine.disk_created(): print "Disk Cloned" _Command = ( "vmrun -T ws snapshot " + '"' + _ClonedMachine._m_directory + _ClonedMachine._m_config_file + '"' + " NewMachineSnapShot" ) print "Creating Snapshot" os.system(_Command) _Thread.sleep(15) # make sure the snapshot finishes before starting _ClonedMachine._m_disk_file = find_snapshot(_ClonedMachine) print "Snapshot Created continuing..." print "Completed copying the virtual disk, joining back to main thread..." _Thread.set_active(False) _ThreadList.remove(_Thread) _CloneLock.release() _ClonedMachine() # callback function is nested inside the virtual machine class _CopyDiskCommand = ( "vmware-vdiskmanager -r " + '"' + _VirtualMachine._m_directory + _VirtualMachine._m_disk_file + '"' + " -t 0 " + '"' + _ClonedMachine._m_directory + _ClonedMachine._m_disk_file + '"' ) # long operation, fork to a seperate thread _Thread = heavy_thread(_os_clone_and_copy, (_CopyDiskCommand,)) _ThreadList.append(_Thread) _CollectionNumber = get_number() _DirCommand = ( "mkdir " + '"' + "/home/jarret/Virtual Machines/collected/virtual_machine" + str(_CollectionNumber) + '"' ) _ClonedMachine._m_collection = "/home/jarret/Virtual Machines/collected/virtual_machine" + str(_CollectionNumber) print _DirCommand os.system(_DirCommand) _Thread.start() _File = open(_VirtualMachine._m_directory + _VirtualMachine._m_config_file) # default open to read only # Check to see if the .vmx file that is being copied will automatically ignore copying the .vmdk file. # If not then add in two lines that will auto answer the prompted message to be true _Matched = False _ReWriteFile = True for _Line in _File: if re.match("uuid.action = *", _Line): _Matched = True elif re.match("scsi0:0.fileName = *", _Line): if re.match("scsi0:0.fileName = " + _ClonedMachine._m_disk_file, _Line): _ReWriteFile = False _File.close() _File = open(_VirtualMachine._m_directory + _VirtualMachine._m_config_file) # default read only _Lines = _File.readlines() if _ReWriteFile: _File = open(_ClonedMachine._m_directory + _ClonedMachine._m_config_file, "w") for _Line in _Lines: if not re.match("scsi0:0.fileName = *", _Line): _File.write(_Line) _File.write("scsi0:0.fileName = " + '"' + _ClonedMachine._m_disk_file + '"') _File.write("\n") _ClonedMachine.set_config_created(True) if _ClonedMachine.config_created(): print "VMX file cloned" if not _Matched: _File = open(_ClonedMachine._m_directory + _ClonedMachine._m_config_file, "a") # open the new file to append _File.write('uuid.action = "create"') _File.write("\n") # new line _File.write('msg.autoAnswer = "TRUE"')
################################################################################ # # Author: Jarret Shook # # Module: ht_test.py # # Modifications: # # 1-April-13: Version 1.0: Last Updated # 1-April-13: Version 1.0: Created # # Timeperiod: ev6 # ################################################################################ ################################################################################ from heavy_thread import heavy_thread ################################################################################ ################################################################################ def foo(_Args): print _Args if __name__ == '__main__': ht = heavy_thread(foo, ('hello',)) ht.start() ht.join()
def clone_vm(_VirtualMachine, _ClonedMachine): _CloneLock.acquire() # ram intensive process therefore cloning will be limited to one VM at a time def _os_clone_and_copy(_StartArg): print "Copying the virtual disk (this operation will take some time)..." print "Forking this operation to a seperate thread..." _Thread.set_active(True) os_copy(_StartArg) _ClonedMachine.set_disk_created(True) print "Disk Cloned" _Command = "sudo vmrun -T ws snapshot " + "\"" + _ClonedMachine._m_directory + _ClonedMachine._m_config_file + "\"" + " NewMachineSnapShot" print "Creating Snapshot" os.system(_Command) _Thread.sleep(20) # make sure the snapshot finishes before starting _ClonedMachine._m_disk_file = find_snapshot(_ClonedMachine) print "Snapshot Created continuing..." print "Completed copying the virtual disk, joining back to main thread..." _Thread.set_active(False) _ThreadList.remove(_Thread) _CloneLock.release() ##################################################### # Call the callback function from the VM class ##################################################### _ClonedMachine() ######################################################## # vmware-vdiskmanager -r <path to source .vmdk> -t 0 <path to destination .vmdk> # # -r: # source disk location # # -t # 0 - to create a growable virtual disk contained in a single virtual disk file # 1 - to create a growable virtual disk split into 2GB files # 2 - to create a preallocated virtual disk contained in a single virtual disk file # 3 - to create a preallocated virtual disk split into 2GB files ######################################################## _CopyDiskCommand = "vmware-vdiskmanager -r " + "\"" + _VirtualMachine._m_directory + _VirtualMachine._m_disk_file + "\"" + " -t 0 " + "\"" + _ClonedMachine._m_directory + _ClonedMachine._m_disk_file + "\"" # long operation, fork to a seperate thread _Thread = heavy_thread(_os_clone_and_copy, (_CopyDiskCommand,)) ######################################################## # Makes and starts thread then continues execution here ######################################################## _ThreadList.append(_Thread) _Url = get_url() if _Url is None: return _DirUrl = _Url[7:] if (_Shelve.has_key(_DirUrl)): while(_Shelve.has_key(_DirUrl)): _Url = get_url() if _Url is None: return _DirUrl = _Url[7:] _SavedUrl = _DirUrl _DirUrl = _DirUrl.replace("/", "_") _DirUrl = _DirUrl.replace(".", "_") _Shelve[_SavedUrl] = _DirUrl _DirCommand = "sudo mkdir " + "\"" + _ClonedMachine._m_collection_destination + _DirUrl + "\"" _ClonedMachine._m_collection = _ClonedMachine._m_collection_destination + _DirUrl _ClonedMachine._m_url = _Url print _DirCommand os.system(_DirCommand) _Thread.start() _File = open(_VirtualMachine._m_directory + _VirtualMachine._m_config_file) # default open to read only ######################################################## # Check to see if the .vmx file that is being copied # will automatically ignore copying the .vmdk file. # If not then add in two lines that will auto answer # the prompted message to be true ######################################################## _Matched = False _ReWriteFile = True for _Line in _File: if re.match("uuid.action = *", _Line): _Matched = True elif re.match("scsi0:0.fileName = *", _Line): if re.match("scsi0:0.fileName = " + _ClonedMachine._m_disk_file, _Line): _ReWriteFile = False _File.close() _File = open(_VirtualMachine._m_directory + _VirtualMachine._m_config_file) # default read only _Lines = _File.readlines() if _ReWriteFile: _File = open(_ClonedMachine._m_directory + _ClonedMachine._m_config_file, 'w') for _Line in _Lines: if not re.match("scsi0:0.fileName = *", _Line): _File.write(_Line) _File.write("scsi0:0.fileName = " + "\"" + _ClonedMachine._m_disk_file + "\"") _File.write("\n") _File.close() _ClonedMachine.set_config_created(True) if _ClonedMachine.config_created(): print "VMX file cloned" if not _Matched: _File = open(_ClonedMachine._m_directory + _ClonedMachine._m_config_file, 'a') # open the new file to append _File.write("uuid.action = \"create\"") _File.write("\n") # new line _File.write("msg.autoAnswer = \"TRUE\"") _File.close() return _ClonedMachine, _Thread