2014-10-27 23:34:49 -04:00
|
|
|
package command
|
2013-07-14 02:29:19 -04:00
|
|
|
|
|
|
|
import (
|
2013-07-14 04:05:26 -04:00
|
|
|
"bytes"
|
2013-07-14 02:29:19 -04:00
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2013-08-08 20:12:04 -04:00
|
|
|
"log"
|
2013-07-14 02:29:19 -04:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2014-10-27 23:34:49 -04:00
|
|
|
"github.com/mitchellh/packer/fix"
|
|
|
|
)
|
2013-07-14 02:29:19 -04:00
|
|
|
|
2014-10-27 23:34:49 -04:00
|
|
|
type FixCommand struct {
|
|
|
|
Meta
|
2013-07-14 02:29:19 -04:00
|
|
|
}
|
|
|
|
|
2014-10-27 23:34:49 -04:00
|
|
|
func (c *FixCommand) Run(args []string) int {
|
|
|
|
env, err := c.Meta.Environment()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing environment: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-07-14 02:29:19 -04:00
|
|
|
cmdFlags := flag.NewFlagSet("fix", flag.ContinueOnError)
|
|
|
|
cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = cmdFlags.Args()
|
|
|
|
if len(args) != 1 {
|
|
|
|
cmdFlags.Usage()
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the file for decoding
|
|
|
|
tplF, err := os.Open(args[0])
|
|
|
|
if err != nil {
|
|
|
|
env.Ui().Error(fmt.Sprintf("Error opening template: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer tplF.Close()
|
|
|
|
|
|
|
|
// Decode the JSON into a generic map structure
|
|
|
|
var templateData map[string]interface{}
|
|
|
|
decoder := json.NewDecoder(tplF)
|
|
|
|
if err := decoder.Decode(&templateData); err != nil {
|
|
|
|
env.Ui().Error(fmt.Sprintf("Error parsing template: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the file since we're done with that
|
|
|
|
tplF.Close()
|
|
|
|
|
2013-07-14 04:05:26 -04:00
|
|
|
input := templateData
|
2014-10-27 23:34:49 -04:00
|
|
|
for _, name := range fix.FixerOrder {
|
2013-07-14 04:05:26 -04:00
|
|
|
var err error
|
2014-10-27 23:34:49 -04:00
|
|
|
fixer, ok := fix.Fixers[name]
|
2013-08-08 20:11:39 -04:00
|
|
|
if !ok {
|
|
|
|
panic("fixer not found: " + name)
|
|
|
|
}
|
|
|
|
|
2013-08-08 20:12:04 -04:00
|
|
|
log.Printf("Running fixer: %s", name)
|
2013-07-14 04:05:26 -04:00
|
|
|
input, err = fixer.Fix(input)
|
|
|
|
if err != nil {
|
|
|
|
env.Ui().Error(fmt.Sprintf("Error fixing: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var output bytes.Buffer
|
|
|
|
encoder := json.NewEncoder(&output)
|
|
|
|
if err := encoder.Encode(input); err != nil {
|
|
|
|
env.Ui().Error(fmt.Sprintf("Error encoding: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var indented bytes.Buffer
|
|
|
|
if err := json.Indent(&indented, output.Bytes(), "", " "); err != nil {
|
|
|
|
env.Ui().Error(fmt.Sprintf("Error encoding: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-07-14 04:10:50 -04:00
|
|
|
result := indented.String()
|
|
|
|
result = strings.Replace(result, `\u003c`, "<", -1)
|
|
|
|
result = strings.Replace(result, `\u003e`, ">", -1)
|
|
|
|
env.Ui().Say(result)
|
2013-07-14 02:29:19 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-10-27 23:34:49 -04:00
|
|
|
func (*FixCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: packer fix [options] TEMPLATE
|
|
|
|
|
|
|
|
Reads the JSON template and attempts to fix known backwards
|
|
|
|
incompatibilities. The fixed template will be outputted to standard out.
|
|
|
|
|
|
|
|
If the template cannot be fixed due to an error, the command will exit
|
|
|
|
with a non-zero exit status. Error messages will appear on standard error.
|
|
|
|
|
|
|
|
Fixes that are run:
|
|
|
|
|
|
|
|
iso-md5 Replaces "iso_md5" in builders with newer "iso_checksum"
|
|
|
|
createtime Replaces ".CreateTime" in builder configs with "{{timestamp}}"
|
|
|
|
virtualbox-gaattach Updates VirtualBox builders using "guest_additions_attach"
|
|
|
|
to use "guest_additions_mode"
|
|
|
|
pp-vagrant-override Replaces old-style provider overrides for the Vagrant
|
|
|
|
post-processor to new-style as of Packer 0.5.0.
|
|
|
|
virtualbox-rename Updates "virtualbox" builders to "virtualbox-iso"
|
|
|
|
`
|
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FixCommand) Synopsis() string {
|
2013-07-14 02:29:19 -04:00
|
|
|
return "fixes templates from old versions of packer"
|
|
|
|
}
|