Merge pull request #697 from paulbellamy/peer-replacement
weave connect/forget take multiple peers Closes #759.
This commit is contained in:
commit
09feb0c53a
4 files changed
+95
-41
No files matched your search
+25
-16
@@ -48,30 +48,39 @@ func (cm *ConnectionMaker) Start() {
|
|||||||
go cm.queryLoop(actionChan)
|
go cm.queryLoop(actionChan)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) InitiateConnection(peer string) error {
|
func (cm *ConnectionMaker) InitiateConnections(peers []string) []error {
|
||||||
host, port, err := net.SplitHostPort(peer)
|
errors := []error{}
|
||||||
if err != nil {
|
addrs := make(map[string]*net.TCPAddr)
|
||||||
host = peer
|
for _, peer := range peers {
|
||||||
port = "0" // we use that as an indication that "no port was supplied"
|
host, port, err := net.SplitHostPort(peer)
|
||||||
}
|
if err != nil {
|
||||||
addr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%s", host, port))
|
host = peer
|
||||||
if err != nil {
|
port = "0" // we use that as an indication that "no port was supplied"
|
||||||
return err
|
}
|
||||||
|
if addr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%s", host, port)); err != nil {
|
||||||
|
errors = append(errors, err)
|
||||||
|
} else {
|
||||||
|
addrs[peer] = addr
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cm.actionChan <- func() bool {
|
cm.actionChan <- func() bool {
|
||||||
cm.cmdLinePeers[peer] = addr
|
for peer, addr := range addrs {
|
||||||
// curtail any existing reconnect interval
|
cm.cmdLinePeers[peer] = addr
|
||||||
if target, found := cm.targets[addr.String()]; found {
|
// curtail any existing reconnect interval
|
||||||
target.tryAfter, target.tryInterval = tryImmediately()
|
if target, found := cm.targets[addr.String()]; found {
|
||||||
|
target.tryAfter, target.tryInterval = tryImmediately()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return nil
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) ForgetConnection(peer string) {
|
func (cm *ConnectionMaker) ForgetConnections(peers []string) {
|
||||||
cm.actionChan <- func() bool {
|
cm.actionChan <- func() bool {
|
||||||
delete(cm.cmdLinePeers, peer)
|
for _, peer := range peers {
|
||||||
|
delete(cm.cmdLinePeers, peer)
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Executable
+26
@@ -0,0 +1,26 @@
|
|||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
. ./config.sh
|
||||||
|
|
||||||
|
C1=10.2.1.4
|
||||||
|
C2=10.2.1.7
|
||||||
|
|
||||||
|
start_suite "Connecting and forgetting routers after launch"
|
||||||
|
|
||||||
|
weave_on $HOST1 launch
|
||||||
|
weave_on $HOST2 launch
|
||||||
|
|
||||||
|
start_container $HOST1 $C1/24 --name=c1
|
||||||
|
start_container $HOST2 $C2/24 --name=c2
|
||||||
|
|
||||||
|
assert_raises "exec_on $HOST1 c1 $PING $C2" 1
|
||||||
|
assert_raises "weave_on $HOST2 connect $HOST1 $HOST2"
|
||||||
|
assert_raises "exec_on $HOST1 c1 $PING $C2"
|
||||||
|
|
||||||
|
assert_raises "weave_on $HOST2 forget $HOST1 $HOST2"
|
||||||
|
assert_raises "weave_on $HOST1 stop"
|
||||||
|
assert_raises "weave_on $HOST1 launch"
|
||||||
|
|
||||||
|
assert_raises "exec_on $HOST1 c1 $PING $C2" 1
|
||||||
|
|
||||||
|
end_suite
|
||||||
@@ -28,8 +28,8 @@ usage() {
|
|||||||
echo "weave launch [-password <password>] [-nickname <nickname>] [-iprange <cidr>] <peer> ..."
|
echo "weave launch [-password <password>] [-nickname <nickname>] [-iprange <cidr>] <peer> ..."
|
||||||
echo "weave launch-dns <cidr>"
|
echo "weave launch-dns <cidr>"
|
||||||
echo "weave launch-proxy [-H <docker_endpoint>] [--with-dns] [--with-ipam]"
|
echo "weave launch-proxy [-H <docker_endpoint>] [--with-dns] [--with-ipam]"
|
||||||
echo "weave connect <peer>"
|
echo "weave connect <peer> ..."
|
||||||
echo "weave forget <peer>"
|
echo "weave forget <peer> ..."
|
||||||
echo "weave run [--with-dns] [<cidr> ...] <docker run args> ..."
|
echo "weave run [--with-dns] [<cidr> ...] <docker run args> ..."
|
||||||
echo "weave start [<cidr> ...] <container_id>"
|
echo "weave start [<cidr> ...] <container_id>"
|
||||||
echo "weave attach [<cidr> ...] <container_id>"
|
echo "weave attach [<cidr> ...] <container_id>"
|
||||||
@@ -562,6 +562,16 @@ echo_addresses() {
|
|||||||
echo $1 $2 $3
|
echo $1 $2 $3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
peer_args() {
|
||||||
|
res=''
|
||||||
|
sep=''
|
||||||
|
for p in "$@" ; do
|
||||||
|
res="$res${sep}peer=$p"
|
||||||
|
sep="&"
|
||||||
|
done
|
||||||
|
echo "$res"
|
||||||
|
}
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# weaveDNS helpers
|
# weaveDNS helpers
|
||||||
######################################################################
|
######################################################################
|
||||||
@@ -878,12 +888,12 @@ case "$COMMAND" in
|
|||||||
echo $PROXY_CONTAINER
|
echo $PROXY_CONTAINER
|
||||||
;;
|
;;
|
||||||
connect)
|
connect)
|
||||||
[ $# -eq 1 ] || usage
|
[ $# -gt 0 ] || usage
|
||||||
http_call $CONTAINER_NAME $HTTP_PORT POST /connect -d "peer=$1"
|
http_call $CONTAINER_NAME $HTTP_PORT POST /connect -d $(peer_args "$@")
|
||||||
;;
|
;;
|
||||||
forget)
|
forget)
|
||||||
[ $# -eq 1 ] || usage
|
[ $# -gt 0 ] || usage
|
||||||
http_call $CONTAINER_NAME $HTTP_PORT POST /forget -d "peer=$1"
|
http_call $CONTAINER_NAME $HTTP_PORT POST /forget -d $(peer_args "$@")
|
||||||
;;
|
;;
|
||||||
status)
|
status)
|
||||||
http_call $CONTAINER_NAME $HTTP_PORT GET /status || true
|
http_call $CONTAINER_NAME $HTTP_PORT GET /status || true
|
||||||
|
|||||||
+28
-19
@@ -1,10 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.google.com/p/gopacket/layers"
|
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"code.google.com/p/gopacket/layers"
|
||||||
"github.com/davecheney/profile"
|
"github.com/davecheney/profile"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
. "github.com/weaveworks/weave/common"
|
. "github.com/weaveworks/weave/common"
|
||||||
@@ -12,12 +19,6 @@ import (
|
|||||||
"github.com/weaveworks/weave/ipam"
|
"github.com/weaveworks/weave/ipam"
|
||||||
weavenet "github.com/weaveworks/weave/net"
|
weavenet "github.com/weaveworks/weave/net"
|
||||||
weave "github.com/weaveworks/weave/router"
|
weave "github.com/weaveworks/weave/router"
|
||||||
"log"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var version = "(unreleased version)"
|
var version = "(unreleased version)"
|
||||||
@@ -143,7 +144,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
router.Start()
|
router.Start()
|
||||||
initiateConnections(router, peers)
|
if errors := router.ConnectionMaker.InitiateConnections(peers); len(errors) > 0 {
|
||||||
|
log.Fatal(errorMessages(errors))
|
||||||
|
}
|
||||||
|
|
||||||
// The weave script always waits for a status call to succeed,
|
// The weave script always waits for a status call to succeed,
|
||||||
// so there is no point in doing "weave launch -httpaddr ''".
|
// so there is no point in doing "weave launch -httpaddr ''".
|
||||||
@@ -155,6 +158,14 @@ func main() {
|
|||||||
SignalHandlerLoop(router)
|
SignalHandlerLoop(router)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func errorMessages(errors []error) string {
|
||||||
|
var result []string
|
||||||
|
for _, err := range errors {
|
||||||
|
result = append(result, err.Error())
|
||||||
|
}
|
||||||
|
return strings.Join(result, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
func options() map[string]string {
|
func options() map[string]string {
|
||||||
options := make(map[string]string)
|
options := make(map[string]string)
|
||||||
flag.Visit(func(f *flag.Flag) {
|
flag.Visit(func(f *flag.Flag) {
|
||||||
@@ -181,14 +192,6 @@ func logFrameFunc(debug bool) weave.LogFrameFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initiateConnections(router *weave.Router, peers []string) {
|
|
||||||
for _, peer := range peers {
|
|
||||||
if err := router.ConnectionMaker.InitiateConnection(peer); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func createAllocator(router *weave.Router, apiPath string, iprangeCIDR string, quorum uint) *ipam.Allocator {
|
func createAllocator(router *weave.Router, apiPath string, iprangeCIDR string, quorum uint) *ipam.Allocator {
|
||||||
allocator, err := ipam.NewAllocator(router.Ourself.Peer.Name, router.Ourself.Peer.UID, router.Ourself.Peer.NickName, iprangeCIDR, quorum)
|
allocator, err := ipam.NewAllocator(router.Ourself.Peer.Name, router.Ourself.Peer.UID, router.Ourself.Peer.NickName, iprangeCIDR, quorum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -250,13 +253,19 @@ func handleHTTP(router *weave.Router, httpAddr string, allocator *ipam.Allocator
|
|||||||
})
|
})
|
||||||
|
|
||||||
muxRouter.Methods("POST").Path("/connect").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
muxRouter.Methods("POST").Path("/connect").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := router.ConnectionMaker.InitiateConnection(r.FormValue("peer")); err != nil {
|
if err := r.ParseForm(); err != nil {
|
||||||
http.Error(w, fmt.Sprint("invalid peer address: ", err), http.StatusBadRequest)
|
http.Error(w, fmt.Sprint("unable to parse form: ", err), http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
if errors := router.ConnectionMaker.InitiateConnections(r.Form["peer"]); len(errors) > 0 {
|
||||||
|
http.Error(w, errorMessages(errors), http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
muxRouter.Methods("POST").Path("/forget").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
muxRouter.Methods("POST").Path("/forget").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
router.ConnectionMaker.ForgetConnection(r.FormValue("peer"))
|
if err := r.ParseForm(); err != nil {
|
||||||
|
http.Error(w, fmt.Sprint("unable to parse form: ", err), http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
router.ConnectionMaker.ForgetConnections(r.Form["peer"])
|
||||||
})
|
})
|
||||||
|
|
||||||
http.Handle("/", muxRouter)
|
http.Handle("/", muxRouter)
|
||||||
|
|||||||
Reference in new issue
Block a user