예제 #1
0
파일: Converge.cs 프로젝트: phoenixyj/haipa
        public static async Task Definition(
            IPowershellEngine engine,
            TypedPsObject <VirtualMachineInfo> vmInfo,
            MachineConfig vmConfig,
            Func <string, Task> reportProgress)
        {
            if (vmInfo.Value.Generation >= 2)
            {
                await engine.GetObjectsAsync <VMFirmwareInfo>(PsCommandBuilder.Create()
                                                              .AddCommand("Get-VMFirmware")
                                                              .AddParameter("VM", vmInfo.PsObject)).MapAsync(async(firmwareInfo) =>
                {
                    if (firmwareInfo.Head.Value.SecureBoot != OnOffState.Off)
                    {
                        await reportProgress($"Configure VM Firmware - Secure Boot: {OnOffState.Off}")
                        .ConfigureAwait(false);

                        await engine.RunAsync(PsCommandBuilder.Create()
                                              .AddCommand("Set-VMFirmware")
                                              .AddParameter("VM", vmInfo.PsObject)
                                              .AddParameter("EnableSecureBoot", OnOffState.Off)).ConfigureAwait(false);
                    }
                }).ConfigureAwait(false);
            }


            if (vmInfo.Value.ProcessorCount != vmConfig.VM.Cpu.Count)
            {
                await reportProgress($"Configure VM Processor: Count: {vmConfig.VM.Cpu.Count}").ConfigureAwait(false);

                await engine.RunAsync(PsCommandBuilder.Create()
                                      .AddCommand("Set-VMProcessor")
                                      .AddParameter("VM", vmInfo.PsObject)
                                      .AddParameter("Count", vmConfig.VM.Cpu.Count)).ConfigureAwait(false);
            }

            var memoryStartupBytes = vmConfig.VM.Memory.Startup * 1024L * 1024;

            if (vmInfo.Value.MemoryStartup != memoryStartupBytes)
            {
                await reportProgress($"Configure VM Memory: Startup: {vmConfig.VM.Memory.Startup} MB").ConfigureAwait(false);

                await engine.RunAsync(PsCommandBuilder.Create()
                                      .AddCommand("Set-VMMemory")
                                      .AddParameter("VM", vmInfo.PsObject)
                                      .AddParameter("StartupBytes", memoryStartupBytes)).ConfigureAwait(false);
            }
        }
예제 #2
0
파일: Converge.cs 프로젝트: phoenixyj/haipa
 public static Task <Either <PowershellFailure, Unit> > SetVMCheckpointType(TypedPsObject <VirtualMachineInfo> vmInfo, CheckpointType checkpointType, IPowershellEngine engine)
 {
     return(engine.RunAsync(new PsCommandBuilder()
                            .AddCommand("Set-VM")
                            .AddParameter("VM", vmInfo.PsObject)
                            .AddParameter("CheckpointType", checkpointType)));
 }
예제 #3
0
 private static Task <Either <PowershellFailure, Unit> > InsertConfigDriveDisk(
     string configDriveIsoPath,
     TypedPsObject <VirtualMachineInfo> vmInfo,
     IPowershellEngine engine)
 {
     return(vmInfo.GetList(l => l.DVDDrives, drive => string.IsNullOrWhiteSpace(drive.Path))
            .HeadOrNone().MatchAsync(
                None: () => engine.RunAsync(
                    PsCommandBuilder.Create().AddCommand("Add-VMDvdDrive")
                    .AddParameter("VM", vmInfo.PsObject).AddParameter("Path", configDriveIsoPath)),
                Some: dvdDriveInfo => engine.RunAsync(PsCommandBuilder.Create()
                                                      .AddCommand("Set-VMDvdDrive")
                                                      .AddParameter("VMDvdDrive", dvdDriveInfo.PsObject)
                                                      .AddParameter("Path", configDriveIsoPath)
                                                      )));
 }
예제 #4
0
        public static async Task <Either <PowershellFailure, TypedPsObject <VirtualMachineInfo> > > Disk(
            VirtualMachineDiskConfig diskConfig,
            IPowershellEngine engine,
            TypedPsObject <VirtualMachineInfo> vmInfo,
            MachineConfig vmConfig,
            Func <string, Task> reportProgress)
        {
            if (!File.Exists(diskConfig.Path))
            {
                await reportProgress($"Create VHD: {diskConfig.Name}").ConfigureAwait(false);

                await engine.RunAsync(PsCommandBuilder.Create().Script(
                                          $"New-VHD -Path \"{diskConfig.Path}\" -ParentPath \"{diskConfig.Template}\" -Differencing"),
                                      reportProgress : p => ReportPowershellProgress($"Create VHD {diskConfig.Name}", p, reportProgress)).ConfigureAwait(false);
            }

            await GetOrCreateInfoAsync(vmInfo,
                                       i => i.HardDrives,
                                       disk => diskConfig.Path.Equals(disk.Path, StringComparison.OrdinalIgnoreCase),
                                       async() =>
            {
                await reportProgress($"Add VHD: {diskConfig.Name}").ConfigureAwait(false);
                return(await engine.GetObjectsAsync <HardDiskDriveInfo>(PsCommandBuilder.Create()
                                                                        .AddCommand("Add-VMHardDiskDrive")
                                                                        .AddParameter("VM", vmInfo.PsObject)
                                                                        .AddParameter("Path", diskConfig.Path)).ConfigureAwait(false));
            }).ConfigureAwait(false);

            return(vmInfo.Recreate());
        }
