-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from monadicus/feat-agent-db-only
feat(agent): basic DB persistence
- Loading branch information
Showing
18 changed files
with
163 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::{ | ||
io::{Read, Write}, | ||
path::PathBuf, | ||
sync::Mutex, | ||
}; | ||
|
||
use snops_common::{ | ||
db::{error::DatabaseError, tree::DbTree, Database as DatabaseTrait}, | ||
format::{DataFormat, DataReadError, DataWriteError}, | ||
}; | ||
|
||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] | ||
#[repr(u8)] | ||
pub enum AgentDbString { | ||
/// JSON web token of agent. | ||
Jwt, | ||
/// Process ID of node. Used to keep track of zombie node processes. | ||
NodePid, | ||
} | ||
|
||
impl DataFormat for AgentDbString { | ||
type Header = u8; | ||
const LATEST_HEADER: Self::Header = 1; | ||
|
||
fn read_data<R: Read>(reader: &mut R, header: &Self::Header) -> Result<Self, DataReadError> { | ||
if *header != Self::LATEST_HEADER { | ||
return Err(DataReadError::unsupported( | ||
"AgentDbString", | ||
Self::LATEST_HEADER, | ||
header, | ||
)); | ||
} | ||
|
||
Ok(match u8::read_data(reader, &())? { | ||
0 => Self::Jwt, | ||
1 => Self::NodePid, | ||
_ => return Err(DataReadError::custom("invalid agent DB string type")), | ||
}) | ||
} | ||
|
||
fn write_data<W: Write>(&self, writer: &mut W) -> Result<usize, DataWriteError> { | ||
(*self as u8).write_data(writer) | ||
} | ||
} | ||
|
||
pub struct Database { | ||
#[allow(unused)] | ||
pub db: sled::Db, | ||
|
||
pub jwt_mutex: Mutex<Option<String>>, | ||
pub strings: DbTree<AgentDbString, String>, | ||
} | ||
|
||
impl DatabaseTrait for Database { | ||
fn open(path: &PathBuf) -> Result<Self, DatabaseError> { | ||
let db = sled::open(path)?; | ||
let strings = DbTree::new(db.open_tree(b"v1/strings")?); | ||
let jwt_mutex = Mutex::new(strings.restore(&AgentDbString::Jwt)?); | ||
|
||
Ok(Self { | ||
db, | ||
jwt_mutex, | ||
strings, | ||
}) | ||
} | ||
} | ||
|
||
impl Database { | ||
pub fn jwt(&self) -> Option<String> { | ||
self.jwt_mutex.lock().unwrap().clone() | ||
} | ||
|
||
pub fn set_jwt(&self, jwt: Option<String>) -> Result<(), DatabaseError> { | ||
let mut lock = self.jwt_mutex.lock().unwrap(); | ||
self.strings | ||
.save_option(&AgentDbString::Jwt, jwt.as_ref())?; | ||
*lock = jwt; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use std::path::PathBuf; | ||
|
||
use self::error::DatabaseError; | ||
|
||
pub mod error; | ||
pub mod tree; | ||
|
||
pub trait Database: Sized { | ||
fn open(path: &PathBuf) -> Result<Self, DatabaseError>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.