packer: Ui automatically appends newline

This commit is contained in:
Mitchell Hashimoto 2013-05-20 23:43:37 -07:00
parent 2ee8859ac0
commit 559777e5b7
3 changed files with 33 additions and 31 deletions

View File

@ -45,7 +45,7 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
return
}
log.Printf("Config: %+v\n", b.config)
log.Printf("Config: %+v", b.config)
// TODO: Validate the configuration
return
@ -58,17 +58,17 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
// Create a new keypair that we'll use to access the instance.
keyName := fmt.Sprintf("packer %s", hex.EncodeToString(identifier.NewUUID().Raw()))
ui.Say("Creating temporary keypair for this instance...\n")
log.Printf("temporary keypair name: %s\n", keyName)
ui.Say("Creating temporary keypair for this instance...")
log.Printf("temporary keypair name: %s", keyName)
keyResp, err := ec2conn.CreateKeyPair(keyName)
if err != nil {
ui.Error("%s\n", err.Error())
ui.Error(err.Error())
return
}
// Make sure the keypair is properly deleted when we exit
defer func() {
ui.Say("Deleting temporary keypair...\n")
ui.Say("Deleting temporary keypair...")
_, err := ec2conn.DeleteKeyPair(keyName)
if err != nil {
ui.Error(
@ -84,27 +84,27 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
MaxCount: 0,
}
ui.Say("Launching a source AWS instance...\n")
ui.Say("Launching a source AWS instance...")
runResp, err := ec2conn.RunInstances(runOpts)
if err != nil {
ui.Error("%s\n", err.Error())
ui.Error(err.Error())
return
}
instance := &runResp.Instances[0]
log.Printf("instance id: %s\n", instance.InstanceId)
log.Printf("instance id: %s", instance.InstanceId)
// Make sure we clean up the instance by terminating it, no matter what
defer func() {
// TODO: error handling
ui.Say("Terminating the source AWS instance...\n")
ui.Say("Terminating the source AWS instance...")
ec2conn.TerminateInstances([]string{instance.InstanceId})
}()
ui.Say("Waiting for instance to become ready...\n")
ui.Say("Waiting for instance to become ready...")
instance, err = waitForState(ec2conn, instance, "running")
if err != nil {
ui.Error("%s\n", err.Error())
ui.Error(err.Error())
return
}
@ -112,7 +112,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
keyring := &ssh.SimpleKeychain{}
err = keyring.AddPEMKey(keyResp.KeyMaterial)
if err != nil {
ui.Say("Error setting up SSH config: %s\n", err.Error())
ui.Say("Error setting up SSH config: %s", err.Error())
return
}
@ -144,7 +144,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
}
if err != nil {
ui.Error("Error connecting to SSH: %s\n", err.Error())
ui.Error("Error connecting to SSH: %s", err.Error())
return
}
@ -159,27 +159,27 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
bufr := bufio.NewReader(remote.Stdout)
line, _ := bufr.ReadString('\n')
ui.Say("%s\n", line)
ui.Say(line)
// Stop the instance so we can create an AMI from it
_, err = ec2conn.StopInstances(instance.InstanceId)
if err != nil {
ui.Error("%s\n", err.Error())
ui.Error(err.Error())
return
}
// Wait for the instance to actual stop
// TODO: Handle diff source states, i.e. this force state sucks
ui.Say("Waiting for the instance to stop...\n")
ui.Say("Waiting for the instance to stop...")
instance.State.Name = "stopping"
instance, err = waitForState(ec2conn, instance, "stopped")
if err != nil {
ui.Error("%s\n", err.Error())
ui.Error(err.Error())
return
}
// Create the image
ui.Say("Creating the AMI...\n")
ui.Say("Creating the AMI...")
createOpts := &ec2.CreateImage{
InstanceId: instance.InstanceId,
Name: b.config.AMIName,
@ -187,18 +187,18 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
createResp, err := ec2conn.CreateImage(createOpts)
if err != nil {
ui.Error("%s\n", err.Error())
ui.Error(err.Error())
return
}
ui.Say("AMI: %s\n", createResp.ImageId)
ui.Say("AMI: %s", createResp.ImageId)
// Wait for the image to become ready
ui.Say("Waiting for AMI to become ready...\n")
ui.Say("Waiting for AMI to become ready...")
for {
imageResp, err := ec2conn.Images([]string{createResp.ImageId}, ec2.NewFilter())
if err != nil {
ui.Error("%s\n", err.Error())
ui.Error(err.Error())
return
}
@ -209,7 +209,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
}
func waitForState(ec2conn *ec2.EC2, originalInstance *ec2.Instance, target string) (i *ec2.Instance, err error) {
log.Printf("Waiting for instance state to become: %s\n", target)
log.Printf("Waiting for instance state to become: %s", target)
i = originalInstance
original := i.State.Name

View File

@ -1,7 +1,9 @@
package packer
import "fmt"
import "io"
import (
"fmt"
"io"
)
// The Ui interface handles all communication for Packer with the outside
// world. This sort of control allows us to strictly control how output
@ -19,14 +21,14 @@ type ReaderWriterUi struct {
}
func (rw *ReaderWriterUi) Say(format string, a ...interface{}) {
_, err := fmt.Fprintf(rw.Writer, format, a...)
_, err := fmt.Fprintf(rw.Writer, format + "\n", a...)
if err != nil {
panic(err)
}
}
func (rw *ReaderWriterUi) Error(format string, a ...interface{}) {
_, err := fmt.Fprintf(rw.Writer, format, a...)
_, err := fmt.Fprintf(rw.Writer, format + "\n", a...)
if err != nil {
panic(err)
}

View File

@ -19,10 +19,10 @@ func TestReaderWriterUi_Error(t *testing.T) {
bufferUi := testUi()
bufferUi.Error("foo")
assert.Equal(readWriter(bufferUi), "foo", "basic output")
assert.Equal(readWriter(bufferUi), "foo\n", "basic output")
bufferUi.Error("%d", 5)
assert.Equal(readWriter(bufferUi), "5", "formatting")
assert.Equal(readWriter(bufferUi), "5\n", "formatting")
}
func TestReaderWriterUi_Say(t *testing.T) {
@ -31,10 +31,10 @@ func TestReaderWriterUi_Say(t *testing.T) {
bufferUi := testUi()
bufferUi.Say("foo")
assert.Equal(readWriter(bufferUi), "foo", "basic output")
assert.Equal(readWriter(bufferUi), "foo\n", "basic output")
bufferUi.Say("%d", 5)
assert.Equal(readWriter(bufferUi), "5", "formatting")
assert.Equal(readWriter(bufferUi), "5\n", "formatting")
}
// This reads the output from the bytes.Buffer in our test object