2013-06-27 10:33:32 -04:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
2013-08-23 18:14:58 -04:00
|
|
|
"archive/tar"
|
2013-06-27 19:05:47 -04:00
|
|
|
"errors"
|
2013-06-27 10:33:32 -04:00
|
|
|
"fmt"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-08-23 18:14:58 -04:00
|
|
|
"io"
|
2013-06-27 10:33:32 -04:00
|
|
|
"io/ioutil"
|
2013-06-27 19:05:47 -04:00
|
|
|
"log"
|
2013-06-27 10:33:32 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-06-27 19:05:47 -04:00
|
|
|
"regexp"
|
2013-06-27 10:33:32 -04:00
|
|
|
)
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
type VBoxProvider struct{}
|
2013-08-15 15:09:22 -04:00
|
|
|
|
2014-02-21 23:02:40 -05:00
|
|
|
func (p *VBoxProvider) KeepInputArtifact() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
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"}
|
2013-12-19 12:28:19 -05:00
|
|
|
|
2013-06-27 10:33:32 -04:00
|
|
|
// Copy all of the original contents into the temporary directory
|
|
|
|
for _, path := range artifact.Files() {
|
2013-08-23 18:14:58 -04:00
|
|
|
// We treat OVA files specially, we unpack those into the temporary
|
|
|
|
// directory so we can get the resulting disk and OVF.
|
2013-08-22 22:47:50 -04:00
|
|
|
if extension := filepath.Ext(path); extension == ".ova" {
|
2013-08-23 18:14:58 -04:00
|
|
|
ui.Message(fmt.Sprintf("Unpacking OVA: %s", path))
|
2013-12-19 16:22:46 -05:00
|
|
|
if err = DecompressOva(dir, path); err != nil {
|
|
|
|
return
|
2013-08-22 22:47:50 -04:00
|
|
|
}
|
|
|
|
} else {
|
2013-12-19 12:28:19 -05:00
|
|
|
ui.Message(fmt.Sprintf("Copying from artifact: %s", path))
|
2013-08-22 22:47:50 -04:00
|
|
|
dstPath := filepath.Join(dir, filepath.Base(path))
|
2013-12-19 16:22:46 -05:00
|
|
|
if err = CopyContents(dstPath, path); err != nil {
|
|
|
|
return
|
2013-08-22 22:47:50 -04:00
|
|
|
}
|
2013-06-27 10:33:32 -04:00
|
|
|
}
|
2013-08-22 22:47:50 -04:00
|
|
|
|
2013-06-27 10:33:32 -04:00
|
|
|
}
|
|
|
|
|
2013-06-29 15:46:05 -04:00
|
|
|
// Rename the OVF file to box.ovf, as required by Vagrant
|
|
|
|
ui.Message("Renaming the OVF to box.ovf...")
|
2013-12-19 16:22:46 -05:00
|
|
|
if err = p.renameOVF(dir); err != nil {
|
|
|
|
return
|
2013-06-29 15:46:05 -04:00
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
// Create the Vagrantfile from the template
|
|
|
|
var baseMacAddress string
|
|
|
|
baseMacAddress, err = p.findBaseMacAddress(dir)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2013-06-27 10:33:32 -04:00
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
vagrantfile = fmt.Sprintf(vboxVagrantfile, baseMacAddress)
|
|
|
|
return
|
2013-06-27 10:33:32 -04:00
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
func (p *VBoxProvider) findOvf(dir string) (string, error) {
|
2013-08-22 22:47:50 -04:00
|
|
|
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.")
|
2013-06-27 19:05:47 -04:00
|
|
|
}
|
|
|
|
|
2013-08-22 22:47:50 -04:00
|
|
|
if len(file_matches) < 1 {
|
2013-06-27 19:05:47 -04:00
|
|
|
return "", errors.New("ovf file couldn't be found")
|
|
|
|
}
|
|
|
|
|
2013-08-22 22:47:50 -04:00
|
|
|
return file_matches[0], err
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
func (p *VBoxProvider) renameOVF(dir string) error {
|
2013-08-22 22:47:50 -04:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
func (p *VBoxProvider) findBaseMacAddress(dir string) (string, error) {
|
2013-08-22 22:47:50 -04:00
|
|
|
log.Println("Looking for OVF for base mac address...")
|
|
|
|
ovf, err := p.findOvf(dir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:05:47 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-08-23 18:14:58 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:22:46 -05:00
|
|
|
var vboxVagrantfile = `
|
2013-06-27 10:33:32 -04:00
|
|
|
Vagrant.configure("2") do |config|
|
2013-12-19 16:22:46 -05:00
|
|
|
config.vm.base_mac = "%s"
|
2013-06-27 10:33:32 -04:00
|
|
|
end
|
|
|
|
`
|