This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.go
127 lines (117 loc) · 3.34 KB
/
run.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 netwrangler
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"strings"
yaml "github.com/ghodss/yaml"
"github.com/rackn/netwrangler/netplan"
"github.com/rackn/netwrangler/rhel"
"github.com/rackn/netwrangler/systemd"
"github.com/rackn/netwrangler/util"
)
var (
// The input formats we accept. internal is the intermediate format netwrangler uses.
SrcFormats = []string{"netplan", "internal"}
// The output formats we can handle. internal is the intermediate format netwrangler uses.
DestFormats = []string{"netplan", "systemd", "rhel", "internal"}
// The MAC address of the device we booted from.
bootMac net.HardwareAddr
)
func fillBootIf(phys []util.Phy) {
if phys != nil && bootMac != nil {
for i := range phys {
phys[i].BootIf = bytes.Equal(phys[i].HardwareAddr, bootMac)
}
}
}
// GatherPhys gathers the physical nics that the system knows about.
// It is currently only supported on Linux systems.
func GatherPhys() ([]util.Phy, error) {
res, err := util.GatherPhys()
fillBootIf(res)
return res, err
}
// GatherPhysFromFile gathers the physical nic information from a saved file.
// This can be used for unit testing or buld offline operations.
func GatherPhysFromFile(src string) (phys []util.Phy, err error) {
var buf []byte
buf, err = ioutil.ReadFile(src)
if err != nil {
err = fmt.Errorf("Error reading phys: %v", err)
return
}
if err = yaml.Unmarshal(buf, &phys); err != nil {
err = fmt.Errorf("Error unmarshalling phys: %v", err)
}
fillBootIf(phys)
return
}
// Write writes out the compiled Layout in the specified format and location
func Write(layout *util.Layout, destFmt, destLoc string, bindMacs bool) error {
var out util.Writer
var err error
switch destFmt {
case "internal":
out = layout
case "netplan":
out = netplan.New(layout)
case "systemd":
out = systemd.New(layout)
case "rhel":
out = rhel.New(layout)
default:
return fmt.Errorf("Unknown output format %s", destFmt)
}
if bindMacs {
out.BindMacs()
}
err = out.Write(destLoc)
if err != nil {
return fmt.Errorf("Error writing '%s': %v", destFmt, err)
}
return nil
}
// Compile transforms network configuration settings from srcLoc in
// srcFmt into destFmt at destLoc, using phys as the base physical
// interfaces to build on. if bindMacs is true, the generated format
// will bind to interface MAC addresses (or other unique physical
// addresses), otherwise the interface names at srcLoc must match what
// is present on the system at the time netwrangler is run.
func Compile(phys []util.Phy, srcFmt, destFmt, srcLoc, destLoc string, bindMacs bool) error {
var (
layout *util.Layout
err error
in util.Reader
)
switch srcFmt {
case "netplan":
in = &netplan.Netplan{}
case "internal":
in = layout
default:
return fmt.Errorf("Unknown input format %s", srcFmt)
}
layout, err = in.Read(srcLoc, phys)
if err != nil {
return fmt.Errorf("Error reading '%s': %v", srcFmt, err)
}
return Write(layout, destFmt, destLoc, bindMacs)
}
// BootMac arranges for setting the BootIf flag on the phy corresponding to the
// interface we booted from. Must be called before phys are gathered.
func BootMac(mac string) error {
if len(mac) == 0 {
return nil
}
mac = strings.ReplaceAll(mac, "-", ":")
parts := strings.Count(mac, ":")
switch parts {
case 6, 8, 20:
mac = mac[3:]
}
var err error
bootMac, err = net.ParseMAC(mac)
return err
}