Esempio n. 1
0
        private void NormalizeTemplate(VmTemplate template, HypervisorServiceConfiguration option)
        {
            if (!template.Iso.HasValue())
            {
                // need to have a backing file to add the cdrom device
                template.Iso = option.IsoStore + "null.iso";
            }

            var isopath = new DatastorePath(template.Iso);

            isopath.Merge(option.IsoStore);
            template.Iso = isopath.ToString();

            foreach (VmDisk disk in template.Disks)
            {
                if (!disk.Path.StartsWith(option.DiskStore)
                    )
                {
                    DatastorePath dspath = new DatastorePath(disk.Path);
                    dspath.Merge(option.DiskStore);
                    disk.Path = dspath.ToString();
                }
                if (disk.Source.HasValue() && !disk.Source.StartsWith(option.DiskStore)
                    )
                {
                    DatastorePath dspath = new DatastorePath(disk.Source);
                    dspath.Merge(option.DiskStore);
                    disk.Source = dspath.ToString();
                }
            }

            if (template.IsolationTag.HasValue())
            {
                string tag = "#" + template.IsolationTag;
                Regex  rgx = new Regex("#.*");
                if (!template.Name.EndsWith(template.IsolationTag))
                {
                    template.Name = rgx.Replace(template.Name, "") + tag;
                }
                foreach (VmNet eth in template.Eth)
                {
                    //don't add tag if referencing a global vlan
                    if (!_vlanman.Contains(eth.Net))
                    {
                        eth.Net = rgx.Replace(eth.Net, "") + tag;
                    }
                }
            }
        }
Esempio n. 2
0
        public async Task <Vm> ChangeConfiguration(string id, VmKeyValue change)
        {
            _logger.LogDebug("changing " + id + " " + change.Key + "=" + change.Value);

            Vm vm = await Load(id);

            if (vm == null)
            {
                throw new InvalidOperationException();
            }

            VimClient host = FindHostByVm(id);
            VmOptions vmo  = null;

            var    segments = change.Value.Split(':');
            string val      = segments.First();
            string tag      = segments.Length > 1
                ? $":{segments.Last()}"
                : "";

            //sanitize inputs
            if (change.Key == "iso")
            {
                // vmo = await GetVmIsoOptions(vm.Name.Tag());
                // if (!vmo.Iso.Contains(change.Value))
                //     throw new InvalidOperationException();
                var isopath = new DatastorePath(val);
                isopath.Merge(host.Options.IsoStore);
                change = new VmKeyValue
                {
                    Key   = "iso",
                    Value = isopath.ToString() + tag
                };
            }

            if (change.Key == "net" && !change.Value.StartsWith("_none_"))
            {
                vmo = await GetVmNetOptions(vm.Name.Tag());

                if (!vmo.Net.Contains(val))
                {
                    throw new InvalidOperationException();
                }
            }

            return(await host.Change(id, change));
        }
Esempio n. 3
0
        public async Task <Vm> ChangeConfiguration(string id, VmKeyValue change)
        {
            _logger.LogDebug("changing " + id + " " + change.Key + "=" + change.Value);
            Vm vm = await Load(id);

            if (vm == null)
            {
                throw new InvalidOperationException();
            }

            VimClient host = FindHostByVm(id);
            VmOptions vmo  = null;

            //sanitize inputs
            if (change.Key == "iso")
            {
                // vmo = await GetVmIsoOptions(vm.Name.Tag());
                // if (!vmo.Iso.Contains(change.Value))
                //     throw new InvalidOperationException();
                var isopath = new DatastorePath(change.Value);
                isopath.Merge(host.Options.IsoStore);
                change = new VmKeyValue
                {
                    Key   = "iso",
                    Value = isopath.ToString()
                };
            }

            if (change.Key == "net")
            {
                vmo = await GetVmNetOptions(vm.Name.Tag());

                if (!vmo.Net.Contains(change.Value))
                {
                    throw new InvalidOperationException();
                }
            }

            return(await host.Change(id, change));
        }
