1
0
mirror of https://github.com/vmware/vsphere-automation-sdk-python.git synced 2024-11-29 20:09:58 -05:00
vsphere-automation-sdk-python/samples/vsphere/vcenter/vm/power.py

124 lines
3.7 KiB
Python
Raw Normal View History

2016-10-26 19:08:23 -04:00
#!/usr/bin/env python
"""
* *******************************************************
* Copyright (c) VMware, Inc. 2016-2018. All Rights Reserved.
* SPDX-License-Identifier: MIT
2016-10-26 19:08:23 -04:00
* *******************************************************
*
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
"""
__author__ = 'VMware, Inc.'
__vcenter_version__ = '6.5+'
from com.vmware.vcenter.vm_client import Power
from vmware.vapi.vsphere.client import create_vsphere_client
2016-10-26 19:08:23 -04:00
from samples.vsphere.common.sample_util import parse_cli_args_vm
from samples.vsphere.common.sample_util import pp
from samples.vsphere.vcenter.setup import testbed
from samples.vsphere.vcenter.helper.vm_helper import get_vm
from samples.vsphere.common.ssl_helper import get_unverified_session
2016-10-26 19:08:23 -04:00
"""
Demonstrates the virtual machine power lifecycle
Sample Prerequisites:
The sample needs an existing VM.
"""
vm = None
vm_name = None
client = None
2016-10-26 19:08:23 -04:00
cleardata = False
def setup(context=None):
global client, cleardata, vm_name
2016-10-26 19:08:23 -04:00
if context:
# Run sample suite via setup script
vm_name = testbed.config['VM_NAME_DEFAULT']
client = context.client
2016-10-26 19:08:23 -04:00
else:
# Run sample in standalone mode
server, username, password, cleardata, skip_verification, vm_name = \
parse_cli_args_vm(testbed.config['VM_NAME_DEFAULT'])
session = get_unverified_session() if skip_verification else None
# Connect to vSphere client
client = create_vsphere_client(server=server,
username=username,
password=password,
session=session)
2016-10-26 19:08:23 -04:00
def run():
global vm
vm = get_vm(client, vm_name)
2016-10-26 19:08:23 -04:00
if not vm:
raise Exception('Sample requires an existing vm with name ({}).'
'Please create the vm first.'.format(vm_name))
print("Using VM '{}' ({}) for Power Sample".format(vm_name, vm))
# Get the vm power state
print('\n# Example: Get current vm power state')
status = client.vcenter.vm.Power.get(vm)
2016-10-26 19:08:23 -04:00
print('vm.Power.get({}) -> {}'.format(vm, pp(status)))
# Power off the vm if it is on
if status == Power.Info(state=Power.State.POWERED_ON):
print('\n# Example: VM is powered on, power it off')
client.vcenter.vm.Power.stop(vm)
2016-10-26 19:08:23 -04:00
print('vm.Power.stop({})'.format(vm))
# Power on the vm
print('# Example: Power on the vm')
client.vcenter.vm.Power.start(vm)
2016-10-26 19:08:23 -04:00
print('vm.Power.start({})'.format(vm))
# Suspend the vm
print('\n# Example: Suspend the vm')
client.vcenter.vm.Power.suspend(vm)
2016-10-26 19:08:23 -04:00
print('vm.Power.suspend({})'.format(vm))
# Resume the vm
print('\n# Example: Resume the vm')
client.vcenter.vm.Power.start(vm)
2016-10-26 19:08:23 -04:00
print('vm.Power.start({})'.format(vm))
# Reset the vm
print('\n# Example: Reset the vm')
client.vcenter.vm.Power.reset(vm)
2016-10-26 19:08:23 -04:00
print('vm.Power.reset({})'.format(vm))
def cleanup():
# Power off the vm
print('\n# Cleanup: Power off the vm')
client.vcenter.vm.Power.stop(vm)
2016-10-26 19:08:23 -04:00
print('vm.Power.stop({})'.format(vm))
status = client.vcenter.vm.Power.get(vm)
2016-10-26 19:08:23 -04:00
if status == Power.Info(state=Power.State.POWERED_OFF,
clean_power_off=True):
print('VM is powered off')
else:
print('vm.Power Warning: Could not power off vm')
def main():
setup()
run()
if cleardata:
cleanup()
if __name__ == '__main__':
main()