This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
post-processor.go
146 lines (117 loc) · 4.35 KB
/
post-processor.go
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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"packer-post-processor-virtualbox-to-hyperv/hyperv"
vbox "github.com/mitchellh/packer/builder/virtualbox/common"
"github.com/mitchellh/packer/helper/config"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/post-processor/vagrant"
"github.com/mitchellh/packer/template/interpolate"
)
type Config struct {
vagrant.Config `mapstructure:",squash"`
StagingDir string `mapstructure:"staging_directory"`
VMName string `mapstructure:"vm_name"`
DiskName string `mapstructure:"disk_name"`
ctx interpolate.Context
}
type PostProcessor struct {
config Config
// embed the "vagrant" post-processor to work around https://github.com/mitchellh/packer/issues/1653
vagrant vagrant.PostProcessor
// used to convert the hard drive format
virtualbox vbox.Driver
}
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &p.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{},
},
}, raws...)
if err != nil {
return err
}
// configure defaults
if p.config.StagingDir == "" {
p.config.StagingDir = fmt.Sprintf("output-%s-hyperv", p.config.PackerBuildName)
}
if p.config.VMName == "" {
p.config.VMName = fmt.Sprintf("packer-%s-%d", p.config.PackerBuildName, interpolate.InitTime.Unix())
}
if p.config.DiskName == "" {
p.config.DiskName = fmt.Sprintf("packer-%s-%d", p.config.PackerBuildName, interpolate.InitTime.Unix())
}
// configure "vagrant" post-processor
if err := p.vagrant.Configure(raws...); err != nil {
return fmt.Errorf("Failed to configure vagrant post-processor: %s", err)
}
// find the VBoxManage executable
if p.virtualbox, err = vbox.NewDriver(); err != nil {
return fmt.Errorf("Failed to create VirtualBox driver: %s", err)
}
if _, err := os.Stat(p.config.StagingDir); err == nil {
return fmt.Errorf("Staging directory '%s' already exists.", p.config.StagingDir)
}
return nil
}
func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
if artifact.BuilderId() != "mitchellh.virtualbox" {
err := fmt.Errorf("Unknown artifact type: %s\nCan only import from virtualbox artifacts.", artifact.BuilderId())
return nil, false, err
}
outVirtualMachines := filepath.Join(p.config.StagingDir, "Virtual Machines")
if err := os.MkdirAll(outVirtualMachines, 0755); err != nil {
return nil, false, fmt.Errorf("Failed to create output directory: %s", err)
}
outVirtualHardDisks := filepath.Join(p.config.StagingDir, "Virtual Hard Disks")
if err := os.MkdirAll(outVirtualHardDisks, 0755); err != nil {
return nil, false, fmt.Errorf("Failed to create output directory: %s", err)
}
vmdk, err := FindVirtualHardDisk(artifact)
if err != nil {
return nil, false, fmt.Errorf("Failed to find VirtualBox hard disk: %s", err)
}
vhd, err := filepath.Abs(filepath.Join(outVirtualHardDisks, fmt.Sprintf("%s.vhd", p.config.DiskName)))
if err != nil {
return nil, false, fmt.Errorf("Failed to get absolute path to VHD: %s", err)
}
if err = p.virtualbox.VBoxManage("clonehd", vmdk, vhd, "--format", "vhd"); err != nil {
return nil, false, fmt.Errorf("Failed to convert VMDK to VHD: %s", err)
}
vm := filepath.Join(outVirtualMachines, "vm.xml")
if err := ioutil.WriteFile(vm, p.CreateVM(vhd), 0644); err != nil {
return nil, false, fmt.Errorf("Failed to write vm.xml: %s", err)
}
newArtifact, err := hyperv.NewArtifact(p.config.StagingDir)
if err != nil {
return nil, false, fmt.Errorf("Failed to create Hyper-V artifact: %s", err)
}
return p.vagrant.PostProcessProvider("hyperv", new(hyperv.HypervProvider), ui, newArtifact)
}
func (p *PostProcessor) CreateVM(vhd string) []byte {
vm := VM_XML
vm = strings.Replace(vm, "INSERT_VM_NAME_HERE", p.config.VMName, 1)
vm = strings.Replace(vm, "INSERT_VHD_PATH_HERE", vhd, 1)
// TODO: update `creation_time`
return []byte(vm)
}
func FindVirtualHardDisk(artifact packer.Artifact) (string, error) {
for _, path := range artifact.Files() {
if strings.ToLower(filepath.Ext(path)) != ".vmdk" {
continue
}
abs, err := filepath.Abs(path)
if err != nil {
return "", fmt.Errorf("Failed to get absolute path to VMDK: %s", err)
}
return abs, nil
}
return "", errors.New("Input artifact did not have a VMDK file")
}