Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hab] Support FS_ROOT environment variable. #530

Merged
merged 1 commit into from
May 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 66 additions & 22 deletions components/common/src/command/package/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,74 @@ use std::str::FromStr;
use ansi_term::Colour::{Blue, Green, Yellow};
use hcore::crypto::{artifact, SigKeyPair};
use hcore::crypto::keys::parse_name_with_rev;
use hcore::fs::CACHE_ARTIFACT_PATH;
use hcore::fs::cache_artifact_path;
use hcore::package::{PackageArchive, PackageIdent, PackageInstall};
use depot_core::data_object;
use depot_client;

use command::ProgressBar;
use error::Result;

pub fn start<P: ?Sized>(url: &str, ident_or_archive: &str, cache_key_path: &P) -> Result<()>
where P: AsRef<Path>
pub fn start<P1: ?Sized, P2: ?Sized, P3: ?Sized>(url: &str,
ident_or_archive: &str,
fs_root_path: &P1,
cache_artifact_path: &P2,
cache_key_path: &P3)
-> Result<()>
where P1: AsRef<Path>,
P2: AsRef<Path>,
P3: AsRef<Path>
{
if Path::new(ident_or_archive).is_file() {
try!(from_archive(url, &ident_or_archive, cache_key_path));
try!(from_archive(url,
&ident_or_archive,
fs_root_path,
cache_artifact_path,
cache_key_path));
} else {
let ident = try!(PackageIdent::from_str(ident_or_archive));
try!(from_url(url, &ident, cache_key_path));
try!(from_url(url,
&ident,
fs_root_path,
cache_artifact_path,
cache_key_path));
}
Ok(())
}

