weave connect/forget take multiple peers
This commit is contained in:
1 parent
5382037c4e
commit
781686ee4a
3 files changed
+53
-39
No files matched your search
+23
-15
@@ -48,30 +48,38 @@ func (cm *ConnectionMaker) Start() {
|
||||
go cm.queryLoop(actionChan)
|
||||
}
|
||||
|
||||
func (cm *ConnectionMaker) InitiateConnection(peer string) error {
|
||||
host, port, err := net.SplitHostPort(peer)
|
||||
if err != nil {
|
||||
host = peer
|
||||
port = "0" // we use that as an indication that "no port was supplied"
|
||||
}
|
||||
addr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%s", host, port))
|
||||
if err != nil {
|
||||
return err
|
||||
func (cm *ConnectionMaker) InitiateConnections(peers []string) error {
|
||||
addrs := make(map[string]*net.TCPAddr)
|
||||
for _, peer := range peers {
|
||||
host, port, err := net.SplitHostPort(peer)
|
||||
if err != nil {
|
||||
host = peer
|
||||
port = "0" // we use that as an indication that "no port was supplied"
|
||||
}
|
||||
addr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%s:%s", host, port))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
addrs[peer] = addr
|
||||
}
|
||||
cm.actionChan <- func() bool {
|
||||
cm.cmdLinePeers[peer] = addr
|
||||
// curtail any existing reconnect interval
|
||||
if target, found := cm.targets[addr.String()]; found {
|
||||
target.tryAfter, target.tryInterval = tryImmediately()
|
||||
for peer, addr := range addrs {
|
||||
cm.cmdLinePeers[peer] = addr
|
||||
// curtail any existing reconnect interval
|
||||
if target, found := cm.targets[addr.String()]; found {
|
||||
target.tryAfter, target.tryInterval = tryImmediately()
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cm *ConnectionMaker) ForgetConnection(peer string) {
|
||||
func (cm *ConnectionMaker) ForgetConnections(peers []string) {
|
||||
cm.actionChan <- func() bool {
|
||||
delete(cm.cmdLinePeers, peer)
|
||||
for _, peer := range peers {
|
||||
delete(cm.cmdLinePeers, peer)
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,8 +28,8 @@ usage() {
|
||||
echo "weave launch [-password <password>] [-nickname <nickname>] [-iprange <cidr>] <peer> ..."
|
||||
echo "weave launch-dns <cidr>"
|
||||
echo "weave launch-proxy [-H <docker_endpoint>] [--with-dns] [--with-ipam]"
|
||||
echo "weave connect <peer>"
|
||||
echo "weave forget <peer>"
|
||||
echo "weave connect <peer> ..."
|
||||
echo "weave forget <peer> ..."
|
||||
echo "weave run [--with-dns] [<cidr> ...] <docker run args> ..."
|
||||
echo "weave start [<cidr> ...] <container_id>"
|
||||
echo "weave attach [<cidr> ...] <container_id>"
|
||||
@@ -562,6 +562,11 @@ echo_addresses() {
|
||||
echo $1 $2 $3
|
||||
}
|
||||
|
||||
peer_args() {
|
||||
local joined=$(printf "&peer=%s" "${@}")
|
||||
echo ${joined:1}
|
||||
}
|
||||
|
||||
######################################################################
|
||||
# weaveDNS helpers
|
||||
######################################################################
|
||||
@@ -878,12 +883,12 @@ case "$COMMAND" in
|
||||
echo $PROXY_CONTAINER
|
||||
;;
|
||||
connect)
|
||||
[ $# -eq 1 ] || usage
|
||||
http_call $CONTAINER_NAME $HTTP_PORT POST /connect -d "peer=$1"
|
||||
[ $# -gt 0 ] || usage
|
||||
http_call $CONTAINER_NAME $HTTP_PORT POST /connect -d $(peer_args "$@")
|
||||
;;
|
||||
forget)
|
||||
[ $# -eq 1 ] || usage
|
||||
http_call $CONTAINER_NAME $HTTP_PORT POST /forget -d "peer=$1"
|
||||
[ $# -gt 0 ] || usage
|
||||
http_call $CONTAINER_NAME $HTTP_PORT POST /forget -d $(peer_args "$@")
|
||||
;;
|
||||
status)
|
||||
http_call $CONTAINER_NAME $HTTP_PORT GET /status || true
|
||||
|
||||
+19
-18
@@ -1,10 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"code.google.com/p/gopacket/layers"
|
||||
"crypto/sha256"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"code.google.com/p/gopacket/layers"
|
||||
"github.com/davecheney/profile"
|
||||
"github.com/gorilla/mux"
|
||||
. "github.com/weaveworks/weave/common"
|
||||
@@ -12,12 +19,6 @@ import (
|
||||
"github.com/weaveworks/weave/ipam"
|
||||
weavenet "github.com/weaveworks/weave/net"
|
||||
weave "github.com/weaveworks/weave/router"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var version = "(unreleased version)"
|
||||
@@ -143,7 +144,9 @@ func main() {
|
||||
}
|
||||
|
||||
router.Start()
|
||||
initiateConnections(router, peers)
|
||||
if err := router.ConnectionMaker.InitiateConnections(peers); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// The weave script always waits for a status call to succeed,
|
||||
// so there is no point in doing "weave launch -httpaddr ''".
|
||||
@@ -181,14 +184,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 {
|
||||
allocator, err := ipam.NewAllocator(router.Ourself.Peer.Name, router.Ourself.Peer.UID, router.Ourself.Peer.NickName, iprangeCIDR, quorum)
|
||||
if err != nil {
|
||||
@@ -250,13 +245,19 @@ func handleHTTP(router *weave.Router, httpAddr string, allocator *ipam.Allocator
|
||||
})
|
||||
|
||||
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("unable to parse form: ", err), http.StatusBadRequest)
|
||||
}
|
||||
if err := router.ConnectionMaker.InitiateConnections(r.Form["peer"]); err != nil {
|
||||
http.Error(w, fmt.Sprint("invalid peer address: ", err), http.StatusBadRequest)
|
||||
}
|
||||
})
|
||||
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user