func TestRoleStateOperations(t *testing.T) { client := testutils.GetTestClient(t) vmname := GenerateName() sa := GetTestStorageAccount(t, client) location := sa.StorageServiceProperties.Location role := NewVMConfiguration(vmname, "Standard_D3") ConfigureDeploymentFromPlatformImage(&role, GetLinuxTestImage(t, client).Name, fmt.Sprintf("http://%s.blob.core.windows.net/sdktest/%s.vhd", sa.ServiceName, vmname), GenerateName()) ConfigureForLinux(&role, "myvm", "azureuser", GeneratePassword()) createRoleConfiguration(t, client, role, location) vmc := vm.NewClient(client) if err := Await(client, func() (management.OperationID, error) { return vmc.ShutdownRole(vmname, vmname, vmname) }); err != nil { t.Error(err) } if err := Await(client, func() (management.OperationID, error) { return vmc.StartRole(vmname, vmname, vmname) }); err != nil { t.Error(err) } if err := Await(client, func() (management.OperationID, error) { return vmc.RestartRole(vmname, vmname, vmname) }); err != nil { t.Error(err) } deleteHostedService(t, client, vmname) }
// WaitForDeploymentPowerState blocks until all role instances in deployment to // reach desired power state. func WaitForDeploymentPowerState(client management.Client, cloudServiceName, deploymentName string, desiredPowerstate vm.PowerState) error { for { deployment, err := vm.NewClient(client).GetDeployment(cloudServiceName, deploymentName) if err != nil { return err } if allInstancesInPowerState(deployment.RoleInstanceList, desiredPowerstate) { return nil } time.Sleep(2 * time.Second) } }
func createRoleConfiguration(t *testing.T, client management.Client, role vm.Role, location string) { vmc := vm.NewClient(client) hsc := hostedservice.NewClient(client) vmname := role.RoleName if err := hsc.CreateHostedService(hostedservice.CreateHostedServiceParameters{ ServiceName: vmname, Location: location, Label: base64.StdEncoding.EncodeToString([]byte(vmname))}); err != nil { t.Error(err) } if err := Await(client, func() (management.OperationID, error) { return vmc.CreateDeployment(role, vmname, vm.CreateDeploymentOptions{}) }); err != nil { t.Error(err) } }
func Example() { dnsName := "test-vm-from-go" storageAccount := "mystorageaccount" location := "West US" vmSize := "Small" vmImage := "b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-14_04-LTS-amd64-server-20140724-en-us-30GB" userName := "******" userPassword := "******" client, err := management.ClientFromPublishSettingsFile("path/to/downloaded.publishsettings", "") if err != nil { panic(err) } // create hosted service if err := hostedservice.NewClient(client).CreateHostedService(hostedservice.CreateHostedServiceParameters{ ServiceName: dnsName, Location: location, Label: base64.StdEncoding.EncodeToString([]byte(dnsName))}); err != nil { panic(err) } // create virtual machine role := NewVMConfiguration(dnsName, vmSize) ConfigureDeploymentFromPlatformImage( &role, vmImage, fmt.Sprintf("http://%s.blob.core.windows.net/sdktest/%s.vhd", storageAccount, dnsName), "") ConfigureForLinux(&role, dnsName, userName, userPassword) ConfigureWithPublicSSH(&role) operationID, err := virtualmachine.NewClient(client). CreateDeployment(role, dnsName, virtualmachine.CreateDeploymentOptions{}) if err != nil { panic(err) } err = client.WaitForOperation(operationID, nil) if err != nil { panic(err) } }
func TestDeployPlatformCaptureRedeploy(t *testing.T) { client := testutils.GetTestClient(t) vmname := GenerateName() sa := GetTestStorageAccount(t, client) location := sa.StorageServiceProperties.Location role := NewVMConfiguration(vmname, "Standard_D3") ConfigureDeploymentFromPlatformImage(&role, GetLinuxTestImage(t, client).Name, fmt.Sprintf("http://%s.blob.core.windows.net/sdktest/%s.vhd", sa.ServiceName, vmname), GenerateName()) ConfigureForLinux(&role, "myvm", "azureuser", GeneratePassword()) ConfigureWithPublicSSH(&role) t.Logf("Deploying VM: %s", vmname) createRoleConfiguration(t, client, role, location) t.Logf("Wait for deployment to enter running state") vmc := vm.NewClient(client) status := vm.DeploymentStatusDeploying for status != vm.DeploymentStatusRunning { deployment, err := vmc.GetDeployment(vmname, vmname) if err != nil { t.Error(err) break } status = deployment.Status } t.Logf("Shutting down VM: %s", vmname) if err := Await(client, func() (management.OperationID, error) { return vmc.ShutdownRole(vmname, vmname, vmname) }); err != nil { t.Error(err) } if err := WaitForDeploymentPowerState(client, vmname, vmname, vm.PowerStateStopped); err != nil { t.Fatal(err) } imagename := GenerateName() t.Logf("Capturing VMImage: %s", imagename) if err := Await(client, func() (management.OperationID, error) { return vmc.CaptureRole(vmname, vmname, vmname, imagename, imagename, nil) }); err != nil { t.Error(err) } im := GetUserImage(t, client, imagename) t.Logf("Found image: %+v", im) newvmname := GenerateName() role = NewVMConfiguration(newvmname, "Standard_D3") ConfigureDeploymentFromPlatformImage(&role, im.Name, fmt.Sprintf("http://%s.blob.core.windows.net/sdktest/%s.vhd", sa.ServiceName, newvmname), GenerateName()) ConfigureForLinux(&role, newvmname, "azureuser", GeneratePassword()) ConfigureWithPublicSSH(&role) t.Logf("Deploying new VM from freshly captured VM image: %s", newvmname) if err := Await(client, func() (management.OperationID, error) { return vmc.CreateDeployment(role, vmname, vm.CreateDeploymentOptions{}) }); err != nil { t.Error(err) } deleteHostedService(t, client, vmname) }