Esempio n. 4
0
        public async Task <string[]> GetFiles(string path, bool recursive)
        {
            await Connect();

            List <string> list    = new List <string>();
            DatastorePath dsPath  = new DatastorePath(path);
            string        oldRoot = "";
            string        pattern = dsPath.File ?? "*";

            RetrievePropertiesResponse response = await _vim.RetrievePropertiesAsync(
                _props,
                FilterFactory.DatastoreFilter(_res)
                );

            ObjectContent[] oc = response.returnval;

            foreach (ObjectContent obj in oc)
            {
                ManagedObjectReference dsBrowser = (ManagedObjectReference)obj.propSet[0].val;

                var capability = obj.propSet[1].val as DatastoreCapability;

                var summary = obj.propSet[2].val as DatastoreSummary;

                // if topLevelDirectory not supported (vsan), map from directory name to guid)
                if (
                    capability.topLevelDirectoryCreateSupportedSpecified &&
                    !capability.topLevelDirectoryCreateSupported &&
                    dsPath.TopLevelFolder.HasValue()
                    )
                {
                    oldRoot = dsPath.TopLevelFolder;
                    string target = summary.url + oldRoot;

                    if (!_dsnsMap.ContainsKey(target))
                    {
                        var result = await _vim.ConvertNamespacePathToUuidPathAsync(
                            _dsns,
                            _datacenter,
                            target
                            );

                        _dsnsMap.Add(target, result.Replace(summary.url, ""));
                    }

                    dsPath.TopLevelFolder = _dsnsMap[target];

                    // vmcloud sddc errors on Search_Datastore()
                    // so force SearchDatastoreSubFolders()
                    recursive = true;
                    pattern   = "*" + Path.GetExtension(dsPath.File);

                    _logger.LogDebug("mapped datastore namespace: " + dsPath.ToString());
                }

                if (summary.name == dsPath.Datastore)
                {
                    ManagedObjectReference task = null;
                    TaskInfo info = null;

                    var spec = new HostDatastoreBrowserSearchSpec
                    {
                        matchPattern = new string[] { pattern },
                    };

                    var results = new List <HostDatastoreBrowserSearchResults>();

                    if (recursive)
                    {
                        try {
                            task = await _vim.SearchDatastoreSubFolders_TaskAsync(
                                dsBrowser, dsPath.FolderPath, spec
                                );

                            info = await WaitForVimTask(task);

                            if (info.result != null)
                            {
                                results.AddRange((HostDatastoreBrowserSearchResults[])info.result);
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex, "error searching datastore.");
                        }
                    }
                    else
                    {
                        task = await _vim.SearchDatastore_TaskAsync(
                            dsBrowser, dsPath.FolderPath, spec
                            );

                        info = await WaitForVimTask(task);

                        if (info.result != null)
                        {
                            results.Add((HostDatastoreBrowserSearchResults)info.result);
                        }
                    }

                    foreach (HostDatastoreBrowserSearchResults result in results)
                    {
                        if (result != null && result.file != null && result.file.Length > 0)
                        {
                            string fp = result.folderPath;
                            if (oldRoot.HasValue())
                            {
                                fp = fp.Replace(dsPath.TopLevelFolder, oldRoot);
                            }

                            if (!fp.EndsWith("/"))
                            {
                                fp += "/";
                            }

                            list.AddRange(result.file.Select(o => fp + o.path));
                        }
                    }
                }
            }
            return(list.ToArray());
        }
Esempio n. 5
0
        public async Task <string[]> GetFiles(string path, bool recursive)
        {
            await Connect();

            List <string> list   = new List <string>();
            DatastorePath dsPath = new DatastorePath(path);

            RetrievePropertiesResponse response = await _vim.RetrievePropertiesAsync(
                _props, FilterFactory.DatastoreFilter(_res));

            ObjectContent[] oc = response.returnval;

            foreach (ObjectContent obj in oc)
            {
                ManagedObjectReference dsBrowser = (ManagedObjectReference)obj.propSet[0].val;
                string dsName = ((DatastoreSummary)obj.propSet[1].val).name;
                if (dsName == dsPath.Datastore)
                {
                    ManagedObjectReference task = null;
                    TaskInfo info = null;
                    HostDatastoreBrowserSearchSpec spec = new HostDatastoreBrowserSearchSpec
                    {
                        matchPattern = new string[] { dsPath.File }
                    };
                    List <HostDatastoreBrowserSearchResults> results = new List <HostDatastoreBrowserSearchResults>();
                    if (recursive)
                    {
                        task = await _vim.SearchDatastoreSubFolders_TaskAsync(
                            dsBrowser, dsPath.FolderPath, spec);

                        info = await WaitForVimTask(task);

                        if (info.result != null)
                        {
                            results.AddRange((HostDatastoreBrowserSearchResults[])info.result);
                        }
                    }
                    else
                    {
                        task = await _vim.SearchDatastore_TaskAsync(
                            dsBrowser, dsPath.FolderPath, spec);

                        info = await WaitForVimTask(task);

                        if (info.result != null)
                        {
                            results.Add((HostDatastoreBrowserSearchResults)info.result);
                        }
                    }

                    foreach (HostDatastoreBrowserSearchResults result in results)
                    {
                        if (result != null && result.file != null && result.file.Length > 0)
                        {
                            string fp = result.folderPath;
                            if (!fp.EndsWith("/"))
                            {
                                fp += "/";
                            }

                            list.AddRange(result.file.Select(o => fp + o.path));
                        }
                    }
                }
            }
            return(list.ToArray());
        }