-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathcommon.go
More file actions
135 lines (110 loc) · 4.06 KB
/
common.go
File metadata and controls
135 lines (110 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package attach
import (
"fmt"
"net"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/lib/constants"
"github.com/vmware/vic/lib/migration/feature"
"github.com/vmware/vic/lib/portlayer/exec"
log "github.com/Sirupsen/logrus"
)
func lookupVCHIP() (net.IP, error) {
// FIXME: THERE MUST BE ANOTHER WAY
// following is from Create@exec.go
ips, err := net.LookupIP(constants.ManagementHostName)
if err != nil {
return nil, err
}
if len(ips) == 0 {
return nil, fmt.Errorf("No IP found on %s", constants.ManagementHostName)
}
if len(ips) > 1 {
return nil, fmt.Errorf("Multiple IPs found on %s: %#v", constants.ManagementHostName, ips)
}
return ips[0], nil
}
func toggle(handle *exec.Handle, id string, connected bool) (*exec.Handle, error) {
// check to see whether id is in Execs, if so set its RunBlock property to connected
session, ok := handle.ExecConfig.Execs[id]
if ok {
if err := compatible(handle); err != nil {
return nil, err
}
if session.Attach {
session.RunBlock = connected
}
}
// get the virtual device list
devices := object.VirtualDeviceList(handle.Config.Hardware.Device)
// select the virtual serial ports
serials := devices.SelectByBackingInfo((*types.VirtualSerialPortURIBackingInfo)(nil))
if len(serials) == 0 {
return nil, fmt.Errorf("Unable to find a device with desired backing")
}
if len(serials) > 1 {
return nil, fmt.Errorf("Multiple matches found with desired backing")
}
serial := serials[0]
ip, err := lookupVCHIP()
if err != nil {
return nil, err
}
log.Debugf("Found a device with desired backing: %#v", serial)
c := serial.GetVirtualDevice().Connectable
b := serial.GetVirtualDevice().Backing.(*types.VirtualSerialPortURIBackingInfo)
serviceURI := fmt.Sprintf("tcp://127.0.0.1:%d", constants.AttachServerPort)
proxyURI := fmt.Sprintf("telnet://%s:%d", ip, constants.SerialOverLANPort)
if b.ProxyURI == proxyURI && c.Connected == connected {
log.Debugf("Already in the desired state, (connected: %t, proxyURI: %s)", connected, proxyURI)
return handle, nil
}
// set the values
log.Debugf("Setting Connected to %t", connected)
c.Connected = connected
if connected && handle.ExecConfig.Sessions[handle.ExecConfig.ID].Attach {
log.Debugf("Setting the start connected state to %t", connected)
c.StartConnected = handle.ExecConfig.Sessions[handle.ExecConfig.ID].Attach
}
log.Debugf("Setting ServiceURI to %s", serviceURI)
b.ServiceURI = serviceURI
log.Debugf("Setting the ProxyURI to %s", proxyURI)
b.ProxyURI = proxyURI
config := &types.VirtualDeviceConfigSpec{
Device: serial,
Operation: types.VirtualDeviceConfigSpecOperationEdit,
}
handle.Spec.DeviceChange = append(handle.Spec.DeviceChange, config)
// check to see whether id is in Sessions, if so set its RunBlock property to connected
// if attach happens before start then this property will be persist in the vmx
// if attach happens after start then this propery will be thrown away by commit (one cannot change persistent extraconfig values if the vm is powered on)
session, ok = handle.ExecConfig.Sessions[id]
if ok {
if session.Attach {
session.RunBlock = connected
}
}
return handle, nil
}
func compatible(h interface{}) error {
if handle, ok := h.(*exec.Handle); ok {
if handle.DataVersion < feature.ExecSupportedVersion {
return fmt.Errorf("attaching exec tasks not supported for this container")
}
return nil
}
return fmt.Errorf("Type assertion failed for %#+v", h)
}