예제 #5
0
        public static async Task <Either <PowershellFailure, TypedPsObject <VirtualMachineInfo> > > Firmware(TypedPsObject <VirtualMachineInfo> vmInfo,
                                                                                                             MachineConfig config, IPowershellEngine engine, Func <string, Task> reportProgress)
        {
            if (vmInfo.Value.Generation < 2)
            {
                return(vmInfo);
            }

            return(await engine.GetObjectsAsync <VMFirmwareInfo>(PsCommandBuilder.Create()
                                                                 .AddCommand("Get-VMFirmware")
                                                                 .AddParameter("VM", vmInfo.PsObject))
                   .BindAsync(firmwareInfoSeq =>
                              firmwareInfoSeq.HeadOrNone()
                              .Match <Either <PowershellFailure, TypedPsObject <VMFirmwareInfo> > >(
                                  None: () => new PowershellFailure {
                Message = "Failed to get VM Firmware"
            },
                                  Some: s => s
                                  ))
                   .BindAsync(async firmwareInfo =>
            {
                if (firmwareInfo.Value.SecureBoot != OnOffState.Off)
                {
                    await reportProgress($"Configure VM Firmware - Secure Boot: {OnOffState.Off}")
                    .ConfigureAwait(false);


                    var res = await engine.RunAsync(PsCommandBuilder.Create()
                                                    .AddCommand("Set-VMFirmware")
                                                    .AddParameter("VM", vmInfo.PsObject)
                                                    .AddParameter("EnableSecureBoot", OnOffState.Off)).MapAsync(
                        r => new TypedPsObject <VirtualMachineInfo>(vmInfo.PsObject)
                        ).ConfigureAwait(false);
                    return res;
                }

                return new TypedPsObject <VirtualMachineInfo>(vmInfo.PsObject);
            }
                              ).ConfigureAwait(false));
        }
예제 #6
0
파일: Converge.cs 프로젝트: phoenixyj/haipa
        public static async Task <Either <PowershellFailure, TypedPsObject <VirtualMachineInfo> > > Cpu(TypedPsObject <VirtualMachineInfo> vmInfo,
                                                                                                        Option <VirtualMachineCpuConfig> cpuConfig, IPowershellEngine engine, Func <string, Task> reportProgress)
        {
            return(await cpuConfig.MatchAsync(
                       None : () => vmInfo,
                       Some : async config =>
            {
                if (vmInfo.Value.ProcessorCount != config.Count)
                {
                    await reportProgress($"Configure VM Processor: Count: {config.Count}").ConfigureAwait(false);

                    await engine.RunAsync(PsCommandBuilder.Create()
                                          .AddCommand("Set-VMProcessor")
                                          .AddParameter("VM", vmInfo.PsObject)
                                          .AddParameter("Count", config.Count)).ConfigureAwait(false);

                    return await vmInfo.RecreateOrReload(engine).ConfigureAwait(false);
                }

                return vmInfo;
            }).ConfigureAwait(false));
        }
예제 #7
0
파일: Converge.cs 프로젝트: phoenixyj/haipa
        public static Task <Either <PowershellFailure, Unit> > DetachUndefinedHardDrives(
            IPowershellEngine engine,
            TypedPsObject <VirtualMachineInfo> vmInfo,
            Seq <VMDriveStorageSettings> plannedStorageSettings,
            Seq <CurrentVMDiskStorageSettings> currentDiskStorageSettings,
            Func <string, Task> reportProgress)

        {
            var planedDiskSettings = plannedStorageSettings.Where(x =>
                                                                  x.Type == VirtualMachineDriveType.VHD || x.Type == VirtualMachineDriveType.SharedVHD)
                                     .Cast <VMDiskStorageSettings>().ToSeq();

            var attachedPaths = planedDiskSettings.Map(s => s.AttachPath).Map(x => x.IfNone(""))
                                .Where(x => !string.IsNullOrWhiteSpace(x));

            var frozenDiskIds = currentDiskStorageSettings.Where(x => x.Frozen).Map(x => x.AttachedVMId);

            return(FindAndApply(vmInfo,
                                i => i.HardDrives,
                                hd =>
            {
                var plannedDiskAtControllerPos = planedDiskSettings
                                                 .FirstOrDefault(x =>
                                                                 x.ControllerLocation == hd.ControllerLocation && x.ControllerNumber == hd.ControllerNumber);

                var detach = plannedDiskAtControllerPos == null;

                if (!detach && plannedDiskAtControllerPos.AttachPath.IsSome)
                {
                    var plannedAttachPath = plannedDiskAtControllerPos.AttachPath.IfNone("");
                    if (hd.Path == null || !hd.Path.Equals(plannedAttachPath, StringComparison.InvariantCultureIgnoreCase))
                    {
                        detach = true;
                    }
                }

                if (detach && frozenDiskIds.Contains(hd.Id))
                {
                    reportProgress(hd.Path != null
                                ? $"Skipping detach of frozen disk {Path.GetFileNameWithoutExtension(hd.Path)}"
                                : $"Skipping detach of unknown frozen disk at controller {hd.ControllerNumber}, Location: {hd.ControllerLocation}");

                    return false;
                }

                if (detach)
                {
                    reportProgress(hd.Path != null
                                ? $"Detaching disk {Path.GetFileNameWithoutExtension(hd.Path)} from controller: {hd.ControllerNumber}, Location: {hd.ControllerLocation}"
                                : $"Detaching unknown disk at controller: {hd.ControllerNumber}, Location: {hd.ControllerLocation}");
                }

                return detach;
            },
                                i => engine.RunAsync(PsCommandBuilder.Create().AddCommand("Remove-VMHardDiskDrive")
                                                     .AddParameter("VMHardDiskDrive", i.PsObject))).Map(x => x.Lefts().HeadOrNone())
                   .MatchAsync(
                       Some: l => LeftAsync <PowershellFailure, Unit>(l).ToEither(),
                       None: () => RightAsync <PowershellFailure, Unit>(Unit.Default)
                       .ToEither()));
        }