forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
166 lines (133 loc) · 3.51 KB
/
builder.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package rpc
import (
"encoding/gob"
"fmt"
"github.com/mitchellh/packer/packer"
"log"
"net/rpc"
)
// An implementation of packer.Builder where the builder is actually executed
// over an RPC connection.
type builder struct {
client *rpc.Client
}
// BuilderServer wraps a packer.Builder implementation and makes it exportable
// as part of a Golang RPC server.
type BuilderServer struct {
builder packer.Builder
}
type BuilderPrepareArgs struct {
Configs []interface{}
}
type BuilderRunArgs struct {
RPCAddress string
ResponseAddress string
}
type BuilderRunResponse struct {
Err error
RPCAddress string
}
func Builder(client *rpc.Client) *builder {
return &builder{client}
}
func (b *builder) Prepare(config ...interface{}) (err error) {
cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &err)
if cerr != nil {
err = cerr
}
return
}
func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
// Create and start the server for the Build and UI
server := rpc.NewServer()
RegisterCache(server, cache)
RegisterHook(server, hook)
RegisterUi(server, ui)
// Create a server for the response
responseL := netListenerInRange(portRangeMin, portRangeMax)
runResponseCh := make(chan *BuilderRunResponse)
go func() {
defer responseL.Close()
var response BuilderRunResponse
defer func() { runResponseCh <- &response }()
conn, err := responseL.Accept()
if err != nil {
response.Err = err
return
}
defer conn.Close()
decoder := gob.NewDecoder(conn)
if err := decoder.Decode(&response); err != nil {
response.Err = fmt.Errorf("Error waiting for Run: %s", err)
}
}()
args := &BuilderRunArgs{
serveSingleConn(server),
responseL.Addr().String(),
}
if err := b.client.Call("Builder.Run", args, new(interface{})); err != nil {
return nil, err
}
response := <-runResponseCh
if response.Err != nil {
return nil, response.Err
}
if response.RPCAddress == "" {
return nil, nil
}
client, err := rpcDial(response.RPCAddress)
if err != nil {
return nil, err
}
return Artifact(client), nil
}
func (b *builder) Cancel() {
if err := b.client.Call("Builder.Cancel", new(interface{}), new(interface{})); err != nil {
log.Printf("Error cancelling builder: %s", err)
}
}
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error {
err := b.builder.Prepare(args.Configs...)
if err != nil {
*reply = NewBasicError(err)
}
return nil
}
func (b *BuilderServer) Run(args *BuilderRunArgs, reply *interface{}) error {
client, err := rpcDial(args.RPCAddress)
if err != nil {
return err
}
responseC, err := tcpDial(args.ResponseAddress)
if err != nil {
return err
}
responseWriter := gob.NewEncoder(responseC)
// Run the build in a goroutine so we don't block the RPC connection
go func() {
defer responseC.Close()
cache := Cache(client)
hook := Hook(client)
ui := &Ui{client}
artifact, responseErr := b.builder.Run(ui, hook, cache)
responseAddress := ""
if responseErr == nil && artifact != nil {
// Wrap the artifact
server := rpc.NewServer()
RegisterArtifact(server, artifact)
responseAddress = serveSingleConn(server)
}
if responseErr != nil {
responseErr = NewBasicError(responseErr)
}
err := responseWriter.Encode(&BuilderRunResponse{responseErr, responseAddress})
if err != nil {
log.Printf("BuildServer.Run error: %s", err)
}
}()
return nil
}
func (b *BuilderServer) Cancel(args *interface{}, reply *interface{}) error {
b.builder.Cancel()
return nil
}