mirror of https://github.com/hashicorp/packer
Once we have produced a qemu VM, we now have the option of using the vagrant post-processor to create a .box file that can be used with the vagrant-libvirt plugin. This uses the new State method of the Artifact API to get necessary information from the builder.pull/1330/head
parent
90a57c411f
commit
60e608dfdb
@ -0,0 +1,64 @@
|
||||
package vagrant
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type LibVirtProvider struct{}
|
||||
|
||||
func (p *LibVirtProvider) KeepInputArtifact() bool {
|
||||
return false
|
||||
}
|
||||
func (p *LibVirtProvider) Process(ui packer.Ui, artifact packer.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
|
||||
diskName := artifact.State("diskName").(string)
|
||||
|
||||
// Copy the disk image into the temporary directory (as box.img)
|
||||
for _, path := range artifact.Files() {
|
||||
if strings.HasSuffix(path, "/"+diskName) {
|
||||
ui.Message(fmt.Sprintf("Copying from artifact: %s", path))
|
||||
dstPath := filepath.Join(dir, "box.img")
|
||||
if err = CopyContents(dstPath, path); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
format := artifact.State("diskType").(string)
|
||||
origSize := artifact.State("diskSize").(uint64)
|
||||
size := origSize / 1024 // In MB, want GB
|
||||
if origSize % 1024 > 0 {
|
||||
// Make sure we don't make the size smaller
|
||||
size++
|
||||
}
|
||||
domainType := artifact.State("domainType").(string)
|
||||
|
||||
// Convert domain type to libvirt driver
|
||||
var driver string
|
||||
switch domainType {
|
||||
case "kvm", "qemu":
|
||||
driver = domainType
|
||||
default:
|
||||
return "", nil, fmt.Errorf("Unknown libvirt domain type: %s", domainType)
|
||||
}
|
||||
|
||||
// Create the metadata
|
||||
metadata = map[string]interface{}{
|
||||
"provider": "libvirt",
|
||||
"format": format,
|
||||
"virtual_size": size,
|
||||
}
|
||||
|
||||
vagrantfile = fmt.Sprintf(libvirtVagrantfile, driver)
|
||||
return
|
||||
}
|
||||
|
||||
var libvirtVagrantfile = `
|
||||
Vagrant.configure("2") do |config|
|
||||
config.vm.provider :libvirt do |libvirt|
|
||||
libvirt.driver = "%s"
|
||||
end
|
||||
end
|
||||
`
|
||||
Loading…
Reference in new issue