2013-05-04 16:47:11 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2013-06-03 18:00:35 -04:00
|
|
|
"log"
|
2013-05-04 16:47:11 -04:00
|
|
|
"net/rpc"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-05-04 16:47:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.Builder where the builder is actually executed
|
|
|
|
// over an RPC connection.
|
2013-05-09 00:05:35 -04:00
|
|
|
type builder struct {
|
2013-05-04 16:47:11 -04:00
|
|
|
client *rpc.Client
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuilderServer wraps a packer.Builder implementation and makes it exportable
|
|
|
|
// as part of a Golang RPC server.
|
|
|
|
type BuilderServer struct {
|
|
|
|
builder packer.Builder
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type BuilderPrepareArgs struct {
|
2013-06-14 15:27:50 -04:00
|
|
|
Configs []interface{}
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-11-02 23:40:06 -04:00
|
|
|
type BuilderPrepareResponse struct {
|
|
|
|
Warnings []string
|
2014-04-26 16:31:22 -04:00
|
|
|
Error *BasicError
|
2013-11-02 23:40:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) Prepare(config ...interface{}) ([]string, error) {
|
|
|
|
var resp BuilderPrepareResponse
|
|
|
|
cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &resp)
|
2013-05-09 16:59:33 -04:00
|
|
|
if cerr != nil {
|
2013-11-02 23:40:06 -04:00
|
|
|
return nil, cerr
|
2013-05-09 16:59:33 -04:00
|
|
|
}
|
2014-04-26 16:31:22 -04:00
|
|
|
var err error = nil
|
|
|
|
if resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
}
|
2013-05-09 16:59:33 -04:00
|
|
|
|
2014-04-26 16:31:22 -04:00
|
|
|
return resp.Warnings, err
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
func (b *builder) Run(ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2013-12-10 15:14:08 -05:00
|
|
|
nextId := b.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(b.mux, nextId)
|
2013-12-10 15:14:08 -05:00
|
|
|
server.RegisterHook(hook)
|
|
|
|
server.RegisterUi(ui)
|
|
|
|
go server.Serve()
|
|
|
|
|
|
|
|
var responseId uint32
|
|
|
|
if err := b.client.Call("Builder.Run", nextId, &responseId); err != nil {
|
2013-06-12 18:58:02 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-12-10 15:14:08 -05:00
|
|
|
if responseId == 0 {
|
2013-06-12 18:58:02 -04:00
|
|
|
return nil, nil
|
2013-06-05 18:36:26 -04:00
|
|
|
}
|
|
|
|
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(b.mux, responseId)
|
2013-05-22 01:13:29 -04:00
|
|
|
if err != nil {
|
2013-06-12 18:58:02 -04:00
|
|
|
return nil, err
|
2013-05-22 01:13:29 -04:00
|
|
|
}
|
|
|
|
|
2013-12-10 15:14:08 -05:00
|
|
|
return client.Artifact(), nil
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 17:44:34 -04:00
|
|
|
func (b *builder) Cancel() {
|
2013-06-03 18:30:09 -04:00
|
|
|
if err := b.client.Call("Builder.Cancel", new(interface{}), new(interface{})); err != nil {
|
2013-08-23 16:58:00 -04:00
|
|
|
log.Printf("Error cancelling builder: %s", err)
|
2013-06-03 18:30:09 -04:00
|
|
|
}
|
2013-06-03 17:44:34 -04:00
|
|
|
}
|
|
|
|
|
2013-11-02 23:40:06 -04:00
|
|
|
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *BuilderPrepareResponse) error {
|
|
|
|
warnings, err := b.builder.Prepare(args.Configs...)
|
|
|
|
*reply = BuilderPrepareResponse{
|
|
|
|
Warnings: warnings,
|
2014-04-26 16:31:22 -04:00
|
|
|
Error: NewBasicError(err),
|
2013-11-02 23:40:06 -04:00
|
|
|
}
|
2013-05-04 16:47:11 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-10 15:14:08 -05:00
|
|
|
func (b *BuilderServer) Run(streamId uint32, reply *uint32) error {
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(b.mux, streamId)
|
2013-05-04 16:47:11 -04:00
|
|
|
if err != nil {
|
2013-12-10 15:14:08 -05:00
|
|
|
return NewBasicError(err)
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
2013-12-10 15:14:08 -05:00
|
|
|
defer client.Close()
|
2013-05-04 16:47:11 -04:00
|
|
|
|
Use the hashicorp/go-getter to download files
* removed packer.Cache and references since packer.Cache is never used except in the download step. The download step now uses the new func packer.CachePath(targetPath) for this, the behavior is the same.
* removed download code from packer that was reimplemented into the go-getter library: progress bar, http download restart, checksuming from file, skip already downloaded files, symlinking, make a download cancellable by context.
* on windows if packer is running without symlinking rights and we are getting a local file, the file will be copied instead to avoid errors.
* added unit tests for step_download that are now CI tested on windows, mac & linux.
* files are now downloaded under cache dir `sha1(filename + "?checksum=" + checksum) + file_extension`
* since the output dir is based on the source url and the checksum, when the checksum fails, the file is auto deleted.
* a download file is protected and locked by a file lock,
* updated docs
* updated go modules and vendors
2019-03-13 07:11:58 -04:00
|
|
|
artifact, err := b.builder.Run(client.Ui(), client.Hook())
|
2013-06-03 18:00:35 -04:00
|
|
|
if err != nil {
|
2013-12-10 15:14:08 -05:00
|
|
|
return NewBasicError(err)
|
2013-06-03 18:00:35 -04:00
|
|
|
}
|
2013-05-22 01:13:29 -04:00
|
|
|
|
2013-12-10 15:14:08 -05:00
|
|
|
*reply = 0
|
|
|
|
if artifact != nil {
|
|
|
|
streamId = b.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(b.mux, streamId)
|
2013-12-10 15:14:08 -05:00
|
|
|
server.RegisterArtifact(artifact)
|
|
|
|
go server.Serve()
|
|
|
|
*reply = streamId
|
|
|
|
}
|
2013-05-04 16:47:11 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2013-06-03 18:30:09 -04:00
|
|
|
|
2013-06-04 01:32:04 -04:00
|
|
|
func (b *BuilderServer) Cancel(args *interface{}, reply *interface{}) error {
|
2013-06-03 18:30:09 -04:00
|
|
|
b.builder.Cancel()
|
|
|
|
return nil
|
|
|
|
}
|