Skip to content

Commit

Permalink
Merge pull request #5 from cvent/add-docker
Browse files Browse the repository at this point in the history
Add docker support
  • Loading branch information
jonathanmorley authored May 15, 2018
2 parents 7e511ab + 94f009c commit 8c92ba2
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
name = "hogan"
version = "0.2.2"
authors = ["Jonathan Morley <[email protected]>"]
build = "build.rs"

[[bin]]
name = "hogan"
Expand All @@ -28,9 +27,3 @@ tempfile = "3.0"
url = "1.7"
walkdir = "2.0"
zip = "0.3.2"

[build-dependencies]
skeptic = "0.13"

[dev-dependencies]
skeptic = "0.13"
46 changes: 46 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# We need to use the Rust build image, because
# we need the Rust compile and Cargo tooling
FROM clux/muslrust:stable as build

# Install cmake as it is not included in muslrust, but is needed by libssh2-sys
RUN apt-get update && apt-get install -y \
cmake \
--no-install-recommends && \
rm -rf /var/lib/apt/lists/*

WORKDIR /app
# Creates a dummy project used to grab dependencies
RUN USER=root cargo init --bin

# Copies over *only* your manifests
COPY ./Cargo.* ./

# Builds your dependencies and removes the
# fake source code from the dummy project
RUN cargo build --release
RUN rm src/*.rs
RUN rm target/x86_64-unknown-linux-musl/release/hogan

# Copies only your actual source code to
# avoid invalidating the cache at all
COPY ./src ./src

# Builds again, this time it'll just be
# your actual source files being built
RUN cargo build --release

FROM alpine:latest as certs
RUN apk --update add ca-certificates

# Create a new stage with a minimal image
# because we already have a binary built
FROM scratch

# Copies standard SSL certs from the "build" stage
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

# Copies the binary from the "build" stage
COPY --from=build /app/target/x86_64-unknown-linux-musl/release/hogan /bin/

# Configures the startup!
ENTRYPOINT ["/bin/hogan"]
6 changes: 0 additions & 6 deletions build.rs

This file was deleted.

37 changes: 18 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use regex::{Regex, RegexBuilder};
use rouille::Response;
use rouille::input::plain_text_body;
use std::fs::File;
use std::io::Write;
use std::mem::replace;
use std::ops::DerefMut;
use std::path::PathBuf;
Expand Down Expand Up @@ -48,8 +49,9 @@ enum AppCommand {
common: AppCommon,

/// Filter environments to render templates for
#[structopt(short = "e", long = "environments-filter", parse(try_from_str = "App::parse_regex"),
default_value = ".+", value_name = "REGEX")]
#[structopt(short = "e", long = "environments-filter",
parse(try_from_str = "App::parse_regex"), default_value = ".+",
value_name = "REGEX")]
environments_regex: Regex,

/// Template source (recursive)
Expand All @@ -58,7 +60,8 @@ enum AppCommand {
templates_path: PathBuf,

/// Filter templates to transform
#[structopt(short = "f", long = "templates-filter", parse(try_from_str = "App::parse_regex"),
#[structopt(short = "f", long = "templates-filter",
parse(try_from_str = "App::parse_regex"),
default_value = "template([-.].+)?\\.(config|ya?ml|properties)",
value_name = "REGEX")]
templates_regex: Regex,
Expand Down Expand Up @@ -86,6 +89,10 @@ struct AppCommon {
#[structopt(short = "k", long = "ssh-key", parse(from_str = "App::parse_path_buf"),
default_value = "~/.ssh/id_rsa", value_name = "FILE")]
ssh_key: PathBuf,

/// Throw errors if values do not exist in configs
#[structopt(short = "s", long = "strict")]
strict: bool,
}

impl App {
Expand All @@ -112,39 +119,31 @@ main!(|args: App, log_level: verbosity| match args.cmd {
templates_regex,
common,
} => {
let mut handlebars = hogan::transform::handlebars();
let mut handlebars = hogan::transform::handlebars(common.strict);

let template_dir = TemplateDir::new(templates_path)?;
let templates = template_dir.find(templates_regex);
let mut templates = template_dir.find(templates_regex);
println!("Loaded {} template file(s)", templates.len());

for template in &templates {
handlebars.register_template_file(&template.path.to_string_lossy(), &template.path)?;
}

let config_dir = ConfigDir::new(common.configs_url, &common.ssh_key)?;
let environments = config_dir.find(App::config_regex(&environments_regex)?);
println!("Loaded {} config file(s)", environments.len());

for environment in environments {
println!("Updating templates for {}", environment.environment);

for template in &templates {
let template_path = template.path.to_string_lossy();
let path = template_path.replace("template", &environment.environment);
let mut file = File::create(&path)?;
for mut template in &mut templates {
debug!("Transforming {:?}", template.path);

debug!("Transforming {}", path);
if let Err(e) =
handlebars.render_to_write(&template_path, &environment.config_data, &mut file)
{
bail!("Error transforming {} due to {}", &path, e);
let rendered = template.render(&handlebars, &environment)?;
if let Err(e) = File::create(&rendered.path)?.write_all(&rendered.contents) {
bail!("Error transforming {:?} due to {}", rendered.path, e);
}
}
}
}
AppCommand::Server { common, port } => {
let mut handlebars = hogan::transform::handlebars();
let mut handlebars = hogan::transform::handlebars(common.strict);

let config_dir = ConfigDir::new(common.configs_url, &common.ssh_key)?;

Expand Down
3 changes: 2 additions & 1 deletion src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use handlebars::Handlebars;

pub mod helpers;

pub fn handlebars() -> Handlebars {
pub fn handlebars(strict: bool) -> Handlebars {
let mut handlebars = Handlebars::new();
handlebars.set_strict_mode(strict);
handlebars.register_helper("comma-list", Box::new(helpers::comma_delimited_list_helper));
handlebars.register_helper("equal", Box::new(helpers::equal_helper));
handlebars.register_helper("eq", Box::new(helpers::equal_helper));
Expand Down
1 change: 0 additions & 1 deletion tests/skeptic.rs

This file was deleted.

0 comments on commit 8c92ba2

Please sign in to comment.