From 1f48fcfb977823922d6d6f81105caf3fec858a7e Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Tue, 24 Aug 2021 16:26:13 -0600 Subject: [PATCH] cleanup wip; change cbor lib --- appmgr/Cargo.lock | 18 ++++++-- appmgr/Cargo.toml | 2 +- appmgr/embassyd.service | 14 ++++++ appmgr/src/install/cleanup.rs | 84 +++++++++++++++++++++++++++++------ appmgr/src/install/mod.rs | 10 +++-- appmgr/src/manager/mod.rs | 4 +- appmgr/src/s9pk/builder.rs | 2 +- appmgr/src/s9pk/reader.rs | 7 ++- appmgr/src/util/mod.rs | 18 +++++--- 9 files changed, 127 insertions(+), 32 deletions(-) create mode 100644 appmgr/embassyd.service diff --git a/appmgr/Cargo.lock b/appmgr/Cargo.lock index 071f1e9dd..00667dbec 100644 --- a/appmgr/Cargo.lock +++ b/appmgr/Cargo.lock @@ -370,6 +370,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "ciborium" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de6836a1b6197d8acdaac74a01af077a26aa6953d2e9251eef061c646b0d432c" +dependencies = [ + "half", + "serde", +] + [[package]] name = "clang-sys" version = "1.2.0" @@ -789,6 +799,7 @@ dependencies = [ "basic-cookies", "bollard", "chrono", + "ciborium", "clap", "cookie_store 0.15.0", "digest 0.9.0", @@ -825,7 +836,6 @@ dependencies = [ "rust-argon2", "scopeguard", "serde", - "serde_cbor 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json", "serde_yaml", "sha2 0.9.5", @@ -2392,7 +2402,7 @@ dependencies = [ "reqwest", "rpc-toolkit-macro", "serde", - "serde_cbor 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_cbor 0.11.2", "serde_json", "thiserror", "tokio 1.9.0", @@ -2574,9 +2584,9 @@ dependencies = [ [[package]] name = "serde_cbor" -version = "0.11.1" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622" +checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5" dependencies = [ "half", "serde", diff --git a/appmgr/Cargo.toml b/appmgr/Cargo.toml index 33daeff09..03718526c 100644 --- a/appmgr/Cargo.toml +++ b/appmgr/Cargo.toml @@ -88,7 +88,7 @@ rpc-toolkit = { version = "*", path = "../../rpc-toolkit/rpc-toolkit" } rust-argon2 = "0.8.3" scopeguard = "1.1" # because avahi-sys fucks your shit up serde = { version = "1.0.118", features = ["derive", "rc"] } -serde_cbor = "0.11.1" +serde_cbor = { package = "ciborium", version = "0.1.0" } serde_json = "1.0.59" serde_toml = { package = "toml", version = "0.5.8" } serde_yaml = "0.8.14" diff --git a/appmgr/embassyd.service b/appmgr/embassyd.service new file mode 100644 index 000000000..aeba538bb --- /dev/null +++ b/appmgr/embassyd.service @@ -0,0 +1,14 @@ +[Unit] +Description=Embassy Daemon +After=network.target systemd-time-wait-sync.service +Requires=network.target +Wants=avahi-daemon.service + +[Service] +Type=simple +ExecStart=/usr/local/bin/embassyd -vvv +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target diff --git a/appmgr/src/install/cleanup.rs b/appmgr/src/install/cleanup.rs index 01dea67e6..b710ff383 100644 --- a/appmgr/src/install/cleanup.rs +++ b/appmgr/src/install/cleanup.rs @@ -1,11 +1,13 @@ use std::borrow::Cow; +use std::path::Path; use anyhow::anyhow; use bollard::Docker; use patch_db::DbHandle; +use super::PKG_PUBLIC_DIR; use crate::context::RpcContext; -use crate::db::model::InstalledPackageDataEntry; +use crate::db::model::{InstalledPackageDataEntry, PackageDataEntry}; use crate::dependencies::DependencyError; use crate::s9pk::manifest::{Manifest, PackageId}; use crate::util::Version; @@ -63,19 +65,75 @@ pub async fn update_dependents<'a, Db: DbHandle, I: IntoIterator( ctx: &RpcContext, db: &mut Db, - info: Result, + id: &PackageId, + version: &Version, ) -> Result<(), Error> { - let man = match info { - Ok(pde) => { - // TODO - - Cow::Owned(pde.manifest) + let pde = crate::db::DatabaseModel::new() + .package_data() + .idx_model(id) + .expect(db) + .await? + .get(db, true) + .await? + .to_owned(); + if let Some(manifest) = match &pde { + PackageDataEntry::Installing { manifest, .. } => Some(manifest), + PackageDataEntry::Updating { manifest, .. } => { + if &manifest.version != version { + Some(manifest) + } else { + None + } } - Err(man) => Cow::Borrowed(man), - }; - ctx.managers - .remove(&(man.id.clone(), man.version.clone())) - .await; - // docker images start9/$APP_ID/*:$VERSION -q | xargs docker rmi + _ => { + log::warn!("{}: Nothing to clean up!", id); + None + } + } { + ctx.managers + .remove(&(manifest.id.clone(), manifest.version.clone())) + .await; + // docker images start9/$APP_ID/*:$VERSION -q | xargs docker rmi + let public_dir_path = Path::new(PKG_PUBLIC_DIR).join(id).join(version.as_str()); + } + + match pde { + PackageDataEntry::Installing { .. } => { + crate::db::DatabaseModel::new() + .package_data() + .remove(db, id) + .await?; + } + PackageDataEntry::Updating { + installed, + manifest, + static_files, + .. + } => { + crate::db::DatabaseModel::new() + .package_data() + .idx_model(id) + .put( + db, + &PackageDataEntry::Installed { + installed, + manifest, + static_files, + }, + ) + .await?; + } + _ => (), + } + Ok(()) // TODO } + +pub async fn uninstall( + ctx: &RpcContext, + db: &mut Db, + entry: InstalledPackageDataEntry, +) -> Result<(), Error> { + //TODO + Ok(()) +} diff --git a/appmgr/src/install/mod.rs b/appmgr/src/install/mod.rs index 06ac64d5c..7ffc21b20 100644 --- a/appmgr/src/install/mod.rs +++ b/appmgr/src/install/mod.rs @@ -34,7 +34,7 @@ use crate::db::model::{ StaticFiles, }; use crate::dependencies::update_current_dependents; -use crate::install::cleanup::update_dependents; +use crate::install::cleanup::{uninstall, update_dependents}; use crate::s9pk::manifest::{Manifest, PackageId}; use crate::s9pk::reader::S9pkReader; use crate::status::{DependencyErrors, MainStatus, Status}; @@ -240,7 +240,9 @@ pub async fn download_install_s9pk( if let Err(e) = res { let mut handle = ctx.db.handle(); - if let Err(e) = cleanup(&ctx, &mut handle, Err(temp_manifest)).await { + let mut tx = handle.begin().await?; + + if let Err(e) = cleanup(&ctx, &mut tx, pkg_id, version).await { log::error!( "Failed to clean up {}@{}: {}: Adding to broken packages", pkg_id, @@ -563,7 +565,9 @@ pub async fn install_s9pk( { configured &= res.configured; } - cleanup(&ctx, &mut tx, Ok(prev)).await?; + if &prev.manifest.version != version { + uninstall(ctx, &mut tx, prev).await?; + } if let Some(res) = manifest .migrations .from(&prev_manifest.version, pkg_id, version, &manifest.volumes) diff --git a/appmgr/src/manager/mod.rs b/appmgr/src/manager/mod.rs index 89b8502dc..595532258 100644 --- a/appmgr/src/manager/mod.rs +++ b/appmgr/src/manager/mod.rs @@ -68,9 +68,9 @@ impl ManagerMap { ) -> Result<(), Error> { let mut lock = self.0.write().await; let id = (manifest.id.clone(), manifest.version.clone()); - if let Some(man) = lock.get(&id) { + if let Some(man) = lock.remove(&id) { if !man.thread.is_empty().await { - return Ok(()); + man.exit().await?; } } lock.insert( diff --git a/appmgr/src/s9pk/builder.rs b/appmgr/src/s9pk/builder.rs index 7a6fb8067..c28a055eb 100644 --- a/appmgr/src/s9pk/builder.rs +++ b/appmgr/src/s9pk/builder.rs @@ -55,7 +55,7 @@ impl< let mut writer = HashWriter::new(Sha512::new(), &mut self.writer); // manifest - serde_cbor::to_writer(&mut writer, self.manifest).with_ctx(|_| { + serde_cbor::ser::into_writer(self.manifest, &mut writer).with_ctx(|_| { ( crate::ErrorKind::Serialization, "Serializing Manifest (CBOR)", diff --git a/appmgr/src/s9pk/reader.rs b/appmgr/src/s9pk/reader.rs index 66600ab2f..e00da7344 100644 --- a/appmgr/src/s9pk/reader.rs +++ b/appmgr/src/s9pk/reader.rs @@ -121,8 +121,13 @@ impl S9pkReader { }) } + pub async fn manifest_raw<'a>(&'a mut self) -> Result, Error> { + self.read_handle(self.toc.manifest).await + } + pub async fn manifest(&mut self) -> Result { - serde_cbor::from_slice(&self.read_handle(self.toc.manifest).await?.to_vec().await?) + let slice = self.manifest_raw().await?.to_vec().await?; + serde_cbor::de::from_reader(slice.as_slice()) .with_ctx(|_| (crate::ErrorKind::ParseS9pk, "Deserializing Manifest (CBOR)")) } diff --git a/appmgr/src/util/mod.rs b/appmgr/src/util/mod.rs index fff8fa570..400956662 100644 --- a/appmgr/src/util/mod.rs +++ b/appmgr/src/util/mod.rs @@ -178,7 +178,7 @@ where { let mut buffer = Vec::new(); reader.read_to_end(&mut buffer).await?; - serde_cbor::from_slice(&buffer) + serde_cbor::de::from_reader(buffer.as_slice()) .map_err(anyhow::Error::from) .with_kind(crate::ErrorKind::Deserialization) } @@ -678,9 +678,8 @@ impl IoFormat { IoFormat::Yaml => { serde_yaml::to_writer(writer, value).with_kind(crate::ErrorKind::Serialization) } - IoFormat::Cbor => { - serde_cbor::to_writer(writer, value).with_kind(crate::ErrorKind::Serialization) - } + IoFormat::Cbor => serde_cbor::ser::into_writer(value, writer) + .with_kind(crate::ErrorKind::Serialization), IoFormat::Toml => writer .write_all( &serde_toml::to_vec( @@ -709,7 +708,12 @@ impl IoFormat { serde_json::to_vec_pretty(value).with_kind(crate::ErrorKind::Serialization) } IoFormat::Yaml => serde_yaml::to_vec(value).with_kind(crate::ErrorKind::Serialization), - IoFormat::Cbor => serde_cbor::to_vec(value).with_kind(crate::ErrorKind::Serialization), + IoFormat::Cbor => { + let mut res = Vec::new(); + serde_cbor::ser::into_writer(value, &mut res) + .with_kind(crate::ErrorKind::Serialization)?; + Ok(res) + } IoFormat::Toml => serde_toml::to_vec( &serde_toml::Value::try_from(value).with_kind(crate::ErrorKind::Serialization)?, ) @@ -734,7 +738,7 @@ impl IoFormat { serde_yaml::from_reader(reader).with_kind(crate::ErrorKind::Deserialization) } IoFormat::Cbor => { - serde_cbor::from_reader(reader).with_kind(crate::ErrorKind::Deserialization) + serde_cbor::de::from_reader(reader).with_kind(crate::ErrorKind::Deserialization) } IoFormat::Toml | IoFormat::TomlPretty => { let mut s = String::new(); @@ -754,7 +758,7 @@ impl IoFormat { serde_yaml::from_slice(slice).with_kind(crate::ErrorKind::Deserialization) } IoFormat::Cbor => { - serde_cbor::from_slice(slice).with_kind(crate::ErrorKind::Deserialization) + serde_cbor::de::from_reader(slice).with_kind(crate::ErrorKind::Deserialization) } IoFormat::Toml | IoFormat::TomlPretty => { serde_toml::from_slice(slice).with_kind(crate::ErrorKind::Deserialization)