/// Given a package name and a base url, downloads the package
/// to the `CACHE_ARTIFACT_PATH`. Returns the filename in the cache as a String
/// to the cache artifact path. Returns the filename in the cache as a String
///
/// # Failures
///
/// * Fails if it cannot create the `CACHE_ARTIFACT_PATH`
/// * Fails if it cannot download the package from the upstream
pub fn from_url<I, P: ?Sized>(url: &str,
ident: &I,
cache_key_path: &P)
-> Result<data_object::Package>
pub fn from_url<I, P1: ?Sized, P2: ?Sized, P3: ?Sized>(url: &str,
ident: &I,
fs_root_path: &P1,
cache_artifact_path: &P2,
cache_key_path: &P3)
-> Result<data_object::Package>
where I: AsRef<PackageIdent>,
P: AsRef<Path>
P1: AsRef<Path>,
P2: AsRef<Path>,
P3: AsRef<Path>
{
println!("{}",
Yellow.bold().paint(format!("» Installing {}", ident.as_ref())));
let pkg_data = try!(depot_client::show_package(url, ident.as_ref()));
for dep in &pkg_data.tdeps {
try!(install_from_depot(url, &dep, dep.as_ref(), cache_key_path.as_ref()));
try!(install_from_depot(url,
&dep,
dep.as_ref(),
fs_root_path.as_ref(),
cache_artifact_path.as_ref(),
cache_key_path.as_ref()));
}
try!(install_from_depot(url,
&pkg_data.ident,
ident.as_ref(),
fs_root_path.as_ref(),
cache_artifact_path.as_ref(),
cache_key_path.as_ref()));
println!("{}",
Blue.paint(format!("★ Install of {} complete with {} packages installed.",
Expand All @@ -85,19 +110,35 @@ pub fn from_url<I, P: ?Sized>(url: &str,
Ok(pkg_data)
}

pub fn from_archive<P1: ?Sized, P2: ?Sized>(url: &str, path: &P1, cache_key_path: &P2) -> Result<()>
pub fn from_archive<P1: ?Sized, P2: ?Sized, P3: ?Sized, P4: ?Sized>(url: &str,
path: &P1,
fs_root_path: &P2,
cache_artifact_path: &P3,
cache_key_path: &P4)
-> Result<()>
where P1: AsRef<Path>,
P2: AsRef<Path>
P2: AsRef<Path>,
P3: AsRef<Path>,
P4: AsRef<Path>
{
println!("{}",
Yellow.bold().paint(format!("» Installing {}", path.as_ref().display())));
let mut archive = PackageArchive::new(PathBuf::from(path.as_ref()));
let ident = try!(archive.ident());
let tdeps = try!(archive.tdeps());
for dep in &tdeps {
try!(install_from_depot(url, &dep, dep.as_ref(), cache_key_path.as_ref()));
try!(install_from_depot(url,
&dep,
dep.as_ref(),
fs_root_path.as_ref(),
cache_artifact_path.as_ref(),
cache_key_path.as_ref()));
}
try!(install_from_archive(url, archive, &ident, cache_key_path.as_ref()));
try!(install_from_archive(url,
archive,
&ident,
fs_root_path.as_ref(),
cache_key_path.as_ref()));
println!("{}",
Blue.paint(format!("★ Install of {} complete with {} packages installed.",
&ident,
Expand All @@ -108,9 +149,11 @@ pub fn from_archive<P1: ?Sized, P2: ?Sized>(url: &str, path: &P1, cache_key_path
fn install_from_depot<P: AsRef<PackageIdent>>(url: &str,
ident: &P,
given_ident: &PackageIdent,
fs_root_path: &Path,
cache_artifact_path: &Path,
cache_key_path: &Path)
-> Result<()> {
match PackageInstall::load(ident.as_ref(), None) {
match PackageInstall::load(ident.as_ref(), Some(&fs_root_path)) {
Ok(_) => {
if given_ident.fully_qualified() {
println!("{} {}", Green.paint("→ Using"), ident.as_ref());
Expand All @@ -128,11 +171,11 @@ fn install_from_depot<P: AsRef<PackageIdent>>(url: &str,
let mut progress = ProgressBar::default();
let mut archive = try!(depot_client::fetch_package(url,
ident.as_ref(),
CACHE_ARTIFACT_PATH,
cache_artifact_path,
Some(&mut progress)));
let ident = try!(archive.ident());
try!(verify(url, &archive, &ident, cache_key_path));
try!(archive.unpack());
try!(archive.unpack(Some(fs_root_path)));
println!("{} {}", Green.bold().paint("✓ Installed"), ident.as_ref());
}
}
Expand All @@ -142,9 +185,10 @@ fn install_from_depot<P: AsRef<PackageIdent>>(url: &str,
fn install_from_archive(url: &str,
archive: PackageArchive,
ident: &PackageIdent,
fs_root_path: &Path,
cache_key_path: &Path)
-> Result<()> {
match PackageInstall::load(ident.as_ref(), None) {
match PackageInstall::load(ident.as_ref(), Some(&fs_root_path)) {
Ok(_) => {
println!("{} {}", Green.paint("→ Using"), ident);
}
Expand All @@ -153,7 +197,7 @@ fn install_from_archive(url: &str,
Green.bold().paint("← Extracting"),
ident);
try!(verify(url, &archive, &ident, cache_key_path));
try!(archive.unpack());
try!(archive.unpack(Some(fs_root_path)));
println!("{} {}", Green.bold().paint("✓ Installed"), ident);
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/common/src/gossip_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl GossipFile {
// this write will fail.
println!("Attempting to decrypt {}", &self.file_name);
let decrypted_bytes = try!(BoxKeyPair::decrypt(&self.body,
&default_cache_key_path()));
&default_cache_key_path(None)));
println!("Successfully decrypted {}", &self.file_name);
try!(new_file.write_all(&decrypted_bytes));
} else {
Expand Down
11 changes: 7 additions & 4 deletions components/core/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@
//! <symkey_base64>
//! ```

use std::path::PathBuf;
use std::path::{Path, PathBuf};

use sodiumoxide::init as nacl_init;

use env as henv;
use fs::CACHE_KEY_PATH;
use fs::cache_key_path;

/// The suffix on the end of a public sig/box file
static PUBLIC_KEY_SUFFIX: &'static str = "pub";
Expand Down Expand Up @@ -265,8 +265,11 @@ pub mod artifact;
pub mod hash;
pub mod keys;

pub fn default_cache_key_path() -> PathBuf {
PathBuf::from(henv::var(CACHE_KEY_PATH_ENV_VAR).unwrap_or(CACHE_KEY_PATH.to_string()))
pub fn default_cache_key_path(fs_root_path: Option<&Path>) -> PathBuf {
match henv::var(CACHE_KEY_PATH_ENV_VAR) {
Ok(val) => PathBuf::from(val),
Err(_) => cache_key_path(fs_root_path),
}
}

pub fn init() {
Expand Down
43 changes: 35 additions & 8 deletions components/core/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,52 @@
// open source license such as the Apache 2.0 License.

use std::env;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use env as henv;

pub const ROOT_PATH: &'static str = "/hab";
/// The default filesystem root path
pub const FS_ROOT_PATH: &'static str = "/";
/// The default root path of the Habitat filesytem
pub const ROOT_PATH: &'static str = "hab";
/// The default download root path for package artifacts, used on package installation
pub const CACHE_ARTIFACT_PATH: &'static str = "/hab/cache/artifacts";
pub const CACHE_ARTIFACT_PATH: &'static str = "hab/cache/artifacts";
/// The default path where cryptographic keys are stored
pub const CACHE_KEY_PATH: &'static str = "/hab/cache/keys";
pub const CACHE_KEY_PATH: &'static str = "hab/cache/keys";
/// The default path where source artifacts are downloaded, extracted, & compiled
pub const CACHE_SRC_PATH: &'static str = "/hab/cache/src";
pub const CACHE_SRC_PATH: &'static str = "hab/cache/src";
/// The root path containing all locally installed packages
pub const PKG_PATH: &'static str = "/hab/pkgs";
pub const PKG_PATH: &'static str = "hab/pkgs";
/// The root path containing all runtime service directories and files
const SVC_PATH: &'static str = "/hab/svc";
const SVC_PATH: &'static str = "hab/svc";

/// Returns the path to the artifacts cache, optionally taking a custom filesystem root.
pub fn cache_artifact_path(fs_root_path: Option<&Path>) -> PathBuf {
match fs_root_path {
Some(fs_root_path) => Path::new(fs_root_path).join(CACHE_ARTIFACT_PATH),
None => Path::new(FS_ROOT_PATH).join(CACHE_ARTIFACT_PATH),
}
}

/// Returns the path to the keys cache, optionally taking a custom filesystem root.
pub fn cache_key_path(fs_root_path: Option<&Path>) -> PathBuf {
match fs_root_path {
Some(fs_root_path) => Path::new(fs_root_path).join(CACHE_KEY_PATH),
None => Path::new(FS_ROOT_PATH).join(CACHE_KEY_PATH),
}
}

/// Returns the path to the src cache, optionally taking a custom filesystem root.
pub fn cache_src_path(fs_root_path: Option<&Path>) -> PathBuf {
match fs_root_path {
Some(fs_root_path) => Path::new(fs_root_path).join(CACHE_SRC_PATH),
None => Path::new(FS_ROOT_PATH).join(CACHE_SRC_PATH),
}
}

/// Returns the root path for a given service's configuration, files, and data.
pub fn svc_path(service_name: &str) -> PathBuf {
PathBuf::from(SVC_PATH).join(service_name)
Path::new("/").join(SVC_PATH).join(service_name)
}

/// Returns the path to a given service's configuration.
Expand Down
5 changes: 3 additions & 2 deletions components/core/src/package/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,16 @@ impl PackageArchive {
/// # Failures
///
/// * If the package cannot be unpacked
pub fn unpack(&self) -> Result<()> {
pub fn unpack(&self, fs_root_path: Option<&Path>) -> Result<()> {
let root = fs_root_path.unwrap_or(Path::new("/"));
let tar_reader = try!(artifact::get_archive_reader(&self.path));
let mut builder = reader::Builder::new();
try!(builder.support_format(ReadFormat::All));
try!(builder.support_filter(ReadFilter::All));
let mut reader = try!(builder.open_stream(tar_reader));
let writer = writer::Disk::new();
try!(writer.set_standard_lookup());
try!(writer.write(&mut reader, Some("/")));
try!(writer.write(&mut reader, Some(root.to_string_lossy().as_ref())));
try!(writer.close());
Ok(())
}
Expand Down
35 changes: 21 additions & 14 deletions components/core/src/package/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,32 @@ use package::{MetaFile, PackageIdent};
#[derive(Clone, Debug)]
pub struct PackageInstall {
ident: PackageIdent,
installed_path: PathBuf,
fs_root_path: PathBuf,
package_root_path: PathBuf,
installed_path: PathBuf,
}

impl PackageInstall {
/// Verifies an installation of a package is within the package home and returns a struct
/// Verifies an installation of a package is within the package path and returns a struct
/// representing that package installation.
///
/// Only the origin and name of a package are required - the latest version/release of a
/// package will be returned if their optional value is not specified. If only a version is
/// specified, the latest release of that package origin, name, and version is returned.
///
/// An optional `home` path may be provided to search for a package in a non-default path.
pub fn load(ident: &PackageIdent, home: Option<&Path>) -> Result<PackageInstall> {
let path = home.unwrap_or(Path::new(PKG_PATH));
let pl = try!(Self::package_list(path));
/// An optional `fs_root` path may be provided to search for a package that is mounted on a
/// filesystem not currently rooted at `/`.
pub fn load(ident: &PackageIdent, fs_root_path: Option<&Path>) -> Result<PackageInstall> {
let fs_root_path = fs_root_path.unwrap_or(Path::new("/"));
let package_root_path = fs_root_path.join(PKG_PATH);
let pl = try!(Self::package_list(&package_root_path));
if ident.fully_qualified() {
if pl.iter().any(|ref p| p.satisfies(ident)) {
Ok(PackageInstall {
ident: ident.clone(),
installed_path: try!(Self::calc_installed_path(ident, path)),
package_root_path: PathBuf::from(path),
fs_root_path: PathBuf::from(fs_root_path),
package_root_path: package_root_path.clone(),
installed_path: try!(Self::calc_installed_path(ident, &package_root_path)),
})
} else {
Err(Error::PackageNotFound(ident.clone()))
Expand All @@ -67,8 +71,9 @@ impl PackageInstall {
if let Some(id) = latest {
Ok(PackageInstall {
ident: id.clone(),
installed_path: try!(Self::calc_installed_path(&id, path)),
package_root_path: PathBuf::from(path),
fs_root_path: PathBuf::from(fs_root_path),
package_root_path: package_root_path.clone(),
installed_path: try!(Self::calc_installed_path(&id, &package_root_path)),
})
} else {
Err(Error::PackageNotFound(ident.clone()))
Expand All @@ -77,13 +82,15 @@ impl PackageInstall {
}

pub fn new_from_parts(ident: PackageIdent,
installed_path: PathBuf,
package_root_path: PathBuf)
fs_root_path: PathBuf,
package_root_path: PathBuf,
installed_path: PathBuf)
-> PackageInstall {
PackageInstall {
ident: ident,
installed_path: installed_path,
fs_root_path: fs_root_path,
package_root_path: package_root_path,
installed_path: installed_path,
}
}

Expand Down Expand Up @@ -255,7 +262,7 @@ impl PackageInstall {
let tdeps = try!(self.tdeps());
let mut deps = Vec::with_capacity(tdeps.len());
for dep in tdeps.iter() {
let dep_install = try!(Self::load(dep, Some(&self.package_root_path)));
let dep_install = try!(Self::load(dep, Some(&self.fs_root_path)));
deps.push(dep_install);
}
Ok(deps)
Expand Down
3 changes: 3 additions & 0 deletions components/hab/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::ptr;

use common;
use hcore;
use hcore::fs::cache_artifact_path;
use hcore::package::{PackageIdent, PackageInstall};
use hcore::url::DEFAULT_DEPOT_URL;

Expand Down Expand Up @@ -76,6 +77,8 @@ pub fn command_from_pkg(command: &str,
println!("Package for {} not found, installing from depot", &ident);
try!(common::command::package::install::from_url(DEFAULT_DEPOT_URL,
ident,
Path::new("/"),
&cache_artifact_path(None),
cache_key_path));
command_from_pkg(&command, &ident, &cache_key_path, retry + 1)
}
Expand Down
Loading