Exemplo n.º 1
0
    def __init__(self,
                 vi_url,
                 vi_username,
                 vi_password,
                 vi_version,
                 debug=False):
        self.debug = debug
        self.log = logging.getLogger('pyvsphere.vmtool')
        if self.debug:
            self.log.setLevel(logging.DEBUG)
        self.vi_url = vi_url or os.environ.get('VI_URL')
        assert self.vi_url, 'either the enviroment variable VI_URL or the url parameter needs to be specified'
        self.vi_username = vi_username or os.environ.get('VI_USERNAME')
        assert self.vi_username, 'either the enviroment variable VI_USERNAME or the username parameter needs to be specified'
        self.vi_password = vi_password or os.environ.get('VI_PASSWORD')
        assert self.vi_password, 'either the enviroment variable VI_PASSWORD or the password parameter needs to be specified'
        self.vi_version = vi_version or os.environ.get('VI_VERSION')

        self.vim = Vim(self.vi_url, debug=False, version=self.vi_version)
        self.log.debug('CONNECTION complete')
        self.vim.login(self.vi_username, self.vi_password)
        self.log.debug('LOGIN complete')

        self.vmops = VmOperations(self.vim)
Exemplo n.º 2
0
class VmTool(object):
    def __init__(self,
                 vi_url,
                 vi_username,
                 vi_password,
                 vi_version,
                 debug=False):
        self.debug = debug
        self.log = logging.getLogger('pyvsphere.vmtool')
        if self.debug:
            self.log.setLevel(logging.DEBUG)
        self.vi_url = vi_url or os.environ.get('VI_URL')
        assert self.vi_url, 'either the enviroment variable VI_URL or the url parameter needs to be specified'
        self.vi_username = vi_username or os.environ.get('VI_USERNAME')
        assert self.vi_username, 'either the enviroment variable VI_USERNAME or the username parameter needs to be specified'
        self.vi_password = vi_password or os.environ.get('VI_PASSWORD')
        assert self.vi_password, 'either the enviroment variable VI_PASSWORD or the password parameter needs to be specified'
        self.vi_version = vi_version or os.environ.get('VI_VERSION')

        self.vim = Vim(self.vi_url, debug=False, version=self.vi_version)
        self.log.debug('CONNECTION complete')
        self.vim.login(self.vi_username, self.vi_password)
        self.log.debug('LOGIN complete')

        self.vmops = VmOperations(self.vim)

    def test(self, options):
        """ Placeholder for random hacking so --test has something to run """
        print 'Status: 95% complete ...'
        time.sleep(10.0)

    def vm_names_from_options(self, options):
        if options.count == 1:
            yield options.vm_name
        else:
            for i in range(options.count):
                yield '%s-%02d' % (options.vm_name, i)

    def clone_vms(self, options):
        instances = dict()
        for vm_name in self.vm_names_from_options(options):
            instance = dict(vm_name=vm_name,
                            base_vm_name=options.base_image,
                            datastore_filter=options.datastore_filter,
                            folder=options.folder,
                            resource_pool=options.resource_pool,
                            cluster=options.cluster)
            instances[vm_name] = instance
        args = {'nuke_old': True}
        return self.vmops.run_on_instances(instances, self.vmops.clone_vm,
                                           args)

    def delete_vms(self, options):
        """ Delete a batch of VMs """
        instances = dict((x, dict(vm_name=x, vm=None))
                         for x in self.vm_names_from_options(options))
        return self.vmops.run_on_instances(instances, self.vmops.delete_vm)

    def list_ips(self, options):
        """ List the IP addresses of a number of VMs """
        instances = dict(
            (x, dict(vm_name=x)) for x in self.vm_names_from_options(options))
        updated_instances = self.vmops.run_on_instances(
            instances, self.vmops.update_vm)
        for instance_id in updated_instances:
            print '%s: %s' % (instance_id,
                              updated_instances[instance_id]['ipv4'])

    def snapshot(self, options):
        vm = self.vim.find_vm_by_name(options.vm_name)
        vm.create_snapshot(options.snapshot, memory=True)

    def list_snapshots(self, options):
        vm = self.vim.find_vm_by_name(options.vm_name, ['snapshot'])
        snapshots = vm.list_snapshots()
        if snapshots:
            current_snapshot = VirtualMachineSnapshot(
                mor=vm.snapshot.currentSnapshot, vim=self.vim)
            for snapshot in snapshots:
                print snapshot.name, '(CURRENT)' if snapshot.snapshot == current_snapshot else ''

    def revert(self, options):
        vm = self.vim.find_vm_by_name(options.vm_name)
        vm.revert_to_current_snapshot()

    def remove_snapshot(self, options):
        vm = self.vim.find_vm_by_name(options.vm_name)
        snapshots = vm.find_snapshots_by_name(options.remove_snapshot)
        if snapshots:
            snapshots[0].snapshot.remove_snapshot(remove_children=True)

    def revert_to_snapshot(self, options):
        vm = self.vim.find_vm_by_name(options.vm_name)
        snapshotinfos = vm.find_snapshots_by_name(options.revert_to_snapshot)
        if len(snapshotinfos) != 1:
            raise InvalidParameterError(
                'there are multiple snapshots with the name %r' %
                options.revert_to_snapshot)
        snapshotinfos[0].snapshot.revert_to_snapshot()