Exemplo n.º 1
0
        private async Task AddHost(string url)
        {
            string hostname = new Uri(url).Host;

            _logger.LogDebug($"Adding host {hostname}");

            if (_hostCache.ContainsKey(hostname))
            {
                await _hostCache[hostname].Disconnect();
                _hostCache.TryRemove(hostname, out VimClient discard);
                await Task.Delay(100);
            }

            HypervisorServiceConfiguration hostOptions = _options.Clone <HypervisorServiceConfiguration>();

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

            hostOptions.Url  = url;
            hostOptions.Host = hostname;
            var vHost = new VimClient(
                hostOptions,
                _vmCache,
                _vlanman,
                _mill.CreateLogger <VimClient>()
                );

            _hostCache.AddOrUpdate(hostname, vHost, (k, v) => (v = vHost));
            _logger.LogDebug($"Added host {hostname}; cache: {_hostCache.Values.Count}");
        }
Exemplo n.º 2
0
        private void NormalizeTemplate(VmTemplate template, HypervisorServiceConfiguration option)
        {
            if (template.Iso.HasValue() && !template.Iso.StartsWith(option.IsoStore))
            {
                template.Iso = option.IsoStore + template.Iso + ".iso";
            }

            // if (template.Source.HasValue() && !template.Source.StartsWith(option.StockStore))
            // {
            //     template.Source = option.StockStore + template.Source + ".vmdk";
            // }

            foreach (VmDisk disk in template.Disks)
            {
                if (!disk.Path.StartsWith(option.DiskStore))
                {
                    disk.Path = option.DiskStore + disk.Path + ".vmdk";
                }
            }

            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)
                {
                    eth.Net = rgx.Replace(eth.Net, "") + tag;
                }
            }
        }
Exemplo n.º 3
0
        private void NormalizeTemplate(VmTemplate template, HypervisorServiceConfiguration option, bool privileged = false)
        {
            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)
                {
                    if (privileged && _vlanman.Contains(eth.Net))
                    {
                        continue;
                    }

                    eth.Net = rgx.Replace(eth.Net, "") + tag;
                }
            }
        }
Exemplo n.º 4
0
 public MockHypervisorService(
     HypervisorServiceConfiguration podConfiguration,
     ILoggerFactory mill
     )
 {
     _optPod = podConfiguration;
     _mill   = mill;
     _logger = _mill.CreateLogger <MockHypervisorService>();
     _vms    = new Dictionary <string, Vm>();
     _tasks  = new Dictionary <string, VmTask>();
     _rand   = new Random();
 }
Exemplo n.º 5
0
 public HypervisorService(
     HypervisorServiceConfiguration options,
     ILoggerFactory mill
     )
 {
     _options     = options;
     _mill        = mill;
     _logger      = _mill.CreateLogger <HypervisorService>();
     _hostCache   = new ConcurrentDictionary <string, VimClient>();
     _affinityMap = new Dictionary <string, VimClient>();
     _vmCache     = new ConcurrentDictionary <string, Vm>();
     _vlanman     = new VlanManager(_options.Vlan);
 }
Exemplo n.º 6
0
 public VimClient(
     HypervisorServiceConfiguration options,
     ConcurrentDictionary <string, Vm> vmCache,
     VlanManager networkManager,
     ILogger <VimClient> logger
     )
 {
     _logger = logger;
     _config = options;
     _logger.LogDebug($"Constructing Client { _config.Host }");
     _tasks        = new Dictionary <string, VimHostTask>();
     _vmCache      = vmCache;
     _pgAllocation = new Dictionary <string, PortGroupAllocation>();
     _vlanman      = networkManager;
     _hostPrefix   = _config.Host.Split('.').FirstOrDefault();
     Task sessionMonitorTask = MonitorSession();
     Task taskMonitorTask    = MonitorTasks();
 }
Exemplo n.º 7
0
        private void NormalizeOptions(HypervisorServiceConfiguration options)
        {
            var regex = new Regex("(]|/)$");

            if (!regex.IsMatch(options.VmStore))
            {
                options.VmStore += "/";
            }

            if (!regex.IsMatch(options.DiskStore))
            {
                options.DiskStore += "/";
            }

            if (!regex.IsMatch(options.IsoStore))
            {
                options.IsoStore += "/";
            }
        }