Ejemplo n.º 1
0
    def setUp(self):
        self.block_size = 1024
        self.test_disk_blocks = 256000  # 250MB
        global skip_tests

        if not skip_tests:
            vixconfig = setupConfigs(
            )  # make sure that the vix configuration file exists
            self.options = Options()
            # we must have a ~/.visdkrc.vcenter file for this to work
            try:
                self.options.load(
                    "~/.visdkrc.esxi-prod-01.vm.kingsolutions.local")
            except:
                skip_tests = True
                raise

            print self.options
            # make a VISDK connection to the vcenter
            self.vim = Vim(self.options.VI_SERVER)
            self.vim.login(self.options.VI_USERNAME, self.options.VI_PASSWORD)

            # get the virtual machine that we want to test with...
            self.vm = self.vim.getVirtualMachine("puppetTestAppServer")

            # Connect to the vcenter with the vix_disk_lib using the managed object reference of the vm we will use
            creds = VixCredentials(
                vmxSpec=self.vm.ref.value,
                host=self.options.VI_SERVER,
                username=self.options.VI_USERNAME,
                password=self.options.VI_PASSWORD,
            )

            self.disk = VixDisk(creds, libdir=libdir, config=vixconfig)
            self.disk.connect(readonly=False)
Ejemplo n.º 2
0
    def testConnection(self):
        vim = Vim(self.options.server, verbose=0, connect=False)
        assert (vim.connected == False)
        assert (vim.loggedin == False)

        vim.connect()
        assert (vim.connected == True)
        assert (vim.loggedin == False)
Ejemplo n.º 3
0
 def testConnection(self):
     vim = Vim(self.options.server, verbose=0, connect=False)
     assert(vim.connected == False)
     assert(vim.loggedin == False)
     
     vim.connect()
     assert(vim.connected == True)
     assert(vim.loggedin == False)
Ejemplo n.º 4
0
    def testLoginExtensionByCertificate(self):
        vim = Vim(self.options.server, verbose=0, certfile=self.options.certfile, keyfile=self.options.keyfile)
        assert(vim.connected == True)

        vim.loginByExtensionCertificate(self.options.extension_key)
        assert(vim.loggedin == True)

        vim.logout()
        assert(vim.loggedin == False)
Ejemplo n.º 5
0
 def testLogin(self):
     vim = Vim(self.options.server, verbose=0)
     assert(vim.connected == True)
     
     vim.login(self.options.username, self.options.password)
     assert(vim.loggedin == True)
     
     vim.logout()
     assert(vim.loggedin == False)
Ejemplo n.º 6
0
class PyvisdkApp(object):
    '''
    Base class implementation of a command line application.  See the :py:mod:`~pyvisdk.app` module for documentation on the format of the options file.
    '''


    def __init__(self, usage=None):
        '''
        Constructor
        '''
        self.options = Options()
        if not usage:
            usage = "usage: %prog [options]"
        self.parser = OptionParser(usage=usage)
        self.parser.add_option("-c", "--config", dest="VI_CONFIG", help="Specify non-default name or location for the VI Perl Toolkit configuration file. Default name and location for Linux is ~/.visdkrc and for Windows is %HOME%\visdk.rc.")
        self.parser.add_option("-p", "--password", dest="VI_PASSWORD", help="Password for the specified username. Sucessful authentication with username and password returns a session object that can be saved and used for subsequent connections using the same or different script. See sessionfile.")
        self.parser.add_option("--portnumber", dest="VI_PORTNUMBER", help="Port used for server connection.")
        self.parser.add_option("--protocol", dest="VI_PROTOCOL", help="Protocol used to connect to server. Default is HTTPS. If the server has been configured for HTTP, set to HTTP. ")
        self.parser.add_option("-s", "--server", dest="VI_SERVER", help="ESX Server or VirtualCenter Management Server host to which you want the application or script to connect. Default is localhost if none specified.")
        self.parser.add_option("--servicepath", dest="VI_SERVICEPATH", help="Service path for server connection. Default is /sdk/webService.")
        self.parser.add_option("--sessionfile", dest="VI_SESSIONFILE", help="Name of file containing the token saved from successful login. Alternative to specifying username and password. Sessions time out after 30 minutes of inactivity.")
        self.parser.add_option("--url", dest="VI_URL", help="Complete URL for SDK connection. An alternative to specifying protocol, server, and servicepath as individual connection parameters. For example, python app_name.py --url https://myserver.mycompany.com/sdk --username root --password mypassword")
        self.parser.add_option("-u", "--username", dest="VI_USERNAME", help="User account that has privileges to connect to the server.")
        self.parser.add_option("-v", "--verbose", dest="VI_VERBOSE", help="Increase loglevel. Use in conjunction with Util::trace subroutine to display additional debugging information. By default, value of --verbose (loglevel) is 0. ")
        self.parser.add_option("-V", "--version", help="Displays script version information, if available.")
        
    def parse(self):
        (cmd_opts, _) = self.parser.parse_args(sys.argv[1:]) # IGNORE W0201
        
        # load up options from the visdkrc file if there is any
        if cmd_opts.VI_CONFIG:
            self.options.load(cmd_opts.VI_CONFIG)
        else:
            self.options.load()
            
        # also, update options with environmental values, overriding those in the configuration file.
        self.options.load_env()
            
        # update our options with what was entered on the command line.  This will override previously
        # entered options
        for name, value in cmd_opts.__dict__.items():
            if value:
                self.options.update({name:value})
        
        # need to have server, username, and password or fail
        if not (self.options.VI_USERNAME and self.options.VI_PASSWORD and self.options.VI_SERVER):
            raise RuntimeError("Must specify --username, --password and --server")


    def connect(self):
        self.vim = Vim(self.options.VI_SERVER)
        self.vim.login(self.options.VI_USERNAME, self.options.VI_PASSWORD)
    
    def disconnect(self):
        self.vim.logout()
Ejemplo n.º 7
0
class TestRemoteCreate(unittest.TestCase):

    def setUp(self):
        self.block_size = 1024
        self.test_disk_blocks = 256000 # 250MB
        global skip_tests
        
        if not skip_tests:
            vixconfig = setupConfigs() # make sure that the vix configuration file exists
            self.options = Options()
            # we must have a ~/.visdkrc.vcenter file for this to work
            try:
                self.options.load("~/.visdkrc.esxi-prod-01.vm.kingsolutions.local")
            except:
                skip_tests = True
                raise
            
            print self.options
            # make a VISDK connection to the vcenter
            self.vim = Vim(self.options.VI_SERVER)
            self.vim.login(self.options.VI_USERNAME, self.options.VI_PASSWORD)

            # get the virtual machine that we want to test with...
            self.vm = self.vim.getVirtualMachine("puppetTestAppServer")
            
            # Connect to the vcenter with the vix_disk_lib using the managed object reference of the vm we will use
            creds = VixCredentials( vmxSpec=self.vm.ref.value, host=self.options.VI_SERVER, username=self.options.VI_USERNAME,
                password=self.options.VI_PASSWORD, )
            
            self.disk = VixDisk(creds, libdir=libdir, config=vixconfig)
            self.disk.connect(readonly=False)

    def tearDown(self):
        if not skip_tests:
            self.disk.disconnect()

    @unittest.skipIf(skip_tests, "Remote connection is not configured")
    def testCreate(self):
        create_remote_disk(self.vm, self.disk, blocks=self.test_disk_blocks, blocksize=self.block_size)
        
        # refresh the layout information
        self.vm.update('layoutEx')
        
        size = self.test_disk_blocks*self.block_size
        remote_size = 0
        for _file in self.vm.layoutEx.file:
            if (_file.type == "diskExtent") and (_file.name.find('test.vmdk') != -1):
                remote_size = _file.size

        self.assertAlmostEqual(remote_size, size,
               msg="Remote file size doesn't match what we where aiming for...", delta=size*.001)
Ejemplo n.º 8
0
    def setUp(self):
        self.block_size = 1024
        self.test_disk_blocks = 256000 # 250MB
        global skip_tests
        
        if not skip_tests:
            vixconfig = setupConfigs() # make sure that the vix configuration file exists
            self.options = Options()
            # we must have a ~/.visdkrc.vcenter file for this to work
            try:
                self.options.load("~/.visdkrc.esxi-prod-01.vm.kingsolutions.local")
            except:
                skip_tests = True
                raise
            
            print self.options
            # make a VISDK connection to the vcenter
            self.vim = Vim(self.options.VI_SERVER)
            self.vim.login(self.options.VI_USERNAME, self.options.VI_PASSWORD)

            # get the virtual machine that we want to test with...
            self.vm = self.vim.getVirtualMachine("puppetTestAppServer")
            
            # Connect to the vcenter with the vix_disk_lib using the managed object reference of the vm we will use
            creds = VixCredentials( vmxSpec=self.vm.ref.value, host=self.options.VI_SERVER, username=self.options.VI_USERNAME,
                password=self.options.VI_PASSWORD, )
            
            self.disk = VixDisk(creds, libdir=libdir, config=vixconfig)
            self.disk.connect(readonly=False)
