docs/cluster.go

82 lines
1.6 KiB
Go

package libcluster
import (
"errors"
"strings"
"sync"
)
var (
ErrNodeNotConnected = errors.New("node is not connected to docker's REST API")
ErrNodeAlreadyRegistered = errors.New("node was already added to the cluster")
)
type Cluster struct {
sync.Mutex
nodes map[string]*Node
}
func NewCluster() *Cluster {
return &Cluster{
nodes: make(map[string]*Node),
}
}
// Register a node within the cluster. The node must have been already
// initialized.
func (c *Cluster) AddNode(n *Node) error {
if !n.IsConnected() {
return ErrNodeNotConnected
}
c.Lock()
defer c.Unlock()
if _, exists := c.nodes[n.ID]; exists {
return ErrNodeAlreadyRegistered
}
c.nodes[n.ID] = n
return nil
}
// Containers returns all the containers in the cluster.
func (c *Cluster) Containers() []*Container {
c.Lock()
defer c.Unlock()
out := []*Container{}
for _, n := range c.nodes {
containers := n.Containers()
for _, container := range containers {
out = append(out, container)
}
}
return out
}
// Container returns the container with ID in the cluster
func (c *Cluster) Container(IdOrName string) *Container {
for _, container := range c.Containers() {
// Match ID prefix.
if strings.HasPrefix(container.Id, IdOrName) {
return container
}
// Match name, /name or engine/name.
for _, name := range container.Names {
if name == IdOrName || name == "/"+IdOrName || container.node.ID+name == IdOrName {
return container
}
}
}
return nil
}
// Nodes returns the list of nodes in the cluster
func (c *Cluster) Nodes() map[string]*Node {
return c.nodes
}