Example #1
0
            os.remove(pathInTemporaryAssemblyDirectory)
        # copy
        with open(self.pathOnHost, "rb") as inputFile:
            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:
Example #2
0
 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)
Example #3
0
        # copy
        with open(self.pathOnHost, "rb") as inputFile:
            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"),
Example #4
0
 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)
Example #5
0
                                     alsoNeedsScreen=alsoNeedsScreen):
            if not printed:
                # first time only printing
                if not alsoNeedsScreen:
                    message = "waiting for GUI to be available to connect to {0}"
                else:  # alsoNeedsScreen
                    message = "waiting for GUI and screen to be available to connect to {0}"
                print message.format(
                    IPAddress.asString(sshParameters.ipaddress))
                sys.stdout.flush()
                printed = True
            if ticker:
                if not ticked:
                    # first time only printing
                    sys.stdout.write("[")
                sys.stdout.write(".")
                sys.stdout.flush()
                ticked = True
            time.sleep(checkIntervalSeconds)
        if ticked:
            # final printing
            sys.stdout.write("]\n")
        if extraSleepSeconds:
            time.sleep(extraSleepSeconds)


if __name__ == "__main__":
    from nrvr.util.requirements import SystemRequirements
    SystemRequirements.commandsRequiredByImplementations([CygwinSshCommand],
                                                         verbose=True)
Example #6
0
        
        # ceiling division as shown at http://stackoverflow.com/questions/14822184/is-there-a-ceiling-equivalent-of-operator-in-python
        blocks = -(-len(ipaddresses) // maxConcurrency)
        if ticker:
            sys.stdout.write("[ping")
            sys.stdout.flush()
        for block in range(blocks):
            for i in range(block * maxConcurrency, min((block + 1) * maxConcurrency, len(ipaddresses))):
                ipaddress = ipaddresses[i]
                thread = Thread(target=ping, args=[ipaddress, i])
                threads[i] = thread
                thread.start()
            for i in range(block * maxConcurrency, min((block + 1) * maxConcurrency, len(ipaddresses))):
                thread = threads[i]
                thread.join()
                threads[i] = None
                if ticker:
                    sys.stdout.write(".")
                    sys.stdout.flush()
        if ticker:
            sys.stdout.write("]\n")
        return filter(lambda result: result != '-', results)

if __name__ == "__main__":
    from nrvr.util.requirements import SystemRequirements
    SystemRequirements.commandsRequiredByImplementations([Ping], verbose=True)
    #
    print Ping.respondingIpAddressesOf(["127.0.0.1", "127.0.0.2"])
    print Ping.respondingIpAddressesOf(map(lambda number: "127.0.0." + str(number), range(1, 31)))
    print Ping.respondingIpAddressesOf(map(lambda number: "127.0.0." + str(number), range(1, 132)))
Example #7
0
                                  copyToStdio=False,
                                  exceptionIfNotZero=False,
                                  exceptionIfAnyStderr=False)
        if ifconfig.returncode != 0 or ifconfig.stderr:
            return None
        interfaceAddressMatch = cls._networkInterfaceAddressRegex.search(
            ifconfig.stdout)
        if not interfaceAddressMatch:
            return None
        interfaceAddress = interfaceAddressMatch.group(1)
        return interfaceAddress


if __name__ == "__main__":
    from nrvr.util.requirements import SystemRequirements
    SystemRequirements.commandsRequiredByImplementations([NetworkInterface],
                                                         verbose=True)
    #
    print NetworkInterface.ipAddressOf("lo")
    print NetworkInterface.ipAddressOf("lo0")
    print NetworkInterface.ipAddressOf("vmnet1")
    print NetworkInterface.ipAddressOf("madesomethingup")


class NetworkConfigurationStaticParameters(
        namedtuple("NetworkConfigurationStaticParameters",
                   ["ipaddress", "netmask", "gateway", "nameservers"])):
    """Static IP options for network configuration.
    
    As implemented only supports IPv4."""

    __slots__ = ()
Example #8
0
from nrvr.distros.ub.kickstarttemplates import UbKickstartTemplates
from nrvr.machine.ports import PortsFile
from nrvr.process.commandcapture import CommandCapture
from nrvr.remote.ssh import SshCommand, ScpCommand
from nrvr.util.download import Download
from nrvr.util.ipaddress import IPAddress
from nrvr.util.nameserver import Nameserver
from nrvr.util.requirements import SystemRequirements
from nrvr.util.times import Timestamp
from nrvr.util.user import ScriptUser
from nrvr.vm.vmware import VmdkFile, VmxFile, VMwareHypervisor, VMwareMachine
from nrvr.vm.vmwaretemplates import VMwareTemplates

