github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/networker/utils.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package networker
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  
    10  	"github.com/juju/utils/exec"
    11  )
    12  
    13  // Functions defined here for easier patching when testing.
    14  var (
    15  	ExecuteCommands     = executeCommands
    16  	Interfaces          = interfaces
    17  	InterfaceIsUp       = interfaceIsUp
    18  	InterfaceHasAddress = interfaceHasAddress
    19  )
    20  
    21  // executeCommands execute a batch of commands one by one.
    22  func executeCommands(commands []string) error {
    23  	for _, command := range commands {
    24  		result, err := exec.RunCommands(exec.RunParams{
    25  			Commands:   command,
    26  			WorkingDir: "/",
    27  		})
    28  		if err != nil {
    29  			return fmt.Errorf("failed to execute %q: %v", command, err)
    30  		}
    31  		if result.Code != 0 {
    32  			return fmt.Errorf(
    33  				"command %q failed (code: %d, stdout: %s, stderr: %s)",
    34  				command, result.Code, result.Stdout, result.Stderr)
    35  		}
    36  		logger.Debugf("command %q (code: %d, stdout: %s, stderr: %s)",
    37  			command, result.Code, result.Stdout, result.Stderr)
    38  	}
    39  	return nil
    40  }
    41  
    42  // interfaceIsUp returns whether the given network interface is up.
    43  func interfaceIsUp(interfaceName string) bool {
    44  	iface, err := net.InterfaceByName(interfaceName)
    45  	if err != nil {
    46  		// Log as warning, because with virtual interfaces, there
    47  		// might be pending commands to execute that actually create
    48  		// the interface first before we can look it up.
    49  		logger.Warningf("cannot tell if %q is up: %v", interfaceName, err)
    50  		return false
    51  	}
    52  	return (iface.Flags & net.FlagUp) != 0
    53  }
    54  
    55  // interfaceHasAddress whether the given network interface has at
    56  // least one assigned address.
    57  func interfaceHasAddress(interfaceName string) bool {
    58  	iface, err := net.InterfaceByName(interfaceName)
    59  	if err != nil {
    60  		// Log as warning, because with virtual interfaces, there
    61  		// might be pending commands to execute that actually create
    62  		// the interface first before we can look it up.
    63  		logger.Warningf("cannot tell if %q has addresses: %v", interfaceName, err)
    64  		return false
    65  	}
    66  	addrs, err := iface.Addrs()
    67  	if err != nil {
    68  		logger.Errorf("cannot get addresses for network interface %q: %v", interfaceName, err)
    69  		return false
    70  	}
    71  	return len(addrs) != 0
    72  }
    73  
    74  // interfaces returns all known network interfaces on the machine.
    75  func interfaces() ([]net.Interface, error) {
    76  	return net.Interfaces()
    77  }