Skip to content

Commit

Permalink
wip: DatabaseMetadata -> InstanceData
Browse files Browse the repository at this point in the history
  • Loading branch information
cilki committed Dec 31, 2024
1 parent 38710cf commit b07bc2c
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 133 deletions.
2 changes: 1 addition & 1 deletion sandpolis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ layer-filesystem = ["dep:fuser"]
layer-health = []
layer-inventory = []
layer-logging = []
layer-packages = []
layer-package = []
layer-shell = []
layer-sysinfo = []
layer-probe = []
Expand Down
93 changes: 37 additions & 56 deletions sandpolis/src/agent/layer/package/mod.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,53 @@
use anyhow::{Result, bail};
use anyhow::{bail, Result};

pub trait PackageManager {
/// Get the location of the package manager's binary on the filesystem.
pub fn getManagerLocation(&self) -> Result<PathBuf> {
bail!("Not implemented");
}

/**
* Get the location of the package manager's binary on the filesystem.
*
* @return The package manager's file path
* @throws Exception
*/
public abstract Path getManagerLocation() throws Exception;

/**
* Get the package manager's version string.
*
* @return The local version
* @throws Exception
*/
public abstract String getManagerVersion() throws Exception;
/// Get the package manager's version string.
pub async fn getManagerVersion(&self) -> Result<String> {
bail!("Not implemented");
}

/**
* Remove old packages from the local package cache.
*
* @throws Exception
*/
public abstract void clean() throws Exception;
/// Remove old packages from the local package cache.
pub async fn clean() -> Result<()> {
bail!("Not implemented");
}

/**
* Get all currently installed packages.
*
* @return All locally installed packages
* @throws Exception
*/
public abstract List<Package> getInstalled() throws Exception;
/// Get all currently installed packages.
pub async fn getInstalled() -> Result<Vec<Package>> {
bail!("Not implemented");
}

/**
* Gather advanced metadata for the given package.
*
* @param name The package name
* @return The package metadata
* @throws Exception
*/
public abstract Package getMetadata(String name) throws Exception;
/// Gather advanced metadata for the given package.
pub async fn getMetadata(name: String) -> Result<Package> {
bail!("Not implemented");
}

/**
* Get all packages that are currently outdated.
*
* @return All packages that have a newer version available
* @throws Exception
*/
public abstract List<Package> getOutdated() throws Exception;
/// Get all packages that are currently outdated.
pub async fn getOutdated() -> Result<Vec<Package>> {
bail!("Not implemented");
}

/// Install the given packages onto the local system.
pub async fn install(packages: Vec<String>) -> Result<()> {
todo!("Not implemented");
}
/// Install the given packages onto the local system.
pub async fn install(packages: Vec<String>) -> Result<()> {
bail!("Not implemented");
}

/// Synchronize the local package database with all remote repositories.
pub async fn refresh() -> Result<()> {
todo!("Not implemented");
}
/// Synchronize the local package database with all remote repositories.
pub async fn refresh() -> Result<()> {
bail!("Not implemented");
}

/// Remove the given packages from the local system.
pub async fn remove(packages: Vec<String>) -> Result<()> {
todo!("Not implemented");
/// Remove the given packages from the local system.
pub async fn remove(packages: Vec<String>) -> Result<()> {
bail!("Not implemented");
}

/// Upgrade the given packages to the latest available version.
pub async fn upgrade(packages: Vec<String>) -> Result<()> {
todo!("Not implemented");
bail!("Not implemented");
}
}
21 changes: 21 additions & 0 deletions sandpolis/src/agent/layer/shell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use tokio::process::Child;

pub struct ShellSession {
pub data: ShellSessionData,
pub process: Child,
}

impl StreamSource<ShellSessionOutputEvent> for ShellSession {
async fn emit(&self) -> ShellSessionOutputEvent {}
}

impl StreamSink<ShellSessionInputEvent> for ShellSession {
async fn accept(&self, event: ShellSessionInputEvent) -> Result<ShellSessionOutputEvent> {}
}

impl Drop for ShellSession {
fn drop(&mut self) {
debug!("Killing child process");
self.process.kill()
}
}
2 changes: 1 addition & 1 deletion sandpolis/src/client/ui/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub fn handle_layer_change(
**current_layer = Layer::Filesystem;
timer.reset();
}
#[cfg(feature = "layer-packages")]
#[cfg(feature = "layer-package")]
if keyboard_input.pressed(KeyCode::KeyP) {
**current_layer = Layer::Packages;
timer.reset();
Expand Down
2 changes: 1 addition & 1 deletion sandpolis/src/client/ui/layer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(feature = "layer-desktop")]
pub mod desktop;
#[cfg(feature = "layer-packages")]
#[cfg(feature = "layer-package")]
pub mod package;
32 changes: 5 additions & 27 deletions sandpolis/src/core/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,7 @@ use std::{
use tracing::debug;

use super::InstanceId;
use super::{Instance, InstanceType};

#[derive(Serialize, Deserialize, Clone, Debug)]
struct DatabaseMetadata {
pub id: InstanceId,
pub os_info: os_info::Info,
}

impl DatabaseMetadata {
pub fn new() -> Self {
DatabaseMetadata {
id: InstanceId::new(&[
#[cfg(feature = "server")]
InstanceType::Server,
#[cfg(feature = "client")]
InstanceType::Client,
#[cfg(feature = "agent")]
InstanceType::Agent,
]),
os_info: os_info::get(),
}
}
}
use super::{Instance, InstanceData, InstanceType};

#[derive(Clone)]
pub struct Database {
Expand All @@ -49,27 +27,27 @@ impl Database {
let db = sled::Config::new().path(path.as_ref()).open()?;

// Query for the local instance
let local: DatabaseMetadata = if let Some(local) = db.get("local")? {
let local: InstanceData = if let Some(local) = db.get("local")? {
let local = serde_json::from_slice(&local)?;
debug!(local = ?local, "Restoring local instance metadata");
local
} else {
let local = DatabaseMetadata::new();
let local = InstanceData::new();
debug!(local = ?local, "Creating new local instance metadata");
db.insert("local", serde_json::to_vec(&local)?)?;
local
};

Ok(Self {
this: Arc::new(Instance {
id: local.id,
db: db.open_tree(local.id)?,
data: local,
}),
db,
})
}

pub fn instance(&self, id: impl Into<InstanceId>) -> Instance {
pub fn instance(&self, id: impl Into<InstanceId>) -> InstanceData {
let id = id.into();

Instance {
Expand Down
2 changes: 1 addition & 1 deletion sandpolis/src/core/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub mod desktop;
#[cfg(feature = "layer-filesystem")]
pub mod filesystem;
#[cfg(feature = "layer-packages")]
#[cfg(feature = "layer-package")]
pub mod package;
#[cfg(feature = "layer-probe")]
pub mod probe;
Expand Down
40 changes: 28 additions & 12 deletions sandpolis/src/core/layer/network/mod.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
use crate::core::InstanceId;
use anyhow::Result;
use std::{collections::HashMap, sync::Arc};
use stream::StreamSource;

pub mod stream;

pub struct NetworkLayerData {}

pub struct NetworkLayer {
db: sled::Tree,

sources: HashMap<u64, Arc<dyn StreamSource>>,
}

struct PingRequest;

enum PingResponse {
Ok,
pub struct PingResponse {
pub time: u64,
pub id: InstanceId,
pub from: Option<PingResponse>,
}

impl NetworkLayer {
pub fn ping(&self, id: InstanceId) {}
}
/// Send a message to the given instance and measure the time/path it took.
pub async fn ping(&self, id: InstanceId) -> Result<PingResponse> {}

// Request the server for a new direct connection.
// message RQ_DirectConnection {
/// Request the server to coordinate a direct connection to the given agent.
pub async fn direct_connect(&self, agent: InstanceId, port: Option<u16>) {}
}

// // The requested node
// int32 sid = 1;
/// Request the server for a new direct connection.
// #[from = "client"]
// #[to = "server"]
pub struct DirectConnectionRequest {
// The requested node
id: InstanceId,

// // An optional listener port. If specified, the requested node will attempt
// // a connection on this port. Otherwise, the server will coordinate the connection.
// int32 port = 3;
// }
// An optional listener port. If specified, the requested node will attempt
// a connection on this port. Otherwise, the server will coordinate the connection.
port: Option<u16>,
}

// Request that the recieving instance establish a new connection to the given host.
// message RQ_CoordinateConnection {
Expand Down
42 changes: 28 additions & 14 deletions sandpolis/src/core/layer/network/stream.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
use anyhow::Result;
use serde::Serialize;

message RQ_STStream {
enum Direction {
UPSTREAM = 0;
DOWNSTREAM = 1;
BIDIRECTIONAL = 2;
}
pub trait StreamSource {
type O: Serialize;

async fn emit(&self) -> Result<Self::O>;
}

pub trait StreamSink {
type I: Deserialize;
type O: Serialize;

async fn accept(&self, input: Self::I) -> Result<Self::O>;
}

pub enum DataStreamDirection {
Upstream,
Downstream,
Bidirectional,
}

int32 stream_id = 1;
pub struct DataStreamRequest {

bool permanent = 2;
pub permanent: bool,

string oid = 3;
repeated string whitelist = 4;

Direction direction = 5;
int32 update_period = 6;
pub direction: DataStreamDirection,
pub update_period: Optional<u64>,
}

enum RS_STStream {
ST_STREAM_OK = 0;
ST_STREAM_INVALID = 1;
ST_STREAM_FAILED = 2;
enum DataStreamResponse {
Ok(u64),
Invalid,
Failed,
}

message EV_STStreamData {
Expand Down
12 changes: 1 addition & 11 deletions sandpolis/src/core/layer/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct PackageLayer {
impl PackageLayer {
pub fn pacman() {}

pub fn packages_iter() -> impl Iterator<Item = Package> {}
// pub fn packages_iter() -> impl Iterator<Item = Package> {}
}

#[derive(Serialize, Deserialize)]
Expand All @@ -31,11 +31,6 @@ pub enum PackageManager {
#[derive(Serialize, Deserialize)]
#[cfg_attr(feature = "client", derive(bevy::prelude::Component))]
pub struct PackageManagerInfo {
#[serde(skip_serializing_if = "String::is_empty")]
pub _id: DocumentId,
#[serde(skip_serializing_if = "String::is_empty")]
pub _rev: String,

/// Type of package manager
pub manager: PackageManager,

Expand All @@ -49,11 +44,6 @@ pub struct PackageManagerInfo {
#[derive(Serialize, Deserialize)]
#[cfg_attr(feature = "client", derive(bevy::prelude::Component))]
pub struct Package {
#[serde(skip_serializing_if = "String::is_empty")]
pub _id: DocumentId,
#[serde(skip_serializing_if = "String::is_empty")]
pub _rev: String,

/// Canonical name/identifier
pub name: String,

Expand Down
8 changes: 6 additions & 2 deletions sandpolis/src/core/layer/shell.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::{collections::HashMap, path::PathBuf};

pub struct ShellLayer {
tree: sled::Tree,
}

pub enum ShellType {
/// Busybox shell
Ash,
Expand All @@ -23,10 +27,10 @@ pub enum ShellType {
Zsh,
}

pub struct ShellSession {
pub struct ShellSessionData {
pub shell_type: ShellType,

/// Path to the executing shell's executable
/// Path to the shell's executable
pub shell: PathBuf,

/// Total CPU time used by the process and all children
Expand Down
Loading

0 comments on commit b07bc2c

Please sign in to comment.