2013-05-03 18:49:15 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2013-08-23 17:39:59 -04:00
|
|
|
"log"
|
2018-09-04 11:57:21 -04:00
|
|
|
"math/rand"
|
2013-05-03 18:49:15 -04:00
|
|
|
"net/rpc"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-05-03 18:49:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.Ui where the Ui is actually executed
|
|
|
|
// over an RPC connection.
|
|
|
|
type Ui struct {
|
2013-12-09 18:44:00 -05:00
|
|
|
client *rpc.Client
|
|
|
|
endpoint string
|
2013-05-03 18:49:15 -04:00
|
|
|
}
|
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
var _ packer.Ui = new(Ui)
|
|
|
|
|
2013-05-03 18:49:15 -04:00
|
|
|
// UiServer wraps a packer.Ui implementation and makes it exportable
|
|
|
|
// as part of a Golang RPC server.
|
|
|
|
type UiServer struct {
|
2018-09-04 11:57:21 -04:00
|
|
|
ui packer.Ui
|
|
|
|
register func(name string, rcvr interface{}) error
|
2013-05-03 18:49:15 -04:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:16:00 -04:00
|
|
|
// The arguments sent to Ui.Machine
|
|
|
|
type UiMachineArgs struct {
|
2013-08-12 13:25:56 -04:00
|
|
|
Category string
|
|
|
|
Args []string
|
2013-08-11 21:16:00 -04:00
|
|
|
}
|
|
|
|
|
2013-06-15 21:24:38 -04:00
|
|
|
func (u *Ui) Ask(query string) (result string, err error) {
|
2013-12-09 17:24:55 -05:00
|
|
|
err = u.client.Call("Ui.Ask", query, &result)
|
2013-06-15 21:24:38 -04:00
|
|
|
return
|
2013-06-14 18:17:03 -04:00
|
|
|
}
|
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
func (u *Ui) Error(message string) {
|
2013-12-09 17:24:55 -05:00
|
|
|
if err := u.client.Call("Ui.Error", message, new(interface{})); err != nil {
|
2013-08-23 17:39:59 -04:00
|
|
|
log.Printf("Error in Ui RPC call: %s", err)
|
2013-05-21 14:58:14 -04:00
|
|
|
}
|
2013-05-08 18:12:48 -04:00
|
|
|
}
|
|
|
|
|
2013-08-11 21:16:00 -04:00
|
|
|
func (u *Ui) Machine(t string, args ...string) {
|
|
|
|
rpcArgs := &UiMachineArgs{
|
2013-08-12 13:25:56 -04:00
|
|
|
Category: t,
|
|
|
|
Args: args,
|
2013-08-11 21:16:00 -04:00
|
|
|
}
|
|
|
|
|
2013-12-09 17:24:55 -05:00
|
|
|
if err := u.client.Call("Ui.Machine", rpcArgs, new(interface{})); err != nil {
|
2013-08-23 17:39:59 -04:00
|
|
|
log.Printf("Error in Ui RPC call: %s", err)
|
2013-08-11 21:16:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-03 14:30:38 -04:00
|
|
|
func (u *Ui) Message(message string) {
|
2013-12-09 17:24:55 -05:00
|
|
|
if err := u.client.Call("Ui.Message", message, new(interface{})); err != nil {
|
2013-08-23 17:39:59 -04:00
|
|
|
log.Printf("Error in Ui RPC call: %s", err)
|
2013-06-03 14:30:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
func (u *Ui) Say(message string) {
|
2013-12-09 17:24:55 -05:00
|
|
|
if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil {
|
2013-08-23 17:39:59 -04:00
|
|
|
log.Printf("Error in Ui RPC call: %s", err)
|
2013-05-21 14:58:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 11:57:21 -04:00
|
|
|
func (u *Ui) ProgressBar() packer.ProgressBar {
|
|
|
|
var callMeMaybe string
|
|
|
|
if err := u.client.Call("Ui.ProgressBar", nil, &callMeMaybe); err != nil {
|
|
|
|
log.Printf("Error in Ui RPC call: %s", err)
|
|
|
|
return new(packer.NoopProgressBar)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RemoteProgressBarClient{
|
|
|
|
id: callMeMaybe,
|
|
|
|
client: u.client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoteProgressBarClient struct {
|
|
|
|
id string
|
|
|
|
client *rpc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ packer.ProgressBar = new(RemoteProgressBarClient)
|
|
|
|
|
|
|
|
func (pb *RemoteProgressBarClient) Start(total uint64) {
|
|
|
|
pb.client.Call(pb.id+".Start", total, new(interface{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *RemoteProgressBarClient) Set(current uint64) {
|
|
|
|
pb.client.Call(pb.id+".Set", current, new(interface{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *RemoteProgressBarClient) Finish() {
|
|
|
|
pb.client.Call(pb.id+".Finish", nil, new(interface{}))
|
|
|
|
}
|
|
|
|
|
2013-06-15 21:24:38 -04:00
|
|
|
func (u *UiServer) Ask(query string, reply *string) (err error) {
|
|
|
|
*reply, err = u.ui.Ask(query)
|
|
|
|
return
|
2013-06-14 18:17:03 -04:00
|
|
|
}
|
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
func (u *UiServer) Error(message *string, reply *interface{}) error {
|
|
|
|
u.ui.Error(*message)
|
2013-05-08 18:12:48 -04:00
|
|
|
|
|
|
|
*reply = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-11 21:16:00 -04:00
|
|
|
func (u *UiServer) Machine(args *UiMachineArgs, reply *interface{}) error {
|
2013-08-12 13:25:56 -04:00
|
|
|
u.ui.Machine(args.Category, args.Args...)
|
2013-08-11 21:16:00 -04:00
|
|
|
|
|
|
|
*reply = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-03 14:30:38 -04:00
|
|
|
func (u *UiServer) Message(message *string, reply *interface{}) error {
|
|
|
|
u.ui.Message(*message)
|
|
|
|
*reply = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
func (u *UiServer) Say(message *string, reply *interface{}) error {
|
|
|
|
u.ui.Say(*message)
|
2013-05-03 18:49:15 -04:00
|
|
|
|
|
|
|
*reply = nil
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-04 11:57:21 -04:00
|
|
|
|
|
|
|
func RandStringBytes(n int) string {
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
|
|
|
b := make([]byte, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UiServer) ProgressBar(_ *string, reply *interface{}) error {
|
|
|
|
bar := u.ui.ProgressBar()
|
|
|
|
|
|
|
|
callbackName := RandStringBytes(6)
|
|
|
|
|
|
|
|
log.Printf("registering progressbar %s", callbackName)
|
|
|
|
err := u.register(callbackName, &RemoteProgressBarServer{bar})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to register a new progress bar rpc server, %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*reply = callbackName
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoteProgressBarServer struct {
|
|
|
|
pb packer.ProgressBar
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *RemoteProgressBarServer) Finish(_ string, _ *interface{}) error {
|
|
|
|
pb.pb.Finish()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *RemoteProgressBarServer) Start(total uint64, _ *interface{}) error {
|
|
|
|
pb.pb.Start(total)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *RemoteProgressBarServer) Set(current uint64, _ *interface{}) error {
|
|
|
|
pb.pb.Set(current)
|
|
|
|
return nil
|
|
|
|
}
|