-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvmware-ova.go
71 lines (62 loc) · 1.67 KB
/
vmware-ova.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
package main
import (
"errors"
"fmt"
"github.com/mitchellh/packer/builder/vmware/vmx"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer/plugin"
"os/exec"
"path/filepath"
//"reflect"
//"strings"
)
type Builder struct {
vmx *vmx.Builder
vmx_path string
ova_path string
}
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
raw, ok := raws[0].(map[interface{}]interface{})
if ok == false {
return nil, errors.New("invalid configuration")
}
b.ova_path, ok = raw["source_path"].(string)
if ok == false {
return nil, errors.New("source_path must be defined in builder config")
}
b.vmx_path = ova_to_vmx_path(b.ova_path)
raw["source_path"] = b.vmx_path
raws[0] = raw
b.vmx = new(vmx.Builder)
return b.vmx.Prepare(raws...)
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
program, err := FindOvfTool()
if err != nil {
ui.Message(fmt.Sprintf("err: %s", err))
}
sourcetype := "--sourceType=OVA"
targettype := "--targetType=VMX"
cmd := exec.Command(program, sourcetype, targettype, b.ova_path, b.vmx_path)
ui.Message(fmt.Sprintf("Converting OVA: %s to VMX: %s.", b.ova_path, b.vmx_path))
cmd.Start()
cmd.Wait()
ui.Message(fmt.Sprintf("OVA conversion completed."))
ui.Message(fmt.Sprintf("Starting VMX conversion."))
return b.vmx.Run(ui, hook, cache)
}
func (b *Builder) Cancel() {
b.vmx.Cancel()
}
func ova_to_vmx_path(ova_path string) string {
basename := ova_path[:len(ova_path)-4]
return basename + "/" + filepath.Base(basename+".vmx")
}
func main() {
server, err := plugin.Server()
if err != nil {
panic(err)
}
server.RegisterBuilder(new(Builder))
server.Serve()
}