Commit 7edfb662 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Clean up the configuration loading mechanisms, ditch toml

parent c29d754f
package main package main
import ( import (
"github.com/BurntSushi/toml" "encoding/json"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer/plugin" "github.com/mitchellh/packer/packer/plugin"
"io"
"log" "log"
"os/exec" "os/exec"
) )
...@@ -11,15 +12,20 @@ import ( ...@@ -11,15 +12,20 @@ import (
// This is the default, built-in configuration that ships with // This is the default, built-in configuration that ships with
// Packer. // Packer.
const defaultConfig = ` const defaultConfig = `
[builders] {
amazon-ebs = "packer-builder-amazon-ebs" "builders": {
vmware = "packer-builder-vmware" "amazon-ebs": "packer-builder-amazon-ebs",
"vmware": "packer-builder-vmware"
[commands] },
build = "packer-command-build"
"commands": {
[provisioners] "build": "packer-command-build"
shell = "packer-provisioner-shell" },
"provisioners": {
"shell": "packer-provisioner-shell"
}
}
` `
type config struct { type config struct {
...@@ -28,44 +34,11 @@ type config struct { ...@@ -28,44 +34,11 @@ type config struct {
Provisioners map[string]string Provisioners map[string]string
} }
// Merge the configurations. Anything in the "new" configuration takes // Decodes configuration in JSON format from the given io.Reader into
// precedence over the "old" configuration. // the config object pointed to.
func mergeConfig(a, b *config) *config { func decodeConfig(r io.Reader, c *config) error {
configs := []*config{a, b} decoder := json.NewDecoder(r)
result := newConfig() return decoder.Decode(c)
for _, config := range configs {
for k, v := range config.Builders {
result.Builders[k] = v
}
for k, v := range config.Commands {
result.Commands[k] = v
}
for k, v := range config.Provisioners {
result.Provisioners[k] = v
}
}
return result
}
// Creates and initializes a new config struct.
func newConfig() *config {
result := new(config)
result.Builders = make(map[string]string)
result.Commands = make(map[string]string)
result.Provisioners = make(map[string]string)
return result
}
// Parses a configuration file and returns a proper configuration
// struct.
func parseConfig(data string) (result *config, err error) {
result = new(config)
_, err = toml.Decode(data, &result)
return
} }
// Returns an array of defined command names. // Returns an array of defined command names.
...@@ -77,6 +50,8 @@ func (c *config) CommandNames() (result []string) { ...@@ -77,6 +50,8 @@ func (c *config) CommandNames() (result []string) {
return return
} }
// This is a proper packer.BuilderFunc that can be used to load packer.Builder
// implementations from the defined plugins.
func (c *config) LoadBuilder(name string) (packer.Builder, error) { func (c *config) LoadBuilder(name string) (packer.Builder, error) {
log.Printf("Loading builder: %s\n", name) log.Printf("Loading builder: %s\n", name)
bin, ok := c.Builders[name] bin, ok := c.Builders[name]
...@@ -101,11 +76,15 @@ func (c *config) LoadCommand(name string) (packer.Command, error) { ...@@ -101,11 +76,15 @@ func (c *config) LoadCommand(name string) (packer.Command, error) {
return plugin.Command(exec.Command(commandBin)) return plugin.Command(exec.Command(commandBin))
} }
// This is a proper implementation of packer.HookFunc that can be used
// to load packer.Hook implementations from the defined plugins.
func (c *config) LoadHook(name string) (packer.Hook, error) { func (c *config) LoadHook(name string) (packer.Hook, error) {
log.Printf("Loading hook: %s\n", name) log.Printf("Loading hook: %s\n", name)
return plugin.Hook(exec.Command(name)) return plugin.Hook(exec.Command(name))
} }
// This is a proper packer.ProvisionerFunc that can be used to load
// packer.Provisioner implementations from defined plugins.
func (c *config) LoadProvisioner(name string) (packer.Provisioner, error) { func (c *config) LoadProvisioner(name string) (packer.Provisioner, error) {
log.Printf("Loading provisioner: %s\n", name) log.Printf("Loading provisioner: %s\n", name)
provBin, ok := c.Provisioners[name] provBin, ok := c.Provisioners[name]
......
package main
import (
"cgl.tideland.biz/asserts"
"testing"
)
func TestConfig_MergeConfig(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
aString := `
[commands]
a = "1"
b = "1"
`
bString := `
[commands]
a = "1"
b = "2"
c = "3"
`
a, _ := parseConfig(aString)
b, _ := parseConfig(bString)
result := mergeConfig(a, b)
assert.Equal(result.Commands["a"], "1", "a should be 1")
assert.Equal(result.Commands["b"], "2", "a should be 2")
assert.Equal(result.Commands["c"], "3", "a should be 3")
}
func TestConfig_ParseConfig_Bad(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := `
[commands]
foo = bar
`
_, err := parseConfig(data)
assert.NotNil(err, "should have an error")
}
func TestConfig_ParseConfig_DefaultConfig(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
_, err := parseConfig(defaultConfig)
assert.Nil(err, "should be able to parse the default config")
}
func TestConfig_ParseConfig_Good(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
data := `
[commands]
foo = "bar"
`
c, err := parseConfig(data)
assert.Nil(err, "should not have an error")
assert.Equal(c.CommandNames(), []string{"foo"}, "should have correct command names")
assert.Equal(c.Commands["foo"], "bar", "should have the command")
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer/plugin" "github.com/mitchellh/packer/packer/plugin"
...@@ -9,47 +10,10 @@ import ( ...@@ -9,47 +10,10 @@ import (
"log" "log"
"os" "os"
"os/user" "os/user"
"path" "path/filepath"
"runtime" "runtime"
) )
func loadGlobalConfig() (result *config, err error) {
mustExist := true
p := os.Getenv("PACKER_CONFIG")
if p == "" {
var u *user.User
u, err = user.Current()
if err != nil {
return
}
p = path.Join(u.HomeDir, ".packerrc")
mustExist = false
}
log.Printf("Loading packer config: %s\n", p)
contents, err := ioutil.ReadFile(p)
if err != nil && !mustExist {
// Don't report an error if it is okay if the file is missing
perr, ok := err.(*os.PathError)
if ok && perr.Op == "open" {
log.Printf("Packer config didn't exist. Ignoring: %s\n", p)
err = nil
}
}
if err != nil {
return
}
result, err = parseConfig(string(contents))
if err != nil {
return
}
return
}
func main() { func main() {
if os.Getenv("PACKER_LOG") == "" { if os.Getenv("PACKER_LOG") == "" {
// If we don't have logging explicitly enabled, then disable it // If we don't have logging explicitly enabled, then disable it
...@@ -64,24 +28,15 @@ func main() { ...@@ -64,24 +28,15 @@ func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
} }
defer plugin.CleanupClients() config, err := loadConfig()
homeConfig, err := loadGlobalConfig()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error loading global Packer configuration: \n\n%s\n", err) fmt.Fprintf(os.Stderr, "Error loading configuration: \n\n%s\n", err)
os.Exit(1) os.Exit(1)
} }
config, err := parseConfig(defaultConfig) log.Printf("Packer config: %+v", config)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing global Packer configuration: \n\n%s\n", err)
os.Exit(1)
}
if homeConfig != nil { defer plugin.CleanupClients()
log.Println("Merging default config with home config...")
config = mergeConfig(config, homeConfig)
}
envConfig := packer.DefaultEnvironmentConfig() envConfig := packer.DefaultEnvironmentConfig()
envConfig.Commands = config.CommandNames() envConfig.Commands = config.CommandNames()
...@@ -107,3 +62,44 @@ func main() { ...@@ -107,3 +62,44 @@ func main() {
plugin.CleanupClients() plugin.CleanupClients()
os.Exit(exitCode) os.Exit(exitCode)
} }
func loadConfig() (*config, error) {
var config config
if err := decodeConfig(bytes.NewBufferString(defaultConfig), &config); err != nil {
return nil, err
}
mustExist := true
configFile := os.Getenv("PACKER_CONFIG")
if configFile == "" {
u, err := user.Current()
if err != nil {
return nil, err
}
configFile = filepath.Join(u.HomeDir, ".packerrc")
mustExist = false
}
log.Printf("Attempting to open config file: %s", configFile)
f, err := os.Open(configFile)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
if mustExist {
return nil, err
}
log.Println("File doesn't exist, but doesn't need to. Ignoring.")
return &config, nil
}
defer f.Close()
if err := decodeConfig(f, &config); err != nil {
return nil, err
}
return &config, nil
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment