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

@@ -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,