-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.go
133 lines (114 loc) · 3.84 KB
/
bridge.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package network
import (
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/juju/network/debinterfaces"
"github.com/juju/juju/network/netplan"
)
// Bridger creates network bridges to support addressable containers.
type Bridger interface {
// Bridge turns existing devices into bridged devices.
Bridge(devices []DeviceToBridge, reconfigureDelay int) error
}
type etcNetworkInterfacesBridger struct {
Clock clock.Clock
DryRun bool
Filename string
Timeout time.Duration
}
var _ Bridger = (*etcNetworkInterfacesBridger)(nil)
func (b *etcNetworkInterfacesBridger) Bridge(devices []DeviceToBridge, reconfigureDelay int) error {
devicesMap := make(map[string]string)
for _, k := range devices {
devicesMap[k.DeviceName] = k.BridgeName
}
params := debinterfaces.ActivationParams{
Clock: clock.WallClock,
Filename: b.Filename,
Devices: devicesMap,
ReconfigureDelay: reconfigureDelay,
Timeout: b.Timeout,
DryRun: b.DryRun,
}
result, err := debinterfaces.BridgeAndActivate(params)
if err != nil {
return errors.Errorf("bridge activation error: %s", err)
}
if result != nil {
logger.Infof("bridgescript result=%v", result.Code)
if result.Code != 0 {
logger.Errorf("bridgescript stdout\n%s\n", result.Stdout)
logger.Errorf("bridgescript stderr\n%s\n", result.Stderr)
return errors.Errorf("bridgescript failed: %s", string(result.Stderr))
}
logger.Tracef("bridgescript stdout\n%s\n", result.Stdout)
logger.Tracef("bridgescript stderr\n%s\n", result.Stderr)
} else {
logger.Infof("bridgescript returned nothing")
}
return nil
}
func newEtcNetworkInterfacesBridger(clock clock.Clock, timeout time.Duration, filename string, dryRun bool) Bridger {
return &etcNetworkInterfacesBridger{
Clock: clock,
DryRun: dryRun,
Filename: filename,
Timeout: timeout,
}
}
// DefaultEtcNetworkInterfacesBridger returns a Bridger instance that
// can parse an interfaces(5) to transform existing devices into
// bridged devices.
func DefaultEtcNetworkInterfacesBridger(timeout time.Duration, filename string) (Bridger, error) {
return newEtcNetworkInterfacesBridger(clock.WallClock, timeout, filename, false), nil
}
type netplanBridger struct {
Clock clock.Clock
Directory string
Timeout time.Duration
}
var _ Bridger = (*netplanBridger)(nil)
func (b *netplanBridger) Bridge(devices []DeviceToBridge, reconfigureDelay int) error {
npDevices := make([]netplan.DeviceToBridge, len(devices))
for i, device := range devices {
npDevices[i] = netplan.DeviceToBridge(device)
}
params := netplan.ActivationParams{
Clock: clock.WallClock,
Directory: b.Directory,
Devices: npDevices,
Timeout: b.Timeout,
}
result, err := netplan.BridgeAndActivate(params)
if err != nil {
return errors.Errorf("bridge activation error: %s", err)
}
if result != nil {
logger.Infof("bridger result=%v", result.Code)
if result.Code != 0 {
logger.Errorf("bridger stdout\n%s\n", result.Stdout)
logger.Errorf("bridger stderr\n%s\n", result.Stderr)
return errors.Errorf("bridger failed: %s", result.Stderr)
}
logger.Tracef("bridger stdout\n%s\n", result.Stdout)
logger.Tracef("bridger stderr\n%s\n", result.Stderr)
} else {
logger.Infof("bridger returned nothing")
}
return nil
}
func newNetplanBridger(clock clock.Clock, timeout time.Duration, directory string) Bridger {
return &netplanBridger{
Clock: clock,
Directory: directory,
Timeout: timeout,
}
}
// DefaultNetplanBridger returns a Bridger instance that can parse a set
// of netplan yaml files to transform existing devices into bridged devices.
func DefaultNetplanBridger(timeout time.Duration, directory string) (Bridger, error) {
return newNetplanBridger(clock.WallClock, timeout, directory), nil
}