2015-08-22 18:41:08 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step configures the VM to enable the VRDP server
|
|
|
|
// on the guest machine.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// driver Driver
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// vrdp_port unit - The port that VRDP is configured to listen on.
|
|
|
|
type StepConfigureVRDP struct {
|
2016-05-23 09:24:16 -04:00
|
|
|
VRDPBindAddress string
|
|
|
|
VRDPPortMin uint
|
|
|
|
VRDPPortMax uint
|
2015-08-22 18:41:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepConfigureVRDP) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
2016-05-23 09:24:16 -04:00
|
|
|
log.Printf("Looking for available port between %d and %d on %s", s.VRDPPortMin, s.VRDPPortMax, s.VRDPBindAddress)
|
2015-08-22 18:41:08 -04:00
|
|
|
var vrdpPort uint
|
2016-02-12 02:53:40 -05:00
|
|
|
portRange := int(s.VRDPPortMax - s.VRDPPortMin)
|
2015-08-22 18:41:08 -04:00
|
|
|
|
|
|
|
for {
|
|
|
|
if portRange > 0 {
|
|
|
|
vrdpPort = uint(rand.Intn(portRange)) + s.VRDPPortMin
|
|
|
|
} else {
|
|
|
|
vrdpPort = s.VRDPPortMin
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Trying port: %d", vrdpPort)
|
2016-05-23 09:24:16 -04:00
|
|
|
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.VRDPBindAddress, vrdpPort))
|
2015-08-22 18:41:08 -04:00
|
|
|
if err == nil {
|
|
|
|
defer l.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"modifyvm", vmName,
|
2016-05-23 09:24:16 -04:00
|
|
|
"--vrdeaddress", fmt.Sprintf("%s", s.VRDPBindAddress),
|
2015-08-22 18:41:08 -04:00
|
|
|
"--vrdeauthtype", "null",
|
|
|
|
"--vrde", "on",
|
|
|
|
"--vrdeport",
|
|
|
|
fmt.Sprintf("%d", vrdpPort),
|
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
|
|
|
err := fmt.Errorf("Error enabling VRDP: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2016-05-23 09:24:16 -04:00
|
|
|
state.Put("vrdpIp", s.VRDPBindAddress)
|
2015-08-22 18:41:08 -04:00
|
|
|
state.Put("vrdpPort", vrdpPort)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepConfigureVRDP) Cleanup(state multistep.StateBag) {}
|