Ejemplo n.º 9
0
 def setUpClass(cls):
     cls.options = get_options()
     cls.vim = Vim(cls.options.server)
     cls.vim.login(cls.options.username, cls.options.password)
     cls.manager = TaskManager(cls.vim)
     cls.obj = cls.vim.getHostSystems()[0]
     cls.cleanUpStaleTasks()
Ejemplo n.º 10
0
 def connect(self):
     connected = False
     logged_in = False
     msg = None
     vim = None
     try:
         vim = Vim(self.host)
         connected = True
     except Exception as e:
         logger.error(e)
         msg = e
     if connected:
         try:
             vim.login(self.username, self.password)
             logged_in = True
         except Exception as e:
             logger.error(e)
             msg = e
     self.connected = connected
     self.error = msg
     self.logged_in = logged_in
     self.vim = vim
     return(connected, msg, logged_in, vim)
Ejemplo n.º 11
0
    def testLogin(self):
        vim = Vim(self.options.server, verbose=0)
        assert (vim.connected == True)

        vim.login(self.options.username, self.options.password)
        assert (vim.loggedin == True)

        vim.logout()
        assert (vim.loggedin == False)
Ejemplo n.º 12
0
    def testLoginExtensionByCertificate(self):
        vim = Vim(self.options.server,
                  verbose=0,
                  certfile=self.options.certfile,
                  keyfile=self.options.keyfile)
        assert (vim.connected == True)

        vim.loginByExtensionCertificate(self.options.extension_key)
        assert (vim.loggedin == True)

        vim.logout()
        assert (vim.loggedin == False)
Ejemplo n.º 13
0
 def testApiType(self):
     vim = Vim(self.options.server, verbose=0)
     assert ("VirtualCenter" == vim.getApiType())
Ejemplo n.º 14
0
 def testAbout(self):
     vim = Vim(self.options.server, verbose=0)
     vim.displayAbout()
     assert (vim.connected == True)
     assert (vim.loggedin == False)
Ejemplo n.º 15
0
 def setUpClass(cls):
     cls.options = get_options()
     cls.vim = Vim(cls.options.server)
     cls.vim.login(cls.options.username, cls.options.password)
     cls.datacenter = cls.vim.getDatacenters()[0]
Ejemplo n.º 16
0
 def testApiType(self):
     vim = Vim(self.options.server, verbose=0)
     assert("VirtualCenter" == vim.getApiType())
Ejemplo n.º 17
0
 def testAbout(self):
     vim = Vim(self.options.server, verbose=0)
     vim.displayAbout()
     assert(vim.connected == True)
     assert(vim.loggedin == False)
