func (*StepValidate) Run(state multistep.StateBag) multistep.StepAction { client := state.Get(constants.RequestManager).(management.Client) ui := state.Get(constants.Ui).(packer.Ui) config := state.Get(constants.Config).(*Config) ui.Say("Validating Azure options...") role := vmutils.NewVMConfiguration(config.tmpVmName, config.InstanceSize) ui.Message("Checking Storage Account...") destinationVhd, err := validateStorageAccount(config, client) if err != nil { err = fmt.Errorf("Error checking storage account: %v", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } ui.Message(fmt.Sprintf("Destination VHD: %s", destinationVhd)) if err := func() error { if config.RemoteSourceImageLink != "" { ui.Message("Checking remote image source link...") response, err := http.DefaultClient.Head(config.RemoteSourceImageLink) if response != nil && response.Body != nil { defer response.Body.Close() } if err != nil { log.Printf("HTTP client returned error: %s", err) return fmt.Errorf("error checking remote image source link: %v", err) } if response.StatusCode != 200 { return fmt.Errorf("Unexpected status while retrieving remote image source at %s: %d %s", config.RemoteSourceImageLink, response.StatusCode, response.Status) } size := float64(response.ContentLength) / 1024 / 1024 / 1024 ui.Say(fmt.Sprintf("Remote image size: %.1f GiB", size)) vmutils.ConfigureDeploymentFromRemoteImage(&role, config.RemoteSourceImageLink, config.OSType, fmt.Sprintf("%s-OSDisk", config.tmpVmName), destinationVhd, "") if config.ResizeOSVhdGB != nil { if float64(*config.ResizeOSVhdGB) < size { return fmt.Errorf("new OS VHD size of %d GiB is smaller than current size of %.1f GiB", *config.ResizeOSVhdGB, size) } ui.Say(fmt.Sprintf("Remote image will be resized to %d GiB", *config.ResizeOSVhdGB)) role.OSVirtualHardDisk.ResizedSizeInGB = *config.ResizeOSVhdGB } } else { ui.Message("Checking image source...") imageList, err := osimage.NewClient(client).ListOSImages() if err != nil { log.Printf("OS image client returned error: %s", err) return err } if osImage, found := FindOSImage(imageList.OSImages, config.OSImageLabel, config.Location); found { vmutils.ConfigureDeploymentFromPlatformImage(&role, osImage.Name, destinationVhd, "") ui.Message(fmt.Sprintf("Image source is OS image %q", osImage.Name)) if osImage.OS != config.OSType { return fmt.Errorf("OS image type (%q) does not match config (%q)", osImage.OS, config.OSType) } if config.ResizeOSVhdGB != nil { if float64(*config.ResizeOSVhdGB) < osImage.LogicalSizeInGB { return fmt.Errorf("new OS VHD size of %d GiB is smaller than current size of %.1f GiB", *config.ResizeOSVhdGB, osImage.LogicalSizeInGB) } ui.Say(fmt.Sprintf("OS image will be resized to %d GiB", *config.ResizeOSVhdGB)) role.OSVirtualHardDisk.ResizedSizeInGB = *config.ResizeOSVhdGB } } else { imageList, err := vmimage.NewClient(client).ListVirtualMachineImages() if err != nil { log.Printf("VM image client returned error: %s", err) return err } if vmImage, found := FindVmImage(imageList.VMImages, "", config.OSImageLabel, config.Location); found { if config.ResizeOSVhdGB != nil { return fmt.Errorf("VM images cannot be resized") } vmutils.ConfigureDeploymentFromVMImage(&role, vmImage.Name, destinationVhd, true) ui.Message(fmt.Sprintf("Image source is VM image %q", vmImage.Name)) if vmImage.OSDiskConfiguration.OS != config.OSType { return fmt.Errorf("VM image type (%q) does not match config (%q)", vmImage.OSDiskConfiguration.OS, config.OSType) } } else { return fmt.Errorf("Can't find VM or OS image '%s' Located at '%s'", config.OSImageLabel, config.Location) } } } return nil }(); err != nil { err = fmt.Errorf("Error determining deployment source: %v", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } if config.OSType == constants.Target_Linux { certThumbprint := state.Get(constants.Thumbprint).(string) if len(certThumbprint) == 0 { err := fmt.Errorf("Certificate Thumbprint is empty") state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } vmutils.ConfigureForLinux(&role, config.tmpVmName, config.UserName, "", certThumbprint) vmutils.ConfigureWithPublicSSH(&role) } else if config.OSType == constants.Target_Windows { password := utils.RandomPassword() state.Put("password", password) vmutils.ConfigureForWindows(&role, config.tmpVmName, config.UserName, password, true, "") vmutils.ConfigureWithPublicRDP(&role) vmutils.ConfigureWithPublicPowerShell(&role) } if config.VNet != "" && config.Subnet != "" { ui.Message("Checking VNet...") if err := checkVirtualNetworkConfiguration(client, config.VNet, config.Subnet, config.Location); err != nil { state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } vmutils.ConfigureWithSubnet(&role, config.Subnet) } for n, d := range config.DataDisks { switch d := d.(type) { case int: ui.Message(fmt.Sprintf("Configuring datadisk %d: new disk with size %d GB...", n, d)) destination := fmt.Sprintf("%s-data-%d.vhd", destinationVhd[:len(destinationVhd)-4], n) ui.Message(fmt.Sprintf("Destination VHD for data disk %d: %s", destinationVhd, n)) vmutils.ConfigureWithNewDataDisk(&role, "", destination, d, vmdisk.HostCachingTypeNone) case string: ui.Message(fmt.Sprintf("Configuring datadisk %d: existing blob (%s)...", n, d)) vmutils.ConfigureWithVhdDataDisk(&role, d, vmdisk.HostCachingTypeNone) default: err := fmt.Errorf("Datadisk %d is not a string nor a number", n) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } } state.Put("role", &role) return multistep.ActionContinue }
func (*StepValidate) Run(state multistep.StateBag) multistep.StepAction { client := state.Get(constants.RequestManager).(management.Client) ui := state.Get(constants.Ui).(packer.Ui) config := state.Get(constants.Config).(*Config) ui.Say("Validating Azure options...") role := vmutils.NewVMConfiguration(config.tmpVmName, config.InstanceSize) ui.Message("Checking Storage Account...") destinationVhd, err := validateStorageAccount(config, client) if err != nil { err = fmt.Errorf("Error checking storage account: %v", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } ui.Message(fmt.Sprintf("Destination VHD: %s", destinationVhd)) if err := func() error { if config.RemoteSourceImageLink != "" { ui.Message("Checking remote image source link...") response, err := http.DefaultClient.Head(config.RemoteSourceImageLink) if response != nil && response.Body != nil { defer response.Body.Close() } if err != nil { log.Printf("HTTP client returned error: %s", err) return fmt.Errorf("error checking remote image source link: %v", err) } if response.StatusCode != 200 { return fmt.Errorf("Unexpected status while retrieving remote image source at %s: %d %s", config.RemoteSourceImageLink, response.StatusCode, response.Status) } size := float64(response.ContentLength) / 1024 / 1024 / 1024 ui.Say(fmt.Sprintf("Remote image size: %.1f GiB", size)) vmutils.ConfigureDeploymentFromRemoteImage(&role, config.RemoteSourceImageLink, config.OSType, fmt.Sprintf("%s-OSDisk", config.tmpVmName), destinationVhd, "") } else { ui.Message("Checking image source...") imageList, err := osimage.NewClient(client).ListOSImages() if err != nil { log.Printf("OS image client returned error: %s", err) return err } if osImage, found := FindOSImage(imageList.OSImages, config.OSImageLabel, config.Location); found { vmutils.ConfigureDeploymentFromPlatformImage(&role, osImage.Name, destinationVhd, "") ui.Message(fmt.Sprintf("Image source is OS image %q", osImage.Name)) if osImage.OS != config.OSType { return fmt.Errorf("OS image type (%q) does not match config (%q)", osImage.OS, config.OSType) } } else { imageList, err := vmimage.NewClient(client).ListVirtualMachineImages() if err != nil { log.Printf("VM image client returned error: %s", err) return err } if vmImage, found := FindVmImage(imageList.VMImages, "", config.OSImageLabel, config.Location); found { vmutils.ConfigureDeploymentFromVMImage(&role, vmImage.Name, destinationVhd, true) ui.Message(fmt.Sprintf("Image source is VM image %q", vmImage.Name)) if vmImage.OSDiskConfiguration.OS != config.OSType { return fmt.Errorf("VM image type (%q) does not match config (%q)", vmImage.OSDiskConfiguration.OS, config.OSType) } } else { return fmt.Errorf("Can't find VM or OS image '%s' Located at '%s'", config.OSImageLabel, config.Location) } } } return nil }(); err != nil { err = fmt.Errorf("Error determining deployment source: %v", err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } if config.OSType == constants.Target_Linux { certThumbprint := state.Get(constants.Thumbprint).(string) if len(certThumbprint) == 0 { err := fmt.Errorf("Certificate Thumbprint is empty") state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } vmutils.ConfigureForLinux(&role, config.tmpVmName, config.UserName, "", certThumbprint) vmutils.ConfigureWithPublicSSH(&role) } else if config.OSType == constants.Target_Windows { password := utils.RandomPassword() state.Put("password", password) vmutils.ConfigureForWindows(&role, config.tmpVmName, config.UserName, password, true, "") vmutils.ConfigureWithPublicRDP(&role) vmutils.ConfigureWithPublicPowerShell(&role) } if config.VNet != "" && config.Subnet != "" { ui.Message("Checking VNet...") if err := checkVirtualNetworkConfiguration(client, config.VNet, config.Subnet, config.Location); err != nil { state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } vmutils.ConfigureWithSubnet(&role, config.Subnet) } state.Put("role", &role) return multistep.ActionContinue }
// Run executes a Packer build and returns a packer.Artifact representing // a Azure VM image. func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { var err error ui.Say("Preparing builder...") ui.Message("Creating Azure Service Management client...") subscriptionID, err := findSubscriptionID(b.config.PublishSettingsPath, b.config.SubscriptionName) if err != nil { return nil, fmt.Errorf("Error creating new Azure client: %v", err) } b.client, err = management.ClientFromPublishSettingsFile(b.config.PublishSettingsPath, subscriptionID) if err != nil { return nil, fmt.Errorf("Error creating new Azure client: %v", err) } // add logger if appropriate b.client = getLoggedClient(b.client) // Set up the state. state := new(multistep.BasicStateBag) state.Put(constants.Config, &b.config) state.Put(constants.RequestManager, b.client) state.Put("hook", hook) state.Put(constants.Ui, ui) // complete flags state.Put(constants.SrvExists, 0) state.Put(constants.CertInstalled, 0) state.Put(constants.CertUploaded, 0) state.Put(constants.VmExists, 0) state.Put(constants.DiskExists, 0) state.Put(constants.VmRunning, 0) state.Put(constants.ImageCreated, 0) ui.Say("Validating Azure Options...") err = b.validateAzureOptions(ui, state) if err != nil { return nil, fmt.Errorf("Some Azure options failed: %s", err) } var steps []multistep.Step if b.config.OSType == targets.Linux { steps = []multistep.Step{ &lin.StepCreateCert{ TmpServiceName: b.config.tmpServiceName, }, &targets.StepCreateService{ Location: b.config.Location, TmpServiceName: b.config.tmpServiceName, }, &targets.StepUploadCertificate{ TmpServiceName: b.config.tmpServiceName, }, &lin.StepCreateVm{ StorageAccount: b.config.StorageAccount, StorageContainer: b.config.StorageContainer, TmpVmName: b.config.tmpVmName, TmpServiceName: b.config.tmpServiceName, InstanceSize: b.config.InstanceSize, Username: b.config.userName, }, &targets.StepPollStatus{ TmpServiceName: b.config.tmpServiceName, TmpVmName: b.config.tmpVmName, OSType: b.config.OSType, }, &communicator.StepConnectSSH{ Config: &b.config.Comm, Host: lin.SSHHost, SSHConfig: lin.SSHConfig(b.config.userName), }, &common.StepProvision{}, &lin.StepGeneralizeOS{ Command: "sudo /usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync", }, &targets.StepStopVm{ TmpVmName: b.config.tmpVmName, TmpServiceName: b.config.tmpServiceName, }, &targets.StepCreateImage{ TmpServiceName: b.config.tmpServiceName, TmpVmName: b.config.tmpVmName, UserImageName: b.config.userImageName, UserImageLabel: b.config.UserImageLabel, RecommendedVMSize: b.config.InstanceSize, }, } } else if b.config.OSType == targets.Windows { // b.config.tmpVmName = "shchTemp" // b.config.tmpServiceName = "shchTemp" steps = []multistep.Step{ &targets.StepCreateService{ Location: b.config.Location, TmpServiceName: b.config.tmpServiceName, }, &win.StepCreateVm{ StorageAccount: b.config.StorageAccount, StorageContainer: b.config.StorageContainer, TmpVmName: b.config.tmpVmName, TmpServiceName: b.config.tmpServiceName, InstanceSize: b.config.InstanceSize, Username: b.config.userName, Password: utils.RandomPassword(), }, &targets.StepPollStatus{ TmpServiceName: b.config.tmpServiceName, TmpVmName: b.config.tmpVmName, OSType: b.config.OSType, }, &win.StepSetProvisionInfrastructure{ VmName: b.config.tmpVmName, ServiceName: b.config.tmpServiceName, StorageAccountName: b.config.StorageAccount, TempContainerName: b.config.tmpContainerName, }, &common.StepProvision{}, &targets.StepStopVm{ TmpVmName: b.config.tmpVmName, TmpServiceName: b.config.tmpServiceName, }, &targets.StepCreateImage{ TmpServiceName: b.config.tmpServiceName, TmpVmName: b.config.tmpVmName, UserImageName: b.config.userImageName, UserImageLabel: b.config.UserImageLabel, RecommendedVMSize: b.config.InstanceSize, }, } } else { return nil, fmt.Errorf("Unkonwn OS type: %s", b.config.OSType) } // Run the steps. if b.config.PackerDebug { b.runner = &multistep.DebugRunner{ Steps: steps, PauseFn: common.MultistepDebugFn(ui), } } else { b.runner = &multistep.BasicRunner{Steps: steps} } b.runner.Run(state) // Report any errors. if rawErr, ok := state.GetOk("error"); ok { return nil, rawErr.(error) } // If we were interrupted or cancelled, then just exit. if _, ok := state.GetOk(multistep.StateCancelled); ok { return nil, errors.New("Build was cancelled.") } if _, ok := state.GetOk(multistep.StateHalted); ok { return nil, errors.New("Build was halted.") } vmImageList, err := vmimage.NewClient(b.client).ListVirtualMachineImages() if err != nil { log.Printf("VM image client returned error: %s", err) return nil, fmt.Errorf("Can't create artifact") } if userImage, found := FindVmImage(vmImageList.VMImages, b.config.userImageName, b.config.UserImageLabel, b.config.Location); found { return &artifact{ imageLabel: userImage.Label, imageName: userImage.Name, mediaLocation: userImage.OSDiskConfiguration.MediaLink, }, nil } else { log.Printf("could not find image %s", b.config.userImageName) return nil, fmt.Errorf("Can't create artifact") } }