def main():
    """Kill the unknown VMs"""
    args = get_args()

    si = vmware_lib.connect(args.host, args.user, args.password, args.port,
                            args.insecure)
    content = si.RetrieveContent()

    print 'Looking for vms'
    vms = GetAllVmsRoot(content.rootFolder.childEntity)
    print 'Checking vm count - is {}'.format(len(vms))
    if vms:
        unknown_vms = FindUnknownVms(vms)
    if unknown_vms:
        print 'Found {} unknown VMs'.format(len(unknown_vms))
        for vm in unknown_vms:
            print vm.name
        if args.destroy:
            print 'Reached destroy'
            for vm in unknown_vms:
                try:
                    if format(vm.runtime.powerState) == 'poweredOn':
                        power_off_vm = vm.PowerOffVM_Task()
                        vmware_lib.wait_for_task(power_off_vm)
                    destroy_vm = vm.Destroy_Task()
                    name = vm.name
                    vmware_lib.wait_for_task(destroy_vm)
                    print 'Successfully destroyed {}'.format(name)
                except:
                    print 'Failed to destroy {}'.format(vm.name)
                    pass
    else:
        print 'No unknown VMs found'
Exemple #2
0
def main():
    """Kill the zombie VMs"""

    global args
    args = get_args()

    if args.password:
        password = args.password
    else:
        password = getpass.getpass(prompt='Enter password for host %s and '
                                   'user %s: ' % (args.host, args.user))

    si = vmware_lib.connect(args.host, args.user, password, args.port,
                            args.insecure)
    content = si.RetrieveContent()

    print 'Getting full list of vms'
    vms = GetAllVmsRoot(content.rootFolder.childEntity)
    print 'Checking vm count - is {}'.format(len(vms))
    if vms:
        zombie_vms = FindZombieVms(vms)
    if zombie_vms:
        print 'Found {} zombie VMs'.format(len(zombie_vms))
        if args.destroy:
            destroy(zombie_vms)
        else:
            message = "Please confirm destroying the above in 'double check' list (Y/N) "
            confirm = raw_input(message)
            if confirm.lower() == 'y':
                destroy(zombie_vms)
            else:
                print "Nothing deleted"
    else:
        print 'No zombie VMs found'
Exemple #3
0
def main():
    """
    Simple command-line program for listing the virtual machines on a system.
    """

    args = GetArgs()
    if args.password:
        password = args.password
    else:
        password = getpass.getpass(prompt='Enter password for host %s and '
                                   'user %s: ' % (args.host, args.user))

    si = vmware_lib.connect(args.host, args.user, password, args.port,
                            args.insecure)

    if not si:
        print("Could not connect to the specified host using specified "
              "username and password")
        return -1

    atexit.register(Disconnect, si)

    content = si.RetrieveContent()
    for child in content.rootFolder.childEntity:
        if hasattr(child, 'vmFolder'):
            datacenter = child
            vmFolder = datacenter.vmFolder
            vmList = vmFolder.childEntity
            for vm in vmList:
                PrintVmInfo(vm)
    return 0
Exemple #4
0
def main():
    """
    Let this thing fly
    """
    args = get_args()

    # connect this thing
    si = vmware_lib.connect(args.host, args.user, args.password, args.port,
                            args.insecure)
    content = si.RetrieveContent()

    vm_object = None

    if args.template_folder:
        folder = vmware_lib.get_obj(content, [vmware_lib.vim.Folder],
                                    args.template_folder)
        for vm in folder.childEntity:
            if vm.name == args.template:
                vm_object = vm
    else:
        vm_object = vmware_lib.get_obj(content,
                                       [vmware_lib.vim.VirtualMachine],
                                       args.template)

    if vm_object:
        clone_vm(content, vm_object, args.vm_name, si, args.datacenter_name,
                 args.vm_folder, args.datastore_name, args.cluster_name,
                 args.resource_pool, args.power_on, args.cpus, args.memory,
                 args.linked_clone)
    else:
        print "template not found"
