你好,游客 登录 注册 搜索
背景:
阅读新闻

利用VMware Infrastructure SDK编程控制虚拟机集群

[日期:2012-12-05] 来源:Linux社区  作者:boytnt [字体:大 中 小]

两年前的一个老项目了,基于VMware Infrastructure 3.5的,整理一下当时的技术资料。至于VMware Infrastructure是什么以及它能干什么,不详细介绍了。

1、通过什么方式访问VI中心

VMware Infrastructure对外提供WebService,供第三方应用调用,以实现针对主机、虚拟机等资源的控制。针对.Net,提供了一个VimService2003.dll,开发时需要把它加入项目引用。

2、如何查看VI集群中各资源的信息

除了使用Vmware Infrastructure Client以外,可以使用浏览器查看,地址是http://localhost:8080/mob。 其中的8080是vfxd服务设置的监听端口。

3、如何登录VI中心

  1. publicclass ViDemo
  2. {
  3. //以下是VI开发中会用到的所有对象
  4. private VimService m_Service;
  5. private ServiceContent m_Content;
  6. private ManagedObjectReference m_SvcRef;
  7. private ManagedObjectReference m_Collector;
  8. private UserSession m_Session;
  9. /// <summary>
  10. /// 登录,耗时会比较长
  11. /// </summary>
  12. publicvoid Connect()
  13. {
  14. m_SvcRef = new ManagedObjectReference();
  15. m_SvcRef.type = "ServiceInstance";
  16. m_SvcRef.Value = "ServiceInstance";
  17. m_Service = new VimService();
  18. m_Service.Url = "http://localhost:8080/sdk";
  19. m_Content = m_Service.RetrieveServiceContent(m_SvcRef);
  20. m_Collector = m_Content.propertyCollector;
  21. if(m_Content.sessionManager != null)
  22. {
  23. m_Session = m_Service.Login(m_Content.sessionManager, "USER", "PASS", null);
  24. }
  25. }
  26. /// <summary>
  27. /// 注销
  28. /// </summary>
  29. publicvoid Disconnect()
  30. {
  31. if(m_Service != null)
  32. {
  33. m_Service.Logout(m_Content.sessionManager);
  34. m_Service.Dispose();
  35. m_Service = null;
  36. m_Content = null;
  37. m_Session = null;
  38. }
  39. }
  40. }

4、虚拟机开机及关机

  1. /// <summary>
  2. /// 虚拟机关机
  3. /// </summary>
  4. publicvoid PowerOff()
  5. {
  6. //虚拟机的资源路径,格式是“中心/vm/虚拟机名称”,本例中中心叫DataCenter,虚拟机是vm100
  7. //不用关心虚拟机在哪台主机上,因为对VI来说,集群是一个整体
  8. string path = "DataCenter/vm/vm100";
  9. //根据虚拟机的资源路径获取资源的引用
  10. ManagedObjectReference vmRef = m_Service.FindByInventoryPath(m_Content.searchIndex, path);
  11. if(vmRef != null)
  12. {
  13. //调用服务上的PowerOffVM_Task来关闭虚拟机(异步),此处没有等待任务完成
  14. //如果是开机,调用PowerOnVM_Task方法
  15. ManagedObjectReference taskRef = m_Service.PowerOffVM_Task(vmRef);
  16. }
  17. }
linux
相关资讯       VMware Infrastructure 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款