pySphere 管理 VCenter

       VMWare 的核心VCenter提供了针对Python的开发接口-pySphere.由于Python能够在Windows/Linux平台良好运行,所以选择Python能够实现更多的平台适应性。上一篇讲过如何使用PowerShell管理VCenter,其实使用Python来管理的具体做法也是大同小异,尤其体现在函数调用上。

       需要说明的是:

       1.使用pySphere必须安装python环境(推荐2.7环境,3.4环境由于语法不同,在安装的时候会大量报错)

       2.pySphere连接vcenter的方式是使用https形式的ssl连接,最好先安装vcenter的加密证书,否则会报错(certificate validation failed)

       3.如果安装证书后还报错,那么在代码import之后,加一句:ssl._create_default_https_context = ssl._create_unverified_context 这句话的作用是设置ssl不验证

下面是我整合的一个针对VCenter管理的一个类,实现了大部分的功能,还在继续完善中,提供源代码出来,欢迎补充。

'''
@Created on 2015-7-24
@author: Justin
'''

import os
import os.path
import time
import socket
import sys
import string
import ssl
import base64
from pysphere import VIServer
from pysphere import VIException, VIApiException, FaultTypes
#import esxi_exception

#to resolve the ssl problem
#import ssl 
ssl._create_default_https_context = ssl._create_unverified_context

#Server Control
class VCenterManagement:
    server_ip    = ''
    user_name    = ''
    password     = ''
    connect_flag = False
    server       = None
    #vm_list      = []

    #def __init__(self):
    
    #Use the given args to connect the esxi server you want
    #@ip[string]: ESXi server's IP address
    #@name[string]: the username used to login the ESXi server
    #@pwd[string]: the password used to login the ESXi server
    def connect_server(self, ip, name, pwd):
        self.server_ip = ip
        self.user_name = name
        self.password  = pwd
        self.server = VIServer()
        self.server.connect(self.server_ip, self.user_name, self.password)
        self.connect_flag = self.server.is_connected()
        if self.connect_flag:
            return True
        return False

    #To get all the definition registered vms from the connected server
    #@param[string]: can be set as ALL, POWER_ON, POWER_OFF, SUSPENDED
    #According to the param, returns a list of VM Paths. You might also filter by datacenter,
    #cluster, or resource pool by providing their name or MORs. 
    #if  cluster is set, datacenter is ignored, and if resource pool is set
    #both, datacenter and cluster are ignored.
    def get_registered_vms(self, param, status=None, datacenter=None, cluster=None, 
                           resource_pool=None):
        if param not in ['ALL', 'POWER_ON', 'POWER_OFF', 'SUSPENDED']:
            print "Get VMs error: param can only be set as ALL, POWER_ON, POWER_OFF, or SUSPENDED."
            return None
        if self.connect_flag == False:
            print "Get VMs error: Server not connected."
            return None
        if param == 'ALL':
            return self.server.get_registered_vms(datacenter, cluster, resource_pool)
        elif param == 'POWER_ON':
            return self.server.get_registered_vms(datacenter, cluster, resource_pool, status='poweredOn')
        elif param == 'POWER_OFF':
            return self.server.get_registered_vms(datacenter, cluster, resource_pool, status='poweredOff')
        elif param == 'SUSPENDED':
            return self.server.get_registered_vms(datacenter, cluster, resource_pool, status='suspended')
        else:
            return None
    
    #Disconnect to the Server
    def disconnect(self):
        if self.connect_flag == True:
            self.server = self.server.disconnect()
            self.connect_flag == False

    #To keep session alive 
    def keep_session_alive(self):
        assert self.server.keep_session_alive()

    #To get the server type
    def get_server_type(self):
        return self.server.get_server_type()

    #To get performance manager
    def get_performance_manager(self):
        return self.server.get_performance_manager()

    #To get the all the server's hosts
    def get_all_hosts(self):
        """
        Returns a dictionary of the existing hosts keys are their names
        and values their ManagedObjectReference object.
        """
        return self.server.get_hosts()
    
    #To get all datastores
    def get_all_datastores(self):
        """
        Returns a dictionary of the existing datastores. Keys are
        ManagedObjectReference and values datastore names.
        """
        return self.server.get_datastores()

    #To get all clusters
    def get_all_clusters(self):
        """
        Returns a dictionary of the existing clusters. Keys are their 
        ManagedObjectReference objects and values their names.
        """
        return self.server.get_clusters()

    #To get all datacenters
    def get_all_datacenters(self):
        """
        Returns a dictionary of the existing datacenters. keys are their
        ManagedObjectReference objects and values their names.
        """
        return self.server.get_datacenters()

    #To get all resource pools
    def get_all_resource_pools(self):
        """
        Returns a dictionary of the existing ResourcePools. keys are their
        ManagedObjectReference objects and values their full path names.
        """
        return self.server.get_resource_pools()
        
    #To get hosts by name
    def get_hosts_by_name(self, from_mor):
        """
        Returns a dictionary of the existing ResourcePools. keys are their
        ManagedObjectReference objects and values their full path names.
        @from_mor: if given, retrieves the hosts contained within the specified 
            managed entity.
        """
        try:
            hosts_dic = self.server.get_hosts(from_mor)
        except:
            print "Get hosts error!"
            return None
        return hosts_dic

    def get_vm_by_name(self,vm_name):
        try:
            vm = self.server.get_vm_by_name(vm_name)
        except:
            print "Get vm error!"
            return None
        return vm
    
    def power_on_vm(self,vm_name):
        try:
            vm = self.get_vm_by_name(vm_name)
            if(vm.is_powered_off()):
                vm.power_on()
                print "vm " + vm_name + " power on success."
            else:
                print "vm " + vm_name + "is already power on"
                return False
        except:
            print "Power on vm " + vm_name + "error"
            return False
        return True

    def power_off_vm(self,vm_name):
        try:
            vm = self.get_vm_by_name(vm_name)
            if(vm.is_powered_on()):
                vm.power_off()
                print "vm " + vm_name + " power off success."
            else:
                print "vm " + vm_name + "is already power off"
                return False
        except:
            print "Power off vm " + vm_name + " error"
            return False
        return True
下面我写了一些测试

server = VCenterManagement()
if(server.connect_server("135.251.34.48","administrator","asb#2345") == True):
    print "connected"
else:
    print "connect fail"

datacenters = server.get_all_datacenters()
for k,v in datacenters.items():
    print "key:" + k + ",value:" + str(v);

clusters = server.get_all_clusters()
for k,v in clusters.items():
    print "key:" + k + ",value:" + str(v);

v = server.get_vm_by_name("MyVM1")
vms = server.get_registered_vms("ALL")
#print vms.get_property("name")
for vm in vms:
    print vm
#server.power_off_vm("MyVM1")
if(v.get_status() == "POWERED ON"):
    server.power_off_vm("MyVM1")
else:
    server.power_on_vm("MyVM1")

print v.get_property("name")
print v.get_status()

server.disconnect()
print "server disconnected!"
运行结果如下:

pySphere 管理 VCenter_第1张图片

总结:Python脚本非常高效,开发效率也是非常高的,尤其在自动化方面,是非常好的帮手,熟悉python能够非常方便地开发一些服务器管理的功能。

你可能感兴趣的:(python,VCenter,pysphere)