swarm

package module
v0.0.0-...-5667256 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2022 License: AGPL-3.0 Imports: 16 Imported by: 1

README

go-swarm

Go

go-swarm is a Go library and command-line tool for managing the creation and maintenance of Docker Swarm cluster.

Features:

  • Creates new Swarm Cluster given a Terraform Clusterfile as input.
  • Retrives information about Swarm Clsuters.
  • Join new workers or managers to an existing Swarm Cluster.
  • Assigning Swarm Labels based on underlying VM Node labels.
  • Add-hoc adding new worker or manager nodes.
  • Draining Swarm nodes.
  • Removing Swarm ndoes.

Install

Currently tehre is a command-line tool called swarm that can be installed with:

go install github.com/aucloud/cmd/swarm@latest

Using as a library is to be documented at a later date.

usage

Using the swarm CLI tool is easy:

$ ./swarm
This is a command-line Docker Swarm Manager

This tool is an implementation of the swarm management library used to help
facilitate and automate the creation and management of Docker Swarm Clusters.

Supported functions include:

- Creating a Swarm Clsuter
- Adding new worker or manager nodes
- Draining nodes
- Removing nodes
- Displaying cluster information

Usage:
  swarm [command]

Available Commands:
  create      Creates a new Swarm Cluster
  help        Help about any command
  info        Retrieve and display Swarm Cluster Information
  status      Retrieve and display Swarm Cluster Status

Flags:
      --config string     config file (default is $HOME/.swarm.yaml)
  -D, --debug             Enable debug logging
  -h, --help              help for swarm
  -A, --ssh-addr string   SSH Address to connect to
  -K, --ssh-key string    SSH Key to use for remote execution (default "$HOME/.ssh/id_rsa")
  -U, --ssh-user string   SSH User to use for remote execution (default "rancher")
  -v, --version           version for swarm

Use "swarm [command] --help" for more information about a command.

For example to create a new Swarm cluster from a Terraform run:

terraform output -json Clusterfile | swarm -D create -

This will take the Clusterfile (a JSON representing the VM Nodes created via Terraform) and create a multi-manager Swarm Cluster and join all worker nodes and display the cluster status at the end.

cat Clusterfile.json
{
  "region": "local",
  "environment": "test",
  "cluster": "c1",
  "domain": "localdomain",
  "nodes": [{
    "hostname": "dm1",
    "public_address": "10.0.0.1",
    "private_address": "172.16.0.1",
    "tags": {
      "role": "manager"
    }
  }]
}

License

go-swarm is licensed under the terms of the AGPLv3

Documentation

Index

Constants

View Source
const (
	// RoleTag is the tag (Custom Attribute in vSphere)
	// for tagging VM(s) with either "manager" or "worker"
	// This is used to assign Docker Swarm roles to VM(s).
	RoleTag = "role"

	// ManagerRole denotates a Docker Swarm role of "manager"
	ManagerRole = "manager"

	// WorkerRole denotates a Docker Swarm role of "worker"
	WorkerRole = "worker"

	// LabelsTag is the tag (Custom Attribute in vSphere)
	// for freeform labels applied to VM(s) in the form
	// `key1=value1&key2=value2&key3&key4`
	// (This uses the URL Query String format).
	LabelsTag = "labels"
)
View Source
const (
	DefaultTimeout = time.Minute * 5
)

Variables

This section is empty.

Functions

func HasString

func HasString(a []string, x string) bool

func ParseLabels

func ParseLabels(q string) (url.Values, error)

Types

type ClusterInfo

type ClusterInfo struct {
	ID        string
	CreatedAt string
}

type Clusterfile

type Clusterfile struct {
	Region      string `json:"region"`
	Environment string `json:"environment"`
	Cluster     string `json:"cluster"`
	Domain      string `json:"domain"`

	Nodes VMNodes `json:"nodes"`
}

Clusterfile represents a set of VMNode(s) as a collection of VM(s) along with the region, enviornment, cluster and domain those nodes belong to.

func ReadClusterfile

func ReadClusterfile(r io.Reader) (Clusterfile, error)

ReadClusterfile reads a `Clusterfile` or `Clusterfile.json` from an `io.Reader` such as an open file or stadnard input and parses it into a `ClusterInfo` struct.

func (*Clusterfile) Validate

func (cf *Clusterfile) Validate() error

type Config

type Config struct {
	Timeout time.Duration
}

func NewDefaultConfig

func NewDefaultConfig() *Config

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages all operations of a Docker Swarm cluster with flexible Switcher implementations that permit talking to Docker Nodes over different types of transport (e.g: local or remote).

func NewManager

func NewManager(switcher Switcher, options ...Option) (*Manager, error)

NewManager constructs a new Manager type with the provider Switcher

func (*Manager) CreateSwarm

func (m *Manager) CreateSwarm(vms VMNodes, force bool) error

CreateSwarm creates a new Docker Swarm cluster given a set of nodes

func (*Manager) DrainNodes

func (m *Manager) DrainNodes(nodes []string) error

