Skip to content

Commit

Permalink
feat: change default dir to current directory (#431)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Both the store and p2p key dirs now default to the
current working directory instead of the home/.ceramic-one directory.

Generally this makes it less error prone when running multiple daemon on
a single host as the naturally isolate from each other using their
working directory. Running multiple daemons locally is a common task in
order to experiment with the p2p nature of ceramic-one.

This is a breaking change. If you previously relied on the home
directory default you must now explicitly pass in the home directory
like so:

    ceramic-one daemon -s ~/.ceramic-one -p ~/.ceramic-one
  • Loading branch information
nathanielc authored Jul 11, 2024
1 parent d7bc380 commit b2e0ff2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 52 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion one/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ cid.workspace = true
clap.workspace = true
futures.workspace = true
git-version = "0.3"
home = "0.5"
hyper.workspace = true
iroh-bitswap.workspace = true
iroh-rpc-client.workspace = true
Expand Down
64 changes: 16 additions & 48 deletions one/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ enum Command {

#[derive(Args, Debug)]
struct DaemonOpts {
#[command(flatten)]
db_opts: DBOpts,

/// Path to libp2p private key directory
#[arg(short, long, default_value = ".", env = "CERAMIC_ONE_P2P_KEY_DIR")]
p2p_key_dir: PathBuf,

/// Bind address of the API endpoint.
#[arg(
short,
Expand Down Expand Up @@ -140,19 +147,13 @@ struct DaemonOpts {
env = "CERAMIC_ONE_IDLE_CONNS_TIMEOUT_MS"
)]
idle_conns_timeout_ms: u64,

#[command(flatten)]
db_opts: DBOpts,

#[command(flatten)]
p2p_key_opts: P2PKeyOpts,
}

#[derive(Args, Debug)]
struct DBOpts {
/// Path to storage directory
#[arg(short, long, env = "CERAMIC_ONE_STORE_DIR")]
store_dir: Option<PathBuf>,
#[arg(short, long, default_value = ".", env = "CERAMIC_ONE_STORE_DIR")]
store_dir: PathBuf,
}

#[derive(Args, Debug)]
Expand Down Expand Up @@ -238,27 +239,6 @@ impl Network {
}
}

#[derive(Args, Debug)]
struct P2PKeyOpts {
/// Path to libp2p private key directory
#[arg(short, long, env = "CERAMIC_ONE_P2P_KEY_DIR")]
p2p_key_dir: Option<PathBuf>,
}

impl P2PKeyOpts {
fn default_directory(&self) -> PathBuf {
// 1 path from options
// 2 path $HOME/.ceramic-one
// 3 pwd/.ceramic-one
self.p2p_key_dir
.clone()
.unwrap_or_else(|| match home::home_dir() {
Some(home_dir) => home_dir.join(".ceramic-one"),
None => PathBuf::from(".ceramic-one"),
})
}
}

/// Run the ceramic one binary process
pub async fn run() -> Result<()> {
let args = Cli::parse();
Expand All @@ -272,35 +252,22 @@ type InterestInterest = FullInterests<Interest>;
type ModelInterest = ReconInterestProvider<Sha256a>;

impl DBOpts {
fn default_directory(&self) -> PathBuf {
// 1 path from options
// 2 path $HOME/.ceramic-one
// 3 pwd/.ceramic-one
match self.store_dir.clone() {
Some(dir) => dir,
None => match home::home_dir() {
Some(home_dir) => home_dir.join(".ceramic-one"),
None => PathBuf::from(".ceramic-one"),
},
}
}
/// This function will create the database directory if it does not exist.
async fn get_database(&self, process_undelivered: bool) -> Result<Databases> {
let dir = self.default_directory();
match tokio::fs::create_dir_all(dir.clone()).await {
match tokio::fs::create_dir_all(&self.store_dir).await {
Ok(_) => {}
Err(err) => match err.kind() {
std::io::ErrorKind::AlreadyExists => {}
_ => {
error!(
dir = %dir.display(),
dir = %self.store_dir.display(),
%err, "failed to create required directory"
);
anyhow::bail!(err);
}
},
}
let sql_db_path = dir.join("db.sqlite3").display().to_string();
let sql_db_path = self.store_dir.join("db.sqlite3").display().to_string();
Self::build_sqlite_dbs(&sql_db_path, process_undelivered).await
}

Expand Down Expand Up @@ -400,8 +367,9 @@ impl Daemon {
{
let network = opts.network.to_network(&opts.local_network_id)?;

let dir = opts.db_opts.default_directory();
debug!("using directory: {}", dir.display());
let store_dir = opts.db_opts.store_dir;
debug!(dir = %store_dir.display(), "using store directory");
debug!(dir = %opts.p2p_key_dir.display(), "using p2p key directory");

// Setup tokio-metrics
MetricsHandle::register(|registry| {
Expand Down Expand Up @@ -449,7 +417,7 @@ impl Daemon {
debug!(?p2p_config, "using p2p config");

// Load p2p identity
let mut kc = Keychain::<DiskStorage>::new(opts.p2p_key_opts.default_directory()).await?;
let mut kc = Keychain::<DiskStorage>::new(opts.p2p_key_dir.clone()).await?;
let keypair = load_identity(&mut kc).await?;
let peer_id = keypair.public().to_peer_id();

Expand Down
9 changes: 7 additions & 2 deletions one/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ pub struct FromIpfsOpts {
input_ipfs_path: PathBuf,

/// Path to storage directory
#[clap(long, short, env = "CERAMIC_ONE_OUTPUT_STORE_PATH")]
output_store_path: Option<PathBuf>,
#[clap(
long,
short,
default_value = ".",
env = "CERAMIC_ONE_OUTPUT_STORE_PATH"
)]
output_store_path: PathBuf,

/// Unique key used to find other Ceramic peers via the DHT
#[arg(long, default_value = "testnet-clay", env = "CERAMIC_ONE_NETWORK")]
Expand Down

0 comments on commit b2e0ff2

Please sign in to comment.