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
9219a19f
Commit
9219a19f
authored
May 07, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer/plugin: Better error handling around command exit cases
parent
ff23b679
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
11 deletions
+56
-11
packer/plugin/command.go
packer/plugin/command.go
+21
-10
packer/plugin/command_test.go
packer/plugin/command_test.go
+7
-0
packer/plugin/plugin_test.go
packer/plugin/plugin_test.go
+28
-1
No files found.
packer/plugin/command.go
View file @
9219a19f
...
...
@@ -2,6 +2,7 @@ package plugin
import
(
"bytes"
"errors"
"github.com/mitchellh/packer/packer"
"net/rpc"
"os/exec"
...
...
@@ -24,17 +25,27 @@ func Command(cmd *exec.Cmd) (result packer.Command, err error) {
return
}
// TODO: timeout
// TODO: check that command is even running
address
:=
""
for
{
line
,
err
:=
out
.
ReadBytes
(
'\n'
)
if
err
==
nil
{
address
=
strings
.
TrimSpace
(
string
(
line
))
break
}
cmdExited
:=
make
(
chan
bool
)
go
func
()
{
cmd
.
Wait
()
cmdExited
<-
true
}()
var
address
string
for
done
:=
false
;
!
done
;
{
select
{
case
<-
cmdExited
:
err
=
errors
.
New
(
"plugin exited before we could connect"
)
return
case
<-
time
.
After
(
10
*
time
.
Millisecond
)
:
if
line
,
err
:=
out
.
ReadBytes
(
'\n'
);
err
==
nil
{
address
=
strings
.
TrimSpace
(
string
(
line
))
done
=
true
}
time
.
Sleep
(
10
*
time
.
Millisecond
)
// Make sure to reset err to nil
err
=
nil
}
}
client
,
err
:=
rpc
.
Dial
(
"tcp"
,
address
)
...
...
packer/plugin/command_test.go
View file @
9219a19f
...
...
@@ -22,3 +22,10 @@ func TestCommand_Good(t *testing.T) {
result
:=
command
.
Synopsis
()
assert
.
Equal
(
result
,
"1"
,
"should return result"
)
}
func
TestCommand_CommandExited
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
_
,
err
:=
Command
(
helperProcess
(
"im-a-command-that-doesnt-work"
))
assert
.
NotNil
(
err
,
"should have an error"
)
}
packer/plugin/plugin_test.go
View file @
9219a19f
package
plugin
import
(
"fmt"
"github.com/mitchellh/packer/packer"
"os"
"os/exec"
"testing"
"time"
)
type
helperCommand
byte
...
...
@@ -38,5 +40,30 @@ func TestHelperProcess(*testing.T) {
return
}
ServeCommand
(
new
(
helperCommand
))
args
:=
os
.
Args
for
len
(
args
)
>
0
{
if
args
[
0
]
==
"--"
{
args
=
args
[
1
:
]
break
}
args
=
args
[
1
:
]
}
if
len
(
args
)
==
0
{
fmt
.
Fprintf
(
os
.
Stderr
,
"No command
\n
"
)
os
.
Exit
(
2
)
}
cmd
,
args
:=
args
[
0
],
args
[
1
:
]
switch
cmd
{
case
"command"
:
ServeCommand
(
new
(
helperCommand
))
case
"start-timeout"
:
time
.
Sleep
(
1
*
time
.
Minute
)
os
.
Exit
(
1
)
default
:
fmt
.
Fprintf
(
os
.
Stderr
,
"Unknown command: %q
\n
"
,
cmd
)
os
.
Exit
(
2
)
}
}
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