66
77 "github.com/mitchellh/multistep"
88 "github.com/mitchellh/packer/packer"
9+ xsclient "github.com/xenserver/go-xenserver-client"
910)
1011
1112type StepGetVNCPort struct {}
@@ -16,30 +17,49 @@ func (self *StepGetVNCPort) Run(state multistep.StateBag) multistep.StepAction {
1617 ui .Say ("Step: forward the instances VNC port over SSH" )
1718
1819 domid := state .Get ("domid" ).(string )
19- // cmd := fmt.Sprintf("xenstore-read /local/domain/%s/console/vnc-port", domid)
20+ cmd := fmt .Sprintf ("xenstore-read /local/domain/%s/console/vnc-port" , domid )
2021
21- expectedport := fmt .Sprintf ("59%s" , domid )
22- cmd1 := fmt .Sprintf ("nohup socat -d -d -lf /tmp/socat-%s TCP4-LISTEN:%s,reuseaddr,fork,tcpwrap=socat,allow-table=all UNIX-CONNECT:/var/run/xen/vnc-%s &>/dev/null &" , expectedport , expectedport , domid )
23- ui .Say (fmt .Sprintf ("nohup socat -d -d -lf /tmp/socat-%s TCP4-LISTEN:%s,reuseaddr,fork,tcpwrap=socat,allow-table=all UNIX-CONNECT:/var/run/xen/vnc-%s &>/dev/null &" , expectedport , expectedport , domid ))
24- remote_vncport , err := ExecuteHostSSHCmd (state , cmd1 )
22+ remote_vncport , err := ExecuteHostSSHCmd (state , cmd )
2523 if err != nil {
2624 ui .Error (fmt .Sprintf ("Unable to get VNC port (is the VM running?): %s" , err .Error ()))
2725 ui .Error (fmt .Sprintf ("XS7.5/7.6 no longer support xenstore-read: Try to use 5900. See https://bugs.xenserver.org/browse/XSO-906" ))
2826 remote_vncport = "5900"
29- //return multistep.ActionHalt
27+ client := state .Get ("client" ).(xsclient.XenAPIClient )
28+ hosts , err := client .GetHosts ()
29+ if err != nil {
30+ ui .Error (fmt .Sprintf ("Could not retrieve hosts in the pool: %s" , err .Error ()))
31+ return multistep .ActionHalt
32+ }
33+ host := hosts [0 ]
34+ host_software_versions , err := host .GetSoftwareVersion ()
35+ xs_version := host_software_versions ["product_version" ].(string )
36+
37+ if err != nil {
38+ ui .Error (fmt .Sprintf ("Could not get the software version: %s" , err .Error ()))
39+ return multistep .ActionHalt
40+ }
41+ if xs_version > "7.6.0" {
42+ ui .Say (fmt .Sprintf ("XS8.0+ no longer support xenstore-read: Make sure to install socat for XS8.0+, attempting to use socat" ))
43+ expectedport := fmt .Sprintf ("59%s" , domid )
44+ cmd1 := fmt .Sprintf ("nohup socat -d -d -lf /tmp/socat-%s TCP4-LISTEN:%s,reuseaddr,fork,tcpwrap=socat,allow-table=all UNIX-CONNECT:/var/run/xen/vnc-%s &>/dev/null &" , expectedport , expectedport , domid )
45+ remote_vncport , err := ExecuteHostSSHCmd (state , cmd1 )
46+ if err != nil {
47+ ui .Say (fmt .Sprintf ("socat not available on XenServer, halting packer ..." ))
48+ return multistep .ActionHalt
49+ }
50+ ui .Say (fmt .Sprintf ("nohup socat -d -d -lf /tmp/socat-%s TCP4-LISTEN:%s,reuseaddr,fork,tcpwrap=socat,allow-table=all UNIX-CONNECT:/var/run/xen/vnc-%s &>/dev/null &" , remote_vncport , expectedport , domid ))
51+ remote_vncport = expectedport
52+ }
3053 }
31- remote_vncport = expectedport
32- remote_port , err := strconv .ParseUint (remote_vncport , 10 , 16 )
3354
34- ui . Say ( fmt . Sprint ( "Exposed VNC port on: %s and using rmote port %d" , expectedport , remote_port ) )
55+ remote_port , err := strconv . ParseUint ( remote_vncport , 10 , 16 )
3556
3657 if err != nil {
3758 ui .Error (fmt .Sprintf ("Unable to convert '%s' to an int" , remote_vncport ))
3859 ui .Error (err .Error ())
3960 return multistep .ActionHalt
4061 }
4162
42- ui .Say (fmt .Sprint ("instance_vnc_port %d" , remote_port ))
4363 state .Put ("instance_vnc_port" , uint (remote_port ))
4464
4565 return multistep .ActionContinue
@@ -56,4 +76,4 @@ func InstanceVNCPort(state multistep.StateBag) (uint, error) {
5676func InstanceVNCIP (state multistep.StateBag ) (string , error ) {
5777 // The port is in Dom0, so we want to forward from localhost
5878 return "127.0.0.1" , nil
59- }
79+ }
0 commit comments