public void Login( string login, string password ) { if ( !VirtualMachineIsRunning() ) { throw new VirtualMachineIsNotReadyException( this, "Virtual machine must be powered on first." ); } _manager.UpdateViewData(); GuestAuthManager auth = new GuestAuthManager( _virtualMachine.Client, _manager.AuthManager ); _authentication = new NamePasswordAuthentication { Username = login, Password = password, InteractiveSession = true }; while ( _virtualMachine.Guest.ToolsStatus.GetValueOrDefault( VirtualMachineToolsStatus.toolsNotRunning ) != VirtualMachineToolsStatus.toolsOk || !_virtualMachine.Guest.InteractiveGuestOperationsReady.GetValueOrDefault( false ) ) { Thread.Sleep( 1000 ); _virtualMachine.UpdateViewData(); if ( !VirtualMachineIsRunning() ) { throw new VirtualMachineIsNotReadyException( this, "Virtual machine must be powered on first." ); } } auth.ValidateCredentialsInGuest( _virtualMachine.MoRef, _authentication ); _fileManager = new GuestFileManager( _virtualMachine.Client, _manager.FileManager ); }
public override string tryGetFileFromGuest(string srcpath, out Exception errorOrNull) { try { NamePasswordAuthentication Auth = new NamePasswordAuthentication { Username = _spec.kernelVMUsername, Password = _spec.kernelVMPassword, InteractiveSession = true }; VimClientImpl vClient = conn.getConnection(); VirtualMachine underlyingVM = conn.getMachine(); GuestOperationsManager gom = (GuestOperationsManager)vClient.GetView(vClient.ServiceContent.GuestOperationsManager, null); GuestAuthManager guestAuthManager = vClient.GetView(gom.AuthManager, null) as GuestAuthManager; guestAuthManager.ValidateCredentialsInGuest(underlyingVM.MoRef, Auth); GuestFileManager GFM = vClient.GetView(gom.FileManager, null) as GuestFileManager; FileTransferInformation transferOutput = GFM.InitiateFileTransferFromGuest(underlyingVM.MoRef, Auth, srcpath); string nodeIpAddress = vClient.ServiceUrl; nodeIpAddress = nodeIpAddress.Remove(nodeIpAddress.LastIndexOf('/')); string url = transferOutput.Url.Replace("https://*", nodeIpAddress); using (WebClient webClient = new WebClient()) { errorOrNull = null; return(webClient.DownloadString(url)); } } catch (Exception e) { errorOrNull = e; return(null); } }
private byte[] ReadFileFromVirtualMachine( string fileInVirtualMachine ) { _vmWareVirtualMachine.UpdateViewData(); GuestOperationsManager operationsInGuestManager = new GuestOperationsManager( _vmWareVirtualMachine.Client, _vmWareVirtualMachine.Client.ServiceContent. GuestOperationsManager ); operationsInGuestManager.UpdateViewData(); GuestFileManager fileManager = new GuestFileManager( _vmWareVirtualMachine.Client, operationsInGuestManager.FileManager ); NamePasswordAuthentication authentication = new NamePasswordAuthentication { Username = VirtualMachineUserName, Password = VirtualMachineUserPassword, InteractiveSession = false }; string url = fileManager.InitiateFileTransferFromGuest( _vmWareVirtualMachine.MoRef, authentication, fileInVirtualMachine ).Url; url = url.Replace( "*", FarmUrl ); string cookie = _vmWareVirtualMachine.Client.VimService.CookieContainer.GetCookies( new Uri( _vmWareVirtualMachine.Client.VimService.Url ) )[ 0 ].ToString(); WebClient webClient = new WebClient { Credentials = _vmWareVirtualMachine.Client.VimService.Credentials }; webClient.Headers.Add( HttpRequestHeader.Cookie, cookie ); byte[] fileData = webClient.DownloadData( url ); webClient.Dispose(); return fileData; }
public override void copyToGuest(string dstpath, string srcpath, cancellableDateTime deadline = null) { if (!File.Exists(srcpath)) { throw new Exception("src file not found"); } // TODO: deadline is ignored here NamePasswordAuthentication Auth = new NamePasswordAuthentication { Username = _spec.kernelVMUsername, Password = _spec.kernelVMPassword, InteractiveSession = true }; VimClientImpl _vClient = conn.getConnection(); VirtualMachine _underlyingVM = conn.getMachine(); GuestOperationsManager gom = (GuestOperationsManager)_vClient.GetView(_vClient.ServiceContent.GuestOperationsManager, null); GuestAuthManager guestAuthManager = _vClient.GetView(gom.AuthManager, null) as GuestAuthManager; guestAuthManager.ValidateCredentialsInGuest(_underlyingVM.MoRef, Auth); GuestFileManager GFM = _vClient.GetView(gom.FileManager, null) as GuestFileManager; System.IO.FileInfo FileToTransfer = new System.IO.FileInfo(srcpath); GuestFileAttributes GFA = new GuestFileAttributes() { AccessTime = FileToTransfer.LastAccessTimeUtc, ModificationTime = FileToTransfer.LastWriteTimeUtc }; if (dstpath.EndsWith("\\")) { dstpath += Path.GetFileName(srcpath); } string transferOutput = GFM.InitiateFileTransferToGuest(_underlyingVM.MoRef, Auth, dstpath, GFA, FileToTransfer.Length, true); string nodeIpAddress = _vClient.ServiceUrl.ToString(); nodeIpAddress = nodeIpAddress.Remove(nodeIpAddress.LastIndexOf('/')); transferOutput = transferOutput.Replace("https://*", nodeIpAddress); Uri oUri = new Uri(transferOutput); using (WebClient webClient = new WebClient()) { webClient.UploadFile(oUri, "PUT", srcpath); } }
public override void mkdir(string newDir, cancellableDateTime deadline) { // TODO: timeouts NamePasswordAuthentication Auth = new NamePasswordAuthentication { Username = _spec.kernelVMUsername, Password = _spec.kernelVMPassword, InteractiveSession = true }; VimClientImpl _vClient = conn.getConnection(); VirtualMachine _underlyingVM = conn.getMachine(); GuestOperationsManager gom = (GuestOperationsManager)_vClient.GetView(_vClient.ServiceContent.GuestOperationsManager, null); GuestAuthManager guestAuthManager = _vClient.GetView(gom.AuthManager, null) as GuestAuthManager; guestAuthManager.ValidateCredentialsInGuest(_underlyingVM.MoRef, Auth); GuestFileManager GFM = _vClient.GetView(gom.FileManager, null) as GuestFileManager; GFM.MakeDirectoryInGuest(_underlyingVM.MoRef, Auth, newDir, true); }
public void AuthenticateInGuest() { _manager.UpdateViewData(); GuestAuthManager auth = new GuestAuthManager( _virtualHost, _manager.AuthManager ); while ( true ) { try { auth.ValidateCredentialsInGuest( _vm.MoRef, _authentication ); break; } catch ( VimException ) { Thread.Sleep( 10000 ); } } _fileManager = new GuestFileManager( _virtualHost, _manager.FileManager ); }