-
Notifications
You must be signed in to change notification settings - Fork 115
/
windows_net_manager.go
194 lines (164 loc) · 5.14 KB
/
windows_net_manager.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package net
import (
"fmt"
gonet "net"
"strings"
"time"
"github.com/pivotal-golang/clock"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type MACAddressDetector interface {
MACAddresses() (map[string]string, error)
}
func NewMACAddressDetector() MACAddressDetector {
return macAddressDetector{}
}
type macAddressDetector struct{}
func (m macAddressDetector) MACAddresses() (map[string]string, error) {
ifs, err := gonet.Interfaces()
if err != nil {
return nil, bosherr.WrapError(err, "Detecting Mac Addresses")
}
macs := make(map[string]string, len(ifs))
for _, f := range ifs {
macs[f.HardwareAddr.String()] = f.Name
}
return macs, nil
}
type NetworkInterfaces func() ([]gonet.Interface, error)
func NewNetworkInterfaces() NetworkInterfaces {
return gonet.Interfaces
}
type WindowsNetManager struct {
runner boshsys.CmdRunner
interfaceConfigurationCreator InterfaceConfigurationCreator
macAddressDetector MACAddressDetector
logTag string
logger boshlog.Logger
clock clock.Clock
}
func NewWindowsNetManager(
runner boshsys.CmdRunner,
interfaceConfigurationCreator InterfaceConfigurationCreator,
macAddressDetector MACAddressDetector,
logger boshlog.Logger,
clock clock.Clock,
) Manager {
return WindowsNetManager{
runner: runner,
interfaceConfigurationCreator: interfaceConfigurationCreator,
macAddressDetector: macAddressDetector,
logTag: "WindowsNetManager",
logger: logger,
clock: clock,
}
}
const (
SetDNSTemplate = `
[array]$interfaces = Get-DNSClientServerAddress
$dns = @("%s")
foreach($interface in $interfaces) {
Set-DnsClientServerAddress -InterfaceAlias $interface.InterfaceAlias -ServerAddresses ($dns -join ",")
}
`
ResetDNSTemplate = `
[array]$interfaces = Get-DNSClientServerAddress
foreach($interface in $interfaces) {
Set-DnsClientServerAddress -InterfaceAlias $interface.InterfaceAlias -ResetServerAddresses
}
`
NicSettingsTemplate = `
$connectionName=(get-wmiobject win32_networkadapter | where-object {$_.MacAddress -eq '%s'}).netconnectionid
netsh interface ip set address $connectionName static %s %s %s
`
)
func (net WindowsNetManager) GetConfiguredNetworkInterfaces() ([]string, error) {
panic("Not implemented")
}
func (net WindowsNetManager) ComputeNetworkConfig(networks boshsettings.Networks) (
[]StaticInterfaceConfiguration,
[]DHCPInterfaceConfiguration,
[]string,
error,
) {
nonVipNetworks := boshsettings.Networks{}
for networkName, networkSettings := range networks {
if networkSettings.IsVIP() {
continue
}
nonVipNetworks[networkName] = networkSettings
}
staticConfigs, dhcpConfigs, err := net.buildInterfaces(nonVipNetworks)
if err != nil {
return nil, nil, nil, err
}
dnsNetwork, _ := nonVipNetworks.DefaultNetworkFor("dns")
dnsServers := dnsNetwork.DNS
return staticConfigs, dhcpConfigs, dnsServers, nil
}
func (net WindowsNetManager) SetupNetworking(networks boshsettings.Networks, errCh chan error) error {
nonVipNetworks := boshsettings.Networks{}
for networkName, networkSettings := range networks {
if networkSettings.IsVIP() {
continue
}
nonVipNetworks[networkName] = networkSettings
}
staticConfigs, _, dnsServers, err := net.ComputeNetworkConfig(networks)
if err != nil {
return bosherr.WrapError(err, "Computing network configuration")
}
err = net.setupInterfaces(staticConfigs)
if err != nil {
return err
}
dns := net.setupDNS(dnsServers)
net.clock.Sleep(5 * time.Second)
return dns
}
func (net WindowsNetManager) setupInterfaces(staticConfigs []StaticInterfaceConfiguration) error {
for _, conf := range staticConfigs {
var gateway string
if conf.IsDefaultForGateway {
gateway = conf.Gateway
}
content := fmt.Sprintf(NicSettingsTemplate, conf.Mac, conf.Address, conf.Netmask, gateway)
_, _, _, err := net.runner.RunCommand("-Command", content)
if err != nil {
return bosherr.WrapError(err, "Configuring interface")
}
}
return nil
}
func (net WindowsNetManager) buildInterfaces(networks boshsettings.Networks) (
[]StaticInterfaceConfiguration,
[]DHCPInterfaceConfiguration,
error,
) {
interfacesByMacAddress, err := net.macAddressDetector.MACAddresses()
if err != nil {
return nil, nil, bosherr.WrapError(err, "Getting network interfaces")
}
staticConfigs, dhcpConfigs, err := net.interfaceConfigurationCreator.CreateInterfaceConfigurations(
networks, interfacesByMacAddress)
if err != nil {
return nil, nil, bosherr.WrapError(err, "Creating interface configurations")
}
return staticConfigs, dhcpConfigs, nil
}
func (net WindowsNetManager) setupDNS(dnsServers []string) error {
var content string
if len(dnsServers) > 0 {
content = fmt.Sprintf(SetDNSTemplate, strings.Join(dnsServers, `","`))
} else {
content = ResetDNSTemplate
}
_, _, _, err := net.runner.RunCommand("-Command", content)
if err != nil {
return bosherr.WrapError(err, "Setting DNS servers")
}
return nil
}