Exemplo n.º 1
0
        public async Task StartStopVirtualMachineAsync(string virtualMachineName, string cloudServiceName, VirtualMachineAction action)
        {
            using (var computeClient = new ComputeManagementClient(_credentials))
            {
                HostedServiceGetDetailedResponse vm;
                try
                {
                    vm = await computeClient.HostedServices.GetDetailedAsync(cloudServiceName);

                    //  Console.WriteLine("Found cloud service: " + cloudServiceName);

                    Console.WriteLine(string.Format("Found cloud service: {0}", cloudServiceName));
                }
                catch (Exception)
                {
                    Console.WriteLine(string.Format("Virtual Machine for [{0}] cloud was not found!", cloudServiceName));
                    return;
                }

                Console.WriteLine(string.Format("Fetching deployment for virtual machine [{0}].", virtualMachineName));
                var deployment = vm.Deployments.ToList().First(x => x.Name == virtualMachineName);
                //var deployment = vm.Deployments.ToList().First(x => x.Name == cloudServiceName);

                if (deployment == null)
                {
                    Console.Write(string.Format("Failed to fetch deployment for virtual machine [{0}] Start/Stop will exit and do nothing",
                                                virtualMachineName));

                    return;
                }
                var deploymantSlotName = deployment.Name;
                var serviceName        = vm.ServiceName;

                Console.WriteLine("Fetching instance.");
                // GSUHackfest Note #1 by Brent - April 30th
                // the line that has been commented out worked for Gabriele's tests with a machine he
                // provisioned via SCAMP. But it didn't work with VMs deployed via the Azure portal.
                // we'll need to revist this later to try and reconcile the differences.
                //var instance = deployment.RoleInstances.First(x => x.HostName == virtualMachineName);
                var instance = deployment.RoleInstances.First(x => x.RoleName == virtualMachineName);

                Console.WriteLine(string.Format("Machine Name[{0}] is currently at [{1}] state",
                                                virtualMachineName,
                                                instance.InstanceStatus));

                Console.WriteLine(string.Format("Machine Name[{0}] is currently at [{1}] state (if not at ReadyRole or StoppedVM the following start/stop will fail)",
                                                virtualMachineName,
                                                instance.InstanceStatus));

                if (action == VirtualMachineAction.Start)
                {
                    if (instance.InstanceStatus == "ReadyRole")
                    {
                        Console.WriteLine(string.Format("VM  [{0}] Deploymentslot[{1}] roleName [{2}] already started (no start will be execute)", serviceName, deploymantSlotName, instance.RoleName));

                        return;
                    }

                    Console.WriteLine(string.Format("Issuing Management Start cmd Service[{0}] Deploymentslot[{1}] roleName [{2}]", serviceName, deploymantSlotName, instance.RoleName));
                    //TODO this is strange but for now i leave it a is. Need to be refactored.
                    // refer to "GSUHackfest Note #1" above
                    //await computeClient.VirtualMachines.StartAsync(serviceName, deploymantSlotName, instance.HostName);

                    Console.WriteLine(string.Format("Machine Name[{0}] Starting..",
                                                    virtualMachineName));

                    await computeClient.VirtualMachines.StartAsync(serviceName, deploymantSlotName, instance.RoleName);

                    Console.WriteLine(string.Format("Machine Name[{0}] start command issued..",
                                                    virtualMachineName));
                }
                else
                {
                    if (instance.InstanceStatus == "StoppedVM" || instance.InstanceStatus == "StoppedDeallocated")
                    {
                        Console.WriteLine(string.Format("VM  [{0}] Deploymentslot[{1}] roleName [{2}] already stopped (no stop will be execute)", serviceName, deploymantSlotName, instance.RoleName));
                        return;
                    }

                    // ensures no compute charges for the stopped VM
                    VirtualMachineShutdownParameters shutdownParms = new VirtualMachineShutdownParameters();
                    shutdownParms.PostShutdownAction = PostShutdownAction.StoppedDeallocated;

                    // refer to "GSUHackfest Note #1" above
                    //computeClient.VirtualMachines.Shutdown(serviceName, deploymantSlotName, instance.HostName, shutdownParms);
                    // computeClient.VirtualMachines.Shutdown(serviceName, deploymantSlotName, instance.RoleName, shutdownParms);
                    Console.WriteLine(string.Format("Machine Name[{0}] Stopping..",
                                                    virtualMachineName));

                    await computeClient.VirtualMachines.ShutdownAsync(serviceName, deploymantSlotName, instance.RoleName, shutdownParms);

                    Console.WriteLine(string.Format("Machine Name[{0}] stop command issued..",
                                                    virtualMachineName));
                }
            }
        }
