post-processor/manifest: fixes interpolation of output

* rename `filename` to `output` for consistent with other post-processors
* interpolate output with variables

Signed-off-by: Vasiliy Tolstov <v.tolstov@selfip.ru>
This commit is contained in:
Vasiliy Tolstov 2016-11-21 11:04:16 +03:00
parent 902ac5ab2e
commit b29d0b4378
1 changed files with 15 additions and 14 deletions

View File

@ -18,9 +18,9 @@ import (
type Config struct {
common.PackerConfig `mapstructure:",squash"`
Filename string `mapstructure:"filename"`
StripPath bool `mapstructure:"strip_path"`
ctx interpolate.Context
OutputPath string `mapstructure:"output"`
StripPath bool `mapstructure:"strip_path"`
ctx interpolate.Context
}
type PostProcessor struct {
@ -44,8 +44,12 @@ func (p *PostProcessor) Configure(raws ...interface{}) error {
return err
}
if p.config.Filename == "" {
p.config.Filename = "packer-manifest.json"
if p.config.OutputPath == "" {
p.config.OutputPath = "packer-manifest.json"
}
if err = interpolate.Validate(p.config.OutputPath, &p.config.ctx); err != nil {
return fmt.Errorf("Error parsing target template: %s", err)
}
return nil
@ -85,7 +89,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, source packer.Artifact) (packe
// Create a lock file with exclusive access. If this fails we will retry
// after a delay.
lockFilename := p.config.Filename + ".lock"
lockFilename := p.config.OutputPath + ".lock"
for i := 0; i < 3; i++ {
// The file should not be locked for very long so we'll keep this short.
time.Sleep((time.Duration(i) * 200 * time.Millisecond))
@ -97,20 +101,17 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, source packer.Artifact) (packe
}
defer os.Remove(lockFilename)
// TODO fix error on first run:
// * Post-processor failed: open packer-manifest.json: no such file or directory
//
// Read the current manifest file from disk
contents := []byte{}
if contents, err = ioutil.ReadFile(p.config.Filename); err != nil && !os.IsNotExist(err) {
return source, true, fmt.Errorf("Unable to open %s for reading: %s", p.config.Filename, err)
if contents, err = ioutil.ReadFile(p.config.OutputPath); err != nil && !os.IsNotExist(err) {
return source, true, fmt.Errorf("Unable to open %s for reading: %s", p.config.OutputPath, err)
}
// Parse the manifest file JSON, if we have one
manifestFile := &ManifestFile{}
if len(contents) > 0 {
if err = json.Unmarshal(contents, manifestFile); err != nil {
return source, true, fmt.Errorf("Unable to parse content from %s: %s", p.config.Filename, err)
return source, true, fmt.Errorf("Unable to parse content from %s: %s", p.config.OutputPath, err)
}
}
@ -126,8 +127,8 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, source packer.Artifact) (packe
// Write JSON to disk
if out, err := json.MarshalIndent(manifestFile, "", " "); err == nil {
if err = ioutil.WriteFile(p.config.Filename, out, 0664); err != nil {
return source, true, fmt.Errorf("Unable to write %s: %s", p.config.Filename, err)
if err = ioutil.WriteFile(p.config.OutputPath, out, 0664); err != nil {
return source, true, fmt.Errorf("Unable to write %s: %s", p.config.OutputPath, err)
}
} else {
return source, true, fmt.Errorf("Unable to marshal JSON %s", err)