Documentation
¶
Index ¶
- func DockerRunCmd(image, cmdFile, name string, mounts, env []string) string
- func PrepareCmdFile(ctx context.Context, cmd string) (string, func(), error)
- func SingularityRunCmd(image, cmdFile string, mounts []string) string
- type Container
- type Interactor
- type OperationErr
- type Operator
- func (o *Operator) GetContainerByID(ctx context.Context, id string) (*Container, error)
- func (o *Operator) GetContainerByPath(ctx context.Context, path string, dir string) (*Container, error)
- func (o *Operator) GetCurrentContainers(ctx context.Context) ([]*Container, error)
- func (o *Operator) GetNewContainerByName(ctx context.Context, name string) (*Container, error)
- func (o *Operator) GetNewContainers(ctx context.Context) ([]*Container, error)
- func (o *Operator) KillContainer(ctx context.Context, containerID string) error
- func (o *Operator) RememberCurrentContainers(ctx context.Context) error
- type OperatorError
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DockerRunCmd ¶
DockerRunCmd returns a `docker run` command line that will:
* Pull the given image if it is missing. * Create a container with the given name.
- That will mount the current working directory inside the container and use it as the workdir.
- That will also mount any given disk locations, in the format "/local/path:/inside/container/path" (the colon and inside path being optional if the same as local path).
- That will set the given environment variables inside the container to their values outside the container.
- That will run the command in the given file (by piping the file contents to /bin/sh); use PrepareCmdFile() to create one.
* Automatically remove the container when it exits.
func PrepareCmdFile ¶
PrepareCmdFile creates a temporary file containing the given command and returns its path, as well as a method you can defer that will delete the file.
File deletion errors in the returned method are logged using clog and the given ctx.
func SingularityRunCmd ¶
SingularityRunCmd returns a `singularity shell` command line that will:
- Pull the given image if it is missing, creating a sif image if it's a docker image.
- Create a container.
- That will run the command in the given file (by piping the file contents to the container's shell); use PrepareCmdFile() to create one.
- That will mount the given disk locations, in the format "/local/path:/inside/container/path" (the colon and inside path being optional if the same as local path). The CWD is always mounted at / in container.
- That will have all environment variables outside the container replicated inside the container.
- Automatically remove the container when it exits.
Types ¶
type Container ¶
Container struct represents a container type with specific properties.
func (*Container) HasName ¶
HasName checks if a given name is one of this container's names. Make sure to call the TrimNamePrefixes on the container before calling this function.
func (*Container) Stats ¶
Stats returns the current memory usage (RSS, in MB) and total CPU usage (in seconds) of this container.
func (*Container) TrimNamePrefixes ¶
func (c *Container) TrimNamePrefixes()
TrimNamePrefixes trims the '/' from this container's names. You must call this after adding names that might contain the prefix.
type Interactor ¶
type Interactor interface {
ContainerList(ctx context.Context) ([]*Container, error)
ContainerStats(ctx context.Context, containerID string) (*Stats, error)
ContainerKill(ctx context.Context, containerID string) error
}
Interactor defines some methods to query containers.
type OperationErr ¶
type OperationErr string
OperationErr is supplied to OperatorErr to define the reasons for the failed operations.
const ( ErrContainerList OperationErr = "Could not list the containers" ErrContainerStats OperationErr = "Could not get the container stats" ErrContainerKill OperationErr = "Container could not be killed" )
ErrContainer* are the reasons for the failed operations.
type Operator ¶
type Operator struct {
// contains filtered or unexported fields
}
Operator offers some methods for working with containers. You must use the NewOperator() method to make one, or the methods won't work.
func NewOperator ¶
func NewOperator(cntrInteractor Interactor) *Operator
NewOperator creates a new Operator, working on containers using the supplied Interactor. eg. to create an operator that can work with Docker containers:
import (
"github.com/moby/moby/client"
"github.com/VertebrateResequencing/wr/container/docker"
)
....
{
cli, err := client.NewClientWithOpts(client.FromEnv)
dockerInterator := docker.NewInteractor(cli)
dockerOperator := NewOperator(dockerInterator)
cntrList, err := dockerOperator.GetCurrentContainers(ctx)
}
...
func (*Operator) GetContainerByID ¶
GetContainerByID checks if the given id is the ID of a current container. If so, returns that container.
func (*Operator) GetContainerByPath ¶
func (o *Operator) GetContainerByPath(ctx context.Context, path string, dir string) (*Container, error)
GetContainerByPath checks the file at the given path, and if the first line contains the ID of a container, that container is returned.
The path can contain globs: `?` to match any non-separator character and `*` to match any number of them. The first file to match the glob and also contain a valid id is the one used.
In case the name is a relative file path, the second argument is the absolute path to the working directory where your container was created.
This method is useful if container IDs are written to file, as they are eg. in the case of docker when using the --cidfile argument.
func (*Operator) GetCurrentContainers ¶
GetCurrentContainers returns current containers.
func (*Operator) GetNewContainerByName ¶
GetNewContainerByName returns a container with given name. Only new containers (those not remembered by the last RememberCurrentContainers() call) are considered when searching for a container with that name.
func (*Operator) GetNewContainers ¶
GetNewContainers returns the list of all the new containers that now exists and weren't previously remembered by a RememberCurrentContainers() call.
func (*Operator) KillContainer ¶
KillContainer kills the container with the given ID.
type OperatorError ¶
type OperatorError struct {
Type OperationErr // one of our OperationErr constants
Err error
}
OperatorError records an error and a reason that caused it.
func (*OperatorError) Error ¶
func (et *OperatorError) Error() string
Error returns an error with the reason for a failed operation.
func (*OperatorError) Unwrap ¶
func (et *OperatorError) Unwrap() error
Unwrap method returns contained error of OperatorErr.