2021-12-06 12:24:22 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NodeList return all swarm nodes.
|
|
|
|
func (d *Docker) NodeList(ctx context.Context) (nodes []swarm.Node, err error) {
|
|
|
|
var c *client.Client
|
|
|
|
c, err = d.client()
|
|
|
|
if err == nil {
|
|
|
|
nodes, err = c.NodeList(ctx, types.NodeListOptions{})
|
|
|
|
if err == nil {
|
|
|
|
sort.Slice(nodes, func(i, j int) bool {
|
|
|
|
return nodes[i].Description.Hostname < nodes[j].Description.Hostname
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeUpdate update a node.
|
|
|
|
func (d *Docker) NodeUpdate(ctx context.Context, id string, version uint64, spec *swarm.NodeSpec) error {
|
|
|
|
return d.call(func(cli *client.Client) (err error) {
|
|
|
|
return cli.NodeUpdate(ctx, id, newVersion(version), *spec)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeRemove remove a swarm node from cluster.
|
|
|
|
func (d *Docker) NodeRemove(ctx context.Context, id string) error {
|
|
|
|
return d.call(func(cli *client.Client) (err error) {
|
|
|
|
return cli.NodeRemove(ctx, id, types.NodeRemoveOptions{})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeCount return number of swarm nodes.
|
|
|
|
func (d *Docker) NodeCount(ctx context.Context) (count int, err error) {
|
|
|
|
err = d.call(func(cli *client.Client) (err error) {
|
|
|
|
var nodes []swarm.Node
|
|
|
|
nodes, err = cli.NodeList(ctx, types.NodeListOptions{})
|
|
|
|
if err == nil {
|
|
|
|
count = len(nodes)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeInspect return node information.
|
|
|
|
func (d *Docker) NodeInspect(ctx context.Context, id string) (node swarm.Node, raw []byte, err error) {
|
|
|
|
err = d.call(func(cli *client.Client) (err error) {
|
|
|
|
node, raw, err = cli.NodeInspectWithRaw(ctx, id)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-12-17 12:13:58 +00:00
|
|
|
|
2021-12-20 06:28:43 +00:00
|
|
|
func (d *Docker) NodeMap() (map[string]*Node, error) {
|
2021-12-23 06:40:14 +00:00
|
|
|
nodes, err := d.nodes.Get(true)
|
2021-12-17 12:13:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-23 06:40:14 +00:00
|
|
|
return nodes.(map[string]*Node), nil
|
2021-12-17 12:13:58 +00:00
|
|
|
}
|