0
votes

This is only an issue when I create a new deployment. Deploying AzureVms to an existing deployment (or existing cloud service) works correctly. However, I can only create new deployments properly using powershell and not c#.

The Powershell that does work:

$imageName = (Get-AzureVMImage | Where-Object { $_.Label -eq "Windows 7 Enterprise SP1 (x64)" } | Sort-Object PublishedDate -Descending | Select-Object -First 1).ImageName
$vm = New-AzureVMConfig -Name "MyName01" -InstanceSize "Small" -Image $imageName
$vm | Add-AzureProvisioningConfig -TimeZone "Eastern Standard Time" -Windows -AdminUsername "Username01" -Password "Password01"
$vm | Set-AzureSubnet "MySubnet" | Out-Null
$operation = New-AzureVM -ServiceName "MyCloudService" -Location "Central US" -VMs $vm -VNetName "MyVnet"
if($operation.OperationStatus -ne "Succeeded") {throw "Operation Status = " + $operation.OperationStatus}

When I do this, the VM is created correctly. I'll define correctly here with three criteria on top of the fact that it is provisioned and I can RDP in: - The VM is returned by the cmdlet Get-AzureVM - The DNS Name within the Azure portal > Virtual Machines node (not preview portal) is mycloudservice.cloudapp.net - The deployment name is called mycloudservice

I have tried many iterations of the c#, but as an example of one that doesn't work if the cloud service doesn't exist yet. I've tried both supplying and not supplying a DNSSetting value. I'm defining doesn't work as: - The VM is NOT returned by the cmdlet Get-AzureVM - The DNS Name within the Azure portal (not preview portal) is some random alphanumeric string + '.cloudapp.net' - The deployment name is the same random alphanumeric string as the DNS host

using (var computeclient = new ComputeManagementClient(credentials))
{
//create cloud service if it doesn't exist
if(computeclient.HostedServices.Get(cloudServiceName) == null)
computeclient.HostedServices.Create(
new HostedServiceCreateParameters
{
    Label = cloudServiceName,
    Location = "Central US",
    ServiceName = cloudServiceName
});

//define parameters specific to the vm (role)
var deploymentAttributes = new Role
{
    RoleName = hostname,
    RoleSize = createParameters.VmSize,
    RoleType = VirtualMachineRoleType.PersistentVMRole.ToString(),
    DataVirtualHardDisks = dataHardDrives,
    ConfigurationSets = configurationSets,
    ProvisionGuestAgent = true,
    Label = cloudServiceName,
    VMImageName = createParameters
};


//create deployment parameters and kick off!
DnsServer dnsServer = new DnsServer();
dnsServer.Address = dnsIp;
dnsServer.Name = dnsHostname;
DnsSettings dnsSettings = new DnsSettings();
dnsSettings.DnsServers = new List<DnsServer>() { dnsServer };
var createDeploymentParameters = new     VirtualMachineCreateDeploymentParameters
{
    Name = cloudServiceName,
    Label = cloudServiceName,
    DeploymentSlot = DeploymentSlot.Staging,
    Roles = new List<Role> { deploymentAttributes },
    VirtualNetworkName = vnetName,
    DnsSettings = dnsSettings,
};
var deploymentResult = computeclient.VirtualMachines.CreateDeployment(
    cloudServiceName,
    createDeploymentParameters);
}
1

1 Answers

0
votes

Just figured this out - posting this question gave me an idea. Simply deploying to production instead of staging fixes this (DeploymentSlot = DeploymentSlot.Production).