Search in sources :

Example 6 with Jinjava

use of com.hubspot.jinjava.Jinjava in project zeppelin by apache.

the class K8sRemoteInterpreterProcess method sparkUiWebUrlFromTemplate.

@VisibleForTesting
String sparkUiWebUrlFromTemplate(String templateString, int port, String serviceName, String serviceDomain) {
    ImmutableMap<String, Object> binding = ImmutableMap.of("PORT", port, "SERVICE_NAME", serviceName, ENV_SERVICE_DOMAIN, serviceDomain);
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
        Jinjava jinja = new Jinjava();
        return jinja.render(templateString, binding);
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
    }
}
Also used : Jinjava(com.hubspot.jinjava.Jinjava) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with Jinjava

use of com.hubspot.jinjava.Jinjava in project zeppelin by apache.

the class K8sSpecTemplate method render.

public String render(String template) {
    ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
        JinjavaConfig config = JinjavaConfig.newBuilder().withLstripBlocks(true).withTrimBlocks(true).build();
        Jinjava jinja = new Jinjava(config);
        return jinja.render(template, this);
    } finally {
        Thread.currentThread().setContextClassLoader(oldCl);
    }
}
Also used : JinjavaConfig(com.hubspot.jinjava.JinjavaConfig) Jinjava(com.hubspot.jinjava.Jinjava)

Example 8 with Jinjava

use of com.hubspot.jinjava.Jinjava in project zeppelin by apache.

the class DockerSpecTemplate method render.

public String render(String template) {
    ClassLoader oldClazzLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
        Jinjava jinja = new Jinjava();
        return jinja.render(template, this);
    } finally {
        Thread.currentThread().setContextClassLoader(oldClazzLoader);
    }
}
Also used : Jinjava(com.hubspot.jinjava.Jinjava)

Example 9 with Jinjava

use of com.hubspot.jinjava.Jinjava in project curiostack by curioswitch.

the class GenerateApiServerTask method render.

