Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builder/virtualbox: group keyboard codes #4305

Merged
merged 3 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions builder/virtualbox/common/step_type_boot_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *StepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction
return multistep.ActionHalt
}

for _, code := range scancodes(command) {
for _, code := range gathercodes(scancodes(command)) {
if code == "wait" {
time.Sleep(1 * time.Second)
continue
Expand All @@ -91,7 +91,9 @@ func (s *StepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction
pauseFn(multistep.DebugLocationAfterRun, fmt.Sprintf("boot_command[%d]: %s", i, command), state)
}

if err := driver.VBoxManage("controlvm", vmName, "keyboardputscancode", code); err != nil {
args := []string{"controlvm", vmName, "keyboardputscancode"}
args = append(args, strings.Split(code, " ")...)
if err := driver.VBoxManage(args...); err != nil {
err := fmt.Errorf("Error sending boot command: %s", err)
state.Put("error", err)
ui.Error(err.Error())
Expand All @@ -105,6 +107,27 @@ func (s *StepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction

func (*StepTypeBootCommand) Cleanup(multistep.StateBag) {}

// Gather scancodes to send to console efficiently.
func gathercodes(codes []string) (gathered []string) {
working := []string{}
pushWorking := func() {
if len(working) > 0 {
gathered = append(gathered, strings.Join(working, " "))
working = []string{}
}
}
for _, code := range codes {
if strings.HasPrefix(code, "wait") {
pushWorking()
gathered = append(gathered, code)
} else {
working = append(working, code)
}
}
pushWorking()
return
}

func scancodes(message string) []string {
// Scancodes reference: http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
//
Expand Down
33 changes: 33 additions & 0 deletions builder/virtualbox/common/step_type_boot_command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package common

import (
"reflect"
"testing"
)

func TestStepTypeBootCommand_gather(t *testing.T) {
input := [][]string{
{"02", "82", "wait1", "03", "83"},
{"02", "82", "03", "83"},
{"wait5", "wait1", "wait10"},
{"wait5", "02", "82", "03", "83", "wait1", "wait10"},
{"wait1"},
{"01"},
}

expected := [][]string{
{"02 82", "wait1", "03 83"},
{"02 82 03 83"},
{"wait5", "wait1", "wait10"},
{"wait5", "02 82 03 83", "wait1", "wait10"},
{"wait1"},
{"01"},
}

for i, data := range input {
gathered := gathercodes(data)
if !reflect.DeepEqual(gathered, expected[i]) {
t.Fatalf("%#v did not equal expected %#v", gathered, expected[i])
}
}
}