2013-08-09 18:10:24 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2013-08-09 19:50:24 -04:00
|
|
|
jsonutil "github.com/mitchellh/packer/common/json"
|
2013-08-09 18:10:24 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-08-09 19:45:29 -04:00
|
|
|
"io/ioutil"
|
2013-08-09 18:10:24 -04:00
|
|
|
"log"
|
2013-08-09 19:45:29 -04:00
|
|
|
"os"
|
2013-08-09 18:10:24 -04:00
|
|
|
)
|
|
|
|
|
2013-08-09 18:21:24 -04:00
|
|
|
// BuildOptions is a set of options related to builds that can be set
|
|
|
|
// from the command line.
|
|
|
|
type BuildOptions struct {
|
2013-08-09 19:45:29 -04:00
|
|
|
UserVarFiles []string
|
|
|
|
UserVars map[string]string
|
|
|
|
Except []string
|
|
|
|
Only []string
|
2013-08-09 18:10:24 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 18:21:24 -04:00
|
|
|
// Validate validates the options
|
|
|
|
func (f *BuildOptions) Validate() error {
|
2013-08-09 18:10:24 -04:00
|
|
|
if len(f.Except) > 0 && len(f.Only) > 0 {
|
|
|
|
return errors.New("Only one of '-except' or '-only' may be specified.")
|
|
|
|
}
|
|
|
|
|
2013-08-09 19:45:29 -04:00
|
|
|
if len(f.UserVarFiles) > 0 {
|
|
|
|
for _, path := range f.UserVarFiles {
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
return fmt.Errorf("Cannot access: %s", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 18:10:24 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-09 18:46:59 -04:00
|
|
|
// AllUserVars returns the user variables, compiled from both the
|
|
|
|
// file paths and the vars on the command line.
|
|
|
|
func (f *BuildOptions) AllUserVars() (map[string]string, error) {
|
|
|
|
all := make(map[string]string)
|
|
|
|
|
2013-08-09 19:45:29 -04:00
|
|
|
// Copy in the variables from the files
|
|
|
|
for _, path := range f.UserVarFiles {
|
|
|
|
fileVars, err := readFileVars(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range fileVars {
|
|
|
|
all[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 18:46:59 -04:00
|
|
|
// Copy in the command-line vars
|
|
|
|
for k, v := range f.UserVars {
|
|
|
|
all[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return all, nil
|
|
|
|
}
|
|
|
|
|
2013-08-09 18:10:24 -04:00
|
|
|
// Builds returns the builds out of the given template that pass the
|
2013-08-09 18:21:24 -04:00
|
|
|
// configured options.
|
|
|
|
func (f *BuildOptions) Builds(t *packer.Template, cf *packer.ComponentFinder) ([]packer.Build, error) {
|
2013-08-09 18:10:24 -04:00
|
|
|
buildNames := t.BuildNames()
|
|
|
|
builds := make([]packer.Build, 0, len(buildNames))
|
|
|
|
for _, buildName := range buildNames {
|
|
|
|
if len(f.Except) > 0 {
|
|
|
|
found := false
|
|
|
|
for _, except := range f.Except {
|
|
|
|
if buildName == except {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
|
|
|
log.Printf("Skipping build '%s' because specified by -except.", buildName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(f.Only) > 0 {
|
|
|
|
found := false
|
|
|
|
for _, only := range f.Only {
|
|
|
|
if buildName == only {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
log.Printf("Skipping build '%s' because not specified by -only.", buildName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Creating build: %s", buildName)
|
|
|
|
build, err := t.Build(buildName, cf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to create build '%s': \n\n%s", buildName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
builds = append(builds, build)
|
|
|
|
}
|
|
|
|
|
|
|
|
return builds, nil
|
|
|
|
}
|
2013-08-09 19:45:29 -04:00
|
|
|
|
|
|
|
func readFileVars(path string) (map[string]string, error) {
|
|
|
|
bytes, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := make(map[string]string)
|
2013-08-09 19:50:24 -04:00
|
|
|
err = jsonutil.Unmarshal(bytes, &vars)
|
2013-08-09 19:45:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return vars, nil
|
|
|
|
}
|