DrainNodes drains one or more nodes from an existing Docker Swarm cluster and blocks until there are no more tasks running on thoese nodes.

func (*Manager) GetInfo

func (m *Manager) GetInfo() (NodeInfo, error)

GetInfo returns information about the current node

func (*Manager) GetManagers

func (m *Manager) GetManagers() ([]NodeInfo, error)

GetManagers returns a list of manager nodes and their information

func (*Manager) GetNodes

func (m *Manager) GetNodes() ([]NodeStatus, error)

GetNodes returns all nodes in the cluster

func (*Manager) JoinToken

func (m *Manager) JoinToken(tokenType string) (string, error)

JoinToken retrieves the current join token for the given type "manager" or "worker" from any of the managers in the cluster

func (*Manager) LabelNode

func (m *Manager) LabelNode(node VMNode) error

func (*Manager) Runner

func (m *Manager) Runner() runcmd.Runner

Runner returns the current Runner for the current Switcher being used

func (*Manager) SwitchNode

func (m *Manager) SwitchNode(nodeAddr string) error

SwitchNode switches to a new node given by nodeAddr to perform operations on

func (*Manager) SwitchNodeVia

func (m *Manager) SwitchNodeVia(nodeAddr string) error

SwitchNodeVia switches to a new node given by nodeAddr by jumping through the current node as a "bastion" host to perform operations on the node.

func (*Manager) Switcher

func (m *Manager) Switcher() Switcher

Switcher returns the current Switcher for the manager being used

func (*Manager) UpdateSwarm

func (m *Manager) UpdateSwarm(vms VMNodes) error

UpdateSwarm updates an existing Docker Swarm cluster by adding any missing manager or worker nodes that aren't already part of the cluster

type NodeInfo

type NodeInfo struct {
	ID     string
	Name   string
	Labels []string

	OSType          string
	OSVersion       string
	KernelVersion   string
	OperatingSystem string

	NCPU     int
	MemTotal int64

	ServerVersion string

	Swarm SwarmInfo
}

func (NodeInfo) IsManager

func (node NodeInfo) IsManager() bool

type NodeStatus

type NodeStatus struct {
	ID            string
	Hostname      string
	EngineVersion string
	Availability  string
	ManagerStatus string
	Status        string
}

type Nodes

type Nodes []NodeStatus

type Option

type Option func(*Config) error

func WithTimeout

func WithTimeout(timeout time.Duration) Option

type RemoteManager

type RemoteManager struct {
	NodeID string
	Addr   string
}

type SwarmInfo

type SwarmInfo struct {
	NodeID           string
	NodeAddr         string
	LocalNodeState   string
	ControlAvailable bool

	Nodes          int
	Managers       int
	RemoteManagers []RemoteManager

	Cluster ClusterInfo
}

type Switcher

type Switcher interface {
	fmt.Stringer
	Switch(ctx context.Context, nodeAddr string) error
	SwitchVia(ctx context.Context, nodeAddr string) error
	Runner() runcmd.Runner
}

Switcher is the interface that describes how to switch between Docker Nodes Implementations must implement the `Swithc()` method that should return an appropriate `runcmd.Runner` interface type for operating on Docker Nodes.

func NewLocalSwitcher

func NewLocalSwitcher() (Switcher, error)

NewLocalSwitcher constructs a new Switcher that talks directly to a local Docker UNIX Socket on a single-node

func NewNullSwitcher

func NewNullSwitcher() (Switcher, error)

func NewSSHSwitcher

func NewSSHSwitcher(user, addr, key string, timeout time.Duration) (Switcher, error)

NewSSHSwitcher constructs a new Switcher that connect to remote Docker nodes' UNIX Sockets over SSH

type TaskStatus

type TaskStatus struct {
	ID           string
	Name         string
	Image        string
	Error        string
	Node         string
	Ports        string
	CurrentState string
	DesiredState string
}

func (TaskStatus) Shutdown

func (t TaskStatus) Shutdown() bool

type Tasks

type Tasks []TaskStatus

func (Tasks) AllShutdown

func (ts Tasks) AllShutdown() bool

type VMNode

type VMNode struct {
	Hostname       string            `json:"hostname"`
	PublicAddress  string            `json:"public_address"`
	PrivateAddress string            `json:"private_address"`
	Tags           map[string]string `json:"tags"`
}

VMNode represents a single VM Node and at a bare minimum contains the node's hostname, private and public ip addresses as well as a list of tags used to label the nodes for different purposes such as Manager ndoes.

func (VMNode) GetTag

func (vm VMNode) GetTag(name string) string

func (VMNode) HasTag

func (vm VMNode) HasTag(name, value string) bool

func (VMNode) Stirng

func (vm VMNode) Stirng() string

type VMNodes

type VMNodes []VMNode

func (VMNodes) FilterByPrivateAddress

func (vms VMNodes) FilterByPrivateAddress(address string) VMNodes

func (VMNodes) FilterByPublicAddress

func (vms VMNodes) FilterByPublicAddress(address string) VMNodes

func (VMNodes) FilterByTag

func (vms VMNodes) FilterByTag(name, value string) VMNodes

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL