private List<Template> loadTemplates(File templatePath) { List<File> files; try (Stream<Path> paths = Files.walk(Paths.get(templatePath.getAbsolutePath()), 1)) { files = paths.map(i -> i.toAbsolutePath().toFile()).collect(Collectors.toList()); } catch (IOException e) { throw new SkipperException("Could not process files in template path " + templatePath, e); } List<Template> templates = new ArrayList<>(); for (File file : files) { if (isYamlFile(file)) { Template template = new Template(); template.setName(file.getName()); try { template.setData(new String(Files.readAllBytes(file.toPath()), "UTF-8")); } catch (IOException e) { throw new SkipperException("Could read template file " + file.getAbsoluteFile(), e); } templates.add(template); } } return templates; }
private static String applyManifestTemplate(Package packageToDeploy, Map<String, ?> model) { // Aggregate all valid manifests into one big doc. StringBuilder sb = new StringBuilder(); // Top level templates. List<Template> templates = packageToDeploy.getTemplates(); if (templates != null) { for (Template template : templates) { String templateAsString = new String(template.getData()); com.samskivert.mustache.Template mustacheTemplate = Mustache.compiler().compile(templateAsString); sb.append("\n---\n# Source: " + template.getName() + "\n"); sb.append(mustacheTemplate.execute(model)); } } for (Package pkg : packageToDeploy.getDependencies()) { String packageName = pkg.getMetadata().getName(); Map<String, Object> modelForDependency; if (model.containsKey(packageName)) { modelForDependency = (Map<String, Object>) model.get(pkg.getMetadata().getName()); } else { modelForDependency = new TreeMap<>(); } sb.append(applyManifestTemplate(pkg, modelForDependency)); } return sb.toString(); }
private static String applyManifestTemplate(Package packageToDeploy, Map<String, ?> model) { // Aggregate all valid manifests into one big doc. StringBuilder sb = new StringBuilder(); // Top level templates. List<Template> templates = packageToDeploy.getTemplates(); if (templates != null) { for (Template template : templates) { String templateAsString = new String(template.getData()); com.samskivert.mustache.Template mustacheTemplate = Mustache.compiler().compile(templateAsString); sb.append("\n---\n# Source: " + template.getName() + "\n"); sb.append(mustacheTemplate.execute(model)); } } for (Package pkg : packageToDeploy.getDependencies()) { String packageName = pkg.getMetadata().getName(); Map<String, Object> modelForDependency; if (model.containsKey(packageName)) { modelForDependency = (Map<String, Object>) model.get(pkg.getMetadata().getName()); } else { modelForDependency = new TreeMap<>(); } sb.append(applyManifestTemplate(pkg, modelForDependency)); } return sb.toString(); }
private List<Template> loadTemplates(File templatePath) { List<File> files; try (Stream<Path> paths = Files.walk(Paths.get(templatePath.getAbsolutePath()), 1)) { files = paths.map(i -> i.toAbsolutePath().toFile()).collect(Collectors.toList()); } catch (IOException e) { throw new SkipperException("Could not process files in template path " + templatePath, e); } List<Template> templates = new ArrayList<>(); for (File file : files) { if (isYamlFile(file)) { Template template = new Template(); template.setName(file.getName()); try { template.setData(new String(Files.readAllBytes(file.toPath()), "UTF-8")); } catch (IOException e) { throw new SkipperException("Could read template file " + file.getAbsoluteFile(), e); } templates.add(template); } } return templates; }
@Override public Template getSpringCloudDeployerApplicationTemplate() { org.springframework.core.io.Resource resource = new ClassPathResource( "/org/springframework/cloud/skipper/io/generic-template.yml"); String genericTempateData = null; try { genericTempateData = StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset()); } catch (IOException e) { throw new IllegalArgumentException("Can't load generic template", e); } Template template = new Template(); template.setData(genericTempateData); try { template.setName(resource.getURL().toString()); } catch (IOException e) { log.error("Could not get URL of resource " + resource.getDescription(), e); throw new SkipperServerException("Could not get URL of resource " + resource.getDescription(), e); } return template; }
@Override public Template getSpringCloudDeployerApplicationTemplate() { org.springframework.core.io.Resource resource = new ClassPathResource( "/org/springframework/cloud/skipper/io/generic-template.yml"); String genericTempateData = null; try { genericTempateData = StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset()); } catch (IOException e) { throw new IllegalArgumentException("Can't load generic template", e); } Template template = new Template(); template.setData(genericTempateData); try { template.setName(resource.getURL().toString()); } catch (IOException e) { log.error("Could not get URL of resource " + resource.getDescription(), e); throw new SkipperServerException("Could not get URL of resource " + resource.getDescription(), e); } return template; }