2013-05-03 15:49:15 -07:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2018-09-05 12:50:53 +02:00
|
|
|
"io"
|
2013-08-23 14:39:59 -07:00
|
|
|
"log"
|
2013-05-03 15:49:15 -07:00
|
|
|
"net/rpc"
|
2018-01-22 17:21:10 -08:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-05-03 15:49:15 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.Ui where the Ui is actually executed
|
|
|
|
// over an RPC connection.
|
|
|
|
type Ui struct {
|
2013-12-09 15:44:00 -08:00
|
|
|
client *rpc.Client
|
|
|
|
endpoint string
|
2013-05-03 15:49:15 -07:00
|
|
|
}
|
|
|
|
|
2018-09-04 17:57:21 +02:00
|
|
|
var _ packer.Ui = new(Ui)
|
|
|
|
|
2013-05-03 15:49:15 -07:00
|
|
|
// UiServer wraps a packer.Ui implementation and makes it exportable
|
|
|
|
// as part of a Golang RPC server.
|
|
|
|
type UiServer struct {
|
2018-09-04 17:57:21 +02:00
|
|
|
ui packer.Ui
|
|
|
|
register func(name string, rcvr interface{}) error
|
2013-05-03 15:49:15 -07:00
|
|
|
}
|
|
|
|
|
2013-08-11 18:16:00 -07:00
|
|
|
// The arguments sent to Ui.Machine
|
|
|
|
type UiMachineArgs struct {
|
2013-08-12 10:25:56 -07:00
|
|
|
Category string
|
|
|
|
Args []string
|
2013-08-11 18:16:00 -07:00
|
|
|
}
|
|
|
|
|
2013-06-15 18:24:38 -07:00
|
|
|
func (u *Ui) Ask(query string) (result string, err error) {
|
2013-12-09 14:24:55 -08:00
|
|
|
err = u.client.Call("Ui.Ask", query, &result)
|
2013-06-15 18:24:38 -07:00
|
|
|
return
|
2013-06-14 15:17:03 -07:00
|
|
|
}
|
|
|
|
|
2013-05-27 15:12:48 -07:00
|
|
|
func (u *Ui) Error(message string) {
|
2013-12-09 14:24:55 -08:00
|
|
|
if err := u.client.Call("Ui.Error", message, new(interface{})); err != nil {
|
2013-08-23 14:39:59 -07:00
|
|
|
log.Printf("Error in Ui RPC call: %s", err)
|
2013-05-21 11:58:14 -07:00
|
|
|
}
|
2013-05-08 15:12:48 -07:00
|
|
|
}
|
|
|
|
|
2013-08-11 18:16:00 -07:00
|
|
|
func (u *Ui) Machine(t string, args ...string) {
|
|
|
|
rpcArgs := &UiMachineArgs{
|
2013-08-12 10:25:56 -07:00
|
|
|
Category: t,
|
|
|
|
Args: args,
|
2013-08-11 18:16:00 -07:00
|
|
|
}
|
|
|
|
|
2013-12-09 14:24:55 -08:00
|
|
|
if err := u.client.Call("Ui.Machine", rpcArgs, new(interface{})); err != nil {
|
2013-08-23 14:39:59 -07:00
|
|
|
log.Printf("Error in Ui RPC call: %s", err)
|
2013-08-11 18:16:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-03 11:30:38 -07:00
|
|
|
func (u *Ui) Message(message string) {
|
2013-12-09 14:24:55 -08:00
|
|
|
if err := u.client.Call("Ui.Message", message, new(interface{})); err != nil {
|
2013-08-23 14:39:59 -07:00
|
|
|
log.Printf("Error in Ui RPC call: %s", err)
|
2013-06-03 11:30:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 15:12:48 -07:00
|
|
|
func (u *Ui) Say(message string) {
|
2013-12-09 14:24:55 -08:00
|
|
|
if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil {
|
2013-08-23 14:39:59 -07:00
|
|
|
log.Printf("Error in Ui RPC call: %s", err)
|
2013-05-21 11:58:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:21:58 -07:00
|
|
|
func (u *Ui) ProgressBar(identifier string) packer.ProgressBar {
|
|
|
|
if err := u.client.Call("Ui.ProgressBar", identifier, new(interface{})); err != nil {
|
|
|
|
log.Printf("Err or in Ui RPC call: %s", err)
|
|
|
|
}
|
|
|
|
return &RemoteProgressBarClient{
|
|
|
|
id: identifier,
|
|
|
|
client: u.client,
|
2018-09-04 17:57:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:21:58 -07:00
|
|
|
type RemoteProgressBarClient struct {
|
|
|
|
id string
|
|
|
|
client *rpc.Client
|
|
|
|
}
|
2018-09-04 17:57:21 +02:00
|
|
|
|
2018-09-12 16:21:58 -07:00
|
|
|
var _ packer.ProgressBar = new(RemoteProgressBarClient)
|
|
|
|
|
|
|
|
func (pb *RemoteProgressBarClient) Start(total int64) {
|
|
|
|
pb.client.Call(pb.id+".Start", total, new(interface{}))
|
2018-09-04 17:57:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 16:21:58 -07:00
|
|
|
func (pb *RemoteProgressBarClient) Add(current int64) {
|
|
|
|
pb.client.Call(pb.id+".Add", current, new(interface{}))
|
2018-09-04 17:57:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 16:21:58 -07:00
|
|
|
func (pb *RemoteProgressBarClient) Finish() {
|
|
|
|
pb.client.Call(pb.id+".Finish", nil, new(interface{}))
|
2018-09-04 17:57:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 16:21:58 -07:00
|
|
|
func (pb *RemoteProgressBarClient) NewProxyReader(r io.Reader) io.Reader {
|
2018-09-05 12:50:53 +02:00
|
|
|
return &packer.ProxyReader{Reader: r, ProgressBar: pb}
|
|
|
|
}
|
|
|
|
|
2013-06-15 18:24:38 -07:00
|
|
|
func (u *UiServer) Ask(query string, reply *string) (err error) {
|
|
|
|
*reply, err = u.ui.Ask(query)
|
|
|
|
return
|
2013-06-14 15:17:03 -07:00
|
|
|
}
|
|
|
|
|
2013-05-27 15:12:48 -07:00
|
|
|
func (u *UiServer) Error(message *string, reply *interface{}) error {
|
|
|
|
u.ui.Error(*message)
|
2013-05-08 15:12:48 -07:00
|
|
|
|
|
|
|
*reply = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-11 18:16:00 -07:00
|
|
|
func (u *UiServer) Machine(args *UiMachineArgs, reply *interface{}) error {
|
2013-08-12 10:25:56 -07:00
|
|
|
u.ui.Machine(args.Category, args.Args...)
|
2013-08-11 18:16:00 -07:00
|
|
|
|
|
|
|
*reply = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-03 11:30:38 -07:00
|
|
|
func (u *UiServer) Message(message *string, reply *interface{}) error {
|
|
|
|
u.ui.Message(*message)
|
|
|
|
*reply = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-27 15:12:48 -07:00
|
|
|
func (u *UiServer) Say(message *string, reply *interface{}) error {
|
|
|
|
u.ui.Say(*message)
|
2013-05-03 15:49:15 -07:00
|
|
|
|
|
|
|
*reply = nil
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-04 17:57:21 +02:00
|
|
|
|
2018-09-12 16:21:58 -07:00
|
|
|
// ProgressBar registers a rpc progress bar server identified by identifier.
|
|
|
|
// ProgressBar expects identifiers to be unique across runs
|
|
|
|
// since for examples an iso download should be cached.
|
|
|
|
func (u *UiServer) ProgressBar(identifier string, reply *interface{}) error {
|
|
|
|
|
|
|
|
bar := u.ui.ProgressBar(identifier)
|
|
|
|
log.Printf("registering progressbar for '%s'", identifier)
|
|
|
|
err := u.register(identifier, &UiProgressBarServer{bar})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to register a new progress bar rpc server, %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*reply = identifier
|
|
|
|
|
2018-09-04 17:57:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:21:58 -07:00
|
|
|
type UiProgressBarServer struct {
|
|
|
|
bar packer.ProgressBar
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *UiProgressBarServer) Finish(_ string, _ *interface{}) error {
|
|
|
|
pb.bar.Finish()
|
2018-09-04 17:57:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:21:58 -07:00
|
|
|
func (pb *UiProgressBarServer) Start(total int64, _ *interface{}) error {
|
|
|
|
pb.bar.Start(total)
|
2018-09-04 17:57:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:21:58 -07:00
|
|
|
func (pb *UiProgressBarServer) Add(current int64, _ *interface{}) error {
|
|
|
|
pb.bar.Add(current)
|
2018-09-04 17:57:21 +02:00
|
|
|
return nil
|
|
|
|
}
|