def main():
    """
    Let this thing fly
    """
    args = get_args()

    # connect this thing
    si = vmware_lib.connect(args.host, args.user, args.password, args.port,
                            args.insecure)
    content = si.RetrieveContent()

    if args.vm:
        if args.cold_migrate:
            vm = vmware_lib.get_obj(content, [vmware_lib.vim.VirtualMachine],
                                    args.vm)
            if vm.runtime.powerState == 'poweredOn':
                print 'Powering off {}'.format(args.vm)
                task = vm.PowerOffVM_Task()
                tasks.wait_for_tasks(si, [task])

        print 'Migrating VM'
        result = vmware_lib.migrate_vm(content,
                                       args.vm,
                                       rebalance=False,
                                       limit=90)
        if args.cold_migrate:
            print 'Powering on VM'
            task = vm.PowerOnVM_Task()
            tasks.wait_for_tasks(si, [task])
        return result

    if args.esxi_host:
        host = vmware_lib.get_obj(content, [vmware_lib.vim.HostSystem],
                                  args.esxi_host)
        if host:
            if args.migrate_vms:
                vmware_lib.migrate_host_vms(content, host, args.skip,
                                            args.rebalance, args.limit)

            if args.maintenance_mode or args.maintenance_mode == False:
                vmware_lib.maintenance_mode(host, args.maintenance_mode)
            if args.reboot:
                print 'Rebooting {}'.format(host.name)
                vmware_lib.wait_for_task(host.Reboot(force=False))
            if args.reconnect:
                vmware_lib.reconnect_host(host, args.host_user,
                                          args.host_password)
        else:
            print 'Cannot find host {}'.format(args.esxi_host)
            sys.exit(1)
Exemple #6
0
def main():
    args = get_args()

    # connect this thing
    si = vmware_lib.connect(args.vcenter, args.vcenter_user,
                            args.vcenter_password, args.vcenter_port,
                            args.vcenter_insecure)
    content = si.RetrieveContent()

    for vm_name in args.vms:
        vm = vmware_lib.get_obj(content, [vmware_lib.vim.VirtualMachine],
                                vm_name)
        poweroff(args, si, vm)
        tintri_snapshot(args, vm)
        poweron(args, si, vm)
Exemple #7
0
def main():
    """
    Let this thing fly
    """
    args = get_args()

    # connect this thing
    si = vmware_lib.connect(args.host, args.user, args.password, args.port,
                            args.insecure)
    content = si.RetrieveContent()

    vm = vmware_lib.get_obj(content, [vmware_lib.vim.VirtualMachine], args.vm)

    # does the actual vm reboot
    try:
        vm.RebootGuest()
    except:
        # forceably shutoff/on
        # need to do if vmware guestadditions isn't running
        vm.ResetVM_Task()
Exemple #8
0
def main():
    """
    Do some stuff
    """
    args = get_args()

    si = vmware_lib.connect(args.host, args.user, args.password, args.port, args.insecure)
    content = si.RetrieveContent()

    if args.folder:
        folder = vmware_lib.get_obj(content, [vmware_lib.vim.Folder], args.folder)
        for vm in folder.childEntity:
            if vm.name == args.vm_name:
                vm_object = vm
    else:
        vm_object = vmware_lib.get_obj(content, [vmware_lib.vim.VirtualMachine], args.vm_name)

    if vm_object == None:
        print 'Cannot find {}'.format(args.vm_name)
        return

    if args.snapshot_name:
        print "Creating snapshot {} for {}".format(args.snapshot_name, args.vm_name)
        take_vm_snapshot(si, vm_object, args.snapshot_name)
    if args.revert:
        print "Reverting to latest snapshot for {}".format(args.vm_name)
        revert_to_latest_snapshot(si, vm_object)
        tasks.wait_for_tasks(si, [vm_object.PowerOn()])
    if args.destroy_all:
        print "Destroying snapshots for {}".format(args.vm_name)
        task = vm_object.RemoveAllSnapshots_Task()
        if args.wait_for_task:
            vmware_lib.wait_for_task(task)
            print 'All snapshots for {} have been destroyed'.format(args.vm_name)
    if args.list_snapshots:
        try:
            snaps = vm_object.snapshot.rootSnapshotList
            for snap in snaps:
                print snap
        except AttributeError:
            print 'No snapshots found for {}'.format(args.vm_name)
