Example #1
0
        private static Task <Either <PowershellFailure, VMStorageSettings> > ConfigToVMStorageSettings(MachineConfig config, HostSettings hostSettings)
        {
            var projectName       = Prelude.Some("default");
            var environmentName   = Prelude.Some("default");
            var dataStoreName     = Prelude.Some("default");
            var storageIdentifier = Option <string> .None;

            var names = new StorageNames()
            {
                ProjectName     = projectName,
                EnvironmentName = environmentName,
                DataStoreName   = dataStoreName,
            };

            if (!string.IsNullOrWhiteSpace(config.VM.Slug))
            {
                storageIdentifier = Prelude.Some(config.VM.Slug);
            }

            return(ResolveStorageBasePath(names, hostSettings.DefaultDataPath).MapAsync(path => new VMStorageSettings
            {
                StorageNames = names,
                StorageIdentifier = storageIdentifier,
                VMPath = path
            }));
        }
Example #2
0
        public static (StorageNames Names, Option <string> StorageIdentifier) PathToStorageNames(string path, string defaultPath)
        {
            var projectName       = Prelude.Some("default");
            var environmentName   = Prelude.Some("default");
            var dataStoreName     = Option <string> .None;
            var dataStorePath     = Option <string> .None;
            var storageIdentifier = Option <string> .None;

            if (path.StartsWith(defaultPath, StringComparison.InvariantCultureIgnoreCase))
            {
                dataStoreName = Prelude.Some("default");
                dataStorePath = defaultPath;
            }

            if (dataStorePath.IsSome)
            {
                var pathAfterDS = path.Remove(0, dataStorePath.ValueUnsafe().Length).TrimStart('\\');

                var pathRoot = Path.GetPathRoot(pathAfterDS);
                if (pathRoot.StartsWith("haipa_p"))
                {
                    projectName = pathRoot.Remove(0, "haipa_p".Length);
                }

                var idCandidate = pathAfterDS;

                if (idCandidate.Contains(Path.DirectorySeparatorChar))
                {
                    idCandidate = Path.GetDirectoryName(pathAfterDS);
                }

                if (!string.IsNullOrWhiteSpace(idCandidate) && pathRoot != idCandidate)
                {
                    storageIdentifier = idCandidate;
                }
            }

            var names = new StorageNames
            {
                DataStoreName   = dataStoreName,
                ProjectName     = projectName,
                EnvironmentName = environmentName
            };

            return(names, storageIdentifier);
        }
Example #3
0
        private static Task <Either <PowershellFailure, string> > ResolveStorageBasePath(StorageNames names, string defaultPath)
        {
            return((
                       from dsName in names.DataStoreName.ToEitherAsync(new PowershellFailure {
                Message = "Unknown data store name. Cannot resolve path"
            })
                       from projectName in names.ProjectName.ToEitherAsync(new PowershellFailure {
                Message = "Unknown project name. Cannot resolve path"
            })
                       from environmentName in names.EnvironmentName.ToEitherAsync(new PowershellFailure {
                Message = "Unknown environment name. Cannot resolve path"
            })
                       from dsPath in LookupVMDatastorePathInEnvironment(dsName, environmentName, defaultPath).ToAsync()
                       from projectPath in JoinPathAndProject(dsPath, projectName).ToAsync()
                       select projectPath

                       ).ToEither());
        }
Example #4
0
        private static Task <Either <PowershellFailure, VMDriveStorageSettings> > DriveConfigToDriveStorageSettings(int index,
                                                                                                                    VirtualMachineDriveConfig driveConfig, VMStorageSettings storageSettings, HostSettings hostSettings)
        {
            const int controllerNumber   = 0;     //currently this will not be configurable, but keep it here at least as constant
            var       controllerLocation = index; //later, when adding controller config support, we will have to add a logic to

            //set location relative to the free slots for each controller


            //if it is not a vhd, we only need controller settings
            if (driveConfig.Type != VirtualMachineDriveType.VHD)
            {
                VMDriveStorageSettings result;
                if (driveConfig.Type == VirtualMachineDriveType.DVD)
                {
                    result = new VMDVdStorageSettings
                    {
                        ControllerNumber   = controllerNumber,
                        ControllerLocation = controllerLocation,
                        Type = VirtualMachineDriveType.DVD
                    };
                }
                else
                {
                    result = new VMDriveStorageSettings
                    {
                        ControllerNumber   = controllerNumber,
                        ControllerLocation = controllerLocation,
                        Type = driveConfig.Type.GetValueOrDefault(VirtualMachineDriveType.PHD)
                    };
                }

                return(Prelude.RightAsync <PowershellFailure, VMDriveStorageSettings>(result).ToEither());
            }

            //so far for the simple part, now the complicated case - a vhd disk...

            var projectName       = Prelude.Some("default");
            var environmentName   = Prelude.Some("default");
            var dataStoreName     = Prelude.Some("default");
            var storageIdentifier = Option <string> .None;

            var names = new StorageNames()
            {
                ProjectName     = projectName,
                EnvironmentName = environmentName,
                DataStoreName   = dataStoreName,
            };



            if (storageIdentifier.IsNone)
            {
                storageIdentifier = storageSettings.StorageIdentifier;
            }


            return
                ((from resolvedPath in ResolveStorageBasePath(names, hostSettings.DefaultVirtualHardDiskPath).ToAsync()
                  from identifier in storageIdentifier.ToEither(new PowershellFailure
            {
                Message = $"Unexpected missing storage identifier for disk '{driveConfig.Name}'."
            })
                  .ToAsync()
                  .ToEither().ToAsync()

                  let planned = new VMDiskStorageSettings
            {
                Type = driveConfig.Type.Value,
                StorageNames = names,
                StorageIdentifier = storageIdentifier,
                ParentPath = driveConfig.Template,
                Path = Path.Combine(resolvedPath, identifier),
                AttachPath = Path.Combine(Path.Combine(resolvedPath, identifier), $"{driveConfig.Name}.vhdx"),
                // ReSharper disable once StringLiteralTypo
                Name = driveConfig.Name,
                SizeBytes = driveConfig.Size.ToOption().Match(None: () => 1 * 1024L * 1024 * 1024,
                                                              Some: s => s * 1024L * 1024 * 1024),
                ControllerNumber = controllerNumber,
                ControllerLocation = controllerLocation
            }
                  select planned as VMDriveStorageSettings).ToEither());
        }