forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
117 lines (94 loc) · 2.47 KB
/
build.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
package rpc
import (
"github.com/mitchellh/packer/packer"
"net/rpc"
)
// An implementation of packer.Build where the build is actually executed
// over an RPC connection.
type build struct {
client *rpc.Client
}
// BuildServer wraps a packer.Build implementation and makes it exportable
// as part of a Golang RPC server.
type BuildServer struct {
build packer.Build
}
type BuildRunArgs struct {
UiRPCAddress string
}
func Build(client *rpc.Client) *build {
return &build{client}
}
func (b *build) Name() (result string) {
b.client.Call("Build.Name", new(interface{}), &result)
return
}
func (b *build) Prepare() (err error) {
if cerr := b.client.Call("Build.Prepare", new(interface{}), &err); cerr != nil {
return cerr
}
return
}
func (b *build) Run(ui packer.Ui, cache packer.Cache) ([]packer.Artifact, error) {
// Create and start the server for the UI
server := rpc.NewServer()
RegisterCache(server, cache)
RegisterUi(server, ui)
args := &BuildRunArgs{serveSingleConn(server)}
var result []string
if err := b.client.Call("Build.Run", args, &result); err != nil {
return nil, err
}
artifacts := make([]packer.Artifact, len(result))
for i, addr := range result {
client, err := rpc.Dial("tcp", addr)
if err != nil {
return nil, err
}
artifacts[i] = Artifact(client)
}
return artifacts, nil
}
func (b *build) SetDebug(val bool) {
if err := b.client.Call("Build.SetDebug", val, new(interface{})); err != nil {
panic(err)
}
}
func (b *build) Cancel() {
if err := b.client.Call("Build.Cancel", new(interface{}), new(interface{})); err != nil {
panic(err)
}
}
func (b *BuildServer) Name(args *interface{}, reply *string) error {
*reply = b.build.Name()
return nil
}
func (b *BuildServer) Prepare(args interface{}, reply *error) error {
*reply = b.build.Prepare()
return nil
}
func (b *BuildServer) Run(args *BuildRunArgs, reply *[]string) error {
client, err := rpc.Dial("tcp", args.UiRPCAddress)
if err != nil {
return err
}
artifacts, err := b.build.Run(&Ui{client}, Cache(client))
if err != nil {
return NewBasicError(err)
}
*reply = make([]string, len(artifacts))
for i, artifact := range artifacts {
server := rpc.NewServer()
RegisterArtifact(server, artifact)
(*reply)[i] = serveSingleConn(server)
}
return nil
}
func (b *BuildServer) SetDebug(val *bool, reply *interface{}) error {
b.build.SetDebug(*val)
return nil
}
func (b *BuildServer) Cancel(args *interface{}, reply *interface{}) error {
b.build.Cancel()
return nil
}