github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/post-processor/vagrant/vmware.go (about) 1 package vagrant 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/packer/common" 6 "github.com/mitchellh/packer/packer" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 ) 11 12 type VMwareBoxConfig struct { 13 common.PackerConfig `mapstructure:",squash"` 14 15 OutputPath string `mapstructure:"output"` 16 VagrantfileTemplate string `mapstructure:"vagrantfile_template"` 17 18 tpl *packer.ConfigTemplate 19 } 20 21 type VMwareBoxPostProcessor struct { 22 config VMwareBoxConfig 23 } 24 25 func (p *VMwareBoxPostProcessor) Configure(raws ...interface{}) error { 26 md, err := common.DecodeConfig(&p.config, raws...) 27 if err != nil { 28 return err 29 } 30 31 p.config.tpl, err = packer.NewConfigTemplate() 32 if err != nil { 33 return err 34 } 35 p.config.tpl.UserVars = p.config.PackerUserVars 36 37 // Accumulate any errors 38 errs := common.CheckUnusedConfig(md) 39 40 validates := map[string]*string{ 41 "output": &p.config.OutputPath, 42 "vagrantfile_template": &p.config.VagrantfileTemplate, 43 } 44 45 for n, ptr := range validates { 46 if err := p.config.tpl.Validate(*ptr); err != nil { 47 errs = packer.MultiErrorAppend( 48 errs, fmt.Errorf("Error parsing %s: %s", n, err)) 49 } 50 } 51 52 if errs != nil && len(errs.Errors) > 0 { 53 return errs 54 } 55 56 return nil 57 } 58 59 func (p *VMwareBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { 60 // Compile the output path 61 outputPath, err := p.config.tpl.Process(p.config.OutputPath, &OutputPathTemplate{ 62 ArtifactId: artifact.Id(), 63 BuildName: p.config.PackerBuildName, 64 Provider: "vmware", 65 }) 66 if err != nil { 67 return nil, false, err 68 } 69 70 // Create a temporary directory for us to build the contents of the box in 71 dir, err := ioutil.TempDir("", "packer") 72 if err != nil { 73 return nil, false, err 74 } 75 defer os.RemoveAll(dir) 76 77 // Copy all of the original contents into the temporary directory 78 for _, path := range artifact.Files() { 79 ui.Message(fmt.Sprintf("Copying: %s", path)) 80 81 dstPath := filepath.Join(dir, filepath.Base(path)) 82 if err := CopyContents(dstPath, path); err != nil { 83 return nil, false, err 84 } 85 } 86 87 if p.config.VagrantfileTemplate != "" { 88 f, err := os.Open(p.config.VagrantfileTemplate) 89 if err != nil { 90 return nil, false, err 91 } 92 defer f.Close() 93 94 contents, err := ioutil.ReadAll(f) 95 if err != nil { 96 return nil, false, err 97 } 98 99 // Create the Vagrantfile from the template 100 vf, err := os.Create(filepath.Join(dir, "Vagrantfile")) 101 if err != nil { 102 return nil, false, err 103 } 104 defer vf.Close() 105 106 vagrantfileContents, err := p.config.tpl.Process(string(contents), nil) 107 if err != nil { 108 return nil, false, fmt.Errorf("Error writing Vagrantfile: %s", err) 109 } 110 vf.Write([]byte(vagrantfileContents)) 111 vf.Close() 112 } 113 114 // Create the metadata 115 metadata := map[string]string{"provider": "vmware_desktop"} 116 if err := WriteMetadata(dir, metadata); err != nil { 117 return nil, false, err 118 } 119 120 // Compress the directory to the given output path 121 ui.Message(fmt.Sprintf("Compressing box...")) 122 if err := DirToBox(outputPath, dir); err != nil { 123 return nil, false, err 124 } 125 126 return NewArtifact("vmware", outputPath), false, nil 127 }