Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
559777e5
Commit
559777e5
authored
May 20, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: Ui automatically appends newline
parent
2ee8859a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
31 deletions
+33
-31
builder/amazonebs/builder.go
builder/amazonebs/builder.go
+23
-23
packer/ui.go
packer/ui.go
+6
-4
packer/ui_test.go
packer/ui_test.go
+4
-4
No files found.
builder/amazonebs/builder.go
View file @
559777e5
...
@@ -45,7 +45,7 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
...
@@ -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) {
...
@@ -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) {
...
@@ -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) {
...
@@ -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) {
...
@@ -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) {
...
@@ -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) {
...
@@ -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) {
...
@@ -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
...
...
packer/ui.go
View file @
559777e5
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 {
...
@@ -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
)
}
}
...
...
packer/ui_test.go
View file @
559777e5
...
@@ -19,10 +19,10 @@ func TestReaderWriterUi_Error(t *testing.T) {
...
@@ -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) {
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment