Package io.fabric8.maven.docker.service
Class RunService
- java.lang.Object
-
- io.fabric8.maven.docker.service.RunService
-
public class RunService extends Object
Service class for helping in running containers.- Since:
- 16/06/15
- Author:
- roland
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRunService.ContainerDescriptor
-
Constructor Summary
Constructors Constructor Description RunService(DockerAccess docker, QueryService queryService, ContainerTracker tracker, LogOutputSpecFactory logConfig, Logger log)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddShutdownHookForStoppingContainers(boolean keepContainer, boolean removeVolumes, boolean removeCustomNetworks)Add a shutdown hook in order to stop all registered containersStringcreateAndStartContainer(ImageConfiguration imageConfig, PortMapping portMapping, GavLabel gavLabel, Properties properties, File baseDir, String defaultContainerNamePattern, Date buildTimestamp)Create and start a container with the given image configuration.StringcreateContainer(ImageConfiguration imageConfig, PortMapping portMapping, GavLabel gavLabel, Properties properties, File baseDir, String defaultContainerNamePattern, Date buildTimestamp)Create a container with the given image configuration.voidcreateCustomNetworkIfNotExistant(String customNetwork)PortMappingcreatePortMapping(RunImageConfiguration runConfig, Properties properties)Create port mapping for a specific configuration as it can be used when creating containersList<String>createVolumesAsPerVolumeBinds(ServiceHub hub, List<String> binds, List<VolumeConfiguration> volumes)Creates a Volume if a volume is referred to during startup in bind mount mapping and a VolumeConfiguration existsStringexecInContainer(String containerId, String command, ImageConfiguration imageConfiguration)Create and start a Exec container with the given image configuration.List<RunService.ContainerDescriptor>getContainers(GavLabel gavLabel)Get all started containers.List<StartOrderResolver.Resolvable>getImagesConfigsInOrder(QueryService queryService, List<ImageConfiguration> images)Get the proper order for images to startStringlookupContainer(String lookup)Lookup a container that has been startedvoidremoveContainer(String containerId, boolean removeVolumes)Remove a container.voidremoveCustomNetworks(Collection<Network> networks)voidstopContainer(String containerId, ImageConfiguration imageConfig, boolean keepContainer, boolean removeVolumes)Stop a container immediately by id.voidstopPreviouslyStartedContainer(String containerId, boolean keepContainer, boolean removeVolumes)Lookup up whether a certain has been already started and registered.voidstopStartedContainers(boolean keepContainer, boolean removeVolumes, boolean removeCustomNetworks, GavLabel gavLabel)Stop all registered container
-
-
-
Constructor Detail
-
RunService
public RunService(DockerAccess docker, QueryService queryService, ContainerTracker tracker, LogOutputSpecFactory logConfig, Logger log)
-
-
Method Detail
-
execInContainer
public String execInContainer(String containerId, String command, ImageConfiguration imageConfiguration) throws DockerAccessException, ExecException
Create and start a Exec container with the given image configuration.- Parameters:
containerId- container id to run exec command againstcommand- command to executeimageConfiguration- configuration of the container's image- Returns:
- the exec container id
- Throws:
DockerAccessException- if access to the docker backend failsExecException
-
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 nameportMapping- container port mappinggavLabel- label to tag the started container withproperties- properties to fill in with dynamically assigned portsdefaultContainerNamePattern- pattern to use for naming containers. Can be null in which case a default pattern is usedbuildTimestamp- 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 idremoveVolumes- 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 nameportMapping- container port mappinggavLabel- label to tag the started container withproperties- properties to fill in with dynamically assigned portsdefaultContainerNamePattern- pattern to use for naming containers. Can be null in which case a default pattern is usedbuildTimestamp- 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 stopimageConfig- image configuration for this containerkeepContainer- whether to keep container or to remove them after stoppingsremoveVolumes- whether to remove volumes after stopping- Throws:
DockerAccessExceptionExecException
-
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 stopkeepContainer- whether to keep container or to remove them after stoppingsremoveVolumes- whether to remove volumes after stopping- Throws:
DockerAccessExceptionExecException
-
stopStartedContainers
public void stopStartedContainers(boolean keepContainer, boolean removeVolumes, boolean removeCustomNetworks, GavLabel gavLabel) throws DockerAccessException, ExecExceptionStop all registered container- Parameters:
keepContainer- whether to keep container or to remove them after stoppingremoveVolumes- whether to remove volumes after stopping- Throws:
DockerAccessException- if during stopping of a container sth failsExecException
-
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,
nullotherwise.
-
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 ornullfor all containers- Returns:
- the containers for the given label or an empty collection
-
getImagesConfigsInOrder
public List<StartOrderResolver.Resolvable> getImagesConfigsInOrder(QueryService queryService, List<ImageConfiguration> images)
Get the proper order for images to start- Parameters:
images- list of images for which the order should be created- Returns:
- list of images in the right startup order
-
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 configurationproperties- 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
-
createCustomNetworkIfNotExistant
public void createCustomNetworkIfNotExistant(String customNetwork) throws DockerAccessException
- Throws:
DockerAccessException
-
removeCustomNetworks
public void removeCustomNetworks(Collection<Network> networks) throws DockerAccessException
- Throws:
DockerAccessException
-
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 hubbinds- volume binds present in ImageConfigurationvolumes- VolumeConfigs present- Returns:
- List of volumes created
- Throws:
DockerAccessException
-
-