# this is a good way to preflight check
SystemRequirements.commandsRequiredByImplementations([IsoImage,
                                                      VmdkFile, VMwareHypervisor,
                                                      SshCommand, ScpCommand],
                                                     verbose=True)
# this is a good way to preflight check
VMwareHypervisor.localRequired()

# BEGIN essential example code
ipaddress = "192.168.0.166"
# a possible modification pointed out
# makes sense e.g. if used together with whateverVm.vmxFile.setEthernetAdapter(adapter, "hostonly")
#ipaddress = IPAddress.numberWithinSubnet(VMwareHypervisor.localHostOnlyIPAddress, 166)
rootpw = "redwood"
# Ubuntu kickstart supports only one regular user
regularUser = ("jack","rainbow")
# one possible way of making new VM names and directories
name = IPAddress.nameWithNumber("example", ipaddress, separator=None)
exampleVm = VMwareMachine(ScriptUser.loggedIn.userHomeRelative("vmware/examples/%s/%s.vmx" % (name, name)))
Example #9
0
                    # first time only printing
                    sys.stdout.write("[")
                sys.stdout.write(".")
                sys.stdout.flush()
                ticked = True
            time.sleep(checkIntervalSeconds)
        if ticked:
            # final printing
            sys.stdout.write("]\n")
            sys.stdout.flush()
        if extraSleepSeconds:
            time.sleep(extraSleepSeconds)

if __name__ == "__main__":
    from nrvr.util.requirements import SystemRequirements
    SystemRequirements.commandsRequiredByImplementations([SshCommand], verbose=True)
    #
    SshCommand.removeKnownHostKey("localhost")
    SshCommand.acceptKnownHostKey(SshParameters("localhost", "i", "madeitup"))
    # fictional address
    _exampleSshParameters = SshParameters("10.123.45.67", "root", "redwood")
#    _sshExample1 = SshCommand(_exampleSshParameters, "hostname")
#    print "returncode=" + str(_sshExample1.returncode)
#    print "output=" + _sshExample1.output
#    _sshExample2 = SshCommand(_exampleSshParameters, ["ls"])
#    print "returncode=" + str(_sshExample2.returncode)
#    print "output=" + _sshExample2.output
#    _sshExample3 = SshCommand(_exampleSshParameters, ["ls", "-al"])
#    print "returncode=" + str(_sshExample3.returncode)
#    print "output=" + _sshExample3.output
#    _sshExample4 = SshCommand(_exampleSshParameters, ["ls", "doesntexist"], exceptionIfNotZero=False)
        Return the IP address of the interface, or None."""
        # as implemented absent ifconfig command return as if interface not found
        ifconfig = CommandCapture(["ifconfig", networkInterfaceName],
                                  copyToStdio=False,
                                  exceptionIfNotZero=False, exceptionIfAnyStderr=False)
        if ifconfig.returncode != 0 or ifconfig.stderr:
            return None
        interfaceAddressMatch = cls._networkInterfaceAddressRegex.search(ifconfig.stdout)
        if not interfaceAddressMatch:
            return None
        interfaceAddress = interfaceAddressMatch.group(1)
        return interfaceAddress

if __name__ == "__main__":
    from nrvr.util.requirements import SystemRequirements
    SystemRequirements.commandsRequiredByImplementations([NetworkInterface], verbose=True)
    #
    print NetworkInterface.ipAddressOf("lo")
    print NetworkInterface.ipAddressOf("lo0")
    print NetworkInterface.ipAddressOf("vmnet1")
    print NetworkInterface.ipAddressOf("madesomethingup")


class NetworkConfigurationStaticParameters(namedtuple("NetworkConfigurationStaticParameters",
                                                      ["ipaddress", "netmask", "gateway", "nameservers"])):
    """Static IP options for network configuration.
    
    As implemented only supports IPv4."""

    __slots__ = ()
Example #11
0
optionsParser = OptionParser(
    usage="%prog [options] vmxfile command [arguments]",
    description="""Send a command to a VM.

Assumes .ports file to exist and to have an entry for ssh for the user.""",
    version="%prog 1.0")
optionsParser.add_option("-u",
                         "--user",
                         type="string",
                         dest="user",
                         help="user, default %default",
                         default="root")
(options, args) = optionsParser.parse_args()

# preflight checks
SystemRequirements.commandsRequiredByImplementations([SshCommand],
                                                     verbose=False)

if len(args) < 1:
    optionsParser.error("did not find vmxfile argument")
vmx = args.pop(0)
vm = VMwareMachine(vmx)

if len(args) < 1:
    optionsParser.error("did not find command argument")
commandAndArguments = args[0:]

sshCommand = vm.sshCommand(commandAndArguments, user=options.user)

print sshCommand.output
#!/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
Example #13
0
#!/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