private static void Enable() { ExecutePSCmdlet executeEnableWinRmCmd = new ExecutePSCmdlet(); string enableWinRmCmd = "Set-WSManQuickConfig -Force"; executeEnableWinRmCmd.Execute(Resources.EnablingWinRM, enableWinRmCmd); if (executeEnableWinRmCmd.ErrorOccurred == true) { throw new ApplicationFailedException(Resources.ErrorEnablingWinRM); } }
private static void Configure() { string[] configureCmds = new string[3] { "Set-WSManInstance -ComputerName \"localhost\" -resourceuri \"winrm/config/service/auth\" -valueset @{Basic=\"true\"} ", "Set-WSManInstance -ComputerName \"localhost\" -resourceuri \"winrm/config/client\" -valueset @{AllowUnencrypted=\"true\"} ", "Set-WSManInstance -ComputerName \"localhost\" -resourceuri \"winrm/config/client\" -valueset @{TrustedHosts=\"*\"} " }; string[] messages = new string[3] { Resources.ConfigWinRMAuth, Resources.ConfigWinRMEnc, Resources.ConfigWinRMTrustedHost }; for (int i = 0; i < 3; i++) { ExecutePSCmdlet executeWinRmConfigCmd = new ExecutePSCmdlet(); executeWinRmConfigCmd.Execute(messages[i], configureCmds[i]); if (executeWinRmConfigCmd.ErrorOccurred == true) { throw new ApplicationFailedException(Resources.ErrorConfigWinRM); } } }
private void WaitTillReady() { string getAzureRoleCmd = string.Format(CultureInfo.InvariantCulture, "Get-AzureRole -ServiceName \"{0}\" -Slot Production -InstanceDetails", Service); while (true) { System.Threading.Thread.Sleep(5000); ExecutePSCmdlet executeGetAzureRoleStatus = new ExecutePSCmdlet(); executeGetAzureRoleStatus.Execute("Fetching the VM status.", getAzureRoleCmd); if (executeGetAzureRoleStatus.ErrorOccurred == true) { throw new ApplicationFailedException("Error fetching VM status."); } bool areAllReady = true; foreach (PSObject eachResult in executeGetAzureRoleStatus.OutputData) { string vmstatus = eachResult.Properties["InstanceStatus"].Value.ToString(); string vmName = eachResult.Properties["InstanceName"].Value.ToString(); if (vmstatus != "ReadyRole") { areAllReady = false; } if (vmstatus == "ProvisioningFailed") { throw new ApplicationFailedException(string.Format(CultureInfo.InvariantCulture, "Failed to initialize VM {0}.", vmName)); } WriteObject(string.Format(CultureInfo.InvariantCulture, "Status of VM {0} is {1}", vmName, vmstatus)); } if (areAllReady == true) { break; } } }
private bool ServiceExists() { string command = string.Format("@(Get-AzureService | % {{ $_ }} | WHERE-Object {{$_.ServiceName -eq \"{0}\" }}).Count", Service.Trim()); ExecutePSCmdlet executeGetServiceCmd = new ExecutePSCmdlet(); executeGetServiceCmd.Execute(string.Format(CultureInfo.InvariantCulture, Resources.VerifyIfServiceExist, Service), command); if (executeGetServiceCmd.OutputData == null || executeGetServiceCmd.OutputData.Count() == 0) { return false; } return int.Parse(executeGetServiceCmd.OutputData.ElementAt(0).ToString()) == 1; }
private string GetWin2KImageName() { // get all images and look for the desired pattern in family name ExecutePSCmdlet executeGetAzureVMImageCmd = new ExecutePSCmdlet(); string getAzureVMImageCmd = "Get-AzureVMImage"; executeGetAzureVMImageCmd.Execute(Resources.ExecutingGetAzureVMImage, getAzureVMImageCmd); if (executeGetAzureVMImageCmd.ErrorOccurred == true) { throw new ApplicationFailedException(Resources.GetAzureVMImageFailed); } if (executeGetAzureVMImageCmd.OutputData != null) { foreach (PSObject eachImageDetails in executeGetAzureVMImageCmd.OutputData) { PSPropertyInfo imageFamilyProperty = eachImageDetails.Properties["ImageFamily"]; PSPropertyInfo imageLocationProperty = eachImageDetails.Properties["Location"]; if ((imageFamilyProperty != null) && (imageFamilyProperty.Value != null) && imageFamilyProperty.Value.ToString().Contains(Resources.Win2KR8SP1FamilyName) && //Check if location is not null (imageLocationProperty != null) && (imageLocationProperty.Value != null) && //Check if image is present at the location where deployment is being created. (imageLocationProperty.Value.ToString().ToLowerInvariant().Contains(this.Location.ToLowerInvariant()) == true) && (eachImageDetails.Properties["ImageName"] != null) && (eachImageDetails.Properties["ImageName"].Value != null)) { return eachImageDetails.Properties["ImageName"].Value.ToString(); } } } throw new ApplicationFailedException(string.Format(Resources.VMImageNotFoundFmt, Resources.Win2KR8SP1FamilyName)); }
private string GetOSDiskForVM(string vm) { string getAzureOSDiskCmd = string.Format(CultureInfo.InvariantCulture, "Get-AzureVM -ServiceName \"{0}\" -Name \"{1}\" | Get-AzureOSDisk ", Service, vm); ExecutePSCmdlet executeGetAzureOSDiskCmd = new ExecutePSCmdlet(); executeGetAzureOSDiskCmd.Execute(string.Format(CultureInfo.InvariantCulture, Resources.FetchigOSDiskForVM, vm), getAzureOSDiskCmd); if (executeGetAzureOSDiskCmd.ErrorOccurred == true) { throw new ApplicationFailedException(string.Format(CultureInfo.InvariantCulture, Resources.ErrorFetchingOSDiskForVM, vm)); } string osDiskName = executeGetAzureOSDiskCmd.OutputData.ElementAt(0).Properties["DiskName"].Value.ToString(); WriteObject(string.Format(CultureInfo.InvariantCulture, Resources.OSDiskNameForVM, vm, osDiskName)); return osDiskName; }
private List<string> GetExistingVMs() { string getAzureVMCmd = string.Format(CultureInfo.InvariantCulture, "Get-AzureVM -ServiceName \"{0}\"", Service); ExecutePSCmdlet executeGetAzureVMCmd = new ExecutePSCmdlet(); executeGetAzureVMCmd.Execute(string.Format(CultureInfo.InvariantCulture, Resources.FetchingExistingVMs, Service), getAzureVMCmd); List<string> vms = new List<string>(); if (executeGetAzureVMCmd.ErrorOccurred == true) { return vms; } if (executeGetAzureVMCmd.OutputData != null) { foreach (PSObject eachVMDetails in executeGetAzureVMCmd.OutputData) { PSPropertyInfo nameProperty = eachVMDetails.Properties["Name"]; if (nameProperty != null) { vms.Add(eachVMDetails.Properties["Name"].Value.ToString().ToUpperInvariant()); } else { //We don't have name property in the received output object...so just print the output. eachVMDetails.ToString(); } } } return vms; }
private void DeleteVM(string vm) { int currentRetryCount = 1; int waitPeriodInSeconds = _waitPeriod / 1000; while (true) { string removeVMCmd = string.Format(CultureInfo.InvariantCulture, "Remove-AzureVM -ServiceName \"{0}\" -Name \"{1}\" ", Service, vm); ExecutePSCmdlet executeRemoveVMCmd = new ExecutePSCmdlet(); executeRemoveVMCmd.Execute(string.Format(CultureInfo.InvariantCulture, Resources.DeletingVM, vm), removeVMCmd); if (executeRemoveVMCmd.ErrorOccurred == true) { if (currentRetryCount == _retryCount) { throw new ApplicationFailedException(string.Format(CultureInfo.InvariantCulture, Resources.ErrorDeletingVM, vm)); } else { WriteWarning(string.Format(CultureInfo.InvariantCulture, Resources.WarnAzureVMDeleteError, vm, waitPeriodInSeconds)); System.Threading.Thread.Sleep(_waitPeriod); //Wait before retry. } } else { WriteObject(string.Format(CultureInfo.InvariantCulture, Resources.VMDeletionSuccess, vm)); break; } currentRetryCount++; } }
private void DeleteOSDisk(string disk) { int currentRetryCount = 1; int waitPeriodInSeconds = _waitPeriodDiskDelete / 1000; while (true) { string removeAzureDiskCmd = string.Format(CultureInfo.InvariantCulture, "Remove-AzureDisk -DiskName \"{0}\" -DeleteVHD", disk); ExecutePSCmdlet executeRemoveAzureDiskCmd = new ExecutePSCmdlet(); executeRemoveAzureDiskCmd.Execute(string.Format(CultureInfo.InvariantCulture, Resources.DeletingAzureDisk, disk), removeAzureDiskCmd); if (executeRemoveAzureDiskCmd.ErrorOccurred == true) { if (currentRetryCount == _retryCount) { WriteWarning(string.Format(CultureInfo.InvariantCulture, Resources.ErrorDeletingAzureDisk, disk)); break; } else { WriteWarning(string.Format(CultureInfo.InvariantCulture, Resources.WarnErrorDeletingAzureDisk, disk, waitPeriodInSeconds)); System.Threading.Thread.Sleep(_waitPeriodDiskDelete); //Wait before retry. } } else { WriteObject(string.Format(CultureInfo.InvariantCulture, Resources.AzureDiskDeleted, disk)); break; } currentRetryCount++; } }
private void CreateVMs() { int i = 1; int waitPeriodInSeconds = _waitPeriod / 1000; string createVMCmdTemplate = "New-AzureVMConfig -Name \"{0}\" -InstanceSize {1} -ImageName \"{2}\" | Add-AzureProvisioningConfig -Windows -Password {3} -AdminUserName \"{4}\" -EnableWinRMHttp | New-AzureVM –ServiceName \"{5}\""; List<string> existingVMs = GetExistingVMs(); for (int j = 0; j < Count; j++) { string vmname = string.Concat(VMNamePrefix, "-", i++); ValidateVMName(vmname); //If VM with same name already exists and we are not supposed to delete it..Then give message and continue. if (existingVMs.Contains(vmname.ToUpperInvariant()) == true) { if (_force == false) { WriteObject(string.Format(CultureInfo.InvariantCulture, Resources.VMAlreadyExists, vmname)); continue; } else { DeleteVMWithOSDisk(vmname); } } // get vm image name if none was specified if (string.IsNullOrEmpty(ImageName)) ImageName = GetWin2KImageName(); int currentRetryCount = 1; string createVMCmd = string.Format(CultureInfo.InvariantCulture, createVMCmdTemplate, new string[] { vmname, InstanceSize, ImageName, AdminPassword, AdminUsername, Service }); while (true) { ExecutePSCmdlet executeCreateAzureVMCmd = new ExecutePSCmdlet(); executeCreateAzureVMCmd.Execute(string.Format(CultureInfo.InvariantCulture, "Creating VM {0}", vmname), createVMCmd); if (executeCreateAzureVMCmd.ErrorOccurred == true) { if (currentRetryCount == _retryCount) { throw new ApplicationFailedException(string.Format(CultureInfo.InvariantCulture, "Attempt to create VM {0} failed.", vmname)); } else { WriteWarning(string.Format(CultureInfo.InvariantCulture, Resources.WarnAzureVMCreationFailed, vmname, waitPeriodInSeconds)); System.Threading.Thread.Sleep(waitPeriodInSeconds); } } else { WriteObject(string.Format(CultureInfo.InvariantCulture, "VM {0} created.", vmname)); break; } currentRetryCount++; } } }
private void CreateService() { string createServiceCmd = string.Format(CultureInfo.InvariantCulture, "New-AzureService -ServiceName \"{0}\" -Location \"{1}\" ", Service, Location); ExecutePSCmdlet executeCreateServiceCmd = new ExecutePSCmdlet(); executeCreateServiceCmd.Execute(string.Format(CultureInfo.InvariantCulture, Resources.CreatingService, Service), createServiceCmd); if (executeCreateServiceCmd.ErrorOccurred == true) { throw new ApplicationFailedException(string.Format(CultureInfo.InvariantCulture, Resources.ErrorCreatingService, Service)); } }
private string GetIPAddress(string vmName) { string getAzureVMCmd = string.Format(CultureInfo.InvariantCulture, "Get-AzureVM -ServiceName \"{0}\" -Name \"{1}\" ", Service, vmName); string message = string.Format(CultureInfo.InvariantCulture, "Fetching details for VM {0}", vmName); ExecutePSCmdlet executeGetAzureVMCmd = new ExecutePSCmdlet(); executeGetAzureVMCmd.Execute(message, getAzureVMCmd); if (executeGetAzureVMCmd.ErrorOccurred == true) { throw new ApplicationFailedException(string.Format(CultureInfo.InvariantCulture, "Error fetching details for VM '{0}'", vmName)); } return executeGetAzureVMCmd.OutputData.ElementAt(0).Properties["IpAddress"].Value.ToString(); }
private string GetPort(string vmName) { string getAzureEndPointCmd = string.Format(CultureInfo.InvariantCulture, "Get-AzureVM -ServiceName \"{0}\" -Name \"{1}\" | Get-AzureEndPoint", Service, vmName); string message = string.Format(CultureInfo.InvariantCulture, "Fetching Ports for VM '{0}'", vmName); ExecutePSCmdlet executeGetAzureEndPointsCmd = new ExecutePSCmdlet(); executeGetAzureEndPointsCmd.Execute(message, getAzureEndPointCmd); if (executeGetAzureEndPointsCmd.ErrorOccurred == true) { throw new ApplicationFailedException(string.Format(CultureInfo.InvariantCulture, "Error fetching Ports for VM '{0}'", vmName)); } string port = string.Empty; foreach (PSObject eachEndPoint in executeGetAzureEndPointsCmd.OutputData) { string endPointName = eachEndPoint.Properties["Name"].Value.ToString(); if (endPointName.Contains(EndpointPrefix) == true) { port = eachEndPoint.Properties["Port"].Value.ToString(); break; } } return port; }
private void AddEndPointsToVMs(string[] vmNames) { string endpointNameTemplate = EndpointName + "-{0}-{1}"; //DEBUG ENABLED string cmdTemplate = "Get-AzureVM -ServiceName \"{0}\" -Name \"{1}\" | Add-AzureEndpoint -Name \"{2}\" -Protocol \"{3}\" -PublicPort {4} -LocalPort {5} | Update-AzureVM -verbose -debug"; string cmdTemplate = "Get-AzureVM -ServiceName \"{0}\" -Name \"{1}\" | Add-AzureEndpoint -Name \"{2}\" -Protocol \"{3}\" -PublicPort {4} -LocalPort {5} | Update-AzureVM -verbose"; string messageTemplate = "Adding endpoint mapping [{0}, {1}] for the VM '{2}'"; string failedMessageTemplate = "Failed to add endpoint VM '{0}"; string successMessageTemplate = "Endpoint mapping [{0}, {1}] added for the VM '{2}"; for (int i = 0; i < vmNames.Length; i++) { int publicPort = PublicPort + i; string[] cmdParams = new string[] { Service, vmNames[i], string.Format(CultureInfo.InvariantCulture, endpointNameTemplate, PrivatePort, publicPort), Protocol, publicPort.ToString(CultureInfo.InvariantCulture), PrivatePort.ToString(CultureInfo.InvariantCulture) }; string cmd = string.Format(CultureInfo.InvariantCulture, cmdTemplate, cmdParams); ExecutePSCmdlet executeAddAzureEndPointCmd = new ExecutePSCmdlet(); string message = string.Format(CultureInfo.InvariantCulture, messageTemplate, publicPort, PrivatePort, vmNames[i]); executeAddAzureEndPointCmd.Execute(message, cmd); if (executeAddAzureEndPointCmd.ErrorOccurred == true) { throw new ApplicationFailedException(string.Format(CultureInfo.InvariantCulture, failedMessageTemplate, vmNames[i])); } WriteObject(string.Format(CultureInfo.InvariantCulture, successMessageTemplate, publicPort, PrivatePort, vmNames[i])); } }
private void SetAccountAsDefault() { ExecutePSCmdlet executeSetAzureSubscriptionCmd = new ExecutePSCmdlet(); string enableWinRmCmd = "Set-AzureSubscription -CurrentStorageAccount \"{0}\" -SubscriptionName \"{1}\" "; executeSetAzureSubscriptionCmd.Execute(string.Format(CultureInfo.InvariantCulture, Resources.SetDefaultStorageAcc, StorageAccount, Subscription), string.Format(CultureInfo.InvariantCulture, enableWinRmCmd, StorageAccount, Subscription)); if (executeSetAzureSubscriptionCmd.ErrorOccurred == true) { throw new ApplicationFailedException(Resources.ErrorSettingDefaultStorageAcc); } }