def main():
    """Migrate VMs to target datastore"""
    args = get_args()

    si = vmware_lib.connect(args.host, args.user, args.password, args.port,
                            args.insecure)
    content = si.RetrieveContent()

    source_datastore = vmware_lib.get_obj(content, [vmware_lib.vim.Datastore],
                                          args.datastore_source)
    destination_datastore = vmware_lib.get_obj(content,
                                               [vmware_lib.vim.Datastore],
                                               args.datastore_destination)
    if source_datastore and destination_datastore:
        print 'Found {} and {}. Checking for VMs.'.format(
            args.datastore_source, args.datastore_destination)
        if source_datastore.vm:
            print 'Found {} VMs on {}. Starting migration to {}.'.format(
                len(source_datastore.vm), args.datastore_source,
                args.datastore_destination)
            for vm in source_datastore.vm:
                if destination_datastore in vm.summary.runtime.host.parent.datastore:
                    vmware_lib.migrate_vm_datastore(vm, destination_datastore)
                else:
                    print 'Destination datastore {} is not available in the {} cluster.'.format(
                        args.datastore_destination,
                        vm.summary.runtime.host.parent.name)
                    sys.exit(1)
                    break
            print 'Successfully migrated all VMs'
        else:
            print 'There are no VMs to migrate on {}.'.format(
                args.datastore_source)
    else:
        if not source_datastore:
            print 'Cannot locate datastore {}'.format(args.datastore_source)
            sys.exit(1)
        if not destination_datastore:
            print 'Cannot locate datastore {}'.format(
                args.datastore_destination)
            sys.exit(1)
Exemple #10
0
def main():
    """Add datastore to cluster or host"""
    args = get_args()

    si = vmware_lib.connect(args.host, args.user, args.password, args.port,
                            args.insecure)
    content = si.RetrieveContent()
    dsspec = vmware_lib.datastore_spec(args.datastore,
                                       args.local_datastore_name)

    if args.mount:
        if args.cluster:
            try:
                cluster = vmware_lib.get_obj(
                    content, [vmware_lib.vim.ClusterComputeResource],
                    args.cluster)
                for host in cluster.host:
                    mount = vmware_lib.mount_datastore(dsspec, host)
                    if mount == True:
                        print 'Successfully mounted {} on {}'.format(
                            args.datastore, host.name)
                    else:
                        print mount
            except:
                return 'Could not mount {} on {}'.format(
                    dsspec.localPath, args.cluster)
        elif args.esxi_host:
            try:
                host = vmware_lib.get_obj(content, [vmware_lib.vim.HostSystem],
                                          args.esxi_host)
                mount = vmware_lib.mount_datastore(dsspec, host)
                if mount == True:
                    print 'Successfully mounted {} on {}'.format(
                        args.datastore, host.name)
                else:
                    print mount
            except:
                raise
                print 'Failed to mount {} on {}'.format(
                    args.datastore, args.esxi_host)
    elif args.unmount:
        print 'reached unmount'
        if args.cluster:
            cluster = vmware_lib.get_obj(
                content, [vmware_lib.vim.ClusterComputeResource], args.cluster)
            for host in cluster.host:
                if vmware_lib.unmount_datastore(dsspec.localPath, host):
                    print 'Unmounted {} on {}'.format(dsspec.localPath,
                                                      host.name)
                else:
                    '{} is not mounted on {}'.format(dsspec.localPath,
                                                     host.name)
        elif args.esxi_host:
            print 'reached host'
            host = vmware_lib.get_obj(content, [vmware_lib.vim.HostSystem],
                                      args.esxi_host)
            if vmware_lib.unmount_datastore(dsspec.localPath, host):
                print 'Successfully unmounted {} on {}'.format(
                    dsspec.localPath, host.name)
            else:
                print '{} is not mounted on {}'.format(dsspec.localPath,
                                                       host.name)
        else:
            print 'No host or cluster were provided to unmount the volume'
    else:
        print 'No mount or unmount action was specified'