Skip to content

Commit

Permalink
Merge pull request #3306 from habitat-sh/fix-loading
Browse files Browse the repository at this point in the history
Approved by: @nobody from Nowhere
Merged by: The Sentinels
  • Loading branch information
thesentinels authored Sep 25, 2017
2 parents 512e163 + 4ae7097 commit 84c8dba
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
6 changes: 2 additions & 4 deletions components/core/src/package/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,8 @@ impl PackageInstall {
match self.read_metafile(MetaFile::Path) {
Ok(body) => {
let v = env::split_paths(&body)
.map(|p| {
self.fs_root_path.join(PathBuf::from(
&p.strip_prefix("/").unwrap(),
))
.filter_map(|p| {
p.strip_prefix("/").ok().map(|p| self.fs_root_path.join(p))
})
.collect();
Ok(v)
Expand Down
36 changes: 36 additions & 0 deletions components/sup/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,41 @@ impl Manager {
}
}

/// Read all spec files and rewrite them to disk migrating their format from a previous
/// Supervisor's to the one currently running.
fn migrate_specs(fs_cfg: &FsCfg) {
// JW: In the future we should write spec files to the Supervisor's DAT file in a more
// appropriate machine readable format. We'll need to wait until we modify how we load and
// unload services, though. Right now we watch files on disk and communicate with the
// Supervisor asynchronously. We need to move to communicating directly with the
// Supervisor's main loop through IPC.
match SpecWatcher::spec_files(&fs_cfg.specs_path) {
Ok(specs) => {
for spec_file in specs {
match ServiceSpec::from_file(&spec_file) {
Ok(spec) => {
if let Err(err) = spec.to_file(&spec_file) {
outputln!(
"Unable to migrate service spec, {}, {}",
spec_file.display(),
err
);
}
}
Err(err) => {
outputln!(
"Unable to migrate service spec, {}, {}",
spec_file.display(),
err
);
}
}
}
}
Err(err) => outputln!("Unable to migrate service specs, {}", err),
}
}

fn new(cfg: ManagerConfig, fs_cfg: FsCfg, launcher: LauncherCli) -> Result<Manager> {
let current = PackageIdent::from_str(&format!("{}/{}", SUP_PKG_IDENT, VERSION)).unwrap();
let self_updater = if cfg.auto_update {
Expand Down Expand Up @@ -259,6 +294,7 @@ impl Manager {
peer.set_gossip_port(peer_addr.port() as i32);
server.member_list.add_initial_member(peer);
}
Self::migrate_specs(&fs_cfg);
Ok(Manager {
self_updater: self_updater,
updater: ServiceUpdater::new(server.clone()),
Expand Down
34 changes: 18 additions & 16 deletions components/sup/src/manager/spec_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use std::collections::{HashMap, HashSet};
use std::error::Error as StdErr;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::channel;
Expand Down Expand Up @@ -52,6 +52,22 @@ impl SpecWatcher {
Self::run_with::<RecommendedWatcher, _>(path)
}

pub fn spec_files<T>(watch_path: T) -> Result<Vec<PathBuf>>
where
T: AsRef<Path>,
{
Ok(
glob(&watch_path
.as_ref()
.join(SPEC_FILE_GLOB)
.display()
.to_string())?
.filter_map(|p| p.ok())
.filter(|p| p.is_file())
.collect(),
)
}

pub fn initial_events(&mut self) -> Result<Vec<SpecWatcherEvent>> {
self.generate_events(HashMap::new())
}
Expand Down Expand Up @@ -210,14 +226,8 @@ impl SpecWatcher {
}

pub fn specs_from_watch_path<'a>(&self) -> Result<HashMap<String, ServiceSpec>> {
let spec_files: Vec<PathBuf> =
glob(&self.watch_path.join(SPEC_FILE_GLOB).display().to_string())?
.filter_map(|p| p.ok())
.filter(|p| p.is_file())
.collect();

let mut specs = HashMap::new();
for spec_file in spec_files {
for spec_file in Self::spec_files(&self.watch_path)? {
let spec = match ServiceSpec::from_file(&spec_file) {
Ok(s) => s,
Err(e) => {
Expand Down Expand Up @@ -267,14 +277,6 @@ impl SpecWatcher {
);
continue;
}
// Migrate spec files using an older format
if let Err(err) = spec.to_file(&spec_file) {
outputln!(
"Unable to migrate service spec, {}, {}",
spec_file.display(),
err
);
}
specs.insert(spec.ident.name.clone(), spec);
}
Ok(specs)
Expand Down

0 comments on commit 84c8dba

Please sign in to comment.