53421 发表于 2015-4-4 17:23:41

虚拟化应用(二)管理 vCenter Server 实体

  正如企业中有多个管理部门一样,在一个虚拟化系统中也分为若干个实体。
测试代码下载
测试环境:
Windows Server 2008 R2 DataCenter
VMWare Workstation 7.1
VMWare vSphere 4.1、VMWarevCenter Server 4.1
JDK 1.6 u-21 x64、Eclipse ForJavaEE 3.6 x64
VMWare vSphere Java API 2.1 GA、VMWarevSphere PowerCLI 4.1(PowerShell 2.0)
  PowerGUI Script Editor Free Edition x64
  
  在VMWare vCenter Server中,其实体结构如下:

  处于最顶层的是一个服务实例对象,可以简单的认为是一个虚拟服务器。下一级是根目录,是一个容器对象;第三层中的Datacenter并不是物理服务器中的Datacenter,只是一个管理单元,可以是Folder、Datacenter或二者的混合体;
  再往下又细分为虚拟机和计算机资源等。实体的种类很多,除了Folder、Datacenter,还有VirtualMachine、ComputerResource、ClusterComputerResource、ResourcePool和HostSystem。本次我们只关注Folder和Datacenter,后续篇章再学习其他实体。
  安装好VMWare vCenter Server,使用vClient登录后可以看到这些实体结构:

  在GUI中可以方便的对实体结构进行管理,如新建、修改、移动、删除文件夹、数据中心等。但是,有时管理员需要同时管理大量的虚拟机,这就需要用程序来自动化管理了。2010.8.25官方放出了vSphere Java API 2.1 GA,这次版本全面支持了前不久发布的vSphere 4.1,修复了一些bug:(英文原文)
  Bug Fixes
3049871 Missing SetPublicKey in ExtensionManager
3040909 getProfile in HostProfilesManager throws a classcastexceptio
3042149 match version in SessionManager.cloneSession() method
3042167 xml characters not escaped (partial fix with password which is more likely to contain special chars)
  同时也放出了vSphere Java API 3.0的开发计划,代码代号"Crescendo"(逐渐增强),具体细节在这篇博客查看。
  
  一、下面开始用程序来管理vCenter Server中的部分实体。

[*]新建一个Java项目,添加vSphere Java API 2.1 GA的Jar包:

  建议将源代码添加进Eclipse,可以随时查看指定方法或类的源代码:

  

[*]管理实体:
  
代码

