use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.
the class WatchService method watch.
public synchronized void watch(WatchContext context, BuildService.BuildContext buildContext, List<ImageConfiguration> images) throws DockerAccessException, MojoExecutionException {
// Important to be be a single threaded scheduler since watch jobs must run serialized
ScheduledExecutorService executor = null;
try {
executor = Executors.newSingleThreadScheduledExecutor();
for (StartOrderResolver.Resolvable resolvable : runService.getImagesConfigsInOrder(queryService, images)) {
final ImageConfiguration imageConfig = (ImageConfiguration) resolvable;
String imageId = queryService.getImageId(imageConfig.getName());
String containerId = runService.lookupContainer(imageConfig.getName());
ImageWatcher watcher = new ImageWatcher(imageConfig, context, imageId, containerId);
long interval = watcher.getInterval();
WatchMode watchMode = watcher.getWatchMode(imageConfig);
log.info("Watching " + imageConfig.getName() + (watchMode != null ? " using " + watchMode.getDescription() : ""));
ArrayList<String> tasks = new ArrayList<>();
if (imageConfig.getBuildConfiguration() != null && imageConfig.getBuildConfiguration().getAssemblyConfiguration() != null) {
if (watcher.isCopy()) {
String containerBaseDir = imageConfig.getBuildConfiguration().getAssemblyConfiguration().getTargetDir();
schedule(executor, createCopyWatchTask(watcher, context.getMojoParameters(), containerBaseDir), interval);
tasks.add("copying artifacts");
}
if (watcher.isBuild()) {
schedule(executor, createBuildWatchTask(watcher, context.getMojoParameters(), watchMode == WatchMode.both, buildContext), interval);
tasks.add("rebuilding");
}
}
if (watcher.isRun() && watcher.getContainerId() != null) {
schedule(executor, createRestartWatchTask(watcher), interval);
tasks.add("restarting");
}
if (tasks.size() > 0) {
log.info("%s: Watch for %s", imageConfig.getDescription(), StringUtils.join(tasks.toArray(), " and "));
}
}
log.info("Waiting ...");
if (!context.isKeepRunning()) {
runService.addShutdownHookForStoppingContainers(context.isKeepContainer(), context.isRemoveVolumes(), context.isAutoCreateCustomNetworks());
}
wait();
} catch (InterruptedException e) {
log.warn("Interrupted");
} finally {
if (executor != null) {
executor.shutdownNow();
}
}
}
use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.
the class BuildMojo method buildAndTag.
protected void buildAndTag(ServiceHub hub, ImageConfiguration imageConfig) throws MojoExecutionException, DockerAccessException {
EnvUtil.storeTimestamp(getBuildTimestampFile(), getBuildTimestamp());
BuildService.BuildContext buildContext = getBuildContext();
ImagePullManager pullManager = getImagePullManager(determinePullPolicy(imageConfig.getBuildConfiguration()), autoPull);
BuildService buildService = hub.getBuildService();
buildService.buildImage(imageConfig, pullManager, buildContext);
if (!skipTag) {
buildService.tagImage(imageConfig.getName(), imageConfig);
}
}
use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.
the class LogsMojo method executeInternal.
@Override
protected void executeInternal(ServiceHub hub) throws MojoExecutionException, DockerAccessException {
QueryService queryService = hub.getQueryService();
LogDispatcher logDispatcher = getLogDispatcher(hub);
for (ImageConfiguration image : getResolvedImages()) {
String imageName = image.getName();
if (logAll) {
for (Container container : queryService.getContainersForImage(imageName)) {
doLogging(logDispatcher, image, container.getId());
}
} else {
Container container = queryService.getLatestContainerForImage(imageName);
if (container != null) {
doLogging(logDispatcher, image, container.getId());
}
}
}
if (follow) {
// Block forever ....
waitForEver();
}
}
use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.
the class SourceMojo method executeInternal.
@Override
protected void executeInternal(ServiceHub hub) throws DockerAccessException, MojoExecutionException {
MojoParameters params = createMojoParameters();
List<ImageConfiguration> imageConfigs = new ArrayList<>();
for (ImageConfiguration imageConfig : getResolvedImages()) {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
if (buildConfig != null) {
if (buildConfig.skip()) {
log.info("%s: Skipped creating source", imageConfig.getDescription());
} else {
imageConfigs.add(imageConfig);
}
}
}
if (sourceMode == BuildImageSelectMode.first && imageConfigs.size() > 0) {
ImageConfiguration imageConfig = imageConfigs.get(0);
File dockerTar = hub.getArchiveService().createDockerBuildArchive(imageConfig, params);
projectHelper.attachArtifact(project, getArchiveType(imageConfig), getClassifier(null), dockerTar);
} else {
for (ImageConfiguration imageConfig : imageConfigs) {
File dockerTar = hub.getArchiveService().createDockerBuildArchive(imageConfig, params);
String alias = imageConfig.getAlias();
if (alias == null) {
throw new IllegalArgumentException("Image " + imageConfig.getDescription() + " must have an 'alias' configured to be " + "used as a classifier for attaching a docker build tar as source to the maven build");
}
projectHelper.attachArtifact(project, getArchiveType(imageConfig), getClassifier(alias), dockerTar);
}
}
}
use of io.fabric8.maven.docker.config.ImageConfiguration in project docker-maven-plugin by fabric8io.
the class StartMojo method startImage.
private void startImage(final ImageConfiguration image, final ServiceHub hub, final ExecutorCompletionService<StartedContainer> startingContainers, final PortMapping.PropertyWriteHelper portMappingPropertyWriteHelper) {
final RunService runService = hub.getRunService();
final Properties projProperties = project.getProperties();
final RunImageConfiguration runConfig = image.getRunConfiguration();
final PortMapping portMapping = runService.createPortMapping(runConfig, projProperties);
final LogDispatcher dispatcher = getLogDispatcher(hub);
startingContainers.submit(new Callable<StartedContainer>() {
@Override
public StartedContainer call() throws Exception {
final String containerId = runService.createAndStartContainer(image, portMapping, getPomLabel(), projProperties, project.getBasedir());
// Update port-mapping writer
portMappingPropertyWriteHelper.add(portMapping, runConfig.getPortPropertyFile());
if (showLogs(image)) {
dispatcher.trackContainerLog(containerId, serviceHubFactory.getLogOutputSpecFactory().createSpec(containerId, image));
}
// Wait if requested
hub.getWaitService().wait(image, projProperties, containerId);
WaitConfiguration waitConfig = runConfig.getWaitConfiguration();
if (waitConfig != null && waitConfig.getExec() != null && waitConfig.getExec().getPostStart() != null) {
try {
runService.execInContainer(containerId, waitConfig.getExec().getPostStart(), image);
} catch (ExecException exp) {
if (waitConfig.getExec().isBreakOnError()) {
throw exp;
} else {
log.warn("Cannot run postStart: %s", exp.getMessage());
}
}
}
return new StartedContainer(image, containerId);
}
});
}
Aggregations