compose

package
v0.0.0-...-dc5648e Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2018 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package compose is the main rocker-compose facade. It provides functions to execute various rocker-compose tasks based on the given manifest.

Rocker-compose is a docker composition tool with idempotency features for deploying applications that consist of multiple containers.

Index

Constants

This section is empty.

Variables

View Source
var NoAction = &noAction{}

NoAction is an empty action which does nothing

Functions

func GetBridgeIP

func GetBridgeIP(client *docker.Client) (ip string, err error)

GetBridgeIP gets the ip address of docker network bridge it is useful when you want to loose couple containers and not have tightly link them container A may publish port 8125 to host network and container B may access this port through a bridge ip address; it's a hacky solution, any better way to obtain bridge ip without ssh access to host machine is welcome

Here we create a dummy container and look at .NetworkSettings.Gateway value

TODO: maybe we don't need this anymore since docker 1.8 seem to specify all existing containers

in a /etc/hosts file of every contianer. Need to research it further.

https://github.com/docker/docker/issues/1143 https://github.com/docker/docker/issues/11247

func NewContainerFormatter

func NewContainerFormatter(container *Container, level log.Level) log.Formatter

NewContainerFormatter returns an object that is given to logrus to better format contaienr output

func PullDockerImage

func PullDockerImage(client *docker.Client, image *imagename.ImageName, auth *docker.AuthConfigurations) (*docker.Image, error)

PullDockerImage pulls an image and streams to a logger respecting terminal features

func WalkActions

func WalkActions(actions []Action, fn func(action Action))

WalkActions recursively though all action and applies given function to every action.

Types

type Action

type Action interface {
	Execute(client Client) error
	String() string
}

Action interface describes action that can be done by rocker-compose docker client

func NewEnsureContainerExistAction

func NewEnsureContainerExistAction(c *Container) Action

NewEnsureContainerExistAction makes action that ensures that container exists

func NewEnsureContainerStateAction

func NewEnsureContainerStateAction(c *Container) Action

NewEnsureContainerStateAction makes action that ensures that container state is a desired one

func NewRemoveContainerAction

func NewRemoveContainerAction(c *Container) Action

NewRemoveContainerAction makes action that removes a container

func NewRunContainerAction

func NewRunContainerAction(c *Container) Action

NewRunContainerAction makes action that runs a container

func NewStepAction

func NewStepAction(async bool, actions ...Action) Action

NewStepAction makes a "step" wrapper which holds the list of actions that may run in parallel. Multiple steps can only run one by one. Steps can be nested.

func NewWaitContainerAction

func NewWaitContainerAction(c *Container) Action

NewWaitContainerAction makes action that waits for container

type Client

type Client interface {
	GetContainers(global bool) ([]*Container, error)
	RemoveContainer(container *Container) error
	RunContainer(container *Container) error
	EnsureContainerExist(name *Container) error
	EnsureContainerState(name *Container) error
	PullAll(containers []*Container, vars template.Vars) error
	Clean(config *config.Config) error
	AttachToContainers(container []*Container) error
	AttachToContainer(container *Container) error
	FetchImages(containers []*Container, vars template.Vars) error
	WaitForContainer(container *Container) error
	GetPulledImages() []*imagename.ImageName
	GetRemovedImages() []*imagename.ImageName
	Pin(local, hub bool, vars template.Vars, containers []*Container) error
}

Client interface describes a rocker-compose client that can do various operations needed for rocker-compose to make changes.

type Compose

type Compose struct {
	Manifest *config.Config
	DryRun   bool
	Attach   bool
	Pull     bool
	Remove   bool
	Wait     time.Duration
	// contains filtered or unexported fields
}

Compose is the main object that executes actions and holds runtime information.

func New

func New(config *Config) (*Compose, error)

New makes a new Compose object

func (*Compose) CleanAction

func (compose *Compose) CleanAction() error

CleanAction implements 'rocker-compose clean'

func (*Compose) PinAction

func (compose *Compose) PinAction(local, hub bool) (template.Vars, error)

PinAction implements 'rocker-compose pin'

func (*Compose) PullAction

func (compose *Compose) PullAction() error

PullAction implements 'rocker-compose pull'

func (*Compose) RecoverAction

func (compose *Compose) RecoverAction() error

RecoverAction implements 'rocker-compose recover'

TODO: It duplicates the code of RunAction a bit. Also, do we need this function at all?

Docker starts containers of "restart=always" automatically after daemon restart.

func (*Compose) RunAction

func (compose *Compose) RunAction() error

RunAction implements 'rocker-compose run'

func (*Compose) WritePlan

func (compose *Compose) WritePlan(resp *ansible.Response) *ansible.Response

WritePlan saves various rocker-compose change information to the ansible.Response object TODO: should compose know about ansible.Response at all?

maybe it should give some data struct back to main?

type Config

type Config struct {
	Manifest   *config.Config
	Docker     *docker.Client
	Force      bool
	DryRun     bool
	Attach     bool
	Pull       bool
	Remove     bool
	Recover    bool
	Wait       time.Duration
	Auth       *docker.AuthConfigurations
	KeepImages int
}

Config is a configuration object which is passed to compose.New() for creating the new Compose instance.

type Container

type Container struct {
	ID            string
	Image         *imagename.ImageName
	ImageResolved *imagename.ImageName
	ImageID       string
	Name          *config.ContainerName
	Created       time.Time
	State         *ContainerState
	Config        *config.Container
	Io            *ContainerIo
	// contains filtered or unexported fields
}

Container object represents a single container produced by a rocker-compose spec

func GetContainersFromConfig

func GetContainersFromConfig(cfg *config.Config) []*Container

GetContainersFromConfig returns the list of Container objects from a spec Config object.

func NewContainerFromConfig

func NewContainerFromConfig(name *config.ContainerName, containerConfig *config.Container) *Container

NewContainerFromConfig makes a single Container object from a spec Config object.

func NewContainerFromDocker

func NewContainerFromDocker(dockerContainer *docker.Container) (*Container, error)

NewContainerFromDocker converts a container object given by docker client to a local Container object

func (*Container) CreateContainerOptions

func (a *Container) CreateContainerOptions() (*docker.CreateContainerOptions, error)

CreateContainerOptions returns create configuration eatable by go-dockerclient

func (*Container) IsEqualTo

func (a *Container) IsEqualTo(b *Container) bool

IsEqualTo returns true if current and given containers are equal by all dimensions. It compares configuration, image id (image can be updated), state (running, craeted)

func (*Container) IsSameKind

func (a *Container) IsSameKind(b *Container) bool

IsSameKind returns true if current and given containers have same name, without considering namespace

func (*Container) IsSameNamespace

func (a *Container) IsSameNamespace(b *Container) bool

IsSameNamespace returns true if current and given containers are from same namespace

func (Container) String

func (a Container) String() string

String returns container name

type ContainerIo

type ContainerIo struct {
	Stdout io.Writer
	Stderr io.Writer
	// contains filtered or unexported fields
}

ContainerIo initializes and maintains container I/O and also owns 'done' channel that can be used by other actors

func NewContainerIo

func NewContainerIo(container *Container) *ContainerIo

NewContainerIo makes ContainerIo objects and initializes formatters for container's stdout and stderr streams

func (*ContainerIo) Done

func (cio *ContainerIo) Done(err error)

Done marks I/O not alive and if it not becomes alive during next 1 second, then it sends to 'done' channel

func (*ContainerIo) Resurrect

func (cio *ContainerIo) Resurrect()

Resurrect marks I/O alive

func (*ContainerIo) Wait

func (cio *ContainerIo) Wait() error

Wait waits for 'done' event for a container I/O

type ContainerState

type ContainerState struct {
	Running    bool
	Paused     bool
	Restarting bool
	OOMKilled  bool
	Pid        int
	ExitCode   int
	Error      string
	StartedAt  time.Time
	FinishedAt time.Time
}

ContainerState represents the state of a container.

func (*ContainerState) IsEqualState

func (a *ContainerState) IsEqualState(b *ContainerState) bool

IsEqualState returns true if current and given containers have the same state

type Diff

type Diff interface {
	Diff(expected []*Container, actual []*Container) ([]Action, error)
}

Diff describes a comparison functionality of two container sets: expected and actual 'expected' is a list of containers from a spec (compose.yml) 'actual' is a list of existing containers in a docker daemon

The Diff function should return the action list that is needed to transition form the 'actual' state to the 'expected' one.

func NewDiff

func NewDiff(ns string) Diff

NewDiff returns an implementation of Diff object

type DockerClient

type DockerClient struct {
	Docker     *docker.Client
	Attach     bool
	Wait       time.Duration
	Auth       *docker.AuthConfigurations
	KeepImages int
	Recover    bool
	// contains filtered or unexported fields
}