Ejemplo n.º 18
0
class PyvisdkApp(object):
    '''
    Base class implementation of a command line application.  See the :py:mod:`~pyvisdk.app` module for documentation on the format of the options file.
    '''
    def __init__(self, usage=None):
        '''
        Constructor
        '''
        self.options = Options()
        if not usage:
            usage = "usage: %prog [options]"
        self.parser = OptionParser(usage=usage)
        self.parser.add_option(
            "-c",
            "--config",
            dest="VI_CONFIG",
            help=
            "Specify non-default name or location for the VI Perl Toolkit configuration file. Default name and location for Linux is ~/.visdkrc and for Windows is %HOME%\visdk.rc."
        )
        self.parser.add_option(
            "-p",
            "--password",
            dest="VI_PASSWORD",
            help=
            "Password for the specified username. Sucessful authentication with username and password returns a session object that can be saved and used for subsequent connections using the same or different script. See sessionfile."
        )
        self.parser.add_option("--portnumber",
                               dest="VI_PORTNUMBER",
                               help="Port used for server connection.")
        self.parser.add_option(
            "--protocol",
            dest="VI_PROTOCOL",
            help=
            "Protocol used to connect to server. Default is HTTPS. If the server has been configured for HTTP, set to HTTP. "
        )
        self.parser.add_option(
            "-s",
            "--server",
            dest="VI_SERVER",
            help=
            "ESX Server or VirtualCenter Management Server host to which you want the application or script to connect. Default is localhost if none specified."
        )
        self.parser.add_option(
            "--servicepath",
            dest="VI_SERVICEPATH",
            help=
            "Service path for server connection. Default is /sdk/webService.")
        self.parser.add_option(
            "--sessionfile",
            dest="VI_SESSIONFILE",
            help=
            "Name of file containing the token saved from successful login. Alternative to specifying username and password. Sessions time out after 30 minutes of inactivity."
        )
        self.parser.add_option(
            "--url",
            dest="VI_URL",
            help=
            "Complete URL for SDK connection. An alternative to specifying protocol, server, and servicepath as individual connection parameters. For example, python app_name.py --url https://myserver.mycompany.com/sdk --username root --password mypassword"
        )
        self.parser.add_option(
            "-u",
            "--username",
            dest="VI_USERNAME",
            help="User account that has privileges to connect to the server.")
        self.parser.add_option(
            "-v",
            "--verbose",
            dest="VI_VERBOSE",
            help=
            "Increase loglevel. Use in conjunction with Util::trace subroutine to display additional debugging information. By default, value of --verbose (loglevel) is 0. "
        )
        self.parser.add_option(
            "-V",
            "--version",
            help="Displays script version information, if available.")

    def parse(self):
        (cmd_opts, _) = self.parser.parse_args(sys.argv[1:])  # IGNORE W0201

        # load up options from the visdkrc file if there is any
        if cmd_opts.VI_CONFIG:
            self.options.load(cmd_opts.VI_CONFIG)
        else:
            self.options.load()

        # also, update options with environmental values, overriding those in the configuration file.
        self.options.load_env()

        # update our options with what was entered on the command line.  This will override previously
        # entered options
        for name, value in cmd_opts.__dict__.items():
            if value:
                self.options.update({name: value})

        # need to have server, username, and password or fail
        if not (self.options.VI_USERNAME and self.options.VI_PASSWORD
                and self.options.VI_SERVER):
            raise RuntimeError(
                "Must specify --username, --password and --server")

    def connect(self):
        self.vim = Vim(self.options.VI_SERVER)
        self.vim.login(self.options.VI_USERNAME, self.options.VI_PASSWORD)

    def disconnect(self):
        self.vim.logout()
Ejemplo n.º 19
0
 def connect(self):
     self.vim = Vim(self.options.VI_SERVER)
     self.vim.login(self.options.VI_USERNAME, self.options.VI_PASSWORD)
Ejemplo n.º 20
0
class TestRemoteCreate(unittest.TestCase):
    def setUp(self):
        self.block_size = 1024
        self.test_disk_blocks = 256000  # 250MB
        global skip_tests

        if not skip_tests:
            vixconfig = setupConfigs(
            )  # make sure that the vix configuration file exists
            self.options = Options()
            # we must have a ~/.visdkrc.vcenter file for this to work
            try:
                self.options.load(
                    "~/.visdkrc.esxi-prod-01.vm.kingsolutions.local")
            except:
                skip_tests = True
                raise

            print self.options
            # make a VISDK connection to the vcenter
            self.vim = Vim(self.options.VI_SERVER)
            self.vim.login(self.options.VI_USERNAME, self.options.VI_PASSWORD)

            # get the virtual machine that we want to test with...
            self.vm = self.vim.getVirtualMachine("puppetTestAppServer")

            # Connect to the vcenter with the vix_disk_lib using the managed object reference of the vm we will use
            creds = VixCredentials(
                vmxSpec=self.vm.ref.value,
                host=self.options.VI_SERVER,
                username=self.options.VI_USERNAME,
                password=self.options.VI_PASSWORD,
            )

            self.disk = VixDisk(creds, libdir=libdir, config=vixconfig)
            self.disk.connect(readonly=False)

    def tearDown(self):
        if not skip_tests:
            self.disk.disconnect()

    @unittest.skipIf(skip_tests, "Remote connection is not configured")
    def testCreate(self):
        create_remote_disk(self.vm,
                           self.disk,
                           blocks=self.test_disk_blocks,
                           blocksize=self.block_size)

        # refresh the layout information
        self.vm.update('layoutEx')

        size = self.test_disk_blocks * self.block_size
        remote_size = 0
        for _file in self.vm.layoutEx.file:
            if (_file.type
                    == "diskExtent") and (_file.name.find('test.vmdk') != -1):
                remote_size = _file.size

        self.assertAlmostEqual(
            remote_size,
            size,
            msg="Remote file size doesn't match what we where aiming for...",
            delta=size * .001)
Ejemplo n.º 21
0
 def setUpClass(cls):
     cls.options = get_options()
     cls.vim = Vim(cls.options.server)
     cls.vim.login(cls.options.username, cls.options.password)
Ejemplo n.º 22
0
 def connect(self):
     self.vim = Vim(self.options.VI_SERVER)
     self.vim.login(self.options.VI_USERNAME, self.options.VI_PASSWORD)