mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-04 06:19:44 +00:00
configure datadir on context
This commit is contained in:
committed by
Aiden McClelland
parent
ee381ebce7
commit
3877e43b84
@@ -13,10 +13,12 @@ use serde::Deserialize;
|
||||
use sqlx::migrate::MigrateDatabase;
|
||||
use sqlx::{Sqlite, SqlitePool};
|
||||
use tokio::fs::File;
|
||||
use tokio::sync::broadcast::Sender;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use crate::manager::ManagerMap;
|
||||
use crate::net::NetController;
|
||||
use crate::shutdown::Shutdown;
|
||||
use crate::util::{from_toml_async_reader, AsyncFileExt};
|
||||
use crate::{Error, ResultExt};
|
||||
|
||||
@@ -26,50 +28,39 @@ pub struct RpcContextConfig {
|
||||
pub bind_rpc: Option<SocketAddr>,
|
||||
pub bind_ws: Option<SocketAddr>,
|
||||
pub tor_control: Option<SocketAddr>,
|
||||
pub db: Option<PathBuf>,
|
||||
pub secret_store: Option<PathBuf>,
|
||||
pub revision_cache_size: Option<usize>,
|
||||
pub datadir: Option<PathBuf>,
|
||||
}
|
||||
|
||||
pub struct RpcContextSeed {
|
||||
pub bind_rpc: SocketAddr,
|
||||
pub bind_ws: SocketAddr,
|
||||
pub db: PatchDb,
|
||||
pub secret_store: SqlitePool,
|
||||
pub docker: Docker,
|
||||
pub net_controller: Arc<NetController>,
|
||||
pub managers: ManagerMap,
|
||||
pub revision_cache_size: usize,
|
||||
pub revision_cache: RwLock<VecDeque<Arc<Revision>>>,
|
||||
pub metrics_cache: RwLock<Option<crate::system::Metrics>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RpcContext(Arc<RpcContextSeed>);
|
||||
impl RpcContext {
|
||||
pub async fn init<P: AsRef<Path>>(cfg_path: Option<P>) -> Result<Self, Error> {
|
||||
let cfg_path = cfg_path
|
||||
impl RpcContextConfig {
|
||||
pub async fn load<P: AsRef<Path>>(path: Option<P>) -> Result<Self, Error> {
|
||||
let cfg_path = path
|
||||
.as_ref()
|
||||
.map(|p| p.as_ref())
|
||||
.unwrap_or(Path::new(crate::CONFIG_PATH));
|
||||
let base = if let Some(f) = File::maybe_open(cfg_path)
|
||||
if let Some(f) = File::maybe_open(cfg_path)
|
||||
.await
|
||||
.with_ctx(|_| (crate::ErrorKind::Filesystem, cfg_path.display().to_string()))?
|
||||
{
|
||||
from_toml_async_reader(f).await?
|
||||
from_toml_async_reader(f).await
|
||||
} else {
|
||||
RpcContextConfig::default()
|
||||
};
|
||||
let db = PatchDb::open(
|
||||
base.db
|
||||
.unwrap_or_else(|| Path::new("/mnt/embassy-os/embassy.db").to_owned()),
|
||||
)
|
||||
.await?;
|
||||
Ok(RpcContextConfig::default())
|
||||
}
|
||||
}
|
||||
pub fn datadir(&self) -> &Path {
|
||||
self.datadir
|
||||
.as_ref()
|
||||
.map(|a| a.as_path())
|
||||
.unwrap_or_else(|| Path::new("/embassy-data"))
|
||||
}
|
||||
pub async fn db(&self) -> Result<PatchDb, Error> {
|
||||
PatchDb::open(self.datadir().join("main").join("embassy.db"))
|
||||
.await
|
||||
.map_err(Error::from)
|
||||
}
|
||||
pub async fn secret_store(&self) -> Result<SqlitePool, Error> {
|
||||
let secret_store_url = format!(
|
||||
"sqlite://{}",
|
||||
base.secret_store
|
||||
.unwrap_or_else(|| Path::new("/mnt/embassy-os/secrets.db").to_owned())
|
||||
.display()
|
||||
self.datadir().join("main").join("secrets.db").display()
|
||||
);
|
||||
if !Sqlite::database_exists(&secret_store_url).await? {
|
||||
Sqlite::create_database(&secret_store_url).await?;
|
||||
@@ -79,26 +70,48 @@ impl RpcContext {
|
||||
.run(&secret_store)
|
||||
.await
|
||||
.with_kind(crate::ErrorKind::Database)?;
|
||||
Ok(secret_store)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RpcContextSeed {
|
||||
pub bind_rpc: SocketAddr,
|
||||
pub bind_ws: SocketAddr,
|
||||
pub datadir: PathBuf,
|
||||
pub db: PatchDb,
|
||||
pub secret_store: SqlitePool,
|
||||
pub docker: Docker,
|
||||
pub net_controller: NetController,
|
||||
pub managers: ManagerMap,
|
||||
pub revision_cache_size: usize,
|
||||
pub revision_cache: RwLock<VecDeque<Arc<Revision>>>,
|
||||
pub metrics_cache: RwLock<Option<crate::system::Metrics>>,
|
||||
pub shutdown: Sender<Option<Shutdown>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RpcContext(Arc<RpcContextSeed>);
|
||||
impl RpcContext {
|
||||
pub async fn init<P: AsRef<Path>>(
|
||||
cfg_path: Option<P>,
|
||||
shutdown: Sender<Option<Shutdown>>,
|
||||
) -> Result<Self, Error> {
|
||||
let base = RpcContextConfig::load(cfg_path).await?;
|
||||
let db = base.db().await?;
|
||||
let secret_store = base.secret_store().await?;
|
||||
let docker = Docker::connect_with_unix_defaults()?;
|
||||
let net_controller = Arc::new(
|
||||
NetController::init(
|
||||
([127, 0, 0, 1], 80).into(),
|
||||
crate::net::tor::os_key(&mut secret_store.acquire().await?).await?,
|
||||
base.tor_control
|
||||
.unwrap_or(SocketAddr::from(([127, 0, 0, 1], 9051))),
|
||||
)
|
||||
.await?,
|
||||
);
|
||||
let managers = ManagerMap::init(
|
||||
&mut db.handle(),
|
||||
&mut secret_store.acquire().await?,
|
||||
docker.clone(),
|
||||
net_controller.clone(),
|
||||
let net_controller = NetController::init(
|
||||
([127, 0, 0, 1], 80).into(),
|
||||
crate::net::tor::os_key(&mut secret_store.acquire().await?).await?,
|
||||
base.tor_control
|
||||
.unwrap_or(SocketAddr::from(([127, 0, 0, 1], 9051))),
|
||||
)
|
||||
.await?;
|
||||
let managers = ManagerMap::default();
|
||||
let seed = Arc::new(RpcContextSeed {
|
||||
bind_rpc: base.bind_rpc.unwrap_or(([127, 0, 0, 1], 5959).into()),
|
||||
bind_ws: base.bind_ws.unwrap_or(([127, 0, 0, 1], 5960).into()),
|
||||
datadir: base.datadir().to_path_buf(),
|
||||
db,
|
||||
secret_store,
|
||||
docker,
|
||||
@@ -107,9 +120,18 @@ impl RpcContext {
|
||||
revision_cache_size: base.revision_cache_size.unwrap_or(512),
|
||||
revision_cache: RwLock::new(VecDeque::new()),
|
||||
metrics_cache: RwLock::new(None),
|
||||
shutdown,
|
||||
});
|
||||
let res = Self(seed);
|
||||
res.managers
|
||||
.init(
|
||||
&res,
|
||||
&mut res.db.handle(),
|
||||
&mut res.secret_store.acquire().await?,
|
||||
)
|
||||
.await?;
|
||||
// TODO: handle apps in bad / transient state
|
||||
Ok(Self(seed))
|
||||
Ok(res)
|
||||
}
|
||||
pub async fn package_registry_url(&self) -> Result<Url, Error> {
|
||||
Ok(
|
||||
|
||||
Reference in New Issue
Block a user