risk mitigation (#2115)

* don't lock db on shutdown

* reorder init
This commit is contained in:
Aiden McClelland
2023-01-13 14:41:09 -07:00
committed by Aiden McClelland
parent 76b5234f7b
commit a0bc16c255
4 changed files with 19 additions and 32 deletions

View File

@@ -88,17 +88,13 @@ async fn inner_main(cfg_path: Option<PathBuf>) -> Result<Option<Shutdown>, Error
.map_ok(|_| tracing::debug!("Metrics daemon Shutdown")) .map_ok(|_| tracing::debug!("Metrics daemon Shutdown"))
.await?; .await?;
let mut shutdown = shutdown_recv let shutdown = shutdown_recv
.recv() .recv()
.await .await
.with_kind(crate::ErrorKind::Unknown)?; .with_kind(crate::ErrorKind::Unknown)?;
sig_handler.abort(); sig_handler.abort();
if let Some(shutdown) = &mut shutdown {
drop(shutdown.db_handle.take());
}
(rpc_ctx, shutdown) (rpc_ctx, shutdown)
}; };
rpc_ctx.shutdown().await?; rpc_ctx.shutdown().await?;

View File

@@ -45,7 +45,6 @@ pub fn restart(#[context] ctx: DiagnosticContext) -> Result<(), Error> {
.send(Some(Shutdown { .send(Some(Shutdown {
datadir: ctx.datadir.clone(), datadir: ctx.datadir.clone(),
disk_guid: ctx.disk_guid.clone(), disk_guid: ctx.disk_guid.clone(),
db_handle: None,
restart: true, restart: true,
})) }))
.expect("receiver dropped"); .expect("receiver dropped");

View File

@@ -12,7 +12,7 @@ use tokio::process::Command;
use crate::context::rpc::RpcContextConfig; use crate::context::rpc::RpcContextConfig;
use crate::db::model::ServerStatus; use crate::db::model::ServerStatus;
use crate::install::PKG_ARCHIVE_DIR; use crate::install::PKG_ARCHIVE_DIR;
use crate::sound::{BEP, CIRCLE_OF_5THS_SHORT}; use crate::sound::BEP;
use crate::util::Invoke; use crate::util::Invoke;
use crate::Error; use crate::Error;
@@ -197,15 +197,30 @@ pub struct InitResult {
pub async fn init(cfg: &RpcContextConfig) -> Result<InitResult, Error> { pub async fn init(cfg: &RpcContextConfig) -> Result<InitResult, Error> {
let secret_store = cfg.secret_store().await?; let secret_store = cfg.secret_store().await?;
tracing::info!("Opened Postgres");
crate::ssh::sync_keys_from_db(&secret_store, "/home/start9/.ssh/authorized_keys").await?;
tracing::info!("Synced SSH Keys");
let db = cfg.db(&secret_store).await?; let db = cfg.db(&secret_store).await?;
tracing::info!("Opened PatchDB");
let mut handle = db.handle(); let mut handle = db.handle();
crate::db::DatabaseModel::new() crate::db::DatabaseModel::new()
.server_info() .server_info()
.lock(&mut handle, LockType::Write) .lock(&mut handle, LockType::Write)
.await?; .await?;
let receipts = InitReceipts::new(&mut handle).await?; let receipts = InitReceipts::new(&mut handle).await?;
if let Some(wifi_interface) = &cfg.wifi_interface {
crate::net::wifi::synchronize_wpa_supplicant_conf(
&cfg.datadir().join("main"),
wifi_interface,
&receipts.last_wifi_region.get(&mut handle).await?,
)
.await?;
tracing::info!("Synchronized WiFi");
}
let should_rebuild = tokio::fs::metadata(SYSTEM_REBUILD_PATH).await.is_ok() let should_rebuild = tokio::fs::metadata(SYSTEM_REBUILD_PATH).await.is_ok()
|| &*receipts.server_version.get(&mut handle).await? < &emver::Version::new(0, 3, 3, 1); || &*receipts.server_version.get(&mut handle).await? < &emver::Version::new(0, 3, 3, 1);
@@ -239,6 +254,7 @@ pub async fn init(cfg: &RpcContextConfig) -> Result<InitResult, Error> {
.invoke(crate::ErrorKind::Journald) .invoke(crate::ErrorKind::Journald)
.await?; .await?;
tracing::info!("Mounted Logs"); tracing::info!("Mounted Logs");
let tmp_dir = cfg.datadir().join("package-data/tmp"); let tmp_dir = cfg.datadir().join("package-data/tmp");
if tokio::fs::metadata(&tmp_dir).await.is_err() { if tokio::fs::metadata(&tmp_dir).await.is_err() {
tokio::fs::create_dir_all(&tmp_dir).await?; tokio::fs::create_dir_all(&tmp_dir).await?;
@@ -318,18 +334,6 @@ pub async fn init(cfg: &RpcContextConfig) -> Result<InitResult, Error> {
.await?; .await?;
tracing::info!("Enabled Docker QEMU Emulation"); tracing::info!("Enabled Docker QEMU Emulation");
crate::ssh::sync_keys_from_db(&secret_store, "/home/start9/.ssh/authorized_keys").await?;
tracing::info!("Synced SSH Keys");
if let Some(wifi_interface) = &cfg.wifi_interface {
crate::net::wifi::synchronize_wpa_supplicant_conf(
&cfg.datadir().join("main"),
wifi_interface,
&receipts.last_wifi_region.get(&mut handle).await?,
)
.await?;
tracing::info!("Synchronized WiFi");
}
receipts receipts
.status_info .status_info
.set( .set(

View File

@@ -1,7 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use patch_db::{LockType, PatchDbHandle};
use rpc_toolkit::command; use rpc_toolkit::command;
use crate::context::RpcContext; use crate::context::RpcContext;
@@ -16,7 +15,6 @@ pub struct Shutdown {
pub datadir: PathBuf, pub datadir: PathBuf,
pub disk_guid: Option<Arc<String>>, pub disk_guid: Option<Arc<String>>,
pub restart: bool, pub restart: bool,
pub db_handle: Option<Arc<PatchDbHandle>>,
} }
impl Shutdown { impl Shutdown {
/// BLOCKING /// BLOCKING
@@ -87,16 +85,11 @@ impl Shutdown {
#[command(display(display_none))] #[command(display(display_none))]
pub async fn shutdown(#[context] ctx: RpcContext) -> Result<(), Error> { pub async fn shutdown(#[context] ctx: RpcContext) -> Result<(), Error> {
let mut db = ctx.db.handle();
crate::db::DatabaseModel::new()
.lock(&mut db, LockType::Write)
.await?;
ctx.shutdown ctx.shutdown
.send(Some(Shutdown { .send(Some(Shutdown {
datadir: ctx.datadir.clone(), datadir: ctx.datadir.clone(),
disk_guid: Some(ctx.disk_guid.clone()), disk_guid: Some(ctx.disk_guid.clone()),
restart: false, restart: false,
db_handle: Some(Arc::new(db)),
})) }))
.map_err(|_| ()) .map_err(|_| ())
.expect("receiver dropped"); .expect("receiver dropped");
@@ -105,16 +98,11 @@ pub async fn shutdown(#[context] ctx: RpcContext) -> Result<(), Error> {
#[command(display(display_none))] #[command(display(display_none))]
pub async fn restart(#[context] ctx: RpcContext) -> Result<(), Error> { pub async fn restart(#[context] ctx: RpcContext) -> Result<(), Error> {
let mut db = ctx.db.handle();
crate::db::DatabaseModel::new()
.lock(&mut db, LockType::Write)
.await?;
ctx.shutdown ctx.shutdown
.send(Some(Shutdown { .send(Some(Shutdown {
datadir: ctx.datadir.clone(), datadir: ctx.datadir.clone(),
disk_guid: Some(ctx.disk_guid.clone()), disk_guid: Some(ctx.disk_guid.clone()),
restart: true, restart: true,
db_handle: Some(Arc::new(db)),
})) }))
.map_err(|_| ()) .map_err(|_| ())
.expect("receiver dropped"); .expect("receiver dropped");