package brooks.chapter6.manageinventory;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.SearchIndex;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.Task;
public class EntityManage {
    /**
   * @param args
   * @throws MalformedURLException
   * @throws RemoteException
   */
    private static void fnAddEntity(URL v_Url, String v_strUserName,
            String v_strUserPass) {
      try {
            ServiceInstance si = new ServiceInstance(v_Url, v_strUserName,
                  v_strUserPass, true);
            Folder rootFolder = si.getRootFolder();
            Folder secFolder = rootFolder.createFolder("cnBlogs");
            secFolder.createDatacenter("HomePage");
            secFolder.createDatacenter("News");
            secFolder.createDatacenter("Blogs");
            secFolder.createDatacenter("BBS");
            secFolder.createDatacenter("Attention");
            si.getServerConnection().logout();
            System.out.println("添加实体成功!");
      } catch (Exception ex) {
            ex.printStackTrace();
      }
    }
    private static void fnDeleteEntity(URL v_Url, String v_strUserName,
            String v_strUserPass) {
      try {
            ServiceInstance si = new ServiceInstance(v_Url, v_strUserName,
                  v_strUserPass, true);
            Folder rootFolder = si.getRootFolder();
            ManagedEntity[] mes = rootFolder.getChildEntity();
            for (int i = 0; mes != null && i < mes.length; i++) {
                if ("cnBlogs".equals(mes.getName())) {
                  mes.destroy_Task();
                }
                if ("HomePage".equals(mes.getName())) {
                  mes.destroy_Task();
                }
                if ("News".equals(mes.getName())) {
                  mes.destroy_Task();
                }
                if ("Blogs".equals(mes.getName())) {
                  mes.destroy_Task();
                }
                if ("BBS".equals(mes.getName())) {
                  mes.destroy_Task();
                }
                if ("Attention".equals(mes.getName())) {
                  mes.destroy_Task();
                }
            }
            si.getServerConnection().logout();
            System.out.println("删除实体成功!");
      } catch (Exception ex) {
            ex.printStackTrace();
      }
    }
    private static void fnMoveEntity(URL v_Url, String v_strUserName,
            String v_strUserPass) {
      try {
            ServiceInstance si = new ServiceInstance(v_Url, v_strUserName,
                  v_strUserPass, true);
            Folder rootFolder = si.getRootFolder();
            SearchIndex index = si.getSearchIndex();
            rootFolder.createFolder("Folder1");
            Folder folder2 = rootFolder.createFolder("Folder2");
            ManagedEntity me = index.findByInventoryPath("Folder1");
            folder2.moveIntoFolder_Task(new ManagedEntity[] { me });
            System.out.println("移动实体成功!");
      } catch (Exception ex) {
            ex.printStackTrace();
      }
    }
    private static void fnRenameEntity(URL v_Url, String v_strUserName,
            String v_strUserPass) {
      try {
            ServiceInstance si = new ServiceInstance(v_Url, v_strUserName,
                  v_strUserPass, true);
            Folder rootFolder = si.getRootFolder();
            Folder folder1 = rootFolder.createFolder("Folder1");
            Task task = folder1.rename_Task("Folder2");
            if (task.waitForTask() == Task.SUCCESS) {
                System.out.println("重命名实体成功!");
            } else {
                System.out.println("重命名实体失败!");
            }
            si.getServerConnection().logout();
      } catch (Exception ex) {
            ex.printStackTrace();
      }
    }
    public static void main(String[] args) throws MalformedURLException {
      // TODO Auto-generated method stub
      fnAddEntity(new URL(args), args, args);
      fnRenameEntity(new URL(args), args, args);
      fnMoveEntity(new URL(args), args, args);
      fnDeleteEntity(new URL(args), args, args);
    }
}
  

  
  配置下命令行参数:

  
  二、使用PowerShell来管理实体。
  vSphere PowerCLI提供了丰富的命令,今天偶然在网上找到一个PDF,还不错:

  将PowerShell按不同类别进行了分类,非常便于查找、使用,注意这是2009年7月14日官方发布的,最新版中可能有部分改动,请参照最新官方文档。
  
  2.1、管理Folder。
  2.1.1、如果你还没有添加PSSnapin,则先进行添加:
  Add-PSSnapinVMWare.VimAutomation.Core
  
  2.1.2、连接vCenter Server:
  Connect-VIServer-Server192.168.220.1-Protocolhttps-Port 444 -UserBrooksPC\Administrator-Password******-CredentialFalse(请用你的vCenter Server的IP、端口号、用户名、密码进行替换):
  运行结果:

  
  2.1.3、获取根目录:
  Get-Folder –NoRecursion
  运行结果:

  
  2.1.4、在根目录中新建一个Folder:
  New-Folder-NameFolder0-LocationBrooks
  运行结果:


  
  2.1.5、重命名Folder:
  Set-Folder-FolderFolder0-NameFolder2
  运行结果:


  这个命令有点不太完善,未能提供根据路径定位到具体Folder的方法,若有Folder重名,但位于不同Folder中,则该命令会失败。
  
  2.1.6、移动Folder:
  Move-Folder-FolderFolder2-DestinationcnBlogs
  运行结果:


  
  2.1.6、删除Folder:
  删除时会提示确认,这里居然没有提供不确认的参数,难以批量删除。


  
  2.1.7、获取所有Datacenter:
  Get-Datacenter
  运行结果:

  
  2.1.8、创建一个Datacenter:
  New-Datacenter-Namedc1-LocationcnBlogs
  运行结果:


  
  2.1.9、重命名Datacenter:
  Set-Datacenter-Namedc2-Datacenterdc1
  注意-Name是修改后的名字。
  运行结果:


  
  2.1.10、移动Datacenter时有点问题,遇到一个错误,若有朋友知道解决方法,请指教:
  Move-Datacenter -Datacenter dc2 -Destination cnBlogs

  
  2.1.11、删除Datacenter:
  Remove-Datacenter-Datacenterdc2
  运行结果:

  依然会先提示确认。
  
  小结
  本次我们熟悉了下vCenter Server中的实体架构,能够使用其对象模型和PowerShell脚本进行简单的管理,可以看到,脚本管理非常高效。其实除了VMWare vSphere,Hyper-V、思捷的Xen Server均提供了PowerShell的扩展,后续篇章会继续深入学习ESX和vCenter Server,以及PowerShell在虚拟化管理中的应用。
页: [1]
查看完整版本: 虚拟化应用(二)管理 vCenter Server 实体