implement delete-recovered

This commit is contained in:
Aiden McClelland
2022-02-10 22:31:40 -07:00
committed by Aiden McClelland
parent 55f5e78ae7
commit 76c2679600
5 changed files with 87 additions and 6 deletions

View File

@@ -162,6 +162,16 @@
"nullable": []
}
},
"56b986f2a2b7091d9c3acdd78f75d9842242de1f4da8f3672f2793d9fb256928": {
"query": "DELETE FROM tor WHERE package = ?",
"describe": {
"columns": [],
"parameters": {
"Right": 1
},
"nullable": []
}
},
"5b114c450073f77f466c980a2541293f30087b57301c379630326e5e5c2fb792": {
"query": "REPLACE INTO tor (package, interface, key) VALUES (?, ?, ?)",
"describe": {

View File

@@ -15,7 +15,7 @@ use rpc_toolkit::url::Host;
use rpc_toolkit::Context;
use serde::Deserialize;
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::SqlitePool;
use sqlx::{Acquire, SqlitePool};
use tokio::fs::File;
use tokio::process::Command;
use tokio::sync::{broadcast, oneshot, Mutex, RwLock};
@@ -287,7 +287,13 @@ impl RpcContext {
cleanup_failed(self, &mut db, &package_id).await?;
}
PackageDataEntry::Removing { .. } => {
uninstall(self, &mut db, &package_id).await?;
uninstall(
self,
&mut db,
&mut self.secret_store.acquire().await?,
&package_id,
)
.await?;
}
PackageDataEntry::Installed {
installed:

View File

@@ -3,6 +3,7 @@ use std::collections::{BTreeMap, HashMap};
use bollard::image::ListImagesOptions;
use color_eyre::eyre::eyre;
use patch_db::{DbHandle, LockType, PatchDbHandle};
use sqlx::{Executor, Sqlite};
use tracing::instrument;
use super::{PKG_ARCHIVE_DIR, PKG_DOCKER_DIR};
@@ -233,12 +234,16 @@ pub async fn remove_from_current_dependents_lists<
Ok(())
}
#[instrument(skip(ctx, db))]
pub async fn uninstall(
#[instrument(skip(ctx, secrets, db))]
pub async fn uninstall<Ex>(
ctx: &RpcContext,
db: &mut PatchDbHandle,
secrets: &mut Ex,
id: &PackageId,
) -> Result<(), Error> {
) -> Result<(), Error>
where
for<'a> &'a mut Ex: Executor<'a, Database = Sqlite>,
{
let mut tx = db.begin().await?;
crate::db::DatabaseModel::new()
.package_data()
@@ -288,5 +293,18 @@ pub async fn uninstall(
tokio::fs::remove_dir_all(&volumes).await?;
}
tx.commit(None).await?;
remove_tor_keys(secrets, &entry.manifest.id).await?;
Ok(())
}
#[instrument(skip(secrets))]
pub async fn remove_tor_keys<Ex>(secrets: &mut Ex, id: &PackageId) -> Result<(), Error>
where
for<'a> &'a mut Ex: Executor<'a, Database = Sqlite>,
{
let id_str = id.as_str();
sqlx::query!("DELETE FROM tor WHERE package = ?", id_str)
.execute(secrets)
.await?;
Ok(())
}

View File

@@ -564,7 +564,17 @@ pub async fn uninstall_impl(ctx: RpcContext, id: PackageId) -> Result<WithRevisi
drop(handle);
tokio::spawn(async move {
if let Err(e) = cleanup::uninstall(&ctx, &mut ctx.db.handle(), &id).await {
if let Err(e) = async {
cleanup::uninstall(
&ctx,
&mut ctx.db.handle(),
&mut ctx.secret_store.acquire().await?,
&id,
)
.await
}
.await
{
let err_str = format!("Uninstall of {} Failed: {}", id, e);
tracing::error!("{}", err_str);
tracing::debug!("{:?}", e);
@@ -593,6 +603,42 @@ pub async fn uninstall_impl(ctx: RpcContext, id: PackageId) -> Result<WithRevisi
})
}
#[command(rename = "delete-recovered", display(display_none))]
pub async fn delete_recovered(
#[context] ctx: RpcContext,
#[arg] id: PackageId,
) -> Result<WithRevision<()>, Error> {
let mut handle = ctx.db.handle();
let mut tx = handle.begin().await?;
let mut sql_tx = ctx.secret_store.begin().await?;
let mut recovered_packages = crate::db::DatabaseModel::new()
.recovered_packages()
.get_mut(&mut tx)
.await?;
recovered_packages.remove(&id).ok_or_else(|| {
Error::new(
eyre!("{} not in recovered-packages", id),
crate::ErrorKind::NotFound,
)
})?;
recovered_packages.save(&mut tx).await?;
let volumes = ctx.datadir.join(crate::volume::PKG_VOLUME_DIR).join(&id);
if tokio::fs::metadata(&volumes).await.is_ok() {
tokio::fs::remove_dir_all(&volumes).await?;
}
cleanup::remove_tor_keys(&mut sql_tx, &id).await?;
let res = tx.commit(None).await?;
sql_tx.commit().await?;
Ok(WithRevision {
revision: res,
response: (),
})
}
#[instrument(skip(ctx, temp_manifest, s9pk))]
pub async fn download_install_s9pk(
ctx: &RpcContext,

View File

@@ -92,6 +92,7 @@ pub fn server() -> Result<(), RpcError> {
install::install,
install::sideload,
install::uninstall,
install::delete_recovered,
install::list,
install::update::update,
config::config,