miscellaneous fixes

This commit is contained in:
Aiden McClelland
2021-09-27 13:48:19 -06:00
committed by Aiden McClelland
parent 1eeffc0bf8
commit e3d8861199
12 changed files with 188 additions and 182 deletions

View File

@@ -6,6 +6,7 @@ use embassy::db::subscribe;
use embassy::middleware::auth::auth;
use embassy::middleware::cors::cors;
use embassy::middleware::diagnostic::diagnostic;
#[cfg(feature = "avahi")]
use embassy::net::mdns::MdnsController;
use embassy::net::tor::tor_health_check;
use embassy::shutdown::Shutdown;

View File

@@ -10,6 +10,7 @@ use tokio::fs::File;
use tokio::sync::broadcast::Sender;
use url::Host;
use crate::shutdown::Shutdown;
use crate::util::io::from_toml_async_reader;
use crate::util::AsyncFileExt;
use crate::{Error, ResultExt};
@@ -18,6 +19,7 @@ use crate::{Error, ResultExt};
#[serde(rename_all = "kebab-case")]
pub struct DiagnosticContextConfig {
pub bind_rpc: Option<SocketAddr>,
pub zfs_pool_name: Option<String>,
}
impl DiagnosticContextConfig {
pub async fn load<P: AsRef<Path>>(path: Option<P>) -> Result<Self, Error> {
@@ -34,12 +36,19 @@ impl DiagnosticContextConfig {
Ok(Self::default())
}
}
pub fn zfs_pool_name(&self) -> &str {
self.zfs_pool_name
.as_ref()
.map(|s| s.as_str())
.unwrap_or("embassy-data")
}
}
pub struct DiagnosticContextSeed {
pub bind_rpc: SocketAddr,
pub shutdown: Sender<()>,
pub shutdown: Sender<Option<Shutdown>>,
pub error: Arc<RpcError>,
pub zfs_pool_name: Arc<String>,
}
#[derive(Clone)]
@@ -54,6 +63,7 @@ impl DiagnosticContext {
bind_rpc: cfg.bind_rpc.unwrap_or(([127, 0, 0, 1], 5959).into()),
shutdown,
error: Arc::new(error.into()),
zfs_pool_name: Arc::new(cfg.zfs_pool_name().to_owned()),
})))
}
}

View File

@@ -5,12 +5,13 @@ use rpc_toolkit::yajrc::RpcError;
use crate::context::DiagnosticContext;
use crate::logs::{display_logs, fetch_logs, LogResponse, LogSource};
use crate::shutdown::Shutdown;
use crate::util::display_none;
use crate::Error;
pub const SYSTEMD_UNIT: &'static str = "embassy-init";
#[command(subcommands(error, logs, exit))]
#[command(subcommands(error, logs, exit, restart, forget_disk))]
pub fn diagnostic() -> Result<(), Error> {
Ok(())
}
@@ -37,13 +38,20 @@ pub async fn logs(
#[command(display(display_none))]
pub fn exit(#[context] ctx: DiagnosticContext) -> Result<(), Error> {
ctx.shutdown.send(()).expect("receiver dropped");
ctx.shutdown.send(None).expect("receiver dropped");
Ok(())
}
#[command(display(display_none))]
pub fn restart(#[context] ctx: DiagnosticContext) -> Result<(), Error> {
todo!()
ctx.shutdown
.send(Some(Shutdown {
zfs_pool: ctx.zfs_pool_name.clone(),
db_handle: None,
restart: true,
}))
.expect("receiver dropped");
Ok(())
}
#[command(rename = "forget-disk", display(display_none))]

View File

@@ -100,7 +100,7 @@ pub async fn cleanup(ctx: &RpcContext, id: &PackageId, version: &Version) -> Res
ctx.docker.remove_image(&image.id, None, None).await
}))
.await?;
let docker_path = Path::new(PKG_DOCKER_DIR).join(id).join(version.as_str());
let docker_path = ctx.datadir.join(id).join(version.as_str());
if tokio::fs::metadata(&docker_path).await.is_ok() {
tokio::fs::remove_dir_all(&docker_path).await?;
}

View File

