Class RunService


  • public class RunService
    extends Object
    Service class for helping in running containers.
    Since:
    16/06/15
    Author:
    roland
    • Method Detail

      • createContainer

        public String createContainer​(ImageConfiguration imageConfig,
                                      PortMapping portMapping,
                                      GavLabel gavLabel,
                                      Properties properties,
                                      File baseDir,
                                      String defaultContainerNamePattern,
                                      Date buildTimestamp)
                               throws DockerAccessException
        Create a container with the given image configuration.
        Parameters:
        imageConfig - image configuration holding the run information and the image name
        portMapping - container port mapping
        gavLabel - label to tag the started container with
        properties - properties to fill in with dynamically assigned ports
        defaultContainerNamePattern - pattern to use for naming containers. Can be null in which case a default pattern is used
        buildTimestamp - date which should be used as the timestamp when calculating container names
        Returns:
        the container id
        Throws:
        DockerAccessException - if access to the docker backend fails
      • removeContainer

        public void removeContainer​(String containerId,
                                    boolean removeVolumes)
                             throws DockerAccessException
        Remove a container.
        Parameters:
        containerId - the container id
        removeVolumes - whether to remove volumes with container
        Throws:
        DockerAccessException - if access to the docker backend fails
      • createAndStartContainer

        public String createAndStartContainer​(ImageConfiguration imageConfig,
                                              PortMapping portMapping,
                                              GavLabel gavLabel,
                                              Properties properties,
                                              File baseDir,
                                              String defaultContainerNamePattern,
                                              Date buildTimestamp)
                                       throws DockerAccessException
        Create and start a container with the given image configuration.
        Parameters:
        imageConfig - image configuration holding the run information and the image name
        portMapping - container port mapping
        gavLabel - label to tag the started container with
        properties - properties to fill in with dynamically assigned ports
        defaultContainerNamePattern - pattern to use for naming containers. Can be null in which case a default pattern is used
        buildTimestamp - date which should be used as the timestamp when calculating container names
        Returns:
        the container id
        Throws:
        DockerAccessException - if access to the docker backend fails
      • stopContainer

        public void stopContainer​(String containerId,
                                  ImageConfiguration imageConfig,
                                  boolean keepContainer,
                                  boolean removeVolumes)
                           throws DockerAccessException,
                                  ExecException
        Stop a container immediately by id.
        Parameters:
        containerId - the container to stop
        imageConfig - image configuration for this container
        keepContainer - whether to keep container or to remove them after stoppings
        removeVolumes - whether to remove volumes after stopping
        Throws:
        DockerAccessException
        ExecException
      • stopPreviouslyStartedContainer

        public void stopPreviouslyStartedContainer​(String containerId,
                                                   boolean keepContainer,
                                                   boolean removeVolumes)
                                            throws DockerAccessException,
                                                   ExecException
        Lookup up whether a certain has been already started and registered. If so, stop it
        Parameters:
        containerId - the container to stop
        keepContainer - whether to keep container or to remove them after stoppings
        removeVolumes - whether to remove volumes after stopping
        Throws:
        DockerAccessException
        ExecException
      • stopStartedContainers

        public void stopStartedContainers​(boolean keepContainer,
                                          boolean removeVolumes,
                                          boolean removeCustomNetworks,
                                          GavLabel gavLabel)
                                   throws DockerAccessException,
                                          ExecException
        Stop all registered container
        Parameters:
        keepContainer - whether to keep container or to remove them after stopping
        removeVolumes - whether to remove volumes after stopping
        Throws:
        DockerAccessException - if during stopping of a container sth fails
        ExecException
      • lookupContainer

        public String lookupContainer​(String lookup)
        Lookup a container that has been started
        Parameters:
        lookup - a container by id or alias
        Returns:
        the container id if the container exists, null otherwise.
      • getContainers

        public List<RunService.ContainerDescriptor> getContainers​(GavLabel gavLabel)
        Get all started containers. The containers are returned in order of their registration. If no pom label is given, then all containers are returned.
        Parameters:
        gavLabel - the label for which to get the containers or null for all containers
        Returns:
        the containers for the given label or an empty collection
      • createPortMapping

        public PortMapping createPortMapping​(RunImageConfiguration runConfig,
                                             Properties properties)
        Create port mapping for a specific configuration as it can be used when creating containers
        Parameters:
        runConfig - the cun configuration
        properties - properties to lookup variables
        Returns:
        the portmapping
      • addShutdownHookForStoppingContainers

        public void addShutdownHookForStoppingContainers​(boolean keepContainer,
                                                         boolean removeVolumes,
                                                         boolean removeCustomNetworks)
        Add a shutdown hook in order to stop all registered containers
      • createVolumesAsPerVolumeBinds

        public List<String> createVolumesAsPerVolumeBinds​(ServiceHub hub,
                                                          List<String> binds,
                                                          List<VolumeConfiguration> volumes)
                                                   throws DockerAccessException
        Creates a Volume if a volume is referred to during startup in bind mount mapping and a VolumeConfiguration exists
        Parameters:
        hub - Service hub
        binds - volume binds present in ImageConfiguration
        volumes - VolumeConfigs present
        Returns:
        List of volumes created
        Throws:
        DockerAccessException