Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1847 from weaveworks/1613-weaveutil
Browse files Browse the repository at this point in the history
extract script utilities into single binary; LGTM
  • Loading branch information
bboreham committed Jan 7, 2016
2 parents 8804f88 + d010000 commit 987d896
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 127 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ prog/sigproxy/sigproxy
prog/weavewait/weavewait
prog/weavewait/weavewait_noop
prog/weavewait/weavewait_nomcast
prog/netcheck/netcheck
prog/docker_tls_args/docker_tls_args
prog/weaveutil/weaveutil
prog/plugin/plugin
testing/cover/cover
testing/runner/runner
Expand All @@ -59,8 +58,7 @@ prog/weaveexec/weavewait
prog/weaveexec/weavewait_noop
prog/weaveexec/weavewait_nomcast
prog/weaveexec/sigproxy
prog/weaveexec/netcheck
prog/weaveexec/docker_tls_args
prog/weaveexec/weaveutil
prog/weaveexec/docker*.tgz
Vagrantfile.local
test/tls/tls
Expand Down
17 changes: 7 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ SIGPROXY_EXE=prog/sigproxy/sigproxy
WEAVEWAIT_EXE=prog/weavewait/weavewait
WEAVEWAIT_NOOP_EXE=prog/weavewait/weavewait_noop
WEAVEWAIT_NOMCAST_EXE=prog/weavewait/weavewait_nomcast
NETCHECK_EXE=prog/netcheck/netcheck
DOCKERTLSARGS_EXE=prog/docker_tls_args/docker_tls_args
WEAVEUTIL_EXE=prog/weaveutil/weaveutil
DOCKERPLUGIN_EXE=prog/plugin/plugin
RUNNER_EXE=tools/runner/runner
TEST_TLS_EXE=test/tls/tls

EXES=$(WEAVER_EXE) $(SIGPROXY_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(WEAVEWAIT_NOOP_EXE) $(WEAVEWAIT_NOMCAST_EXE) $(NETCHECK_EXE) $(DOCKERTLSARGS_EXE) $(DOCKERPLUGIN_EXE) $(TEST_TLS_EXE)
EXES=$(WEAVER_EXE) $(SIGPROXY_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(WEAVEWAIT_NOOP_EXE) $(WEAVEWAIT_NOMCAST_EXE) $(WEAVEUTIL_EXE) $(DOCKERPLUGIN_EXE) $(TEST_TLS_EXE)

WEAVER_UPTODATE=.weaver.uptodate
WEAVEEXEC_UPTODATE=.weaveexec.uptodate
Expand Down Expand Up @@ -70,23 +69,22 @@ else
endif
$(NETGO_CHECK)

$(NETCHECK_EXE): common/*.go common/*/*.go net/*.go
$(WEAVEUTIL_EXE): common/*.go common/*/*.go net/*.go
go get -tags netgo ./$(@D)
go build $(BUILD_FLAGS) -o $@ ./$(@D)
$(NETGO_CHECK)

$(WEAVER_EXE): router/*.go mesh/*.go ipam/*.go ipam/*/*.go nameserver/*.go prog/weaver/*.go
$(WEAVEPROXY_EXE): proxy/*.go prog/weaveproxy/*.go
$(NETCHECK_EXE): prog/netcheck/*.go
$(WEAVEUTIL_EXE): prog/weaveutil/*.go

# These next programs need separate rules as they fail the netgo check in
# the main build stanza due to not importing net package
$(SIGPROXY_EXE): prog/sigproxy/*.go
$(DOCKERTLSARGS_EXE): prog/docker_tls_args/*.go
$(DOCKERPLUGIN_EXE): prog/plugin/*.go plugin/net/*.go plugin/ipam/*.go plugin/skel/*.go api/*.go common/docker/*.go
$(TEST_TLS_EXE): test/tls/*.go

$(SIGPROXY_EXE) $(DOCKERTLSARGS_EXE) $(DOCKERPLUGIN_EXE) $(TEST_TLS_EXE):
$(SIGPROXY_EXE) $(DOCKERPLUGIN_EXE) $(TEST_TLS_EXE):
go get -tags netgo ./$(@D)
go build $(BUILD_FLAGS) -o $@ ./$(@D)

Expand All @@ -106,15 +104,14 @@ $(WEAVER_UPTODATE): prog/weaver/Dockerfile $(WEAVER_EXE)
$(SUDO) DOCKER_HOST=$(DOCKER_HOST) docker build -t $(WEAVER_IMAGE) prog/weaver
touch $@

$(WEAVEEXEC_UPTODATE): prog/weaveexec/Dockerfile prog/weaveexec/symlink $(DOCKER_DISTRIB) weave $(SIGPROXY_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(WEAVEWAIT_NOOP_EXE) $(WEAVEWAIT_NOMCAST_EXE) $(NETCHECK_EXE) $(DOCKERTLSARGS_EXE)
$(WEAVEEXEC_UPTODATE): prog/weaveexec/Dockerfile prog/weaveexec/symlink $(DOCKER_DISTRIB) weave $(SIGPROXY_EXE) $(WEAVEPROXY_EXE) $(WEAVEWAIT_EXE) $(WEAVEWAIT_NOOP_EXE) $(WEAVEWAIT_NOMCAST_EXE) $(WEAVEUTIL_EXE)
cp weave prog/weaveexec/weave
cp $(SIGPROXY_EXE) prog/weaveexec/sigproxy
cp $(WEAVEPROXY_EXE) prog/weaveexec/weaveproxy
cp $(WEAVEWAIT_EXE) prog/weaveexec/weavewait
cp $(WEAVEWAIT_NOOP_EXE) prog/weaveexec/weavewait_noop
cp $(WEAVEWAIT_NOMCAST_EXE) prog/weaveexec/weavewait_nomcast
cp $(NETCHECK_EXE) prog/weaveexec/netcheck
cp $(DOCKERTLSARGS_EXE) prog/weaveexec/docker_tls_args
cp $(WEAVEUTIL_EXE) prog/weaveexec/weaveutil
cp $(DOCKER_DISTRIB) prog/weaveexec/docker.tgz
$(SUDO) DOCKER_HOST=$(DOCKER_HOST) docker build -t $(WEAVEEXEC_IMAGE) prog/weaveexec
touch $@
Expand Down
54 changes: 0 additions & 54 deletions prog/netcheck/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion prog/weaveexec/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 2 additions & 37 deletions prog/weaver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -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())
Expand Down
39 changes: 39 additions & 0 deletions prog/weaveutil/datapath.go
Original file line number Diff line number Diff line change
@@ -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])
}
21 changes: 9 additions & 12 deletions prog/docker_tls_args/main.go → prog/weaveutil/docker_tls_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 changes: 59 additions & 0 deletions prog/weaveutil/main.go
Original file line number Diff line number Diff line change
@@ -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 changes: 28 additions & 0 deletions prog/weaveutil/netcheck.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 987d896

Please sign in to comment.