Exemplo n.º 2
0
        public async Task StartStopVirtualMachineAsync(string virtualMachineName, string cloudServiceName, VirtualMachineAction action)
        {
            using (var computeClient = new ComputeManagementClient(_credentials))
            {
                HostedServiceGetDetailedResponse vm;
                try
                {
                    vm = await computeClient.HostedServices.GetDetailedAsync(cloudServiceName);
                    //  Console.WriteLine("Found cloud service: " + cloudServiceName);

                    Console.WriteLine(string.Format("Found cloud service: {0}", cloudServiceName));
                }
                catch (Exception)
                {
                    Console.WriteLine(string.Format("Virtual Machine for [{0}] cloud was not found!", cloudServiceName));
                    return;
                }

                Console.WriteLine(string.Format("Fetching deployment for virtual machine [{0}].", virtualMachineName));
                var deployment = vm.Deployments.ToList().First(x => x.Name == virtualMachineName);
                //var deployment = vm.Deployments.ToList().First(x => x.Name == cloudServiceName);

                if (deployment == null)
                {
                    Console.Write(string.Format("Failed to fetch deployment for virtual machine [{0}] Start/Stop will exit and do nothing", 
                        virtualMachineName));

                    return;
                }
                    var deploymantSlotName = deployment.Name;
                    var serviceName = vm.ServiceName;

                    Console.WriteLine("Fetching instance.");
                    // GSUHackfest Note #1 by Brent - April 30th
                    // the line that has been commented out worked for Gabriele's tests with a machine he
                    // provisioned via SCAMP. But it didn't work with VMs deployed via the Azure portal. 
                    // we'll need to revist this later to try and reconcile the differences. 
                    //var instance = deployment.RoleInstances.First(x => x.HostName == virtualMachineName);
                    var instance = deployment.RoleInstances.First(x => x.RoleName == virtualMachineName);

                    Console.WriteLine(string.Format("Machine Name[{0}] is currently at [{1}] state", 
                                                    virtualMachineName,
                                                    instance.InstanceStatus));

                    Console.WriteLine(string.Format("Machine Name[{0}] is currently at [{1}] state (if not at ReadyRole or StoppedVM the following start/stop will fail)",
                                                virtualMachineName,
                                                instance.InstanceStatus));

                if (action == VirtualMachineAction.Start)
                    {
                        if (instance.InstanceStatus == "ReadyRole")
                        {
                            Console.WriteLine(string.Format("VM  [{0}] Deploymentslot[{1}] roleName [{2}] already started (no start will be execute)", serviceName, deploymantSlotName, instance.RoleName));

                        return;
                        }

                        Console.WriteLine(string.Format("Issuing Management Start cmd Service[{0}] Deploymentslot[{1}] roleName [{2}]", serviceName, deploymantSlotName, instance.RoleName));
                    //TODO this is strange but for now i leave it a is. Need to be refactored.
                    // refer to "GSUHackfest Note #1" above
                    //await computeClient.VirtualMachines.StartAsync(serviceName, deploymantSlotName, instance.HostName);

                    Console.WriteLine(string.Format("Machine Name[{0}] Starting..",
                                                virtualMachineName));

                    await computeClient.VirtualMachines.StartAsync(serviceName, deploymantSlotName, instance.RoleName);

                    Console.WriteLine(string.Format("Machine Name[{0}] start command issued..",
                                          virtualMachineName));

                    }
                    else
                    {
                        if (instance.InstanceStatus == "StoppedVM" || instance.InstanceStatus == "StoppedDeallocated")
                        {
                            Console.WriteLine(string.Format("VM  [{0}] Deploymentslot[{1}] roleName [{2}] already stopped (no stop will be execute)", serviceName, deploymantSlotName, instance.RoleName));
                            return;
                        }

                        // ensures no compute charges for the stopped VM
                        VirtualMachineShutdownParameters shutdownParms = new VirtualMachineShutdownParameters();
                        shutdownParms.PostShutdownAction = PostShutdownAction.StoppedDeallocated;

                    // refer to "GSUHackfest Note #1" above
                    //computeClient.VirtualMachines.Shutdown(serviceName, deploymantSlotName, instance.HostName, shutdownParms);
                    // computeClient.VirtualMachines.Shutdown(serviceName, deploymantSlotName, instance.RoleName, shutdownParms);
                    Console.WriteLine(string.Format("Machine Name[{0}] Stopping..",
                            virtualMachineName));

                    await computeClient.VirtualMachines.ShutdownAsync(serviceName, deploymantSlotName, instance.RoleName, shutdownParms);
                    Console.WriteLine(string.Format("Machine Name[{0}] stop command issued..",
                      virtualMachineName));

                }
            }
            
        }