builder/openstack: convert interpolation

This commit is contained in:
Mitchell Hashimoto 2015-05-27 13:02:57 -07:00
parent 31bdb4853c
commit 3b29fa5e40
5 changed files with 24 additions and 100 deletions

View File

@ -3,14 +3,14 @@ package openstack
import ( import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings" "strings"
"github.com/mitchellh/gophercloud-fork-40444fb" "github.com/mitchellh/gophercloud-fork-40444fb"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/template/interpolate"
) )
// AccessConfig is for common configuration related to openstack access // AccessConfig is for common configuration related to openstack access
@ -86,36 +86,8 @@ func (c *AccessConfig) Region() string {
return common.ChooseString(c.RawRegion, os.Getenv("SDK_REGION"), os.Getenv("OS_REGION_NAME")) return common.ChooseString(c.RawRegion, os.Getenv("SDK_REGION"), os.Getenv("OS_REGION_NAME"))
} }
func (c *AccessConfig) Prepare(t *packer.ConfigTemplate) []error { func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
if t == nil {
var err error
t, err = packer.NewConfigTemplate()
if err != nil {
return []error{err}
}
}
templates := map[string]*string{
"username": &c.Username,
"password": &c.Password,
"api_key": &c.ApiKey,
"provider": &c.Provider,
"project": &c.Project,
"tenant_id": &c.TenantId,
"region": &c.RawRegion,
"proxy_url": &c.ProxyUrl,
}
errs := make([]error, 0) errs := make([]error, 0)
for n, ptr := range templates {
var err error
*ptr, err = t.Process(*ptr, nil)
if err != nil {
errs = append(
errs, fmt.Errorf("Error processing %s: %s", n, err))
}
}
if strings.HasPrefix(c.Provider, "rackspace") { if strings.HasPrefix(c.Provider, "rackspace") {
if c.Region() == "" { if c.Region() == "" {
errs = append(errs, fmt.Errorf("region must be specified when using rackspace")) errs = append(errs, fmt.Errorf("region must be specified when using rackspace"))

View File

@ -7,46 +7,44 @@ import (
"fmt" "fmt"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common" "github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"log" "log"
"github.com/mitchellh/gophercloud-fork-40444fb" "github.com/mitchellh/gophercloud-fork-40444fb"
"github.com/mitchellh/packer/helper/config"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/template/interpolate"
) )
// The unique ID for this builder // The unique ID for this builder
const BuilderId = "mitchellh.openstack" const BuilderId = "mitchellh.openstack"
type config struct { type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
AccessConfig `mapstructure:",squash"` AccessConfig `mapstructure:",squash"`
ImageConfig `mapstructure:",squash"` ImageConfig `mapstructure:",squash"`
RunConfig `mapstructure:",squash"` RunConfig `mapstructure:",squash"`
tpl *packer.ConfigTemplate ctx interpolate.Context
} }
type Builder struct { type Builder struct {
config config config Config
runner multistep.Runner runner multistep.Runner
} }
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
md, err := common.DecodeConfig(&b.config, raws...) err := config.Decode(&b.config, &config.DecodeOpts{
Interpolate: true,
}, raws...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
b.config.tpl, err = packer.NewConfigTemplate()
if err != nil {
return nil, err
}
b.config.tpl.UserVars = b.config.PackerUserVars
// Accumulate any errors // Accumulate any errors
errs := common.CheckUnusedConfig(md) var errs *packer.MultiError
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(b.config.tpl)...) errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.ImageConfig.Prepare(b.config.tpl)...) errs = packer.MultiErrorAppend(errs, b.config.ImageConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(b.config.tpl)...) errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
if errs != nil && len(errs.Errors) > 0 { if errs != nil && len(errs.Errors) > 0 {
return nil, errs return nil, errs
@ -96,7 +94,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Networks: b.config.Networks, Networks: b.config.Networks,
}, },
&StepWaitForRackConnect{ &StepWaitForRackConnect{
Wait: b.config.RackconnectWait, Wait: b.config.RackconnectWait,
}, },
&StepAllocateIp{ &StepAllocateIp{
FloatingIpPool: b.config.FloatingIpPool, FloatingIpPool: b.config.FloatingIpPool,

View File

@ -2,7 +2,8 @@ package openstack
import ( import (
"fmt" "fmt"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/template/interpolate"
) )
// ImageConfig is for common configuration related to creating Images. // ImageConfig is for common configuration related to creating Images.
@ -10,29 +11,8 @@ type ImageConfig struct {
ImageName string `mapstructure:"image_name"` ImageName string `mapstructure:"image_name"`
} }
func (c *ImageConfig) Prepare(t *packer.ConfigTemplate) []error { func (c *ImageConfig) Prepare(ctx *interpolate.Context) []error {
if t == nil {
var err error
t, err = packer.NewConfigTemplate()
if err != nil {
return []error{err}
}
}
templates := map[string]*string{
"image_name": &c.ImageName,
}
errs := make([]error, 0) errs := make([]error, 0)
for n, ptr := range templates {
var err error
*ptr, err = t.Process(*ptr, nil)
if err != nil {
errs = append(
errs, fmt.Errorf("Error processing %s: %s", n, err))
}
}
if c.ImageName == "" { if c.ImageName == "" {
errs = append(errs, fmt.Errorf("An image_name must be specified")) errs = append(errs, fmt.Errorf("An image_name must be specified"))
} }

View File

@ -3,8 +3,9 @@ package openstack
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/packer/packer"
"time" "time"
"github.com/mitchellh/packer/template/interpolate"
) )
// RunConfig contains configuration for running an instance from a source // RunConfig contains configuration for running an instance from a source
@ -28,15 +29,7 @@ type RunConfig struct {
sshTimeout time.Duration sshTimeout time.Duration
} }
func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { func (c *RunConfig) Prepare(ctx *interpolate.Context) []error {
if t == nil {
var err error
t, err = packer.NewConfigTemplate()
if err != nil {
return []error{err}
}
}
// Defaults // Defaults
if c.SSHUsername == "" { if c.SSHUsername == "" {
c.SSHUsername = "root" c.SSHUsername = "root"
@ -69,25 +62,6 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error {
errs = append(errs, errors.New("An ssh_username must be specified")) errs = append(errs, errors.New("An ssh_username must be specified"))
} }
templates := map[string]*string{
"flavor": &c.Flavor,
"ssh_timeout": &c.RawSSHTimeout,
"ssh_username": &c.SSHUsername,
"ssh_interface": &c.SSHInterface,
"source_image": &c.SourceImage,
"openstack_provider": &c.OpenstackProvider,
"floating_ip_pool": &c.FloatingIpPool,
"floating_ip": &c.FloatingIp,
}
for n, ptr := range templates {
var err error
*ptr, err = t.Process(*ptr, nil)
if err != nil {
errs = append(errs, fmt.Errorf("Error processing %s: %s", n, err))
}
}
c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout) c.sshTimeout, err = time.ParseDuration(c.RawSSHTimeout)
if err != nil { if err != nil {
errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err)) errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err))

View File

@ -14,7 +14,7 @@ type stepCreateImage struct{}
func (s *stepCreateImage) Run(state multistep.StateBag) multistep.StepAction { func (s *stepCreateImage) Run(state multistep.StateBag) multistep.StepAction {
csp := state.Get("csp").(gophercloud.CloudServersProvider) csp := state.Get("csp").(gophercloud.CloudServersProvider)
config := state.Get("config").(config) config := state.Get("config").(Config)
server := state.Get("server").(*gophercloud.Server) server := state.Get("server").(*gophercloud.Server)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)