github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/post-processor/vagrant/aws.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 "log" 9 "os" 10 "path/filepath" 11 "strings" 12 ) 13 14 type AWSBoxConfig struct { 15 common.PackerConfig `mapstructure:",squash"` 16 17 OutputPath string `mapstructure:"output"` 18 VagrantfileTemplate string `mapstructure:"vagrantfile_template"` 19 20 tpl *packer.ConfigTemplate 21 } 22 23 type AWSVagrantfileTemplate struct { 24 Images map[string]string 25 } 26 27 type AWSBoxPostProcessor struct { 28 config AWSBoxConfig 29 } 30 31 func (p *AWSBoxPostProcessor) Configure(raws ...interface{}) error { 32 md, err := common.DecodeConfig(&p.config, raws...) 33 if err != nil { 34 return err 35 } 36 37 p.config.tpl, err = packer.NewConfigTemplate() 38 if err != nil { 39 return err 40 } 41 p.config.tpl.UserVars = p.config.PackerUserVars 42 43 // Accumulate any errors 44 errs := common.CheckUnusedConfig(md) 45 46 validates := map[string]*string{ 47 "output": &p.config.OutputPath, 48 "vagrantfile_template": &p.config.VagrantfileTemplate, 49 } 50 51 for n, ptr := range validates { 52 if err := p.config.tpl.Validate(*ptr); err != nil { 53 errs = packer.MultiErrorAppend( 54 errs, fmt.Errorf("Error parsing %s: %s", n, err)) 55 } 56 } 57 58 if errs != nil && len(errs.Errors) > 0 { 59 return errs 60 } 61 62 return nil 63 } 64 65 func (p *AWSBoxPostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) { 66 // Determine the regions... 67 tplData := &AWSVagrantfileTemplate{ 68 Images: make(map[string]string), 69 } 70 71 for _, regions := range strings.Split(artifact.Id(), ",") { 72 parts := strings.Split(regions, ":") 73 if len(parts) != 2 { 74 return nil, false, fmt.Errorf("Poorly formatted artifact ID: %s", artifact.Id()) 75 } 76 77 tplData.Images[parts[0]] = parts[1] 78 } 79 80 // Compile the output path 81 outputPath, err := p.config.tpl.Process(p.config.OutputPath, &OutputPathTemplate{ 82 ArtifactId: artifact.Id(), 83 BuildName: p.config.PackerBuildName, 84 Provider: "aws", 85 }) 86 if err != nil { 87 return nil, false, err 88 } 89 90 // Create a temporary directory for us to build the contents of the box in 91 dir, err := ioutil.TempDir("", "packer") 92 if err != nil { 93 return nil, false, err 94 } 95 defer os.RemoveAll(dir) 96 97 // Create the Vagrantfile from the template 98 vf, err := os.Create(filepath.Join(dir, "Vagrantfile")) 99 if err != nil { 100 return nil, false, err 101 } 102 defer vf.Close() 103 104 vagrantfileContents := defaultAWSVagrantfile 105 if p.config.VagrantfileTemplate != "" { 106 log.Printf("Using vagrantfile template: %s", p.config.VagrantfileTemplate) 107 f, err := os.Open(p.config.VagrantfileTemplate) 108 if err != nil { 109 err = fmt.Errorf("error opening vagrantfile template: %s", err) 110 return nil, false, err 111 } 112 defer f.Close() 113 114 contents, err := ioutil.ReadAll(f) 115 if err != nil { 116 err = fmt.Errorf("error reading vagrantfile template: %s", err) 117 return nil, false, err 118 } 119 120 vagrantfileContents = string(contents) 121 } 122 123 vagrantfileContents, err = p.config.tpl.Process(vagrantfileContents, tplData) 124 if err != nil { 125 return nil, false, fmt.Errorf("Error writing Vagrantfile: %s", err) 126 } 127 vf.Write([]byte(vagrantfileContents)) 128 vf.Close() 129 130 // Create the metadata 131 metadata := map[string]string{"provider": "aws"} 132 if err := WriteMetadata(dir, metadata); err != nil { 133 return nil, false, err 134 } 135 136 // Compress the directory to the given output path 137 if err := DirToBox(outputPath, dir); err != nil { 138 err = fmt.Errorf("error creating box: %s", err) 139 return nil, false, err 140 } 141 142 return NewArtifact("aws", outputPath), true, nil 143 } 144 145 var defaultAWSVagrantfile = ` 146 Vagrant.configure("2") do |config| 147 config.vm.provider "aws" do |aws| 148 {{ range $region, $ami := .Images }} 149 aws.region_config "{{ $region }}", ami: "{{ $ami }}" 150 {{ end }} 151 end 152 end 153 `