packer: Ui automatically appends newline
This commit is contained in:
parent
2ee8859ac0
commit
559777e5b7
|
@ -45,7 +45,7 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Config: %+v\n", b.config)
|
log.Printf("Config: %+v", b.config)
|
||||||
|
|
||||||
// TODO: Validate the configuration
|
// TODO: Validate the configuration
|
||||||
return
|
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.
|
// Create a new keypair that we'll use to access the instance.
|
||||||
keyName := fmt.Sprintf("packer %s", hex.EncodeToString(identifier.NewUUID().Raw()))
|
keyName := fmt.Sprintf("packer %s", hex.EncodeToString(identifier.NewUUID().Raw()))
|
||||||
ui.Say("Creating temporary keypair for this instance...\n")
|
ui.Say("Creating temporary keypair for this instance...")
|
||||||
log.Printf("temporary keypair name: %s\n", keyName)
|
log.Printf("temporary keypair name: %s", keyName)
|
||||||
keyResp, err := ec2conn.CreateKeyPair(keyName)
|
keyResp, err := ec2conn.CreateKeyPair(keyName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error("%s\n", err.Error())
|
ui.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the keypair is properly deleted when we exit
|
// Make sure the keypair is properly deleted when we exit
|
||||||
defer func() {
|
defer func() {
|
||||||
ui.Say("Deleting temporary keypair...\n")
|
ui.Say("Deleting temporary keypair...")
|
||||||
_, err := ec2conn.DeleteKeyPair(keyName)
|
_, err := ec2conn.DeleteKeyPair(keyName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error(
|
ui.Error(
|
||||||
|
@ -84,27 +84,27 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
|
||||||
MaxCount: 0,
|
MaxCount: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Say("Launching a source AWS instance...\n")
|
ui.Say("Launching a source AWS instance...")
|
||||||
runResp, err := ec2conn.RunInstances(runOpts)
|
runResp, err := ec2conn.RunInstances(runOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error("%s\n", err.Error())
|
ui.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
instance := &runResp.Instances[0]
|
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
|
// Make sure we clean up the instance by terminating it, no matter what
|
||||||
defer func() {
|
defer func() {
|
||||||
// TODO: error handling
|
// TODO: error handling
|
||||||
ui.Say("Terminating the source AWS instance...\n")
|
ui.Say("Terminating the source AWS instance...")
|
||||||
ec2conn.TerminateInstances([]string{instance.InstanceId})
|
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")
|
instance, err = waitForState(ec2conn, instance, "running")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error("%s\n", err.Error())
|
ui.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
|
||||||
keyring := &ssh.SimpleKeychain{}
|
keyring := &ssh.SimpleKeychain{}
|
||||||
err = keyring.AddPEMKey(keyResp.KeyMaterial)
|
err = keyring.AddPEMKey(keyResp.KeyMaterial)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +144,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error("Error connecting to SSH: %s\n", err.Error())
|
ui.Error("Error connecting to SSH: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,27 +159,27 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
|
||||||
|
|
||||||
bufr := bufio.NewReader(remote.Stdout)
|
bufr := bufio.NewReader(remote.Stdout)
|
||||||
line, _ := bufr.ReadString('\n')
|
line, _ := bufr.ReadString('\n')
|
||||||
ui.Say("%s\n", line)
|
ui.Say(line)
|
||||||
|
|
||||||
// Stop the instance so we can create an AMI from it
|
// Stop the instance so we can create an AMI from it
|
||||||
_, err = ec2conn.StopInstances(instance.InstanceId)
|
_, err = ec2conn.StopInstances(instance.InstanceId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error("%s\n", err.Error())
|
ui.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for the instance to actual stop
|
// Wait for the instance to actual stop
|
||||||
// TODO: Handle diff source states, i.e. this force state sucks
|
// 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.State.Name = "stopping"
|
||||||
instance, err = waitForState(ec2conn, instance, "stopped")
|
instance, err = waitForState(ec2conn, instance, "stopped")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error("%s\n", err.Error())
|
ui.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the image
|
// Create the image
|
||||||
ui.Say("Creating the AMI...\n")
|
ui.Say("Creating the AMI...")
|
||||||
createOpts := &ec2.CreateImage{
|
createOpts := &ec2.CreateImage{
|
||||||
InstanceId: instance.InstanceId,
|
InstanceId: instance.InstanceId,
|
||||||
Name: b.config.AMIName,
|
Name: b.config.AMIName,
|
||||||
|
@ -187,18 +187,18 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook) {
|
||||||
|
|
||||||
createResp, err := ec2conn.CreateImage(createOpts)
|
createResp, err := ec2conn.CreateImage(createOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error("%s\n", err.Error())
|
ui.Error(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Say("AMI: %s\n", createResp.ImageId)
|
ui.Say("AMI: %s", createResp.ImageId)
|
||||||
|
|
||||||
// Wait for the image to become ready
|
// 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 {
|
for {
|
||||||
imageResp, err := ec2conn.Images([]string{createResp.ImageId}, ec2.NewFilter())
|
imageResp, err := ec2conn.Images([]string{createResp.ImageId}, ec2.NewFilter())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error("%s\n", err.Error())
|
ui.Error(err.Error())
|
||||||
return
|
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) {
|
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
|
i = originalInstance
|
||||||
original := i.State.Name
|
original := i.State.Name
|
||||||
|
|
10
packer/ui.go
10
packer/ui.go
|
@ -1,7 +1,9 @@
|
||||||
package packer
|
package packer
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
import "io"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
// The Ui interface handles all communication for Packer with the outside
|
// The Ui interface handles all communication for Packer with the outside
|
||||||
// world. This sort of control allows us to strictly control how output
|
// 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{}) {
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rw *ReaderWriterUi) Error(format string, a ...interface{}) {
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,10 +19,10 @@ func TestReaderWriterUi_Error(t *testing.T) {
|
||||||
bufferUi := testUi()
|
bufferUi := testUi()
|
||||||
|
|
||||||
bufferUi.Error("foo")
|
bufferUi.Error("foo")
|
||||||
assert.Equal(readWriter(bufferUi), "foo", "basic output")
|
assert.Equal(readWriter(bufferUi), "foo\n", "basic output")
|
||||||
|
|
||||||
bufferUi.Error("%d", 5)
|
bufferUi.Error("%d", 5)
|
||||||
assert.Equal(readWriter(bufferUi), "5", "formatting")
|
assert.Equal(readWriter(bufferUi), "5\n", "formatting")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReaderWriterUi_Say(t *testing.T) {
|
func TestReaderWriterUi_Say(t *testing.T) {
|
||||||
|
@ -31,10 +31,10 @@ func TestReaderWriterUi_Say(t *testing.T) {
|
||||||
bufferUi := testUi()
|
bufferUi := testUi()
|
||||||
|
|
||||||
bufferUi.Say("foo")
|
bufferUi.Say("foo")
|
||||||
assert.Equal(readWriter(bufferUi), "foo", "basic output")
|
assert.Equal(readWriter(bufferUi), "foo\n", "basic output")
|
||||||
|
|
||||||
bufferUi.Say("%d", 5)
|
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
|
// This reads the output from the bytes.Buffer in our test object
|
||||||
|
|
Loading…
Reference in New Issue