private static void render(String templatePath, Map<String, Object> context, Path outputPath) {
    if (!Files.exists(outputPath.getParent())) {
        try {
            Files.createDirectories(outputPath.getParent());
        } catch (IOException e) {
            throw new UncheckedIOException("Could not create directory.", e);
        }
    }
    Jinjava jinjava = new Jinjava();
    final String template;
    try {
        template = Resources.toString(Resources.getResource(templatePath), StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new UncheckedIOException("Could not read template.", e);
    }
    String rendered = jinjava.render(template, context);
    try {
        Files.writeString(outputPath, rendered);
    } catch (IOException e) {
        throw new UncheckedIOException("Could not write file.", e);
    }
}
Also used : Jinjava(com.hubspot.jinjava.Jinjava) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 10 with Jinjava

use of com.hubspot.jinjava.Jinjava in project curiostack by curioswitch.

the class UpdateGradleWrapperTask method exec.

@TaskAction
public void exec() throws IOException {
    var rootProject = getProject().getRootProject();
    String releaseName = null;
    String urlLinux = null;
    String urlMac = null;
    String urlWindows = null;
    var clients = new AOV3Clients();
    final List<AOV3ListBinaryAssetView> releases;
    try (var client = clients.createClient()) {
        var errors = new ArrayList<AOV3Error>();
        releases = client.assetsForLatest(errors::add, new BigInteger(ToolDependencies.getOpenJdkVersion(rootProject)), AOV3JVMImplementation.HOTSPOT).execute();
    } catch (AOV3Exception | InterruptedException e) {
        throw new GradleException("Could not query for latest AdoptOpenJDK release.", e);
    }
    for (var release : releases) {
        releaseName = release.releaseName();
        var binary = release.binary();
        if (binary.architecture() != AOV3Architecture.X64 || binary.imageType() != AOV3ImageKind.JDK) {
            continue;
        }
        switch(binary.operatingSystem()) {
            case LINUX:
                {
                    urlLinux = binary.package_().link().toASCIIString();
                    break;
                }
            case MAC:
                {
                    urlMac = binary.package_().link().toASCIIString();
                    break;
                }
            case WINDOWS:
                {
                    urlWindows = binary.package_().link().toASCIIString();
                    break;
                }
            default:
        }
    }
    if (releaseName == null || urlLinux == null || urlMac == null || urlWindows == null) {
        throw new GradleException("Could not find OpenJDK packages: " + releases);
    }
    Map<String, String> templateVars = ImmutableMap.<String, String>builder().put("dest_folder", releaseName).put("url_linux", urlLinux).put("url_mac", urlMac).put("url_windows", urlWindows).put("dest_archive_name", releaseName + ".tar.gz.or.zip").put("version", releaseName).build();
    Jinjava jinjava = new Jinjava(JinjavaConfig.newBuilder().withFailOnUnknownTokens(true).build());
    String rendered = jinjava.render(Resources.toString(Resources.getResource("curiostack/get-jdk.sh.tmpl"), StandardCharsets.UTF_8), templateVars);
    Files.writeString(rootProject.file("gradle/get-jdk.sh").toPath(), rendered, StandardCharsets.UTF_8);
    Files.writeString(rootProject.file(".gradle/jdk-release-name.txt").toPath(), releaseName);
    var gradlew = rootProject.file("gradlew").toPath();
    var gradleWrapperLines = Files.readAllLines(gradlew);
    if (gradleWrapperLines.stream().anyMatch(line -> line.contains(". ./gradle/get-jdk.sh"))) {
        return;
    }
    // First line is always shebang, skip it.
    int lineIndexAfterCopyright = 1;
    // Skip empty lines
    for (; lineIndexAfterCopyright < gradleWrapperLines.size(); lineIndexAfterCopyright++) {
        if (!gradleWrapperLines.get(lineIndexAfterCopyright).isEmpty()) {
            break;
        }
    }
    // Skip comment lines, they are all the license
    for (; lineIndexAfterCopyright < gradleWrapperLines.size(); lineIndexAfterCopyright++) {
        if (!gradleWrapperLines.get(lineIndexAfterCopyright).startsWith("#")) {
            break;
        }
    }
    // Found first empty line after license, insert our JDK download script and write it out.
    var linesWithGetJdk = ImmutableList.<String>builderWithExpectedSize(gradleWrapperLines.size() + 2);
    linesWithGetJdk.addAll(gradleWrapperLines.subList(0, lineIndexAfterCopyright));
    linesWithGetJdk.add("").add(". ./gradle/get-jdk.sh");
    linesWithGetJdk.addAll(gradleWrapperLines.subList(lineIndexAfterCopyright, gradleWrapperLines.size()));
    Files.writeString(gradlew, String.join("\n", linesWithGetJdk.build()));
}
Also used : AOV3Clients(net.adoptopenjdk.v3.vanilla.AOV3Clients) ArrayList(java.util.ArrayList) Jinjava(com.hubspot.jinjava.Jinjava) AOV3Exception(net.adoptopenjdk.v3.api.AOV3Exception) AOV3ListBinaryAssetView(net.adoptopenjdk.v3.api.AOV3ListBinaryAssetView) GradleException(org.gradle.api.GradleException) BigInteger(java.math.BigInteger) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

Jinjava (com.hubspot.jinjava.Jinjava)19 URL (java.net.URL)9 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 InterpreterResultMessageOutput (org.apache.zeppelin.interpreter.InterpreterResultMessageOutput)5 File (java.io.File)2 UncheckedIOException (java.io.UncheckedIOException)2 Properties (java.util.Properties)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CommandLine (org.apache.commons.exec.CommandLine)2 DefaultExecuteResultHandler (org.apache.commons.exec.DefaultExecuteResultHandler)2 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)2 ExecuteException (org.apache.commons.exec.ExecuteException)2 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)2 LogOutputStream (org.apache.commons.exec.LogOutputStream)2 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)2 SubmarineUI (org.apache.zeppelin.submarine.commons.SubmarineUI)2 TaskAction (org.gradle.api.tasks.TaskAction)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 JinjavaConfig (com.hubspot.jinjava.JinjavaConfig)1