Skip to content
This repository has been archived by the owner on Apr 18, 2019. It is now read-only.

Commit

Permalink
install configs are in a special place
Browse files Browse the repository at this point in the history
Signed-off-by: mwrock <matt@mattwrock.com>
  • Loading branch information
mwrock committed Dec 7, 2018
1 parent b0a15ec commit 8f32292
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
6 changes: 6 additions & 0 deletions components/core/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ pub fn svc_config_path<T: AsRef<Path>>(service_name: T) -> PathBuf {
svc_path(service_name).join("config")
}

/// Returns the path to the install configuration directory for a given service.
pub fn svc_config_install_path<T: AsRef<Path>>(service_name: T) -> PathBuf {
svc_path(service_name).join("config_install")
}

/// Returns the path to a given service's data.
pub fn svc_data_path<T: AsRef<Path>>(service_name: T) -> PathBuf {
svc_path(service_name).join("data")
Expand Down Expand Up @@ -366,6 +371,7 @@ impl<'a> SvcDir<'a> {
/// instead.
fn create_all_svc_owned_dirs(&self) -> Result<()> {
self.create_svc_owned_dir(svc_config_path(&self.service_name))?;
self.create_svc_owned_dir(svc_config_install_path(&self.service_name))?;
self.create_svc_owned_dir(svc_data_path(&self.service_name))?;
self.create_svc_owned_dir(svc_files_path(&self.service_name))?;
self.create_svc_owned_dir(svc_var_path(&self.service_name))?;
Expand Down
5 changes: 3 additions & 2 deletions components/core/src/templating/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,9 @@ impl CfgRenderer {
/// Compile and write all configuration files to the configuration directory.
///
/// Returns `true` if the configuration has changed.
pub fn compile<T>(&self, service_group_name: &str, pkg: &Pkg, ctx: &T) -> Result<bool>
pub fn compile<P, T>(&self, service_group_name: &str, pkg: &Pkg, render_path: P, ctx: &T) -> Result<bool>
where
P: AsRef<Path>,
T: Serialize,
{
// JW TODO: This function is loaded with IO errors that will be converted a Supervisor
Expand All @@ -497,7 +498,7 @@ impl CfgRenderer {
for (template, _) in self.0.get_templates() {
let compiled = self.0.render(&template, ctx)?;
let compiled_hash = crypto::hash::hash_string(&compiled);
let cfg_dest = pkg.svc_config_path.join(&template);
let cfg_dest = render_path.as_ref().join(&template);
let file_hash = match crypto::hash::hash_file(&cfg_dest) {
Ok(file_hash) => file_hash,
Err(e) => {
Expand Down
25 changes: 13 additions & 12 deletions components/core/src/templating/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@ use templating::hooks::{Hook, InstallHook};

pub use self::context::RenderContext;

pub fn compile_from_package_install(package: &PackageInstall) -> Result<()> {
pub fn compile_for_package_install(package: &PackageInstall) -> Result<()> {
let pkg = package::Pkg::from_install(package.clone())?;

fs::SvcDir::new(&pkg.name, &pkg.svc_user, &pkg.svc_group).create()?;

let cfg = config::Cfg::new(&pkg, None)?;
let ctx = RenderContext::new(&pkg, &cfg);
let cfg_renderer = config::CfgRenderer::new(pkg.path.join("config"))?;
cfg_renderer.compile(&pkg.name, &pkg, &ctx)?;
let cfg_renderer = config::CfgRenderer::new(pkg.path.join("config_install"))?;
cfg_renderer.compile(&pkg.name, &pkg, &pkg.svc_config_install_path, &ctx)?;

if let Some(ref hook) = InstallHook::load(
&pkg.name,
&package.installed_path.join("hooks"),
&fs::svc_hooks_path(&pkg.name)) {
&fs::svc_hooks_path(&pkg.name),
&package.installed_path.join("hooks")) {
println!("compiling install hook");
hook.compile(&pkg.name, &ctx)?;
};

Expand Down Expand Up @@ -391,25 +392,25 @@ test: something"#
let hooks_path = root.join("hooks");
std::fs::create_dir_all(&hooks_path).unwrap();
create_with_content(
hooks_path.join("init"),
&String::from("init message is {{cfg.message}}"),
hooks_path.join("install"),
&String::from("install message is {{cfg.message}}"),
);
let config_path = root.join("config");
let config_path = root.join("config_install");
std::fs::create_dir_all(&config_path).unwrap();
create_with_content(
config_path.join("config.txt"),
&String::from("config message is {{cfg.message}}"),
);

compile_from_package_install(&pkg_install).expect("compile package");
compile_for_package_install(&pkg_install).expect("compile package");

assert_eq!(
file_content(fs::svc_config_path(&pkg_install.ident().name).join("config.txt")),
file_content(fs::svc_config_install_path(&pkg_install.ident().name).join("config.txt")),
"config message is Hello"
);
assert_eq!(
file_content(fs::svc_hooks_path(&pkg_install.ident().name).join("init")),
"init message is Hello"
file_content(fs::svc_hooks_path(&pkg_install.ident().name).join("install")),
"install message is Hello"
);

env::remove_var(fs::FS_ROOT_ENVVAR);
Expand Down
3 changes: 3 additions & 0 deletions components/core/src/templating/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub struct Pkg {
pub path: PathBuf,
pub svc_path: PathBuf,
pub svc_config_path: PathBuf,
pub svc_config_install_path: PathBuf,
pub svc_data_path: PathBuf,
pub svc_files_path: PathBuf,
pub svc_static_path: PathBuf,
Expand All @@ -107,6 +108,7 @@ impl Pkg {
let pkg = Pkg {
svc_path: fs::svc_path(&package.ident.name),
svc_config_path: fs::svc_config_path(&package.ident.name),
svc_config_install_path: fs::svc_config_install_path(&package.ident.name),
svc_data_path: fs::svc_data_path(&package.ident.name),
svc_files_path: fs::svc_files_path(&package.ident.name),
svc_run: fs::svc_path(&package.ident.name).join("run"),
Expand Down Expand Up @@ -172,6 +174,7 @@ impl<'a> Serialize for PkgProxy<'a> {
strukt.serialize_field("path", &p.path)?;
strukt.serialize_field("svc_path", &p.svc_path)?;
strukt.serialize_field("svc_config_path", &p.svc_config_path)?;
strukt.serialize_field("svc_config_install_path", &p.svc_config_install_path)?;
strukt.serialize_field("svc_data_path", &p.svc_data_path)?;
strukt.serialize_field("svc_files_path", &p.svc_files_path)?;
strukt.serialize_field("svc_static_path", &p.svc_static_path)?;
Expand Down

0 comments on commit 8f32292

Please sign in to comment.