github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju-bridge/main.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "flag" 8 "fmt" 9 "os" 10 "strings" 11 "time" 12 13 "github.com/juju/clock" 14 "github.com/juju/juju/network/debinterfaces" 15 ) 16 17 const usage = ` 18 Bridge existing devices 19 20 usage: [ -p ] [ -b <bridge-prefix ] <filename> <device-name>=<bridge-name>... 21 22 Options: 23 24 -p -- parse and print to stdout, no activation 25 26 Example: 27 28 $ juju-bridge /etc/network/interfaces ens3=br-ens3 bond0.150=br-bond0.150 29 ` 30 31 func printParseError(err error) { 32 if pe, ok := err.(*debinterfaces.ParseError); ok { 33 fmt.Printf("error: %q:%d: %s: %s\n", pe.Filename, pe.LineNum, pe.Line, pe.Message) 34 } else { 35 fmt.Printf("error: %v\n", err) 36 } 37 } 38 39 func main() { 40 parseOnlyFlag := flag.Bool("p", false, "parse and print to stdout, no activation") 41 42 flag.Parse() 43 args := flag.Args() 44 45 if len(args) < 2 { 46 fmt.Fprintln(os.Stderr, usage) 47 os.Exit(1) 48 } 49 50 if *parseOnlyFlag { 51 stanzas, err := debinterfaces.Parse(args[0]) 52 53 if err != nil { 54 printParseError(err) 55 os.Exit(1) 56 } 57 58 fmt.Println(debinterfaces.FormatStanzas(debinterfaces.FlattenStanzas(stanzas), 4)) 59 os.Exit(0) 60 } 61 62 devices := make(map[string]string) 63 for _, v := range args[1:] { 64 arg := strings.Split(v, "=") 65 if len(arg) != 2 { 66 fmt.Fprintln(os.Stderr, usage) 67 os.Exit(1) 68 } 69 devices[arg[0]] = arg[1] 70 } 71 72 params := debinterfaces.ActivationParams{ 73 Clock: clock.WallClock, 74 Filename: args[0], 75 Devices: devices, 76 ReconfigureDelay: 10, 77 Timeout: 5 * time.Minute, 78 } 79 80 result, err := debinterfaces.BridgeAndActivate(params) 81 82 if err != nil { 83 printParseError(err) 84 os.Exit(1) 85 } else if result != nil { 86 if result.Code != 0 { 87 if len(result.Stdout) > 0 { 88 fmt.Fprintln(os.Stderr, string(result.Stdout)) 89 } 90 if len(result.Stderr) > 0 { 91 fmt.Fprintln(os.Stderr, string(result.Stderr)) 92 } 93 } 94 os.Exit(result.Code) 95 } 96 }