Search in sources :

Example 1 with PrefixedLogger

use of org.eclipse.jkube.kit.common.PrefixedLogger in project jkube by eclipse.

the class SpringBootWatcher method runRemoteSpringApplication.

private void runRemoteSpringApplication(String url) {
    log.info("Running RemoteSpringApplication against endpoint: " + url);
    String remoteSecret = validateSpringBootDevtoolsSettings();
    ClassLoader classLoader = getClass().getClassLoader();
    if (classLoader instanceof URLClassLoader) {
        URLClassLoader pluginClassLoader = (URLClassLoader) classLoader;
        try (URLClassLoader projectClassLoader = ClassUtil.createProjectClassLoader(getContext().getBuildContext().getProject().getCompileClassPathElements(), log)) {
            URLClassLoader[] classLoaders = { projectClassLoader, pluginClassLoader };
            StringBuilder buffer = new StringBuilder("java -cp ");
            int count = 0;
            for (URLClassLoader urlClassLoader : classLoaders) {
                URL[] urLs = urlClassLoader.getURLs();
                for (URL u : urLs) {
                    if (count++ > 0) {
                        buffer.append(File.pathSeparator);
                    }
                    try {
                        URI uri = u.toURI();
                        File file = new File(uri);
                        buffer.append(file.getCanonicalPath());
                    } catch (Exception e) {
                        throw new IllegalStateException("Failed to create classpath: " + e, e);
                    }
                }
            }
            // Add dev tools to the classpath (the main class is not read from BOOT-INF/lib)
            try {
                File devtools = getSpringBootDevToolsJar(getContext().getBuildContext().getProject());
                buffer.append(File.pathSeparator);
                buffer.append(devtools.getCanonicalPath());
            } catch (Exception e) {
                throw new IllegalStateException("Failed to include devtools in the classpath: " + e, e);
            }
            buffer.append(" -Dspring.devtools.remote.secret=");
            buffer.append(remoteSecret);
            buffer.append(" org.springframework.boot.devtools.RemoteSpringApplication ");
            buffer.append(url);
            try {
                String command = buffer.toString();
                log.debug("Running: " + command);
                final Process process = Runtime.getRuntime().exec(command);
                final AtomicBoolean outputEnabled = new AtomicBoolean(true);
                Runtime.getRuntime().addShutdownHook(new Thread("jkube:watch [spring-boot] shutdown hook") {

                    @Override
                    public void run() {
                        log.info("Terminating the Spring remote client...");
                        outputEnabled.set(false);
                        process.destroy();
                    }
                });
                KitLogger logger = new PrefixedLogger("Spring-Remote", log);
                Thread stdOutPrinter = startOutputProcessor(logger, process.getInputStream(), false, outputEnabled);
                Thread stdErrPrinter = startOutputProcessor(logger, process.getErrorStream(), true, outputEnabled);
                int status = process.waitFor();
                stdOutPrinter.join();
                stdErrPrinter.join();
                if (status != 0) {
                    log.warn("Process returned status: %s", status);
                }
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            } catch (Exception e) {
                throw new RuntimeException("Failed to run RemoteSpringApplication: " + e, e);
            }
        } catch (IOException e) {
            log.warn("Instructed to use project classpath, but cannot. Continuing build if we can: ", e);
        }
    } else {
        throw new IllegalStateException("ClassLoader must be a URLClassLoader but it is: " + classLoader.getClass().getName());
    }
}
Also used : IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KitLogger(org.eclipse.jkube.kit.common.KitLogger) PrefixedLogger(org.eclipse.jkube.kit.common.PrefixedLogger) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 2 with PrefixedLogger

use of org.eclipse.jkube.kit.common.PrefixedLogger in project jkube by eclipse.

the class AssemblyManagerCreateDockerTarArchiveTest method withoutDockerfileAndFinalCustomizer.

@Test
public void withoutDockerfileAndFinalCustomizer() throws IOException {
    // Given
    final JKubeConfiguration jKubeConfiguration = createJKubeConfiguration();
    final BuildConfiguration buildConfiguration = BuildConfiguration.builder().build();
    final AtomicBoolean customized = new AtomicBoolean(false);
    final ArchiverCustomizer finalCustomizer = ac -> {
        customized.set(true);
        return ac;
    };
    // When
    File dockerArchiveFile = assemblyManager.createDockerTarArchive("no-docker-file-and-customizer", jKubeConfiguration, buildConfiguration, prefixedLogger, finalCustomizer);
    // Then
    assertTargetHasDockerDirectories("no-docker-file-and-customizer");
    ArchiveAssertions.assertThat(dockerArchiveFile).isFile().hasName("docker-build.tar").hasSameContentAsDirectory(getExpectedDirectory("without-dockerfile-and-final-customizer"));
    assertDockerFile("no-docker-file-and-customizer").hasContent(DOCKERFILE_DEFAULT_FALLBACK_CONTENT);
    assertBuildDirectoryFileTree("no-docker-file-and-customizer").containsExactlyInAnyOrder("Dockerfile", "jkube-generated-layer-final-artifact", "jkube-generated-layer-final-artifact/maven", "jkube-generated-layer-final-artifact/maven/test-0.1.0.jar", "maven");
    assertThat(customized).isTrue();
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) FileAssertions(org.eclipse.jkube.kit.common.assertj.FileAssertions) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) Path(java.nio.file.Path) JavaProject(org.eclipse.jkube.kit.common.JavaProject) Before(org.junit.Before) PrintWriter(java.io.PrintWriter) ArchiveAssertions(org.eclipse.jkube.kit.common.assertj.ArchiveAssertions) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) AbstractListAssert(org.assertj.core.api.AbstractListAssert) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) AbstractFileAssert(org.assertj.core.api.AbstractFileAssert) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) List(java.util.List) Rule(org.junit.Rule) Assembly(org.eclipse.jkube.kit.common.Assembly) ObjectAssert(org.assertj.core.api.ObjectAssert) ListAssert(org.assertj.core.api.ListAssert) PrefixedLogger(org.eclipse.jkube.kit.common.PrefixedLogger) Mocked(mockit.Mocked) TemporaryFolder(org.junit.rules.TemporaryFolder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 PrefixedLogger (org.eclipse.jkube.kit.common.PrefixedLogger)2 PrintWriter (java.io.PrintWriter)1 URI (java.net.URI)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Mocked (mockit.Mocked)1 AbstractFileAssert (org.assertj.core.api.AbstractFileAssert)1 AbstractListAssert (org.assertj.core.api.AbstractListAssert)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ListAssert (org.assertj.core.api.ListAssert)1 ObjectAssert (org.assertj.core.api.ObjectAssert)1 Assembly (org.eclipse.jkube.kit.common.Assembly)1 AssemblyConfiguration (org.eclipse.jkube.kit.common.AssemblyConfiguration)1