Skip to content

Commit

Permalink
replace sh ask_version with weaveutil ask-version
Browse files Browse the repository at this point in the history
  • Loading branch information
dtshepherd committed Sep 5, 2017
1 parent 55d8669 commit 6bb8afd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
1 change: 1 addition & 0 deletions prog/weaveutil/main.go
Expand Up @@ -15,6 +15,7 @@ var commands map[string]func([]string) error
func init() {
commands = map[string]func([]string) error{
"help": help,
"ask-version": askVersion,
"netcheck": netcheck,
"docker-tls-args": dockerTLSArgs,
"create-bridge": createBridge,
Expand Down
64 changes: 64 additions & 0 deletions prog/weaveutil/version.go
@@ -0,0 +1,64 @@
package main

import (
"bytes"
"fmt"

docker "github.com/fsouza/go-dockerclient"
)

func askVersion(args []string) error {
if len(args) < 1 {
cmdUsage("ask-version", "<container-name-or-id> <image-name-or-id>")
}
containerID := args[0]
imageID := args[1]

c, err := docker.NewVersionedClientFromEnv("1.18")
if err != nil {
return fmt.Errorf("unable to connect to docker: %s", err)
}

var image string
container, err := c.InspectContainer(containerID)
if err != nil {
img, err := c.InspectImage(imageID)
if err != nil {
return fmt.Errorf("unable to find image %s: %s", imageID, err)
} else {
image = img.ID
}
} else {
image = container.Image
}

//
// TODO: Move this code to a helper in container.go for reuse with other `docker run` commands
//
config := docker.Config{Image: image, Env: []string{"WEAVE_CIDR=none"}, Cmd: []string{"--version"}}
hostConfig := docker.HostConfig{AutoRemove: true, NetworkMode: "none"}
versionContainer, err := c.CreateContainer(docker.CreateContainerOptions{Config: &config, HostConfig: &hostConfig})
if err != nil {
return fmt.Errorf("unable to create container: %s", err)
}

err = c.StartContainer(versionContainer.ID, &hostConfig)
if err != nil {
return fmt.Errorf("unable to start container: %s", err)
}

var buf bytes.Buffer
err = c.AttachToContainer(docker.AttachToContainerOptions{Container: versionContainer.ID, OutputStream: &buf, Stdout: true, Stream: true})
if err != nil {
return fmt.Errorf("unable to attach to container: %s", err)
}

// AutoRemove doesn't work with old Docker daemons (< 1.25), manually remove
err = c.RemoveContainer(docker.RemoveContainerOptions{ID: versionContainer.ID, Force: true})
if err != nil {
return fmt.Errorf("unable to remove container: %s", err)
}

fmt.Print(buf.String())
return nil
}
11 changes: 1 addition & 10 deletions weave
Expand Up @@ -560,15 +560,6 @@ router_bridge_opts() {
[ -z $WEAVE_NO_FASTDP ] || echo --no-fastdp
}

ask_version() {
if check_running $1 2>/dev/null ; then
DOCKERIMAGE=$(docker inspect --format='{{.Image}}' $1 )
elif ! DOCKERIMAGE=$(docker inspect --format='{{.Id}}' $2 2>/dev/null) ; then
echo "Unable to find $2 image." >&2
fi
[ -n "$DOCKERIMAGE" ] && docker run --rm --net=none -e WEAVE_CIDR=none $3 $DOCKERIMAGE $COVERAGE_ARGS --version
}

attach() {
ATTACH_ARGS=""
[ -n "$NO_MULTICAST_ROUTE" ] && ATTACH_ARGS="--no-multicast-route"
Expand Down Expand Up @@ -1339,7 +1330,7 @@ case "$COMMAND" in
;;
version)
[ $# -eq 0 ] || usage
ask_version $CONTAINER_NAME $IMAGE || true
util_op ask-version $CONTAINER_NAME $IMAGE || true
;;
attach-bridge)
if detect_bridge_type ; then
Expand Down

0 comments on commit 6bb8afd

Please sign in to comment.