Merge pull request #10231 from estesp/move-iptables-check

Move iptables check out of runtime init() to separate function
This commit is contained in:
Jessie Frazelle 2015-01-21 10:46:40 -08:00
commit feb02197c1

View file

@ -24,6 +24,7 @@ const (
) )
var ( var (
iptablesPath string
supportsXlock = false supportsXlock = false
ErrIptablesNotFound = errors.New("Iptables not found") ErrIptablesNotFound = errors.New("Iptables not found")
) )
@ -43,8 +44,17 @@ func (e *ChainError) Error() string {
return fmt.Sprintf("Error iptables %s: %s", e.Chain, string(e.Output)) return fmt.Sprintf("Error iptables %s: %s", e.Chain, string(e.Output))
} }
func init() { func initCheck() error {
supportsXlock = exec.Command("iptables", "--wait", "-L", "-n").Run() == nil
if iptablesPath == "" {
path, err := exec.LookPath("iptables")
if err != nil {
return ErrIptablesNotFound
}
iptablesPath = path
supportsXlock = exec.Command(iptablesPath, "--wait", "-L", "-n").Run() == nil
}
return nil
} }
func NewChain(name, bridge string, table Table) (*Chain, error) { func NewChain(name, bridge string, table Table) (*Chain, error) {
@ -258,18 +268,17 @@ func Exists(args ...string) bool {
// Call 'iptables' system command, passing supplied arguments // Call 'iptables' system command, passing supplied arguments
func Raw(args ...string) ([]byte, error) { func Raw(args ...string) ([]byte, error) {
path, err := exec.LookPath("iptables")
if err != nil {
return nil, ErrIptablesNotFound
}
if err := initCheck(); err != nil {
return nil, err
}
if supportsXlock { if supportsXlock {
args = append([]string{"--wait"}, args...) args = append([]string{"--wait"}, args...)
} }
log.Debugf("%s, %v", path, args) log.Debugf("%s, %v", iptablesPath, args)
output, err := exec.Command(path, args...).CombinedOutput() output, err := exec.Command(iptablesPath, args...).CombinedOutput()
if err != nil { if err != nil {
return nil, fmt.Errorf("iptables failed: iptables %v: %s (%s)", strings.Join(args, " "), output, err) return nil, fmt.Errorf("iptables failed: iptables %v: %s (%s)", strings.Join(args, " "), output, err)
} }