python调用vmware api关闭虚拟机

mha的博文中说到mha切换mysql时,如果主库无法正常ssh,为防止脑裂,需要通过控制卡关物理机,或者vmware api关闭虚拟机。这里总结下如何调用api关闭虚拟机。

安装环境:centos6.9

1.下载python安装包

Python-3.3.7.tgz
pip-9.0.1.tar.gz
setuptools-36.5.0.zip

2.安装python3

yum install readline-devel sqlite-devel bzip2-devel openssl-devel.i686 gdbm-devel.i686 libdbi-devel.i686 ncurses-libs zlib-devel xz-devel tcl-devel tk-devel openssl openssl-devel
# cd Python-3.3.7
# ./configure
# make
# make install
# python3 -V
Python 3.3.7
# which python3

/usr/local/bin/python3

3.安装pip3

# cd setuptools-36.5.0
# python3 setup.py install
# cd pip-9.0.1
# python3 setup.py install
# which pip3
/usr/local/bin/pip3
# pip3 -V

pip 9.0.1 from /usr/local/lib/python3.3/site-packages/pip-9.0.1-py3.3.egg (python 3.3)

4.安装依赖并git clone

# pip3 install argparse
# pip3 install pyvmomi
# pip3 install suds-jurko
# pip3 install vcrpy
# git clone https://github.com/vmware/pyvmomi-community-samples.git

该项目提供了很多脚本,其中包括:power_manager.py 

#!/usr/bin/env python
# VMware vSphere Python SDK
# Copyright (c) 2008-2013 VMware, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
A Python script for changing the name of an object. Demonstrates the use
of tasks in an asynchronous way.
"""

import atexit
import argparse
import getpass
import sys
import time

from pyVim import connect
from pyVmomi import vim
from pyVim.task import WaitForTask


def get_args():
    parser = argparse.ArgumentParser()

    parser.add_argument('-t', '--type',
                        required=True,
                        action='store',
                        help='Action type: poweron poweroff powerstatus')

    parser.add_argument('-s', '--host',
                        required=True,
                        action='store',
                        help='Remote host to connect to')

    parser.add_argument('-u', '--user',
                        required=True,
                        action='store',
                        help='User name to use when connecting to host')

    parser.add_argument('-p', '--password',
                        required=False,
                        action='store',
                        help='Password to use when connecting to host')

    parser.add_argument('-o', '--port',
                        required=False,
                        action='store',
                        help="port to use, default 443", default=443)

    parser.add_argument('-i', '--ip',
                        required=True,
                        action='store',
                        help='ip of the entity to look for.')

    args = parser.parse_args()
    if args.password is None:
        args.password = getpass.getpass(
            prompt='Enter password for host %s and user %s: ' %
                   (args.host, args.user))

    args = parser.parse_args()
    return args

args = get_args()

# form a connection...
si = connect.SmartConnect(host=args.host, user=args.user, pwd=args.password,
                          port=args.port)

# doing this means you don't need to remember to disconnect your script/objects
atexit.register(connect.Disconnect, si)

if not si:
    raise SystemExit("Unable to connect to host with supplied info.")

obj = si.content.searchIndex.FindByIp(None, args.ip, True)

if obj is None:
    print ("A object ip %s could not be found" % args.ip)
    exit()

print
print ("vm_name: %s" % obj.name)
print
print

if args.type == "poweron":
    task=obj.PowerOnVM_Task()
    time.sleep(5)
    print ("task_state: %s" %task.info.state)

if args.type == "poweroff":
    task=obj.PowerOffVM_Task()
    time.sleep(5)
    print ("task_state: %s" %task.info.state)
    
print ("vm_powerstate: %s" %obj.runtime.powerState)

print

如果考虑自己开发自动化管理vmware虚拟机的平台,其中的很多脚本可以用得上,不过现在docker是大势所趋,我觉得没必要做类似的平台了。


发布了24 篇原创文章 · 获赞 25 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/sdmei/article/details/79293479