public static void copyDockerfileDirectory(Class<?> containerObject, CubeDockerFile cubeDockerFile, File output) throws IOException { if (cubeDockerFile == null) { throw new IllegalArgumentException("No CubeDockerFile annotation is provided"); } String dockerfilePath = resolveDockerfileLocation(containerObject, cubeDockerFile); copyDockerfileDirectoryFromClasspath(containerObject, dockerfilePath, output); }
private static void copyDockerfileDirectoryFromClasspath(Class<?> containerObject, String dockerfileLocation, File dir) throws IOException { File jar = null; try { jar = Which.jarFile(containerObject); } catch (IllegalArgumentException | IOException e) { throw new IllegalArgumentException(e); } if (jar != null && jar.isFile()) { // files are packaged into a jar/war. extract them dockerfileLocation += "/"; copyDockerfileDirectoryFromPackaged(jar, dockerfileLocation, dir); } else { // Dockerfile is not packaged into a jar file, so copy locally copyDockerfileDirectoryFromLocal(dockerfileLocation, dir); } }
private static void copyDockerfileDirectoryFromLocal(String location, File outputDirectory) throws IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); URL resourceDir = classLoader.getResource(location); if (resourceDir == null) { throw new IllegalArgumentException( String.format("No location found at %s", location) ); } copyFile(outputDirectory, resourceDir); }
private void prepareImageBuild() throws InvocationTargetException, IllegalAccessException, IOException { // @CubeDockerfile is defined as static method if (classHasMethodWithCubeDockerFile) { cubeDockerFileAnnotation = methodWithCubeDockerFile.getAnnotation(CubeDockerFile.class); final Archive<?> archive = (Archive<?>) methodWithCubeDockerFile.invoke(null, new Object[0]); File output = createTemporalDirectoryForCopyingDockerfile(containerName); logger.finer(String.format("Created %s directory for storing contents of %s cube.", output, containerName)); archive.as(ExplodedExporter.class).exportExplodedInto(output); dockerfileLocation = output; } else if (classDefinesCubeDockerFile) { cubeDockerFileAnnotation = containerObjectClass.getAnnotation(CubeDockerFile.class); //Copy Dockerfile and all contains of the same directory in a known directory. File output = createTemporalDirectoryForCopyingDockerfile(containerName); logger.finer(String.format("Created %s directory for storing contents of %s cube.", output, containerName)); DockerFileUtil.copyDockerfileDirectory(containerObjectClass, cubeDockerFileAnnotation, output); dockerfileLocation = output; } else if (classDefinesImage) { cubeImageAnnotation = containerObjectClass.getAnnotation(Image.class); } }
private static String resolveDockerfileLocation(Class<?> containerObject, CubeDockerFile cubeDockerFile) { String prefix = null; if (isSpecificDockerfileLocationSet(cubeDockerFile)) { prefix = cubeDockerFile.value(); } else { prefix = containerObject.getName(); } return prefix.replace('.', '/').replace('$', '/'); }
private void prepareImageBuild() throws InvocationTargetException, IllegalAccessException, IOException { // @CubeDockerfile is defined as static method if (classHasMethodWithCubeDockerFile) { cubeDockerFileAnnotation = methodWithCubeDockerFile.getAnnotation(CubeDockerFile.class); final Archive<?> archive = (Archive<?>) methodWithCubeDockerFile.invoke(null, new Object[0]); File output = createTemporalDirectoryForCopyingDockerfile(containerName); logger.finer(String.format("Created %s directory for storing contents of %s cube.", output, containerName)); archive.as(ExplodedExporter.class).exportExplodedInto(output); dockerfileLocation = output; } else if (classDefinesCubeDockerFile) { cubeDockerFileAnnotation = containerObjectClass.getAnnotation(CubeDockerFile.class); //Copy Dockerfile and all contains of the same directory in a known directory. File output = createTemporalDirectoryForCopyingDockerfile(containerName); logger.finer(String.format("Created %s directory for storing contents of %s cube.", output, containerName)); DockerFileUtil.copyDockerfileDirectory(containerObjectClass, cubeDockerFileAnnotation, output); dockerfileLocation = output; } else if (classDefinesImage) { cubeImageAnnotation = containerObjectClass.getAnnotation(Image.class); } }
private static String resolveDockerfileLocation(Class<?> containerObject, CubeDockerFile cubeDockerFile) { String prefix = null; if (isSpecificDockerfileLocationSet(cubeDockerFile)) { prefix = cubeDockerFile.value(); } else { prefix = containerObject.getName(); } return prefix.replace('.', '/').replace('$', '/'); }
@Test public void shouldCopyDockerfileFromDefaultLocation() throws IOException { File outputDir = folder.newFolder(); DockerFileUtil.copyDockerfileDirectory(TestLocalDockerFile.class, TestLocalDockerFile.class.getAnnotation(CubeDockerFile.class), outputDir); Assert.assertThat(new File(outputDir, "Dockerfile").exists(), Is.is(true)); }
public static void copyDockerfileDirectory(Class<?> containerObject, CubeDockerFile cubeDockerFile, File output) throws IOException { if (cubeDockerFile == null) { throw new IllegalArgumentException("No CubeDockerFile annotation is provided"); } String dockerfilePath = resolveDockerfileLocation(containerObject, cubeDockerFile); copyDockerfileDirectoryFromClasspath(containerObject, dockerfilePath, output); }
private static void copyDockerfileDirectoryFromClasspath(Class<?> containerObject, String dockerfileLocation, File dir) throws IOException { File jar = null; try { jar = Which.jarFile(containerObject); } catch (IllegalArgumentException | IOException e) { throw new IllegalArgumentException(e); } if (jar != null && jar.isFile()) { // files are packaged into a jar/war. extract them dockerfileLocation += "/"; copyDockerfileDirectoryFromPackaged(jar, dockerfileLocation, dir); } else { // Dockerfile is not packaged into a jar file, so copy locally copyDockerfileDirectoryFromLocal(dockerfileLocation, dir); } }
private static void copyDockerfileDirectoryFromLocal(String location, File outputDirectory) throws IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); URL resourceDir = classLoader.getResource(location); if (resourceDir == null) { throw new IllegalArgumentException( String.format("No location found at %s", location) ); } copyFile(outputDirectory, resourceDir); }
@Test public void shouldCopyDockerfileFromJarLocation() throws IOException, ClassNotFoundException { //Creates a jar file with required content JavaArchive jar = ShrinkWrap.create(JavaArchive.class) .addClass(CustomTestJarDockerfiler.class) .addClasses(CubeDockerFile.class) .addAsResource( new StringAsset("FROM java:8-jre"), "/test/Dockerfile" ); File jarDirectory = folder.newFolder(); File jarFile = new File(jarDirectory, "test.jar"); jar.as(ZipExporter.class).exportTo(jarFile); //Creates a class loader that depends on the system one loading the jar. Jars are isolated from the test ones. ClassLoader classloader = new URLClassLoader(new URL[] {jarFile.toURI().toURL()}, null); Class<?> clazz = (Class<?>) Class.forName("org.arquillian.cube.docker.impl.util.DockerFileUtilTest$CustomTestJarDockerfiler", true, classloader); //Executes the test File outputDir = folder.newFolder(); DockerFileUtil.copyDockerfileDirectory(clazz, CustomTestJarDockerfiler.class.getAnnotation(CubeDockerFile.class), outputDir); Assert.assertThat(new File(outputDir, "Dockerfile").exists(), Is.is(true)); }
@Test public void shouldCopyDockerfileFromDefaultJar() throws IOException, ClassNotFoundException { //Creates a jar file with required content JavaArchive jar = ShrinkWrap.create(JavaArchive.class) .addClass(TestJarDockerFile.class) .addClasses(CubeDockerFile.class) .addAsResource( new StringAsset("FROM java:8-jre"), "/org/arquillian/cube/docker/impl/util/DockerFileUtilTest/TestJarDockerFile/Dockerfile" ); File jarDirectory = folder.newFolder(); File jarFile = new File(jarDirectory, "test.jar"); jar.as(ZipExporter.class).exportTo(jarFile); //Creates a class loader that depends on the system one loading the jar. Jars are isolated from the test ones. ClassLoader classloader = new URLClassLoader(new URL[] {jarFile.toURI().toURL()}, null); Class<?> clazz = (Class<?>) Class.forName("org.arquillian.cube.docker.impl.util.DockerFileUtilTest$TestJarDockerFile", true, classloader); //Executes the test File outputDir = folder.newFolder(); DockerFileUtil.copyDockerfileDirectory(clazz, TestJarDockerFile.class.getAnnotation(CubeDockerFile.class), outputDir); Assert.assertThat(new File(outputDir, "Dockerfile").exists(), Is.is(true)); }