Beispiel #1
0
 public void ChangeFileAttributesInGuest(string guestFilePath, GuestFileAttributes fileAttributes)
 {
     //throw new NotImplementedException();
     _vimService.ChangeFileAttributesInGuest(
         _morFileManager,             //VimLib.VimServiceReference.ManagedObjectReference _this,
         _morVM,                      //VimLib.VimServiceReference.ManagedObjectReference vm,
         _NamePasswordAuthentication, //VimLib.VimServiceReference.GuestAuthentication auth,
         guestFilePath,               //string guestFilePath,
         fileAttributes               //VimLib.VimServiceReference.GuestFileAttributes fileAttributes
         );
 }
Beispiel #2
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);
            }
        }
Beispiel #3
0
        public async Task <string> UploadFileToVm(Guid uuid, string username, string password, string filepath, Stream fileStream)
        {
            _logger.LogDebug("UploadFileToVm called");

            ManagedObjectReference vmReference = vmReference = await GetVm(uuid);

            if (vmReference == null)
            {
                var errorMessage = $"could not upload file, vmReference is null";
                _logger.LogDebug(errorMessage);
                return(errorMessage);
            }
            //retrieve the properties specificied
            RetrievePropertiesResponse response = await _client.RetrievePropertiesAsync(
                _props,
                VmFilter(vmReference));

            NetVimClient.ObjectContent[] oc  = response.returnval;
            NetVimClient.ObjectContent   obj = oc[0];

            foreach (DynamicProperty dp in obj.propSet)
            {
                if (dp.val.GetType() == typeof(VirtualMachineSummary))
                {
                    VirtualMachineSummary vmSummary = (VirtualMachineSummary)dp.val;
                    //check vmware tools status
                    var tools_status = vmSummary.guest.toolsStatus;
                    if (tools_status == VirtualMachineToolsStatus.toolsNotInstalled || tools_status == VirtualMachineToolsStatus.toolsNotRunning)
                    {
                        var errorMessage = $"could not upload file, VM Tools is not running";
                        _logger.LogDebug(errorMessage);
                        return(errorMessage);
                    }

                    // user credentials on the VM
                    NamePasswordAuthentication credentialsAuth = new NamePasswordAuthentication()
                    {
                        interactiveSession = false,
                        username           = username,
                        password           = password
                    };
                    ManagedObjectReference fileManager = new ManagedObjectReference()
                    {
                        type  = "GuestFileManager",
                        Value = "guestOperationsFileManager"
                    };
                    // upload the file
                    GuestFileAttributes fileAttributes = new GuestFileAttributes();
                    var fileTransferUrl = _client.InitiateFileTransferToGuestAsync(fileManager, vmReference, credentialsAuth, filepath, fileAttributes, fileStream.Length, true).Result;

                    // Replace IP address with hostname
                    RetrievePropertiesResponse hostResponse = await _client.RetrievePropertiesAsync(_props, HostFilter(vmSummary.runtime.host, "name"));

                    string hostName = hostResponse.returnval[0].propSet[0].val as string;

                    if (!fileTransferUrl.Contains(hostName))
                    {
                        fileTransferUrl = fileTransferUrl.Replace("https://", "");
                        var s = fileTransferUrl.IndexOf("/");
                        fileTransferUrl = "https://" + hostName + fileTransferUrl.Substring(s);
                    }

                    // http put to url
                    using (var httpClientHandler = new HttpClientHandler())
                    {
                        using (var httpClient = new HttpClient(httpClientHandler))
                        {
                            httpClient.DefaultRequestHeaders.Accept.Clear();
                            using (MemoryStream ms = new MemoryStream())
                            {
                                var timeout = _configuration.GetSection("vmOptions").GetValue("Timeout", 3);
                                httpClient.Timeout = TimeSpan.FromMinutes(timeout);
                                fileStream.CopyTo(ms);
                                var fileContent    = new ByteArrayContent(ms.ToArray());
                                var uploadResponse = await httpClient.PutAsync(fileTransferUrl, fileContent);
                            }
                        }
                    }
                }
            }
            return("");
        }