Example #1
0
        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);
            }
        }
        public TestEnvironment( VmWareConfiguration vmWareConfiguration )
        {
            _vmWareConfiguration = vmWareConfiguration;

            _virtualHost = new VimClient();
            _virtualHost.Login(
                string.Format( "https://{0}/sdk", _vmWareConfiguration.FarmUrl ),
                _vmWareConfiguration.FarmLogin,
                _vmWareConfiguration.FarmPassword );

            _vm = ( VirtualMachine )_virtualHost.FindEntityView(
                typeof( VirtualMachine ),
                null,
                new NameValueCollection
                    {
                        {"name", _vmWareConfiguration.VirtualMachineName}
                    },
                null );

            _manager = new GuestOperationsManager( _virtualHost, _virtualHost.ServiceContent.GuestOperationsManager );

            _authentication = new NamePasswordAuthentication
                                  {
                                      Username = _vmWareConfiguration.User,
                                      Password = _vmWareConfiguration.Password,
                                      InteractiveSession = true
                                  };
        }
        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 SphereVirtualMachine( VirtualMachine virtualMachine )
        {
            if ( virtualMachine == null )
            {
                throw new ArgumentNullException( "virtualMachine", "A valid vSphere virtual machine object must be provided." );
            }

            _virtualMachine = virtualMachine;
            _virtualMachine.UpdateViewData();
            _manager = new GuestOperationsManager( _virtualMachine.Client, _virtualMachine.Client.ServiceContent.GuestOperationsManager );
        }
Example #5
0
        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);
            }
        }
Example #6
0
        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);
        }
Example #7
0
        public override IAsyncExecutionResult startExecutableAsync(string toExecute, string args, string workingDir = null)
        {
            string tempDir = String.Format("C:\\users\\{0}\\", _spec.kernelVMUsername);

            if (workingDir == null)
            {
                workingDir = tempDir;
            }

            execFileSet fileSet = prepareForExecution(toExecute, args, tempDir);

            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 = (GuestAuthManager)_vClient.GetView(gom.AuthManager, null);

            guestAuthManager.ValidateCredentialsInGuest(_underlyingVM.MoRef, auth);
            GuestProcessManager guestProcessManager = _vClient.GetView(gom.ProcessManager, null) as GuestProcessManager;
            GuestProgramSpec    progSpec            = new GuestProgramSpec
            {
                ProgramPath      = fileSet.launcherPath,
                Arguments        = "",
                WorkingDirectory = workingDir
            };

            guestProcessManager.StartProgramInGuest(_underlyingVM.MoRef, auth, progSpec);

            return(new asyncExecutionResultViaFile(this, fileSet));
        }