Merge pull request #1847 from weaveworks/1613-weaveutil

extract script utilities into single binary; LGTM
This commit is contained in:
Bryan Boreham committed 2016-01-07 18:53:25 +00:00
commit 987d89632d
10 files changed
+161 -127

No files matched your search

-54
View File
@@ -1,54 +0,0 @@
/* netcheck: check whether a given network or address overlaps with any existing routes */
package main
import (
"fmt"
"net"
"os"
"github.com/docker/docker/pkg/mflag"
weavenet "github.com/weaveworks/weave/net"
)
func fatal(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
type stringList map[string]struct{}
func (ss *stringList) Set(value string) error {
(*ss)[value] = struct{}{}
return nil
}
func (ss *stringList) String() string {
return fmt.Sprintf("%v", *ss)
}
func main() {
ignoreIfaceNames := make(stringList)
mflag.Var(&ignoreIfaceNames, []string{"-ignore-iface"}, "name of interface to ignore)")
mflag.Parse()
if len(mflag.Args()) < 1 {
fmt.Fprintln(os.Stderr, "usage: netcheck [--ignore-iface <iface-name>] network-cidr")
os.Exit(1)
}
cidrStr := mflag.Args()[0]
addr, ipnet, err := net.ParseCIDR(cidrStr)
if err != nil {
fatal(err)
}
if ipnet.IP.Equal(addr) {
err = weavenet.CheckNetworkFree(ipnet, ignoreIfaceNames)
} else {
err = weavenet.CheckAddressOverlap(addr, ignoreIfaceNames)
}
if err != nil {
fatal(err)
}
os.Exit(0)
}
+1 -1
View File
@@ -20,7 +20,7 @@ RUN apk add --update \
&& rm -rf /var/cache/apk/*
ADD ./weave ./sigproxy ./weaveproxy ./symlink /home/weave/
ADD ./netcheck ./docker_tls_args /usr/bin/
ADD ./weaveutil /usr/bin/
ADD ./weavewait /w/w
ADD ./weavewait_noop /w-noop/w
ADD ./weavewait_nomcast /w-nomcast/w
+2 -37
View File
@@ -16,7 +16,6 @@ import (
. "github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/common/docker"
"github.com/weaveworks/weave/common/odp"
"github.com/weaveworks/weave/ipam"
"github.com/weaveworks/weave/mesh"
"github.com/weaveworks/weave/nameserver"
@@ -37,12 +36,7 @@ func main() {
runtime.GOMAXPROCS(procs)
var (
// flags that cause immediate exit
justVersion bool
createDatapath bool
deleteDatapath bool
addDatapathInterface string
justVersion bool
config mesh.Config
networkConfig weave.NetworkConfig
protocolMinVersion int
@@ -79,10 +73,6 @@ func main() {
}
mflag.BoolVar(&justVersion, []string{"#version", "-version"}, false, "print version and exit")
mflag.BoolVar(&createDatapath, []string{"-create-datapath"}, false, "create ODP datapath and exit")
mflag.BoolVar(&deleteDatapath, []string{"-delete-datapath"}, false, "delete ODP datapath and exit")
mflag.StringVar(&addDatapathInterface, []string{"-add-datapath-iface"}, "", "add a network interface to the ODP datapath and exit")
mflag.IntVar(&config.Port, []string{"#port", "-port"}, mesh.Port, "router port")
mflag.IntVar(&protocolMinVersion, []string{"-min-protocol-version"}, mesh.ProtocolMinVersion, "minimum weave protocol version")
mflag.StringVar(&ifaceName, []string{"#iface", "-iface"}, "", "name of interface to capture/inject from (disabled if blank)")
@@ -124,34 +114,9 @@ func main() {
SetLogLevel(logLevel)
switch {
case justVersion:
if justVersion {
fmt.Printf("weave router %s\n", version)
os.Exit(0)
case createDatapath:
odpSupported, err := odp.CreateDatapath(datapathName)
if !odpSupported {
if err != nil {
Log.Error(err)
}
// When the kernel lacks ODP support, exit
// with a special status to distinguish it for
// the weave script.
os.Exit(17)
}
checkFatal(err)
os.Exit(0)
case deleteDatapath:
checkFatal(odp.DeleteDatapath(datapathName))
os.Exit(0)
case addDatapathInterface != "":
checkFatal(odp.AddDatapathInterface(datapathName, addDatapathInterface))
os.Exit(0)
}
Log.Println("Command line options:", options())
+39
View File
@@ -0,0 +1,39 @@
// various fastdp operations
package main
import (
"fmt"
"os"
"github.com/weaveworks/weave/common/odp"
)
func createDatapath(args []string) error {
if len(args) != 1 {
cmdUsage("create-datapath", "<datapath>")
}
odpSupported, err := odp.CreateDatapath(args[0])
if !odpSupported {
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
// When the kernel lacks ODP support, exit with a special
// status to distinguish it for the weave script.
os.Exit(17)
}
return err
}
func deleteDatapath(args []string) error {
if len(args) != 1 {
cmdUsage("delete-datapath", "<datapath>")
}
return odp.DeleteDatapath(args[0])
}
func addDatapathInterface(args []string) error {
if len(args) != 2 {
cmdUsage("add-datapath-interface", "<datapath> <interface>")
}
return odp.AddDatapathInterface(args[0], args[1])
}
@@ -13,14 +13,18 @@ import (
"strings"
)
func main() {
func dockerTLSArgs(args []string) error {
if len(args) > 0 {
cmdUsage("docker-tls-args", "")
}
procRoot := os.Getenv("PROCFS")
if procRoot == "" {
procRoot = "/proc"
}
dirEntries, err := ioutil.ReadDir(procRoot)
checkErr(err)
if err != nil {
return err
}
for _, dirEntry := range dirEntries {
dirName := dirEntry.Name()
@@ -63,15 +67,8 @@ func main() {
}
fmt.Println(strings.Join(tlsArgs, " "))
os.Exit(0)
return nil
}
os.Exit(1)
}
func checkErr(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return fmt.Errorf("cannot locate running docker daemon")
}
+59
View File
@@ -0,0 +1,59 @@
/* weaveutil: collection of operations required by weave script */
package main
import (
"fmt"
"os"
)
var commands map[string]func([]string) error
func init() {
commands = map[string]func([]string) error{
"help": help,
"netcheck": netcheck,
"docker-tls-args": dockerTLSArgs,
"create-datapath": createDatapath,
"delete-datapath": deleteDatapath,
"add-datapath-interface": addDatapathInterface,
}
}
func main() {
if len(os.Args) < 2 {
usage()
os.Exit(1)
}
cmd, found := commands[os.Args[1]]
if !found {
usage()
os.Exit(1)
}
if err := cmd(os.Args[2:]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func help(args []string) error {
if len(args) > 0 {
cmdUsage("help", "")
}
usage()
return nil
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: weaveutil <command> <arg>...")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "where <command> is one of:")
fmt.Fprintln(os.Stderr)
for cmd := range commands {
fmt.Fprintln(os.Stderr, cmd)
}
}
func cmdUsage(cmd string, usage string) {
fmt.Fprintf(os.Stderr, "usage: weaveutil %s %s\n", cmd, usage)
os.Exit(1)
}
+28
View File
@@ -0,0 +1,28 @@
/* netcheck: check whether a given network or address overlaps with any existing routes */
package main
import (
"net"
weavenet "github.com/weaveworks/weave/net"
)
func netcheck(args []string) error {
if len(args) < 1 {
cmdUsage("netcheck", "<cidr> [<interface-to-ignore> ...]")
}
addr, ipnet, err := net.ParseCIDR(args[0])
if err != nil {
return err
}
ignoreIfaceNames := make(map[string]struct{})
for _, ifName := range args[1:] {
ignoreIfaceNames[ifName] = struct{}{}
}
if ipnet.IP.Equal(addr) {
err = weavenet.CheckNetworkFree(ipnet, ignoreIfaceNames)
} else {
err = weavenet.CheckAddressOverlap(addr, ignoreIfaceNames)
}
return err
}