Commit 8a32494e authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Start working on logging across the board

parent 17188f07
...@@ -5,7 +5,6 @@ import "github.com/mitchellh/packer/packer" ...@@ -5,7 +5,6 @@ import "github.com/mitchellh/packer/packer"
type Command byte type Command byte
func (Command) Run(env packer.Environment, arg []string) int { func (Command) Run(env packer.Environment, arg []string) int {
env.Ui().Say("BUILDING!")
return 0 return 0
} }
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer/plugin" "github.com/mitchellh/packer/packer/plugin"
"fmt" "fmt"
"log"
"os" "os"
"os/exec" "os/exec"
) )
...@@ -22,8 +23,10 @@ func main() { ...@@ -22,8 +23,10 @@ func main() {
envConfig := packer.DefaultEnvironmentConfig() envConfig := packer.DefaultEnvironmentConfig()
envConfig.Commands = commandKeys envConfig.Commands = commandKeys
envConfig.CommandFunc = func(n string) (packer.Command, error) { envConfig.CommandFunc = func(n string) (packer.Command, error) {
log.Printf("Loading command: %s\n", n)
commandBin, ok := commands[n] commandBin, ok := commands[n]
if !ok { if !ok {
log.Printf("Command not found: %s\n", n)
return nil, nil return nil, nil
} }
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log"
"net/rpc" "net/rpc"
"os/exec" "os/exec"
packrpc "github.com/mitchellh/packer/packer/rpc" packrpc "github.com/mitchellh/packer/packer/rpc"
...@@ -25,19 +26,23 @@ func Command(cmd *exec.Cmd) (result packer.Command, err error) { ...@@ -25,19 +26,23 @@ func Command(cmd *exec.Cmd) (result packer.Command, err error) {
"PACKER_PLUGIN_MAX_PORT=25000", "PACKER_PLUGIN_MAX_PORT=25000",
} }
out := new(bytes.Buffer) stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
cmd.Env = append(cmd.Env, env...) cmd.Env = append(cmd.Env, env...)
cmd.Stdout = out cmd.Stderr = stderr
cmd.Stdout = stdout
err = cmd.Start() err = cmd.Start()
if err != nil { if err != nil {
return return
} }
// Make sure the command is properly cleaned up in the case of
// an error.
defer func() { defer func() {
// Make sure the command is properly killed in the case of an error
if err != nil { if err != nil {
cmd.Process.Kill() cmd.Process.Kill()
// Log the stderr, which should include any logs from the subprocess
log.Print(stderr.String())
} }
}() }()
...@@ -63,7 +68,7 @@ func Command(cmd *exec.Cmd) (result packer.Command, err error) { ...@@ -63,7 +68,7 @@ func Command(cmd *exec.Cmd) (result packer.Command, err error) {
default: default:
} }
if line, lerr := out.ReadBytes('\n'); lerr == nil { if line, lerr := stdout.ReadBytes('\n'); lerr == nil {
// Trim the address and reset the err since we were able // Trim the address and reset the err since we were able
// to read some sort of address. // to read some sort of address.
address = strings.TrimSpace(string(line)) address = strings.TrimSpace(string(line))
......
...@@ -10,6 +10,7 @@ package plugin ...@@ -10,6 +10,7 @@ package plugin
import ( import (
"fmt" "fmt"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"log"
"net" "net"
"net/rpc" "net/rpc"
"os" "os"
...@@ -30,6 +31,9 @@ func serve(server *rpc.Server) (err error) { ...@@ -30,6 +31,9 @@ func serve(server *rpc.Server) (err error) {
return return
} }
log.Printf("Plugin minimum port: %d\n", minPort)
log.Printf("Plugin maximum port: %d\n", minPort)
var address string var address string
var listener net.Listener var listener net.Listener
for port := minPort; port <= maxPort; port++ { for port := minPort; port <= maxPort; port++ {
...@@ -45,26 +49,34 @@ func serve(server *rpc.Server) (err error) { ...@@ -45,26 +49,34 @@ func serve(server *rpc.Server) (err error) {
defer listener.Close() defer listener.Close()
// Output the address to stdout // Output the address to stdout
log.Printf("Plugin address: %s\n", address)
fmt.Println(address) fmt.Println(address)
os.Stdout.Sync() os.Stdout.Sync()
// Accept a connection // Accept a connection
log.Println("Waiting for connection...")
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
log.Printf("Error accepting connection: %s\n", err.Error())
return return
} }
// Serve a single connection // Serve a single connection
log.Println("Serving a plugin connection...")
server.ServeConn(conn) server.ServeConn(conn)
return return
} }
// Serves a command from a plugin. // Serves a command from a plugin.
func ServeCommand(command packer.Command) { func ServeCommand(command packer.Command) {
log.Println("Preparing to serve a command plugin...")
server := rpc.NewServer() server := rpc.NewServer()
packrpc.RegisterCommand(server, command) packrpc.RegisterCommand(server, command)
if err := serve(server); err != nil { if err := serve(server); err != nil {
panic(err) log.Panic(err)
} }
log.Println("Command successfully served. Exiting.")
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment