How programatically restart a Azure VM role (PaaS VM)

944 views Asked by At

I have a PaaS VM role that need to be restart using Azure Management libraries. I tried following codes but failed with "BadRequest: The operation is not supported on a role of type MyPaaSVmName". But I successfully restarted IaaS VM using below Method1. Is it possible to restart a PaaS VM role using Azure Management Libraries? if not, is there any other way to achieve it using c#.

1.

ComputeManagementClient client = new ComputeManagementClient(cloudCredentials);
client.VirtualMachines.Restart(hostedServiceName, deploymentName, vmName);

2.

ComputeManagementClient client = new ComputeManagementClient(cloudCredentials);
VirtualMachineOperationsExtensions.Restart(client.VirtualMachines, hostserviceName, deploymentName, vmName);

Thank you.

2

There are 2 answers

0
Vins007 On

Found the issue, Method1 should be like this as I am restarting a Role Instance. Method2 is wrong.

client.Deployments.RebootRoleInstanceByDeploymentName(hostserviceName, deploymentName, roleName);
1
Gustavo Lima On

Here's how you can do it using Azure Powershell:

ReSet-AzureRoleInstance -ServiceName "MySvc1" -Slot Staging -InstanceName "MyWebRole_IN_0" –reboot

https://msdn.microsoft.com/en-us/library/azure/dn495202.aspx

And here's a snippet from an Azure Automation Runbook which can reboot all cloud service's instances, per update domain (so you have no downtime):

https://gallery.technet.microsoft.com/Reboot-Cloud-Service-PaaS-b337a06d

$roleInstances = Get-AzureRole -ServiceName $cloudServiceName -Slot Production -InstanceDetails
Write-Output "Retrieved all role instances for cloud service: $cloudServiceName. Number of instances: " + $roleInstances.Count

# Group instances per update domain
$roleInstanceGroups = $roleInstances | Group-Object -AsHashTable -AsString -Property InstanceUpgradeDomain
Write-Output "Number of update domains found: " + $roleInstanceGroups.Keys.Count

# Visit each update domain
foreach ($key in $roleInstanceGroups.Keys)
{
    $count = $perDomainInstances.Count;
    Write-Output "Rebooting $count instances in domain $key"    

    $perDomainInstances = $roleInstanceGroups.Get_Item($key)

    foreach -parallel($instance in $perDomainInstances)
    {
        $instanceName = $instance.InstanceName
        Write-Output "Rebooting instance $instanceName"

        Reset-AzureRoleInstance -ServiceName $cloudServiceName -Slot Production -InstanceName $instanceName -Reboot -ErrorAction Stop
    } 
}