|
26 | 26 | import java.nio.charset.StandardCharsets; |
27 | 27 | import java.nio.file.Files; |
28 | 28 | import java.nio.file.Path; |
| 29 | +import java.nio.file.Paths; |
29 | 30 | import java.util.HashMap; |
30 | 31 | import java.util.Map; |
31 | 32 | import java.util.logging.Logger; |
@@ -66,4 +67,77 @@ public static void generateDockerfile( |
66 | 67 | throw e; |
67 | 68 | } |
68 | 69 | } |
| 70 | + |
| 71 | + public static void generateXlangDockerfile( |
| 72 | + String basePythonContainerImage, |
| 73 | + String containerName, |
| 74 | + File targetDirectory, |
| 75 | + File artifactFile, |
| 76 | + String commandSpec) |
| 77 | + throws IOException, TemplateException { |
| 78 | + Configuration freemarkerConfig = new Configuration(Configuration.VERSION_2_3_32); |
| 79 | + freemarkerConfig.setDefaultEncoding("UTF-8"); |
| 80 | + freemarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); |
| 81 | + freemarkerConfig.setLogTemplateExceptions(true); |
| 82 | + freemarkerConfig.setClassForTemplateLoading(PythonDockerfileGenerator.class, "/"); |
| 83 | + |
| 84 | + String classesDirectory = targetDirectory.getPath() + "/classes"; |
| 85 | + Map<String, Object> parameters = new HashMap<>(); |
| 86 | + parameters.put("baseContainerImage", basePythonContainerImage); |
| 87 | + parameters.put("commandSpec", commandSpec); |
| 88 | + parameters.put("containerName", containerName); |
| 89 | + |
| 90 | + Template template = freemarkerConfig.getTemplate("Dockerfile-xlang-template"); |
| 91 | + |
| 92 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 93 | + OutputStreamWriter writer = new OutputStreamWriter(baos); |
| 94 | + |
| 95 | + try { |
| 96 | + template.process(parameters, writer); |
| 97 | + writer.flush(); |
| 98 | + |
| 99 | + Files.createDirectories(Path.of(classesDirectory + "/" + containerName)); |
| 100 | + |
| 101 | + Files.write( |
| 102 | + Path.of(classesDirectory + "/" + containerName + "/Dockerfile"), |
| 103 | + baos.toString(StandardCharsets.UTF_8).getBytes()); |
| 104 | + |
| 105 | + } catch (Exception e) { |
| 106 | + LOG.warning("Unable to generate Dockerfile for " + containerName); |
| 107 | + throw e; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + Files.createDirectories(Path.of(classesDirectory + "/" + containerName + "/classpath")); |
| 112 | + Files.createDirectories(Path.of(classesDirectory + "/" + containerName + "/libs")); |
| 113 | + |
| 114 | + String artifactPath = artifactFile.getPath(); |
| 115 | + String targetArtifactPath = |
| 116 | + artifactPath.substring(artifactPath.lastIndexOf("/"), artifactPath.length()); |
| 117 | + |
| 118 | + Files.copy( |
| 119 | + Path.of(targetDirectory.getPath() + targetArtifactPath), |
| 120 | + Path.of(classesDirectory + "/" + containerName + "/classpath" + targetArtifactPath)); |
| 121 | + String sourceLibsDirectory = targetDirectory.getPath() + "/extra_libs"; |
| 122 | + String destLibsDirectory = classesDirectory + "/" + containerName + "/libs/"; |
| 123 | + Files.walk(Paths.get(sourceLibsDirectory)) |
| 124 | + .forEach( |
| 125 | + source -> { |
| 126 | + LOG.warning("current source: " + source.toString()); |
| 127 | + LOG.warning("current source libs directory: " + sourceLibsDirectory); |
| 128 | + Path dest = |
| 129 | + Paths.get( |
| 130 | + destLibsDirectory, |
| 131 | + source.toString().substring(sourceLibsDirectory.length())); |
| 132 | + try { |
| 133 | + Files.copy(source, dest); |
| 134 | + } catch (IOException e) { |
| 135 | + LOG.warning("Unable to copy contents of " + sourceLibsDirectory); |
| 136 | + } |
| 137 | + }); |
| 138 | + } catch (Exception e) { |
| 139 | + LOG.warning("unable to copy jar files"); |
| 140 | + throw e; |
| 141 | + } |
| 142 | + } |
69 | 143 | } |
0 commit comments