利用VMware Infrastructure SDK編程控制虛擬機集羣(1)

兩年前的一個老項目了,基於VMware Infrastructure 3.5的,整理一下當時的技術資料。至於VMware Infrastructure是什麼以及它能幹什麼,不詳細介紹了,感興趣的同窗能夠本身百度一下。瀏覽器


一、經過什麼方式訪問VI中心session

VMware Infrastructure對外提供WebService,供第三方應用調用,以實現針對主機、虛擬機等資源的控制。針對.Net,提供了一個VimService2003.dll,開發時須要把它加入項目引用。異步


二、如何查看VI集羣中各資源的信息ide

除了使用Vmware Infrastructure Client之外,可使用瀏覽器查看,地址是http://localhost:8080/mob。 其中的8080是vfxd服務設置的監聽端口。spa


三、如何登陸VI中心
對象

  
  
  
  
  1. public class ViDemo 
  2.     //如下是VI開發中會用到的全部對象 
  3.     private VimService m_Service; 
  4.     private ServiceContent m_Content; 
  5.     private ManagedObjectReference m_SvcRef; 
  6.     private ManagedObjectReference m_Collector; 
  7.     private UserSession m_Session; 
  8.  
  9.     /// <summary> 
  10.     /// 登陸,耗時會比較長 
  11.     /// </summary> 
  12.     public void Connect() 
  13.     { 
  14.         m_SvcRef = new ManagedObjectReference(); 
  15.         m_SvcRef.type = "ServiceInstance"
  16.         m_SvcRef.Value = "ServiceInstance"
  17.      
  18.         m_Service = new VimService(); 
  19.         m_Service.Url = "http://localhost:8080/sdk"
  20.         m_Content = m_Service.RetrieveServiceContent(m_SvcRef); 
  21.         m_Collector = m_Content.propertyCollector; 
  22.         if(m_Content.sessionManager != null
  23.         { 
  24.             m_Session = m_Service.Login(m_Content.sessionManager, "USER""PASS"null); 
  25.         } 
  26.     } 
  27.  
  28.     /// <summary> 
  29.     /// 註銷 
  30.     /// </summary> 
  31.     public void Disconnect() 
  32.     { 
  33.         if(m_Service != null
  34.         { 
  35.             m_Service.Logout(m_Content.sessionManager); 
  36.             m_Service.Dispose(); 
  37.             m_Service = null
  38.             m_Content = null
  39.             m_Session = null
  40.         } 
  41.     } 


四、虛擬機開機及關機資源

  
  
  
  
  1. /// <summary> 
  2. /// 虛擬機關機
  3. /// </summary> 
  4. public void PowerOff() 
  5.     //虛擬機的資源路徑,格式是「中心/vm/虛擬機名稱」,本例中中心叫DataCenter,虛擬機是vm100 
  6. //不用關心虛擬機在哪臺主機上,由於對VI來講,集羣是一個總體
  7.     string path = "DataCenter/vm/vm100"
  8.  
  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.     } 


未完待續……
開發

相關文章
相關標籤/搜索