DockerClient is an implementation of Client interface that do operations to a given docker client

func NewClient

func NewClient(initialClient *DockerClient) (*DockerClient, error)

NewClient makes a new DockerClient object based on configuration params that is given with input DockerClient object.

func (*DockerClient) AttachToContainer

func (client *DockerClient) AttachToContainer(container *Container) error

AttachToContainer attaches to a running container and redirects its streams to log

func (*DockerClient) AttachToContainers

func (client *DockerClient) AttachToContainers(containers []*Container) error

AttachToContainers attaches to all containers that specified to be running

func (*DockerClient) Clean

func (client *DockerClient) Clean(config *config.Config) error

Clean finds the obsolete image tags from container specs that exist in docker daemon, skipping topN images that we want to keep (keep_images, default 5) and deletes them.

func (*DockerClient) EnsureContainerExist

func (client *DockerClient) EnsureContainerExist(container *Container) error

EnsureContainerExist implements ensuring that container exists in docker daemon

func (*DockerClient) EnsureContainerState

func (client *DockerClient) EnsureContainerState(container *Container) error

EnsureContainerState checks that the state of existing docker daemon container equals expected state specified in the spec.

func (*DockerClient) FetchImages

func (client *DockerClient) FetchImages(containers []*Container, vars template.Vars) error

FetchImages fetches the missing images for all containers in the manifest

func (*DockerClient) GetContainers

func (client *DockerClient) GetContainers(global bool) ([]*Container, error)

GetContainers implements the retrieval of existing containers from the docker daemon. It fetches the list and then inspects every container in parallel (pmap). Timeouts after 30 seconds if some inspect operations hanged.

func (*DockerClient) GetPulledImages

func (client *DockerClient) GetPulledImages() []*imagename.ImageName

GetPulledImages returns the list of images pulled by a recent run

func (*DockerClient) GetRemovedImages

func (client *DockerClient) GetRemovedImages() []*imagename.ImageName

GetRemovedImages returns the list of images removed by a recent run

func (*DockerClient) Pin

func (client *DockerClient) Pin(local, hub bool, vars template.Vars, containers []*Container) error

Pin resolves versions for given containers

func (*DockerClient) PullAll

func (client *DockerClient) PullAll(containers []*Container, vars template.Vars) error

PullAll grabs all image names from containers in spec and pulls all of them

func (*DockerClient) RemoveContainer

func (client *DockerClient) RemoveContainer(container *Container) error

RemoveContainer implements removing a container

func (*DockerClient) RunContainer

func (client *DockerClient) RunContainer(container *Container) error

RunContainer implements creating and optionally running a container depending on its state preference.

func (*DockerClient) StartContainer

func (client *DockerClient) StartContainer(container *Container) error

StartContainer implements starting a container If contianer state is "ran" then it waits until container exit and checks exit code; otherwise it waits for configurable '--wait' seconds interval and ensures container not exited.

func (*DockerClient) WaitForContainer

func (client *DockerClient) WaitForContainer(container *Container) (err error)

WaitForContainer waits for a container and checks exit code at the end If exitCode != 0 then fires an error

type ErrContainerBadState

type ErrContainerBadState struct {
	Container  *Container
	Running    bool
	OOMKilled  bool
	ExitCode   int
	ErrorStr   string
	StartedAt  time.Time
	FinishedAt time.Time
}

ErrContainerBadState is an error that describes state inconsistency that can be checked by EnsureContainerState function

func (ErrContainerBadState) Error

func (e ErrContainerBadState) Error() string

Error returns string representation of the error

type Runner

type Runner interface {
	Run([]Action) error
}

Runner interface describes a runnable facade which executes given list of actions

func NewDockerClientRunner

func NewDockerClientRunner(client Client) Runner

NewDockerClientRunner makes a runner that uses a DockerClient for executing actions

func NewDryRunner

func NewDryRunner() Runner

NewDryRunner makes a runner that does not actually execute actions, but prints them

Directories

Path Synopsis
Package ansible is providing data structures and functions for responses when rocker-compose is running in ansible mode (rocker-compose run --ansible)
Package ansible is providing data structures and functions for responses when rocker-compose is running in ansible mode (rocker-compose run --ansible)
Package config holds functionality for processing compose.yml manifests, templating, converting manifests to docker api run spec and comparing them against each other.
Package config holds functionality for processing compose.yml manifests, templating, converting manifests to docker api run spec and comparing them against each other.

Jump to

Keyboard shortcuts

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