Search in sources :

Example 1 with WatchMode

use of org.eclipse.jkube.kit.config.image.WatchMode in project jkube by eclipse.

the class WatchServiceTest method testRestartContainerAndCallPostGoalRestartEnabled.

@Test
public void testRestartContainerAndCallPostGoalRestartEnabled() throws Exception {
    // Given
    AtomicBoolean restarted = new AtomicBoolean(false);
    WatchContext watchContext = WatchContext.builder().watchMode(WatchMode.both).containerRestarter(// Override Restart task to set this value to true
    i -> restarted.set(true)).build();
    WatchService.ImageWatcher imageWatcher = new WatchService.ImageWatcher(imageConfiguration, watchContext, "test-img", "efe1234");
    WatchService watchService = new WatchService(archiveService, buildService, queryService, runService, logger);
    // When
    watchService.restartContainerAndCallPostGoal(imageWatcher, true);
    // Then
    assertTrue(restarted.get());
}
Also used : WatchMode(org.eclipse.jkube.kit.config.image.WatchMode) Files(java.nio.file.Files) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) File(java.io.File) WatchImageConfiguration(org.eclipse.jkube.kit.config.image.WatchImageConfiguration) KitLogger(org.eclipse.jkube.kit.common.KitLogger) Mocked(mockit.Mocked) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) Test(org.junit.Test)

Example 2 with WatchMode

use of org.eclipse.jkube.kit.config.image.WatchMode in project jkube by eclipse.

the class WatchService method watch.

public synchronized void watch(WatchContext context, JKubeConfiguration buildContext, List<ImageConfiguration> images) throws IOException {
    // Important to be be a single threaded scheduler since watch jobs must run serialized
    ScheduledExecutorService executor = null;
    try {
        executor = Executors.newSingleThreadScheduledExecutor();
        for (ImageConfiguration imageConfig : runService.getImagesConfigsInOrder(queryService, images)) {
            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 %s %s", imageConfig.getName(), (watchMode != null ? " using " + watchMode.getDescription() : ""));
            ArrayList<String> tasks = new ArrayList<>();
            if (imageConfig.getBuildConfiguration() != null && imageConfig.getBuildConfiguration().getAssembly() != null) {
                if (watcher.isCopy()) {
                    schedule(executor, createCopyWatchTask(watcher, context.getBuildContext()), interval);
                    tasks.add("copying artifacts");
                }
                if (watcher.isBuild()) {
                    schedule(executor, createBuildWatchTask(watcher, context.getBuildContext(), watchMode == WatchMode.both, buildContext), interval);
                    tasks.add("rebuilding");
                }
            }
            if (watcher.isRun() && watcher.getContainerId() != null) {
                schedule(executor, createRestartWatchTask(watcher), interval);
                tasks.add("restarting");
            }
            if (!tasks.isEmpty()) {
                log.info("%s: Watch for %s", imageConfig.getDescription(), String.join(" and ", tasks));
            }
        }
        log.info("Waiting ...");
        if (!context.isKeepRunning()) {
            runService.addShutdownHookForStoppingContainers(context.isKeepContainer(), context.isRemoveVolumes(), context.isAutoCreateCustomNetworks());
        }
        wait();
    } catch (InterruptedException e) {
        log.warn("Interrupted");
        Thread.currentThread().interrupt();
    } finally {
        if (executor != null) {
            executor.shutdownNow();
        }
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) WatchImageConfiguration(org.eclipse.jkube.kit.config.image.WatchImageConfiguration) RunImageConfiguration(org.eclipse.jkube.kit.config.image.RunImageConfiguration) WatchMode(org.eclipse.jkube.kit.config.image.WatchMode) ArrayList(java.util.ArrayList)

Example 3 with WatchMode

use of org.eclipse.jkube.kit.config.image.WatchMode in project jkube by eclipse.

the class WatchServiceTest method testCopyFilesToContainer.

@Test
public void testCopyFilesToContainer() throws Exception {
    // Given
    AtomicBoolean fileCopied = new AtomicBoolean(false);
    WatchContext watchContext = WatchContext.builder().watchMode(WatchMode.copy).containerCopyTask(f -> fileCopied.compareAndSet(false, true)).build();
    File fileToCopy = Files.createTempFile("test-changed-files", "tar").toFile();
    WatchService.ImageWatcher imageWatcher = new WatchService.ImageWatcher(imageConfiguration, watchContext, "test-img", "efe1234");
    WatchService watchService = new WatchService(archiveService, buildService, queryService, runService, logger);
    // When
    watchService.copyFilesToContainer(fileToCopy, imageWatcher);
    // Then
    assertTrue(fileCopied.get());
}
Also used : WatchMode(org.eclipse.jkube.kit.config.image.WatchMode) Files(java.nio.file.Files) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) File(java.io.File) WatchImageConfiguration(org.eclipse.jkube.kit.config.image.WatchImageConfiguration) KitLogger(org.eclipse.jkube.kit.common.KitLogger) Mocked(mockit.Mocked) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) Test(org.junit.Test)

Example 4 with WatchMode

use of org.eclipse.jkube.kit.config.image.WatchMode in project jkube by eclipse.

the class WatchServiceTest method testCallPostExec.

@Test
public void testCallPostExec() throws Exception {
    // Given
    AtomicBoolean postExecCommandExecuted = new AtomicBoolean(false);
    WatchContext watchContext = WatchContext.builder().watchMode(WatchMode.copy).containerCommandExecutor(imageWatcher -> {
        postExecCommandExecuted.set(true);
        return "Some Output";
    }).build();
    WatchService.ImageWatcher imageWatcher = new WatchService.ImageWatcher(imageConfiguration, watchContext, "test-img", "efe1234");
    WatchService watchService = new WatchService(archiveService, buildService, queryService, runService, logger);
    // When
    watchService.callPostExec(imageWatcher);
    // Then
    assertTrue(postExecCommandExecuted.get());
}
Also used : WatchMode(org.eclipse.jkube.kit.config.image.WatchMode) Files(java.nio.file.Files) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) File(java.io.File) WatchImageConfiguration(org.eclipse.jkube.kit.config.image.WatchImageConfiguration) KitLogger(org.eclipse.jkube.kit.common.KitLogger) Mocked(mockit.Mocked) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) Test(org.junit.Test)

Aggregations

ImageConfiguration (org.eclipse.jkube.kit.config.image.ImageConfiguration)4 WatchImageConfiguration (org.eclipse.jkube.kit.config.image.WatchImageConfiguration)4 WatchMode (org.eclipse.jkube.kit.config.image.WatchMode)4 File (java.io.File)3 Files (java.nio.file.Files)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Mocked (mockit.Mocked)3 WatchContext (org.eclipse.jkube.kit.build.service.docker.watch.WatchContext)3 KitLogger (org.eclipse.jkube.kit.common.KitLogger)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 Before (org.junit.Before)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 RunImageConfiguration (org.eclipse.jkube.kit.config.image.RunImageConfiguration)1