1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
package vagrant
import (
"archive/tar"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"github.com/hashicorp/packer/packer"
)
type VBoxProvider struct{}
func (p *VBoxProvider) KeepInputArtifact() bool {
return false
}
func (p *VBoxProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
// Create the metadata
metadata = map[string]interface{}{"provider": "virtualbox"}
// Copy all of the original contents into the temporary directory
for _, path := range artifact.Files() {
// We treat OVA files specially, we unpack those into the temporary
// directory so we can get the resulting disk and OVF.
if extension := filepath.Ext(path); extension == ".ova" {
ui.Message(fmt.Sprintf("Unpacking OVA: %s", path))
if err = DecompressOva(dir, path); err != nil {
return
}
} else {
ui.Message(fmt.Sprintf("Copying from artifact: %s", path))
dstPath := filepath.Join(dir, filepath.Base(path))
if err = CopyContents(dstPath, path); err != nil {
return
}
}
}
// Rename the OVF file to box.ovf, as required by Vagrant
ui.Message("Renaming the OVF to box.ovf...")
if err = p.renameOVF(dir); err != nil {
return
}
// Create the Vagrantfile from the template
var baseMacAddress string
baseMacAddress, err = p.findBaseMacAddress(dir)
if err != nil {
return
}
vagrantfile = fmt.Sprintf(vboxVagrantfile, baseMacAddress)
return
}
func (p *VBoxProvider) findOvf(dir string) (string, error) {
log.Println("Looking for OVF in artifact...")
file_matches, err := filepath.Glob(filepath.Join(dir, "*.ovf"))
if err != nil {
return "", err
}
if len(file_matches) > 1 {
return "", errors.New("More than one OVF file in VirtualBox artifact.")
}
if len(file_matches) < 1 {
return "", errors.New("ovf file couldn't be found")
}
return file_matches[0], err
}
func (p *VBoxProvider) renameOVF(dir string) error {
log.Println("Looking for OVF to rename...")
ovf, err := p.findOvf(dir)
if err != nil {
return err
}
log.Printf("Renaming: '%s' => box.ovf", ovf)
return os.Rename(ovf, filepath.Join(dir, "box.ovf"))
}
func (p *VBoxProvider) findBaseMacAddress(dir string) (string, error) {
log.Println("Looking for OVF for base mac address...")
ovf, err := p.findOvf(dir)
if err != nil {
return "", err
}
f, err := os.Open(ovf)
if err != nil {
return "", err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
re := regexp.MustCompile(`<Adapter slot="0".+?MACAddress="(.+?)"`)
matches := re.FindSubmatch(data)
if matches == nil {
return "", errors.New("can't find base mac address in OVF")
}
log.Printf("Base mac address: %s", string(matches[1]))
return string(matches[1]), nil
}
// DecompressOva takes an ova file and decompresses it into the target
// directory.
func DecompressOva(dir, src string) error {
log.Printf("Turning ova to dir: %s => %s", src, dir)
srcF, err := os.Open(src)
if err != nil {
return err
}
defer srcF.Close()
tarReader := tar.NewReader(srcF)
for {
hdr, err := tarReader.Next()
if hdr == nil || err == io.EOF {
break
}
if err != nil {
return err
}
// We use the fileinfo to get the file name because we are not
// expecting path information as from the tar header. It's important
// that we not use the path name from the tar header without checking
// for the presence of `..`. If we accidentally allow for that, we can
// open ourselves up to a path traversal vulnerability.
info := hdr.FileInfo()
// Shouldn't be any directories, skip them
if info.IsDir() {
continue
}
// We wrap this in an anonymous function so that the defers
// inside are handled more quickly so we can give up file handles.
err = func() error {
path := filepath.Join(dir, info.Name())
output, err := os.Create(path)
if err != nil {
return err
}
defer output.Close()
os.Chmod(path, info.Mode())
os.Chtimes(path, hdr.AccessTime, hdr.ModTime)
_, err = io.Copy(output, tarReader)
return err
}()
if err != nil {
return err
}
}
return nil
}
var vboxVagrantfile = `
Vagrant.configure("2") do |config|
config.vm.base_mac = "%s"
end
`
|