def genisoimageOptions(self, label=None, udf=False, ignoreJoliet=True): """Auxiliary method, called by cloneWithModifications. Can be overridden by subclass methods genisoimageOptions, which may want to extend the returned list. Could be improved in the future. Could recognize content of .iso image. Could select different options depending on content of .iso image. Maybe could use iso-info -d 9 -i self.isoImagePath. Could be overridden for a subclass.""" # this implementation has been made to be a workable basis for most uses if not label: label = Timestamp.microsecondTimestamp() genisoimageOptions = [] if udf: # udf genisoimageOptions.append("-udf") if not ignoreJoliet: # broader compatibility of filenames and metadata genisoimageOptions.append("-J") genisoimageOptions.extend([ # broader compatibility of filenames and metadata "-r", "-T", "-f", # # possibly needed labeling, # volume id, volume name or label, max 32 characters "-V", label[-32:] ]) return genisoimageOptions
def cloneWithModificationsUsingMount(self, modifications=[], cloneIsoImagePath=None, udf=False, ignoreJoliet=True, pause=False): """Clone with any number of instances of IsoImageModification applied. This is an older implementation which regrettably because of the mount command requires having superuser privileges. It is still here in case a newer implementation doesn't work right, which could be for any of a number of reasons, for example for symbolic links. A temporary assembly directory in the same directory as cloneIsoImagePath needs disk space, but it is removed automatically upon completion of cloning. modifications a list of IsoImageModification instances. cloneIsoImagePath if not given then in same directory with a timestamp in the filename. return IsoImage(cloneIsoImagePath).""" # timestamp to the microsecond should be good enough timestamp = Timestamp.microsecondTimestamp() # ensure there is a cloneIsoImagePath if not cloneIsoImagePath: # insert timestamp before extension isoImagePathSplitext = os.path.splitext(self._isoImagePath) cloneIsoImagePath = isoImagePathSplitext[0] + "." + timestamp + isoImagePathSplitext[1] if os.path.exists(cloneIsoImagePath): raise Exception("won't overwrite already existing {0}".format(cloneIsoImagePath)) temporaryMountDirectory = cloneIsoImagePath + ".mnt" temporaryAssemblyDirectory = cloneIsoImagePath + ".tmpdir" os.mkdir(temporaryMountDirectory, 0755) #os.mkdir(temporaryAssemblyDirectory, 0755) try: # mount self.mount(temporaryMountDirectory, udf=udf) # copy files from original .iso image print "copying files from {0}, this may take a few minutes".format(self._isoImagePath) shutil.copytree(temporaryMountDirectory, temporaryAssemblyDirectory, symlinks=True) # give a chance to look if pause: raw_input("you requested to pause before applying modifications, press Enter to continue:") # apply modifications print "applying modifications into {0}".format(temporaryAssemblyDirectory) for modification in modifications: modification.writeIntoAssembly(temporaryAssemblyDirectory) # give a chance to look if pause: raw_input("you requested to pause after applying modifications, press Enter to continue:") # make new .iso image file print "making new {0}, this may take a few minutes".format(cloneIsoImagePath) genisoimageOptions = self.genisoimageOptions(label=timestamp, udf=udf, ignoreJoliet=ignoreJoliet) CommandCapture(["genisoimage"] + genisoimageOptions + ["-o", cloneIsoImagePath, temporaryAssemblyDirectory], copyToStdio=False, exceptionIfAnyStderr=False) finally: # remove in a specific, hopefully most resilient order self.unmount() shutil.rmtree(temporaryAssemblyDirectory, ignore_errors=True) os.rmdir(temporaryMountDirectory) return IsoImage(cloneIsoImagePath)
def cloneWithModifications(self, modifications=[], cloneIsoImagePath=None, udf=False, ignoreJoliet=True, pause=False): """Clone with any number of instances of IsoImageModification applied. A temporary assembly directory in the same directory as cloneIsoImagePath needs disk space, but it is removed automatically upon completion of cloning. modifications a list of IsoImageModification instances. cloneIsoImagePath if not given then in same directory with a timestamp in the filename. return IsoImage(cloneIsoImagePath).""" # as of 2013-09-29 given known uses of this package and known bugs of iso-info # it appears better to default to ignoreJoliet=True # see https://savannah.gnu.org/bugs/?40130 # see https://savannah.gnu.org/bugs/?40138 # # timestamp to the microsecond should be good enough timestamp = Timestamp.microsecondTimestamp() # ensure there is a cloneIsoImagePath if not cloneIsoImagePath: # insert timestamp before extension isoImagePathSplitext = os.path.splitext(self._isoImagePath) cloneIsoImagePath = isoImagePathSplitext[0] + "." + timestamp + isoImagePathSplitext[1] if os.path.exists(cloneIsoImagePath): raise Exception("won't overwrite already existing {0}".format(cloneIsoImagePath)) temporaryAssemblyDirectory = cloneIsoImagePath + ".tmpdir" #os.mkdir(temporaryAssemblyDirectory, 0755) try: # copy files from original .iso image print "copying files from {0}, this may take a few minutes".format(self._isoImagePath) self.copyToDirectory(temporaryAssemblyDirectory, udf=udf, ignoreJoliet=ignoreJoliet) # give a chance to look if pause: raw_input("you requested to pause before applying modifications, press Enter to continue:") # apply modifications print "applying modifications into {0}".format(temporaryAssemblyDirectory) for modification in modifications: modification.writeIntoAssembly(temporaryAssemblyDirectory) # give a chance to look if pause: raw_input("you requested to pause after applying modifications, press Enter to continue:") # make new .iso image file print "making new {0}, this may take a few minutes".format(cloneIsoImagePath) if SystemRequirements.which("genisoimage"): # preferred choice makeIsoImageCommandName = "genisoimage" elif SystemRequirements.which("mkisofs"): # acceptable choice makeIsoImageCommandName = "mkisofs" else: # preferred choice for error message makeIsoImageCommandName = "genisoimage" genisoimageOptions = self.genisoimageOptions(label=timestamp, udf=udf, ignoreJoliet=ignoreJoliet) CommandCapture([makeIsoImageCommandName] + genisoimageOptions + ["-o", cloneIsoImagePath, temporaryAssemblyDirectory], copyToStdio=False, exceptionIfAnyStderr=False) finally: # remove in a specific, hopefully most resilient order shutil.rmtree(temporaryAssemblyDirectory, ignore_errors=True) return IsoImage(cloneIsoImagePath)
inputFile.seek(start) with open(pathInTemporaryAssemblyDirectory, "wb") as outputFile: current = start while current < stop: remainder = stop - current chunk = min(remainder, 10240) bytes = inputFile.read(chunk) outputFile.write(bytes) current += chunk if __name__ == "__main__": from nrvr.util.requirements import SystemRequirements SystemRequirements.commandsRequiredByImplementations([IsoImage], verbose=True) # import tempfile _testDir = os.path.join(tempfile.gettempdir(), Timestamp.microsecondTimestamp()) os.mkdir(_testDir, 0755) try: _originalDir = os.path.join(_testDir, "cd") os.mkdir(_originalDir, 0755) with open(os.path.join(_originalDir, "cheese.txt"), "w") as outputFile: outputFile.write("please") os.mkdir(os.path.join(_originalDir, "empty")) os.mkdir(os.path.join(_originalDir, "something")) with open(os.path.join(_originalDir, u"something/\xf6sterreichischer K\xe4se.txt"), "w") as outputFile: outputFile.write("stinkt, aber gesund") os.mkdir(os.path.join(_originalDir, "tree")) with open(os.path.join(_originalDir, "tree/leaf.txt"), "w") as outputFile: outputFile.write("green") os.mkdir(os.path.join(_originalDir, "tree/branch")) with open(os.path.join(_originalDir, "tree/branch/fruit.txt"), "w") as outputFile:
def cloneWithModificationsUsingMount(self, modifications=[], cloneIsoImagePath=None, udf=False, ignoreJoliet=True, pause=False): """Clone with any number of instances of IsoImageModification applied. This is an older implementation which regrettably because of the mount command requires having superuser privileges. It is still here in case a newer implementation doesn't work right, which could be for any of a number of reasons, for example for symbolic links. A temporary assembly directory in the same directory as cloneIsoImagePath needs disk space, but it is removed automatically upon completion of cloning. modifications a list of IsoImageModification instances. cloneIsoImagePath if not given then in same directory with a timestamp in the filename. return IsoImage(cloneIsoImagePath).""" # timestamp to the microsecond should be good enough timestamp = Timestamp.microsecondTimestamp() # ensure there is a cloneIsoImagePath if not cloneIsoImagePath: # insert timestamp before extension isoImagePathSplitext = os.path.splitext(self._isoImagePath) cloneIsoImagePath = isoImagePathSplitext[ 0] + "." + timestamp + isoImagePathSplitext[1] if os.path.exists(cloneIsoImagePath): raise Exception("won't overwrite already existing {0}".format( cloneIsoImagePath)) temporaryMountDirectory = cloneIsoImagePath + ".mnt" temporaryAssemblyDirectory = cloneIsoImagePath + ".tmpdir" os.mkdir(temporaryMountDirectory, 0755) #os.mkdir(temporaryAssemblyDirectory, 0755) try: # mount self.mount(temporaryMountDirectory, udf=udf) # copy files from original .iso image print "copying files from {0}, this may take a few minutes".format( self._isoImagePath) shutil.copytree(temporaryMountDirectory, temporaryAssemblyDirectory, symlinks=True) # give a chance to look if pause: raw_input( "you requested to pause before applying modifications, press Enter to continue:" ) # apply modifications print "applying modifications into {0}".format( temporaryAssemblyDirectory) for modification in modifications: modification.writeIntoAssembly(temporaryAssemblyDirectory) # give a chance to look if pause: raw_input( "you requested to pause after applying modifications, press Enter to continue:" ) # make new .iso image file print "making new {0}, this may take a few minutes".format( cloneIsoImagePath) genisoimageOptions = self.genisoimageOptions( label=timestamp, udf=udf, ignoreJoliet=ignoreJoliet) CommandCapture( ["genisoimage"] + genisoimageOptions + ["-o", cloneIsoImagePath, temporaryAssemblyDirectory], copyToStdio=False, exceptionIfAnyStderr=False) finally: # remove in a specific, hopefully most resilient order self.unmount() shutil.rmtree(temporaryAssemblyDirectory, ignore_errors=True) os.rmdir(temporaryMountDirectory) return IsoImage(cloneIsoImagePath)
def cloneWithModifications(self, modifications=[], cloneIsoImagePath=None, udf=False, ignoreJoliet=True, pause=False): """Clone with any number of instances of IsoImageModification applied. A temporary assembly directory in the same directory as cloneIsoImagePath needs disk space, but it is removed automatically upon completion of cloning. modifications a list of IsoImageModification instances. cloneIsoImagePath if not given then in same directory with a timestamp in the filename. return IsoImage(cloneIsoImagePath).""" # as of 2013-09-29 given known uses of this package and known bugs of iso-info # it appears better to default to ignoreJoliet=True # see https://savannah.gnu.org/bugs/?40130 # see https://savannah.gnu.org/bugs/?40138 # # timestamp to the microsecond should be good enough timestamp = Timestamp.microsecondTimestamp() # ensure there is a cloneIsoImagePath if not cloneIsoImagePath: # insert timestamp before extension isoImagePathSplitext = os.path.splitext(self._isoImagePath) cloneIsoImagePath = isoImagePathSplitext[ 0] + "." + timestamp + isoImagePathSplitext[1] if os.path.exists(cloneIsoImagePath): raise Exception("won't overwrite already existing {0}".format( cloneIsoImagePath)) temporaryAssemblyDirectory = cloneIsoImagePath + ".tmpdir" #os.mkdir(temporaryAssemblyDirectory, 0755) try: # copy files from original .iso image print "copying files from {0}, this may take a few minutes".format( self._isoImagePath) self.copyToDirectory(temporaryAssemblyDirectory, udf=udf, ignoreJoliet=ignoreJoliet) # give a chance to look if pause: raw_input( "you requested to pause before applying modifications, press Enter to continue:" ) # apply modifications print "applying modifications into {0}".format( temporaryAssemblyDirectory) for modification in modifications: modification.writeIntoAssembly(temporaryAssemblyDirectory) # give a chance to look if pause: raw_input( "you requested to pause after applying modifications, press Enter to continue:" ) # make new .iso image file print "making new {0}, this may take a few minutes".format( cloneIsoImagePath) if SystemRequirements.which("genisoimage"): # preferred choice makeIsoImageCommandName = "genisoimage" elif SystemRequirements.which("mkisofs"): # acceptable choice makeIsoImageCommandName = "mkisofs" else: # preferred choice for error message makeIsoImageCommandName = "genisoimage" genisoimageOptions = self.genisoimageOptions( label=timestamp, udf=udf, ignoreJoliet=ignoreJoliet) CommandCapture( [makeIsoImageCommandName] + genisoimageOptions + ["-o", cloneIsoImagePath, temporaryAssemblyDirectory], copyToStdio=False, exceptionIfAnyStderr=False) finally: # remove in a specific, hopefully most resilient order shutil.rmtree(temporaryAssemblyDirectory, ignore_errors=True) return IsoImage(cloneIsoImagePath)
while current < stop: remainder = stop - current chunk = min(remainder, 10240) bytes = inputFile.read(chunk) outputFile.write(bytes) current += chunk if __name__ == "__main__": from nrvr.util.requirements import SystemRequirements SystemRequirements.commandsRequiredByImplementations([IsoImage], verbose=True) # import tempfile _testDir = os.path.join(tempfile.gettempdir(), Timestamp.microsecondTimestamp()) os.mkdir(_testDir, 0755) try: _originalDir = os.path.join(_testDir, "cd") os.mkdir(_originalDir, 0755) with open(os.path.join(_originalDir, "cheese.txt"), "w") as outputFile: outputFile.write("please") os.mkdir(os.path.join(_originalDir, "empty")) os.mkdir(os.path.join(_originalDir, "something")) with open( os.path.join(_originalDir, u"something/\xf6sterreichischer K\xe4se.txt"), "w") as outputFile: outputFile.write("stinkt, aber gesund") os.mkdir(os.path.join(_originalDir, "tree")) with open(os.path.join(_originalDir, "tree/leaf.txt"),
exampleVm.sleepUntilHasAcceptedKnownHostKey(ticker=True) # a possible choice pointed out #exampleVm.sshCommand([LinuxUtil.commandToEnableSudo(exampleVm.regularUser)]) # a possible choice pointed out #exampleVm.sshCommand([UbGnome.ubCommandToEnableAutoLogin(exampleVm.regularUser)]) # these ssh commands here are just a demo print "------" print exampleVm.sshCommand(["ls", "-al"]).output print "------" print exampleVm.sshCommand(["ls nonexistent ; echo `hostname`"]).output print "------" # these scp commands here are just a demo exampleDir = os.path.join(tempfile.gettempdir(), Timestamp.microsecondTimestamp()) os.mkdir(exampleDir, 0755) try: sendDir = os.path.join(exampleDir, "send") os.mkdir(sendDir, 0755) exampleFile1 = os.path.join(sendDir, "example1.txt") with open(exampleFile1, "w") as outputFile: outputFile.write("this is an example\n" * 1000000) scpExample1 = exampleVm.scpPutCommand(fromHostPath=exampleFile1, toGuestPath="~/example1.txt") print "returncode=" + str(scpExample1.returncode) print "output=" + scpExample1.output scpExample2 = exampleVm.scpGetCommand(fromGuestPath="/etc/hosts", toHostPath=exampleFile1) print "returncode=" + str(scpExample2.returncode) print "output=" + scpExample2.output with open(exampleFile1, "r") as inputFile: exampleFile1Content = inputFile.read()
#!/usr/bin/python """Example use of NrvrCommander. Public repository - https://github.com/srguiwiz/nrvr-commander Copyright (c) Nirvana Research 2006-2015. Simplified BSD License""" import os.path from nrvr.util.requirements import SystemRequirements from nrvr.util.times import Timestamp from nrvr.vm.vmware import VMwareHypervisor # this is a good way to preflight check SystemRequirements.commandsRequiredByImplementations([VMwareHypervisor], verbose=True) # this is a good way to preflight check VMwareHypervisor.localRequired() # demoing one way of making new VM names and directories, # timestamp to the microsecond should be good enough exampleName = "example" + Timestamp.microsecondTimestamp() exampleVmxFilePath = os.path.join(VMwareHypervisor.local.suggestedDirectory, exampleName, exampleName + ".vmx") print exampleVmxFilePath
#!/usr/bin/python """Example use of NrvrCommander. Public repository - https://github.com/srguiwiz/nrvr-commander Copyright (c) Nirvana Research 2006-2014. Modified BSD License""" import os.path from nrvr.util.requirements import SystemRequirements from nrvr.util.times import Timestamp from nrvr.vm.vmware import VMwareHypervisor # this is a good way to preflight check SystemRequirements.commandsRequiredByImplementations([VMwareHypervisor], verbose=True) # this is a good way to preflight check VMwareHypervisor.localRequired() # demoing one way of making new VM names and directories, # timestamp to the microsecond should be good enough exampleName = "example" + Timestamp.microsecondTimestamp() exampleVmxFilePath = os.path.join(VMwareHypervisor.local.suggestedDirectory, exampleName, exampleName + ".vmx") print exampleVmxFilePath