Ejemplo n.º 1
0
        public async Task <int> CloneDisk(string templateId, string source, string dest)
        {
            ManagedObjectReference task = null;
            TaskInfo info    = null;
            string   pattern = @"blank-(\d+)([^\.]+)";

            await Connect();

            _taskMap.Add(templateId, null);

            await MakeDirectories(dest);

            Match match = Regex.Match(source, pattern);

            if (match.Success)
            {
                //create virtual disk
                int size = 0;
                Int32.TryParse(match.Groups[1].Value, out size);
                string[] parts   = match.Groups[2].Value.Split('-');
                string   adapter = (parts.Length > 1) ? parts[1] : "lsiLogic";
                FileBackedVirtualDiskSpec spec = new FileBackedVirtualDiskSpec
                {
                    diskType    = "thin",
                    adapterType = adapter.Replace("lsilogic", "lsiLogic").Replace("buslogic", "busLogic"),
                    capacityKb  = size * 1024 * 1024
                };
                _logger.LogDebug("creating new blank disk " + dest);
                task = await _vim.CreateVirtualDisk_TaskAsync(
                    _vdm, dest, _datacenter, spec);
            }
            else
            {
                //copy virtual disk
                _logger.LogDebug("cloning new disk " + source + " -> " + dest);
                task = await _vim.CopyVirtualDisk_TaskAsync(
                    _vdm, source, _datacenter, dest, _datacenter, null, false);
            }

            // sometimes returns blank info, so wait a sec to prevent race
            await Task.Delay(1000);

            info = await GetVimTaskInfo(task);

            _taskMap[templateId] = info;

            _logger.LogDebug($"TaskProgress: {templateId} {info.state} {info.progress}% cloned");

            return(info.progress);
        }