Skip to content

Instantly share code, notes, and snippets.

@VAdamec
Created September 22, 2015 03:44
Show Gist options
  • Save VAdamec/8e7cf2553b6020ca8645 to your computer and use it in GitHub Desktop.
Save VAdamec/8e7cf2553b6020ca8645 to your computer and use it in GitHub Desktop.
Set new VM VAPP parameters
#!/usr/bin/python
import optparse
from pprint import pprint
import sys
# from pysphere.vi_vapp import VIVApp
from pysphere import VIServer, VITask, MORTypes, VIProperty
from pysphere.resources import VimService_services as VI
# CFG
vcenter = "vsphere.example.com"
# MAIN method
parser = optparse.OptionParser("usage: %prog [options]")
parser.add_option("-u", dest="vcenteruser", type="string", help="specify login")
parser.add_option("-p", dest="vcenterpass", type="string", help="specify password")
parser.add_option("-s", dest="vmName", type="string", help="specify new vm")
parser.add_option("-d", dest="vmDomain", type="string", help="specify vm domain")
parser.add_option("-n", dest="vmNIC", type="string", help="specify new vm IP")
parser.add_option("-m", dest="vmMASK", type="string", help="specify new vm MASK")
parser.add_option("-g", dest="vmGW", type="string", help="specify new vm GW")
parser.add_option("-r", dest="vmNS", type="string", help="specify new vm NS")
options, arguments = parser.parse_args()
print
print 'Begin configuration loop for VM %s' % options.vmName
vapp = {}
newconfig = {
'add': [],
'edit': [],
'remove': []
}
server = VIServer()
print '%s: Connecting to vsphere %s' % (options.vmName,vcenter)
server.connect(vcenter, options.vcenteruser, options.vcenterpass)
vm = server.get_vm_by_name(options.vmName)
'''
This section enables vApp Options
'''
print '%s: Enabling vApp Options' % options.vmName
request = VI.ReconfigVM_TaskRequestMsg()
_this = request.new__this(vm._mor)
_this.set_attribute_type(vm._mor.get_attribute_type())
request.set_element__this(_this)
spec = request.new_spec()
vappconfig = spec.new_vAppConfig()
vappconfig.set_element_ovfEnvironmentTransport(['com.vmware.guestInfo'])
spec.set_element_vAppConfig(vappconfig)
request.set_element_spec(spec)
task = server._proxy.ReconfigVM_Task(request)._returnval
vi_task = VITask(task, server)
status = vi_task.wait_for_state([vi_task.STATE_SUCCESS,
vi_task.STATE_ERROR])
if status == vi_task.STATE_ERROR:
print "%s: Error enabling vApp options:" % options.vmName, vi_task.get_error_message()
else:
print "%s: VM vApp Options enabled" % options.vmName
#
# This section changes vApp properties
#
newconfig = {
'add':[{'key':10, 'id':'domain', 'value':options.vmDomain, 'category':'domain'},
{'key':11, 'id':'name', 'value':options.vmName, 'category':'hostname'},
{'key':12, 'id':'address', 'value':options.vmNIC, 'category':'address'},
{'key':13, 'id':'mask', 'value':options.vmMASK, 'category':'mask'},
{'key':14, 'id':'gateway', 'value':options.vmGW, 'category':'gateway'},
{'key':15, 'id':'nameserver', 'value':options.vmNS, 'category':'nameserver'},
]
}
request = VI.ReconfigVM_TaskRequestMsg()
_this = request.new__this(vm._mor)
_this.set_attribute_type(vm._mor.get_attribute_type())
request.set_element__this(_this)
spec = request.new_spec()
vappconfig = spec.new_vAppConfig()
properties = []
for operation, items in newconfig.items():
for item in items:
prop = vappconfig.new_property()
prop.set_element_operation(operation)
if operation == 'remove':
prop.set_element_removeKey(item)
else:
info = prop.new_info()
for k,v in item.items():
method = getattr(info, "set_element_" + k)
method(v)
prop.set_element_info(info)
properties.append(prop)
vappconfig.set_element_property(properties)
spec.set_element_vAppConfig(vappconfig)
request.set_element_spec(spec)
task = server._proxy.ReconfigVM_Task(request)._returnval
vi_task = VITask(task, server)
status = vi_task.wait_for_state([vi_task.STATE_SUCCESS,
vi_task.STATE_ERROR])
if status == vi_task.STATE_ERROR:
print "Error:", vi_task.get_error_message()
else:
print "VM successfuly reconfigured"
server.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment