2014-10-27 23:21:13 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-05-23 19:30:45 -04:00
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-06-06 13:52:12 -04:00
|
|
|
"os"
|
2015-05-23 19:30:45 -04:00
|
|
|
|
2019-01-10 09:27:02 -05:00
|
|
|
kvflag "github.com/hashicorp/packer/helper/flag-kv"
|
|
|
|
sliceflag "github.com/hashicorp/packer/helper/flag-slice"
|
2020-04-09 17:38:17 -04:00
|
|
|
"github.com/hashicorp/packer/helper/wrappedstreams"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template"
|
2015-05-23 19:30:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// FlagSetFlags is an enum to define what flags are present in the
|
|
|
|
// default FlagSet returned by Meta.FlagSet
|
|
|
|
type FlagSetFlags uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
FlagSetNone FlagSetFlags = 0
|
|
|
|
FlagSetBuildFilter FlagSetFlags = 1 << iota
|
|
|
|
FlagSetVars
|
2014-10-27 23:21:13 -04:00
|
|
|
)
|
|
|
|
|
2015-05-23 19:30:45 -04:00
|
|
|
// Meta contains the meta-options and functionality that nearly every
|
|
|
|
// Packer command inherits.
|
2014-10-27 23:21:13 -04:00
|
|
|
type Meta struct {
|
2015-05-23 19:30:45 -04:00
|
|
|
CoreConfig *packer.CoreConfig
|
|
|
|
Ui packer.Ui
|
2015-06-29 14:49:45 -04:00
|
|
|
Version string
|
2015-05-23 19:30:45 -04:00
|
|
|
|
|
|
|
// These are set by command-line flags
|
2020-03-12 09:27:56 -04:00
|
|
|
varFiles []string
|
2019-01-10 09:27:02 -05:00
|
|
|
flagVars map[string]string
|
2015-05-23 19:30:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Core returns the core for the given template given the configured
|
|
|
|
// CoreConfig and user variables on this Meta.
|
|
|
|
func (m *Meta) Core(tpl *template.Template) (*packer.Core, error) {
|
|
|
|
// Copy the config so we don't modify it
|
|
|
|
config := *m.CoreConfig
|
|
|
|
config.Template = tpl
|
2020-03-12 09:27:56 -04:00
|
|
|
|
|
|
|
fj := &kvflag.FlagJSON{}
|
2020-03-30 04:31:59 -04:00
|
|
|
// First populate fj with contents from var files
|
2020-03-12 09:27:56 -04:00
|
|
|
for _, file := range m.varFiles {
|
|
|
|
err := fj.Set(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 04:31:59 -04:00
|
|
|
// Now read fj values back into flagvars and set as config.Variables. Only
|
|
|
|
// add to flagVars if the key doesn't already exist, because flagVars comes
|
|
|
|
// from the command line and should not be overridden by variable files.
|
2020-03-19 08:57:22 -04:00
|
|
|
if m.flagVars == nil {
|
|
|
|
m.flagVars = map[string]string{}
|
|
|
|
}
|
2020-03-12 09:27:56 -04:00
|
|
|
for k, v := range *fj {
|
2020-03-30 04:31:59 -04:00
|
|
|
if _, exists := m.flagVars[k]; !exists {
|
|
|
|
m.flagVars[k] = v
|
|
|
|
}
|
2020-03-12 09:27:56 -04:00
|
|
|
}
|
2015-05-23 19:30:45 -04:00
|
|
|
config.Variables = m.flagVars
|
|
|
|
|
|
|
|
// Init the core
|
|
|
|
core, err := packer.NewCore(&config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error initializing core: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return core, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildNames returns the list of builds that are in the given core
|
|
|
|
// that we care about taking into account the only and except flags.
|
|
|
|
func (m *Meta) BuildNames(c *packer.Core) []string {
|
|
|
|
// TODO: test
|
|
|
|
|
|
|
|
// Filter the "only"
|
2019-01-10 09:27:02 -05:00
|
|
|
if len(m.CoreConfig.Only) > 0 {
|
2015-05-23 19:30:45 -04:00
|
|
|
// Build a set of all the available names
|
|
|
|
nameSet := make(map[string]struct{})
|
|
|
|
for _, n := range c.BuildNames() {
|
|
|
|
nameSet[n] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build our result set which we pre-allocate some sane number
|
2019-01-10 09:27:02 -05:00
|
|
|
result := make([]string, 0, len(m.CoreConfig.Only))
|
|
|
|
for _, n := range m.CoreConfig.Only {
|
2015-05-23 19:30:45 -04:00
|
|
|
if _, ok := nameSet[n]; ok {
|
|
|
|
result = append(result, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the "except"
|
2019-01-10 09:27:02 -05:00
|
|
|
if len(m.CoreConfig.Except) > 0 {
|
2015-05-23 19:30:45 -04:00
|
|
|
// Build a set of the things we don't want
|
|
|
|
nameSet := make(map[string]struct{})
|
2019-01-10 09:27:02 -05:00
|
|
|
for _, n := range m.CoreConfig.Except {
|
2015-05-23 19:30:45 -04:00
|
|
|
nameSet[n] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build our result set which is the names of all builds except
|
|
|
|
// those in the given set.
|
|
|
|
names := c.BuildNames()
|
|
|
|
result := make([]string, 0, len(names))
|
|
|
|
for _, n := range names {
|
|
|
|
if _, ok := nameSet[n]; !ok {
|
|
|
|
result = append(result, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// We care about everything
|
|
|
|
return c.BuildNames()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlagSet returns a FlagSet with the common flags that every
|
|
|
|
// command implements. The exact behavior of FlagSet can be configured
|
|
|
|
// using the flags as the second parameter, for example to disable
|
|
|
|
// build settings on the commands that don't handle builds.
|
|
|
|
func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet {
|
|
|
|
f := flag.NewFlagSet(n, flag.ContinueOnError)
|
|
|
|
|
|
|
|
// FlagSetBuildFilter tells us to enable the settings for selecting
|
|
|
|
// builds we care about.
|
|
|
|
if fs&FlagSetBuildFilter != 0 {
|
2019-01-10 09:27:02 -05:00
|
|
|
f.Var((*sliceflag.StringFlag)(&m.CoreConfig.Except), "except", "")
|
|
|
|
f.Var((*sliceflag.StringFlag)(&m.CoreConfig.Only), "only", "")
|
2015-05-23 19:30:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlagSetVars tells us what variables to use
|
|
|
|
if fs&FlagSetVars != 0 {
|
|
|
|
f.Var((*kvflag.Flag)(&m.flagVars), "var", "")
|
2020-03-12 09:27:56 -04:00
|
|
|
f.Var((*kvflag.StringSlice)(&m.varFiles), "var-file", "")
|
2015-05-23 19:30:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an io.Writer that writes to our Ui properly for errors.
|
|
|
|
// This is kind of a hack, but it does the job. Basically: create
|
|
|
|
// a pipe, use a scanner to break it into lines, and output each line
|
|
|
|
// to the UI. Do this forever.
|
|
|
|
errR, errW := io.Pipe()
|
|
|
|
errScanner := bufio.NewScanner(errR)
|
|
|
|
go func() {
|
|
|
|
for errScanner.Scan() {
|
|
|
|
m.Ui.Error(errScanner.Text())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
f.SetOutput(errW)
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateFlags should be called after parsing flags to validate the
|
|
|
|
// given flags
|
|
|
|
func (m *Meta) ValidateFlags() error {
|
|
|
|
// TODO
|
|
|
|
return nil
|
2014-10-27 23:21:13 -04:00
|
|
|
}
|
2019-06-06 13:52:12 -04:00
|
|
|
|
|
|
|
// StdinPiped returns true if the input is piped.
|
|
|
|
func (m *Meta) StdinPiped() bool {
|
2020-04-09 17:38:17 -04:00
|
|
|
fi, err := wrappedstreams.Stdin().Stat()
|
2019-06-06 13:52:12 -04:00
|
|
|
if err != nil {
|
|
|
|
// If there is an error, let's just say its not piped
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return fi.Mode()&os.ModeNamedPipe != 0
|
|
|
|
}
|