Exemplo n.º 1
0
        protected override void DoExecute(ITaskContext context)
        {
            if (string.IsNullOrEmpty(hostName))
            {
                throw new TaskExecutionException("Host name can not be empty!");
            }
            if (string.IsNullOrEmpty(machineName))
            {
                throw new TaskExecutionException("Machine name can not be empty!");
            }
            if (string.IsNullOrEmpty(machineLocation))
            {
                throw new TaskExecutionException("Machine location can not be empty!");
            }
            using (HyperVManager manager = new HyperVManager())
            {
                manager.Connect(hostName);

                if (string.IsNullOrEmpty(diskPath))
                {
                    diskPath = Path.Combine(machineLocation, machineName + ".vhd");
                    IVirtualTask disk = manager.CreateDynamicDisk(diskPath, 50);
                    disk.WaitForCompletion(new TimeSpan(0, 0, 0, 10));
                }

                IVirtualTask t = manager.CreateVirtualMachine(
                    machineName, machineLocation, diskPath, networkAdapterName, mac, memorySize);
                t.WaitForCompletion(new TimeSpan(0, 0, 1, 0));
            }
        }
Exemplo n.º 2
0
        protected override void DoExecute(ITaskContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (string.IsNullOrEmpty(hostName))
            {
                throw new TaskExecutionException("Host name can not be empty!");
            }
            if (string.IsNullOrEmpty(machineName))
            {
                throw new TaskExecutionException("Machine name can not be empty!");
            }
            try
            {
                using (HyperVManager manager = new HyperVManager())
                {
                    manager.Connect(hostName);

                    IVirtualTask t = manager.DeleteVirtualMachine(machineName);

                    t.WaitForCompletion(new TimeSpan(0, 0, 0, 10));
                }
            }
            catch (Exception e)
            {
                if (failIfNotExists)
                {
                    throw new TaskExecutionException("Error deleting virtual machine", e);
                }

                context.WriteDebug("Virtual machine does not exist and was not deleted!");
            }
        }
Exemplo n.º 3
0
        protected override void DoExecute(ITaskContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (string.IsNullOrEmpty(hostName))
            {
                throw new TaskExecutionException("Host name can not be empty!");
            }
            if (string.IsNullOrEmpty(machineName))
            {
                throw new TaskExecutionException("Machine name can not be empty!");
            }
            using (HyperVManager manager = new HyperVManager())
            {
                manager.Connect(hostName);

                IVirtualTask t = manager.StartVirtualMachine(machineName);

                context.WriteDebug("Waiting for machine to start...");
                t.WaitForCompletion(new TimeSpan(0, 0, 2, 0));
            }
        }
Exemplo n.º 4
0
        protected override void DoExecute(ITaskContext context)
        {
            if (string.IsNullOrEmpty(machineName))
            {
                throw new TaskExecutionException("Machine name must be specified!");
            }
            if (string.IsNullOrEmpty(machineLocation))
            {
                throw new TaskExecutionException("Machine location must be specified!");
            }
            if (string.IsNullOrEmpty(baseMachineName))
            {
                throw new TaskExecutionException("Base machine name must be specified!");
            }

            using (HyperVManager manager = new HyperVManager())
            {
                manager.Connect(hostName);

                IVirtualTask t = manager.CloneVirtualMachine(
                    machineName, machineLocation, baseMachineName, diskPath, networkAdapterName, mac);
                t.WaitForCompletion(new TimeSpan(0, 0, 1, 0));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///   Creates new virtual machine with configuration from existing virtual machine.
        /// </summary>
        /// <param name = "machineName">Name of the virtual machine to crate.</param>
        /// <param name = "machineFolder">Full path where virtual machine will be created.</param>
        /// <param name = "baseMachineName">Name of the base virtual machine name.</param>
        /// <param name = "diskPath">Full path and name of base disk image to use.</param>
        /// <param name = "networkAdapterName">Name of the network adapter to use.</param>
        /// <param name = "macAddress">MAC address</param>
        /// <returns>The task</returns>
        public IVirtualTask CloneVirtualMachine(
            string machineName,
            string machineFolder,
            string baseMachineName,
            string diskPath,
            string networkAdapterName,
            string macAddress)
        {
            if (string.IsNullOrEmpty(machineName))
            {
                throw new ArgumentNullException("machineName");
            }
            if (string.IsNullOrEmpty(baseMachineName))
            {
                throw new ArgumentNullException("baseMachineName");
            }

            using (ManagementObject newVm = FindVirtualMachine(machineName))
            {
                if (newVm != null)
                {
                    throw new ArgumentOutOfRangeException("machineName", machineName, "Virtual machine already exists.");
                }
            }

            using (ManagementObject baseVm = FindVirtualMachine(baseMachineName))
            {
                if (baseVm == null)
                {
                    throw new ArgumentOutOfRangeException("baseMachineName", baseMachineName, "Base virtual machine does not exist.");
                }
            }

            using (ManagementObject virtualSystemService = Utility.GetServiceObject(managementScope, "Msvm_VirtualSystemManagementService"))
                using (ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("DefineVirtualSystem"))
                {
                    inParams["ResourcesettingData"] = null;
                    inParams["Sourcesetting"]       = null;
                    inParams["SystemsettingData"]   = GetVirtualSystemGlobalSettingDataInstance(managementScope, machineName);

                    using (ManagementBaseObject outParams = virtualSystemService.InvokeMethod("DefineVirtualSystem", inParams, null))
                    {
                        if (outParams == null)
                        {
                            throw new ArgumentException("Could not execute creation of new virtual machine!");
                        }

                        if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
                        {
                            NewTask(outParams, managementScope);
                        }

                        NewTask((UInt32)outParams["ReturnValue"]);
                    }
                }

            ManagementObject vm = Utility.GetTargetComputer(machineName, managementScope);

            string       newDisk = Path.Combine(machineFolder, machineName + ".vhd");
            IVirtualTask task    = CreateDifferencingDisk(newDisk, diskPath);

            task.WaitForCompletion(new TimeSpan(0, 0, 0, 10));
            AddVirtualHarddrive(managementScope, vm, newDisk);
            ConnectSwitchPort connect = new ConnectSwitchPort(serverName);

            connect.Connect(
                networkAdapterName,
                networkAdapterName + "_ExternalPort",
                machineName,
                "synthetic",
                baseMachineName);

            return(currentTask);
        }