@@ -181,7 +181,7 @@ pub async fn download_install_s9pk(
.join(pkg_id)
.join(version.as_str());
tokio::fs::create_dir_all(&pkg_cache_dir).await?;
let pkg_cache = AsRef::<Path>::as_ref(pkg_id).with_extension("s9pk");
let pkg_cache = pkg_cache_dir.join(AsRef::<Path>::as_ref(pkg_id).with_extension("s9pk"));
let pkg_data_entry = crate::db::DatabaseModel::new()
.package_data()
@@ -415,7 +415,9 @@ pub async fn install_s9pk<R: AsyncRead + AsyncSeek + Unpin>(
log::info!("Install {}@{}: Unpacking Docker Images", pkg_id, version);
progress
.track_read_during(progress_model.clone(), &ctx.db, || async {
let image_tar_dir = Path::new(PKG_DOCKER_DIR)
let image_tar_dir = ctx
.datadir
.join(PKG_DOCKER_DIR)
.join(pkg_id)
.join(version.as_str());
if tokio::fs::metadata(&image_tar_dir).await.is_err() {

View File

@@ -61,7 +61,7 @@ impl<
"Serializing Manifest (CBOR)",
)
})?;
let new_pos = writer.stream_position()?;
let new_pos = writer.inner_mut().stream_position()?;
header.table_of_contents.manifest = FileSection {
position,
length: new_pos - position,
@@ -70,7 +70,7 @@ impl<
// license
std::io::copy(&mut self.license, &mut writer)
.with_ctx(|_| (crate::ErrorKind::Filesystem, "Copying License"))?;
let new_pos = writer.stream_position()?;
let new_pos = writer.inner_mut().stream_position()?;
header.table_of_contents.license = FileSection {
position,
length: new_pos - position,
@@ -79,7 +79,7 @@ impl<
// instructions
std::io::copy(&mut self.instructions, &mut writer)
.with_ctx(|_| (crate::ErrorKind::Filesystem, "Copying Instructions"))?;
let new_pos = writer.stream_position()?;
let new_pos = writer.inner_mut().stream_position()?;
header.table_of_contents.instructions = FileSection {
position,
length: new_pos - position,
@@ -88,7 +88,7 @@ impl<
// icon
std::io::copy(&mut self.icon, &mut writer)
.with_ctx(|_| (crate::ErrorKind::Filesystem, "Copying Icon"))?;
let new_pos = writer.stream_position()?;
let new_pos = writer.inner_mut().stream_position()?;
header.table_of_contents.icon = FileSection {
position,
length: new_pos - position,
@@ -97,7 +97,7 @@ impl<
// docker_images
std::io::copy(&mut self.docker_images, &mut writer)
.with_ctx(|_| (crate::ErrorKind::Filesystem, "Copying Docker Images"))?;
let new_pos = writer.stream_position()?;
let new_pos = writer.inner_mut().stream_position()?;
header.table_of_contents.docker_images = FileSection {
position,
length: new_pos - position,
@@ -106,7 +106,7 @@ impl<
// assets
std::io::copy(&mut self.assets, &mut writer)
.with_ctx(|_| (crate::ErrorKind::Filesystem, "Copying Assets"))?;
let new_pos = writer.stream_position()?;
let new_pos = writer.inner_mut().stream_position()?;
header.table_of_contents.assets = FileSection {
position,
length: new_pos - position,

View File

@@ -9,11 +9,11 @@ use crate::sound::MARIO_DEATH;
use crate::util::{display_none, Invoke};
use crate::Error;
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Shutdown {
zfs_pool: Arc<String>,
restart: bool,
db_handle: Arc<PatchDbHandle>,
pub zfs_pool: Arc<String>,
pub restart: bool,
pub db_handle: Option<Arc<PatchDbHandle>>,
}
impl Shutdown {
/// BLOCKING
@@ -43,7 +43,7 @@ impl Shutdown {
{
log::error!("Error Stopping Docker: {}", e);
}
if let Err(e) = export(&self.zfs_pool).await {
if let Err(e) = export(&*self.zfs_pool).await {
log::error!("Error Exporting ZFS Pool: {}", e);
}
if let Err(e) = MARIO_DEATH.play().await {
@@ -72,7 +72,7 @@ pub async fn shutdown(#[context] ctx: RpcContext) -> Result<(), Error> {
.send(Some(Shutdown {
zfs_pool: ctx.zfs_pool_name.clone(),
restart: false,
db_handle: Arc::new(db),
db_handle: Some(Arc::new(db)),
}))
.map_err(|_| ())
.expect("receiver dropped");
@@ -87,7 +87,7 @@ pub async fn restart(#[context] ctx: RpcContext) -> Result<(), Error> {
.send(Some(Shutdown {
zfs_pool: ctx.zfs_pool_name.clone(),
restart: true,
db_handle: Arc::new(db),
db_handle: Some(Arc::new(db)),
}))
.map_err(|_| ())
.expect("receiver dropped");

View File

@@ -10,7 +10,7 @@ use crate::context::RpcContext;
use crate::util::{display_none, display_serializable, IoFormat};
use crate::{Error, ErrorKind};
static SSH_AUTHORIZED_KEYS_FILE: &str = "~/.ssh/authorized_keys";
static SSH_AUTHORIZED_KEYS_FILE: &str = "/root/.ssh/authorized_keys";
#[derive(serde::Deserialize, serde::Serialize)]
pub struct PubKey(

View File

@@ -686,6 +686,12 @@ impl<H: Digest, W: std::io::Write> HashWriter<H, W> {
pub fn finish(self) -> (H, W) {
(self.hasher, self.writer)
}
pub fn inner(&self) -> &W {
&self.writer
}
pub fn inner_mut(&mut self) -> &mut W {
&mut self.writer
}
}
impl<H: Digest, W: std::io::Write> std::io::Write for HashWriter<H, W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
@@ -697,17 +703,6 @@ impl<H: Digest, W: std::io::Write> std::io::Write for HashWriter<H, W> {
self.writer.flush()
}
}
impl<H: Digest, W: std::io::Write> std::ops::Deref for HashWriter<H, W> {
type Target = W;
fn deref(&self) -> &Self::Target {
&self.writer
}
}
impl<H: Digest, W: std::io::Write> std::ops::DerefMut for HashWriter<H, W> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.writer
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Port(pub u16);