-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver-network.go
127 lines (113 loc) · 2.47 KB
/
driver-network.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
package drivervbox
import (
"fmt"
"github.com/kuttiproject/drivercore"
"github.com/kuttiproject/workspace"
)
// QualifiedNetworkName adds a 'kuttinet' suffix to the specified cluster name.
func (vd *Driver) QualifiedNetworkName(clustername string) string {
return clustername + "kuttinet"
}
// DeleteNetwork deletes a network.
// It does this by running the command:
// VBoxManage natnetwork remove --netname <networkname>
func (vd *Driver) DeleteNetwork(clustername string) error {
if !vd.validate() {
return vd
}
netname := vd.QualifiedNetworkName(clustername)
output, err := workspace.RunWithResults(
vd.vboxmanagepath,
"natnetwork",
"remove",
"--netname",
netname,
)
if err != nil {
return fmt.Errorf(
"could not delete NAT network %s:%v:%s",
netname,
err,
output,
)
}
// Associated dhcpserver must also be deleted
output, err = workspace.RunWithResults(
vd.vboxmanagepath,
"dhcpserver",
"remove",
"--netname",
netname,
)
if err != nil {
return fmt.Errorf(
"could not delete DHCP server %s:%v:%s",
netname,
err,
output,
)
}
return nil
}
// NewNetwork creates a new VirtualBox NAT network.
// It uses the CIDR common to all Kutti networks, and is dhcp-enabled at start.
func (vd *Driver) NewNetwork(clustername string) (drivercore.Network, error) {
if !vd.validate() {
return nil, vd
}
netname := vd.QualifiedNetworkName(clustername)
// Multiple VirtualBox NAT Networks can have the same IP range
// So, all Kutti networks will use the same network CIDR
// We start with dhcp enabled.
output, err := workspace.RunWithResults(
vd.vboxmanagepath,
"natnetwork",
"add",
"--netname",
netname,
"--network",
DefaultNetCIDR,
"--enable",
"--dhcp",
"on",
)
if err != nil {
return nil, fmt.Errorf(
"could not create NAT network %s:%v:%s",
netname,
err,
output,
)
}
// Manually create the associated DHCP server
// Hard-coding a thirty-node limit for now
output, err = workspace.RunWithResults(
vd.vboxmanagepath,
"dhcpserver",
"add",
"--netname",
netname,
"--ip",
dhcpaddress,
"--netmask",
dhcpnetmask,
"--lowerip",
fmt.Sprintf("%s.%d", ipNetAddr, iphostbase),
"--upperip",
fmt.Sprintf("%s.%d", ipNetAddr, iphostbase+29),
"--enable",
)
if err != nil {
return nil, fmt.Errorf(
"could not create DHCP server for network %s:%v:%s",
netname,
err,
output,
)
}
newnetwork := &Network{
name: netname,
netCIDR: DefaultNetCIDR,
}
return newnetwork, err
}