Get host id of connected ESXi

1,035 views
Skip to first unread message

Patrik Kernstock

unread,
Aug 20, 2014, 3:12:16 AM8/20/14
to pysp...@googlegroups.com
Hello,

I'm planning to write a simple script to set the host in maintenance mode, suspend all running VMs and then shutdown the standalone ESXi. I already found a helpful snippet from a other thread at https://groups.google.com/forum/#!topic/pysphere/AjWn8qbjqv0, but I'm not able to get it to work... My problem is, that the functions require a host object, but how can I get the host MOR of the current host, where I am connected to?

Commands like get_hosts() only does return empty arrays, so it may not help me that much:
>>> server.get_hosts()
{}

Thanks for any suggestions!

Regards,
Patrik Kernstock

Patrik Kernstock

unread,
Aug 21, 2014, 4:01:17 AM8/21/14
to pysp...@googlegroups.com
Here's the complete code I currently have:

import sys
from pysphere import VIServer, VITask, VIMor
from pysphere.vi_virtual_machine import VIVirtualMachine
from pysphere.resources import VimService_services as VI

if len(sys.argv) < 4:
print "To less arguments"
sys.exit(1)

server = VIServer()

try:
    server.connect(sys.argv[1], sys.argv[2], sys.argv[3])
except:
    print 'Could not estabilish connection!'
    sys.exit(2);

def host_mor(mor):
    if not VIMor.is_mor(mor):
        return VIMor(mor, "HostSystem")
    return mor

def enter_maintenance_mode(host, timeout, evacuate_powered_off_vms=None):
    host = host_mor(host)
    request = VI.EnterMaintenanceMode_TaskRequestMsg()
    _this = request.new__this(host)
    _this.set_attribute_type(host.get_attribute_type())
    request.set_element__this(_this)
    request.set_element_timeout(timeout)
    if evacuate_powered_off_vms in [False, True]:
        request.set_element_evacuatePoweredOffVms(evacuate_powered_off_vms)

    task = server._proxy.EnterMaintenanceMode_Task(request)._returnval
    return VITask(task, server)

def shutdown_host(host, force):
    host = host_mor(host)
    request = VI.ShutdownHost_TaskRequestMsg()
    _this = request.new__this(host)
    _this.set_attribute_type(host.get_attribute_type())
    request.set_element__this(_this)
    request.set_element_force(force)
    task = server._proxy.ShutdownHost_Task(request)._returnval
    return VITask(task, server)

def suspend_all_powered_on_vms():
    for path in server.get_registered_vms(status='poweredOn'):
        vm = server.get_vm_by_path(path)
        vm.suspend(sync_run=False)

def migrate_powered_on_vms(from_host, to_host):
    from_host = host_mor(from_host)
    to_host = host_mor(to_host)
    props = ['runtime.powerState']
    oc = server._retrieve_properties_traversal(property_names=props,
                                               from_node=from_host,
                                               obj_type='VirtualMachine')
    for o in oc:
        powered_on = False
        if hasattr(o, "PropSet") and o.PropSet:
            for p in o.PropSet:
                if p.Name=='runtime.powerState' and p.Val == 'poweredOn':
                    vm = VIVirtualMachine(server, o.Obj)
                    vm.migrate(sync_run=False, host=to_host)

# Here I need to set the host (the esxi itself)
host = VIMor(server, "HostSystem")

print "Entering maintenance mode..."
task = enter_maintenance_mode(host, 10)
print "State: ",task.get_state()

print "Suspending all VMs..."
suspend_all_powered_on_vms()
print "Done"

print "Shutdown host..."
shutdown_host(host, True)

server.disconnect()


Ujjwal Singh

unread,
Aug 21, 2014, 5:45:44 AM8/21/14
to pysp...@googlegroups.com

server.get_hosts().keys() will give you list of host mor objects

Patrik Kernstock

unread,
Aug 21, 2014, 5:53:00 AM8/21/14
to pysp...@googlegroups.com
I know that .keys() will output the host MOR objects, because this works on a vCenter without problems. But on a standalone ESXi it won't work - like I wrote in the first post, it just returns an empty array.

Patrik Kernstock

unread,
Sep 3, 2014, 5:08:31 AM9/3/14
to pysp...@googlegroups.com
Sadly I still didn't found anything helpful. Couldn't get it to work. Does anyone have a idea what's wrong?

Lasha Tsatsashvili

unread,
Mar 27, 2017, 10:51:53 AM3/27/17
to pysphere
hello for my shutdown_host doesnt work and can u help me 
check my error on link : 
https://groups.google.com/forum/#!topic/pysphere/2ojmjBWFo3Y
Reply all
Reply to author
Forward
0 new messages