mirror of https://github.com/docker/docs.git
add node type
Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
parent
875e7c7e77
commit
2282fc89ef
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
|
@ -11,8 +12,23 @@ import (
|
||||||
|
|
||||||
type InitFunc func(url string) (DiscoveryService, error)
|
type InitFunc func(url string) (DiscoveryService, error)
|
||||||
|
|
||||||
|
type Node struct {
|
||||||
|
url string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNode(url string) *Node {
|
||||||
|
if !strings.Contains(url, "://") {
|
||||||
|
url = "http://" + url
|
||||||
|
}
|
||||||
|
return &Node{url: url}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n Node) String() string {
|
||||||
|
return n.url
|
||||||
|
}
|
||||||
|
|
||||||
type DiscoveryService interface {
|
type DiscoveryService interface {
|
||||||
Fetch() ([]string, error)
|
Fetch() ([]*Node, error)
|
||||||
Watch(int) <-chan time.Time
|
Watch(int) <-chan time.Time
|
||||||
Register(string) error
|
Register(string) error
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,15 +38,16 @@ func Init(uris string) (discovery.DiscoveryService, error) {
|
||||||
client.CreateDir(path, DEFAULT_TTL) // skip error check error because it might already exists
|
client.CreateDir(path, DEFAULT_TTL) // skip error check error because it might already exists
|
||||||
return EtcdDiscoveryService{client: client, path: path}, nil
|
return EtcdDiscoveryService{client: client, path: path}, nil
|
||||||
}
|
}
|
||||||
func (s EtcdDiscoveryService) Fetch() ([]string, error) {
|
func (s EtcdDiscoveryService) Fetch() ([]*discovery.Node, error) {
|
||||||
resp, err := s.client.Get(s.path, true, true)
|
resp, err := s.client.Get(s.path, true, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
nodes := []string{}
|
|
||||||
|
var nodes []*discovery.Node
|
||||||
|
|
||||||
for _, n := range resp.Node.Nodes {
|
for _, n := range resp.Node.Nodes {
|
||||||
nodes = append(nodes, n.Value)
|
nodes = append(nodes, discovery.NewNode(n.Value))
|
||||||
}
|
}
|
||||||
return nodes, nil
|
return nodes, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,20 +21,20 @@ func Init(file string) (discovery.DiscoveryService, error) {
|
||||||
return FileDiscoveryService{path: file}, nil
|
return FileDiscoveryService{path: file}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s FileDiscoveryService) Fetch() ([]string, error) {
|
func (s FileDiscoveryService) Fetch() ([]*discovery.Node, error) {
|
||||||
data, err := ioutil.ReadFile(s.path)
|
data, err := ioutil.ReadFile(s.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lines := []string{}
|
var nodes []*discovery.Node
|
||||||
|
|
||||||
for _, line := range strings.Split(string(data), "\n") {
|
for _, line := range strings.Split(string(data), "\n") {
|
||||||
if line != "" {
|
if line != "" {
|
||||||
lines = append(lines, line)
|
nodes = append(nodes, discovery.NewNode(line))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return lines, nil
|
return nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s FileDiscoveryService) Watch(heartbeat int) <-chan time.Time {
|
func (s FileDiscoveryService) Watch(heartbeat int) <-chan time.Time {
|
||||||
|
|
|
@ -37,7 +37,7 @@ func New(url string) *TokenDiscoveryService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchNodes returns the node for the discovery service at the specified endpoint
|
// FetchNodes returns the node for the discovery service at the specified endpoint
|
||||||
func (s TokenDiscoveryService) Fetch() ([]string, error) {
|
func (s TokenDiscoveryService) Fetch() ([]*discovery.Node, error) {
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/%s/%s", s.url, "clusters", s.token))
|
resp, err := http.Get(fmt.Sprintf("%s/%s/%s", s.url, "clusters", s.token))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -54,7 +54,12 @@ func (s TokenDiscoveryService) Fetch() ([]string, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return addrs, nil
|
var nodes []*discovery.Node
|
||||||
|
for _, addr := range addrs {
|
||||||
|
nodes = append(nodes, discovery.NewNode(addr))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s TokenDiscoveryService) Watch(heartbeat int) <-chan time.Time {
|
func (s TokenDiscoveryService) Watch(heartbeat int) <-chan time.Time {
|
||||||
|
|
|
@ -28,7 +28,7 @@ func TestRegister(t *testing.T) {
|
||||||
addrs, err := discovery.Fetch()
|
addrs, err := discovery.Fetch()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, len(addrs), 1)
|
assert.Equal(t, len(addrs), 1)
|
||||||
assert.Equal(t, addrs[0], expected)
|
assert.Equal(t, addrs[0].String(), "http://"+expected)
|
||||||
|
|
||||||
assert.NoError(t, discovery.Register(expected))
|
assert.NoError(t, discovery.Register(expected))
|
||||||
}
|
}
|
||||||
|
|
18
manage.go
18
manage.go
|
@ -5,7 +5,6 @@ import (
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/codegangsta/cli"
|
"github.com/codegangsta/cli"
|
||||||
|
@ -72,14 +71,11 @@ func manage(c *cli.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
refresh := func(c *cluster.Cluster, nodes []string) {
|
refresh := func(c *cluster.Cluster, nodes []*discovery.Node) {
|
||||||
for _, addr := range nodes {
|
for _, addr := range nodes {
|
||||||
go func(addr string) {
|
go func(node *discovery.Node) {
|
||||||
if !strings.Contains(addr, "://") {
|
if c.Node(node.String()) == nil {
|
||||||
addr = "http://" + addr
|
n := cluster.NewNode(node.String())
|
||||||
}
|
|
||||||
if c.Node(addr) == nil {
|
|
||||||
n := cluster.NewNode(addr)
|
|
||||||
if err := n.Connect(tlsConfig); err != nil {
|
if err := n.Connect(tlsConfig); err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
return
|
return
|
||||||
|
@ -119,7 +115,11 @@ func manage(c *cli.Context) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
} else {
|
} else {
|
||||||
refresh(cluster, c.Args())
|
var nodes []*discovery.Node
|
||||||
|
for _, arg := range c.Args() {
|
||||||
|
nodes = append(nodes, discovery.NewNode(arg))
|
||||||
|
}
|
||||||
|
refresh(cluster, nodes)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue