2013-07-16 00:08:19 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2013-07-29 19:42:35 -04:00
|
|
|
"fmt"
|
2013-07-16 00:08:19 -04:00
|
|
|
"github.com/mitchellh/goamz/aws"
|
2013-08-15 22:17:23 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-07-29 19:42:35 -04:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
2013-07-16 00:08:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// AccessConfig is for common configuration related to AWS access
|
|
|
|
type AccessConfig struct {
|
|
|
|
AccessKey string `mapstructure:"access_key"`
|
|
|
|
SecretKey string `mapstructure:"secret_key"`
|
2013-07-29 19:42:35 -04:00
|
|
|
RawRegion string `mapstructure:"region"`
|
2013-07-16 00:08:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Auth returns a valid aws.Auth object for access to AWS services, or
|
|
|
|
// an error if the authentication couldn't be resolved.
|
|
|
|
func (c *AccessConfig) Auth() (aws.Auth, error) {
|
2013-09-18 16:59:23 -04:00
|
|
|
auth, err := aws.GetAuth(c.AccessKey, c.SecretKey)
|
|
|
|
if err == nil {
|
|
|
|
// Store the accesskey and secret that we got...
|
|
|
|
c.AccessKey = auth.AccessKey
|
|
|
|
c.SecretKey = auth.SecretKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return auth, err
|
2013-07-16 00:08:19 -04:00
|
|
|
}
|
|
|
|
|
2013-07-29 19:42:35 -04:00
|
|
|
// Region returns the aws.Region object for access to AWS services, requesting
|
|
|
|
// the region from the instance metadata if possible.
|
|
|
|
func (c *AccessConfig) Region() (aws.Region, error) {
|
|
|
|
if c.RawRegion != "" {
|
|
|
|
return aws.Regions[c.RawRegion], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
md, err := aws.GetMetaData("placement/availability-zone")
|
|
|
|
if err != nil {
|
|
|
|
return aws.Region{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
region := strings.TrimRightFunc(string(md), unicode.IsLetter)
|
|
|
|
return aws.Regions[region], nil
|
|
|
|
}
|
|
|
|
|
2013-08-15 22:17:23 -04:00
|
|
|
func (c *AccessConfig) Prepare(t *packer.ConfigTemplate) []error {
|
2013-08-08 17:29:46 -04:00
|
|
|
if t == nil {
|
|
|
|
var err error
|
2013-08-15 22:17:23 -04:00
|
|
|
t, err = packer.NewConfigTemplate()
|
2013-08-08 17:29:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
templates := map[string]*string{
|
|
|
|
"access_key": &c.AccessKey,
|
|
|
|
"secret_key": &c.SecretKey,
|
|
|
|
"region": &c.RawRegion,
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 19:42:35 -04:00
|
|
|
if c.RawRegion != "" {
|
|
|
|
if _, ok := aws.Regions[c.RawRegion]; !ok {
|
2013-08-08 17:29:46 -04:00
|
|
|
errs = append(errs, fmt.Errorf("Unknown region: %s", c.RawRegion))
|
2013-07-29 19:42:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 17:29:46 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2013-07-16 00:08:19 -04:00
|
|
|
return nil
|
|
|
|
}
|