-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (76 loc) · 1.89 KB
/
main.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/juju/juju/network/debinterfaces"
"github.com/juju/utils/clock"
)
const usage = `
Bridge existing devices
usage: [ -p ] [ -b <bridge-prefix ] <filename> <device-name>=<bridge-name>...
Options:
-p -- parse and print to stdout, no activation
Example:
$ juju-bridge /etc/network/interfaces ens3=br-ens3 bond0.150=br-bond0.150
`
func printParseError(err error) {
if pe, ok := err.(*debinterfaces.ParseError); ok {
fmt.Printf("error: %q:%d: %s: %s\n", pe.Filename, pe.LineNum, pe.Line, pe.Message)
} else {
fmt.Printf("error: %v\n", err)
}
}
func main() {
parseOnlyFlag := flag.Bool("p", false, "parse and print to stdout, no activation")
flag.Parse()
args := flag.Args()
if len(args) < 2 {
fmt.Fprintln(os.Stderr, usage)
os.Exit(1)
}
if *parseOnlyFlag {
stanzas, err := debinterfaces.Parse(args[0])
if err != nil {
printParseError(err)
os.Exit(1)
}
fmt.Println(debinterfaces.FormatStanzas(debinterfaces.FlattenStanzas(stanzas), 4))
os.Exit(0)
}
devices := make(map[string]string)
for _, v := range args[1:] {
arg := strings.Split(v, "=")
if len(arg) != 2 {
fmt.Fprintln(os.Stderr, usage)
os.Exit(1)
}
devices[arg[0]] = arg[1]
}
params := debinterfaces.ActivationParams{
Clock: clock.WallClock,
Filename: args[0],
Devices: devices,
ReconfigureDelay: 10,
Timeout: 5 * time.Minute,
}
result, err := debinterfaces.BridgeAndActivate(params)
if err != nil {
printParseError(err)
os.Exit(1)
} else if result != nil {
if result.Code != 0 {
if len(result.Stdout) > 0 {
fmt.Fprintln(os.Stderr, string(result.Stdout))
}
if len(result.Stderr) > 0 {
fmt.Fprintln(os.Stderr, string(result.Stderr))
}
}
os.Exit(result.Code)
}
}