Major refactor on types and protofile
detailed daemon container management proto modularised all protobuff rust types
This commit is contained in:
parent
ff7c5ade50
commit
28497eeb99
@ -4,20 +4,47 @@ package deamon;
|
|||||||
|
|
||||||
import "shared.proto";
|
import "shared.proto";
|
||||||
|
|
||||||
message Empty {
|
|
||||||
}
|
|
||||||
|
|
||||||
message NewContainerReq {
|
|
||||||
repeated string port = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
message NewContainerRes {
|
message NewContainerRes {
|
||||||
string status = 1;
|
optional shared.UUID container_id = 1;
|
||||||
|
string status = 2;
|
||||||
|
string ip_address = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ContainerInspectResp {
|
||||||
|
shared.Container containers = 1;
|
||||||
|
repeated shared.MappedPort mapped_ports = 2;
|
||||||
|
string crated_time = 3;
|
||||||
|
optional string ratls_pubkey = 4;
|
||||||
|
optional string mr_signer = 5;
|
||||||
|
optional string mr_enclave = 6;
|
||||||
|
string state = 7;
|
||||||
|
string disk_usage = 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
message LogResp {
|
||||||
|
string std_out = 1;
|
||||||
|
string std_err = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ContainerFilters {
|
||||||
|
string admin_pubkey = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message ContainerListResp {
|
||||||
|
repeated shared.Container containers = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message DeleteContainerRes {
|
||||||
|
optional shared.UUID container_id = 1;
|
||||||
|
string status = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
service DaemonService {
|
service DaemonService {
|
||||||
// rpc CreateContainer (NewContainerReq) returns (NewContainerRes);
|
|
||||||
rpc CreateContainer (shared.Container) returns (NewContainerRes);
|
rpc CreateContainer (shared.Container) returns (NewContainerRes);
|
||||||
// rpc ListContainer (NodeFilters) returns (stream NodeListResp);
|
rpc InspectContainer (shared.UUID) returns (ContainerInspectResp);
|
||||||
|
rpc ContainerLog (shared.UUID) returns (stream LogResp);
|
||||||
|
rpc ListContainers (ContainerFilters) returns (ContainerListResp);
|
||||||
|
rpc DeleteContainer (shared.UUID) returns (DeleteContainerRes);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,16 @@ message SetConfigResponse {
|
|||||||
string status = 1;
|
string status = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message Empty {}
|
message Empty {
|
||||||
|
}
|
||||||
|
message UUID {
|
||||||
|
string uuid = 1;
|
||||||
|
}
|
||||||
|
|
||||||
// The main Config structure
|
// The main Config structure
|
||||||
message ManagerConfigPB {
|
message ManagerConfigPB {
|
||||||
repeated FileEntry filesystem = 1;
|
repeated FileEntry filesystems = 1;
|
||||||
repeated EnvironmentEntry environment = 2;
|
repeated EnvironmentEntry environments = 2;
|
||||||
repeated ChildProcess child_processes = 3;
|
repeated ChildProcess child_processes = 3;
|
||||||
Container container = 4;
|
Container container = 4;
|
||||||
}
|
}
|
||||||
@ -52,12 +56,18 @@ service ConfigManager {
|
|||||||
rpc GetConfig(Empty) returns (ManagerConfigPB) {}
|
rpc GetConfig(Empty) returns (ManagerConfigPB) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message MappedPort {
|
||||||
|
uint32 host_port = 1;
|
||||||
|
uint32 container_port = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
message Container {
|
message Container {
|
||||||
optional string package_url = 1;
|
optional string package_url = 1;
|
||||||
string node = 2;
|
string node = 2;
|
||||||
Resource resource = 3;
|
Resource resource = 3;
|
||||||
string uuid = 4;
|
UUID uuid = 4;
|
||||||
|
string admin_pubkey = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message Resource {
|
message Resource {
|
||||||
|
126
src/lib.rs
126
src/lib.rs
@ -7,128 +7,4 @@ pub mod pb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod config {
|
pub mod pb_types;
|
||||||
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, prost::Oneof)]
|
|
||||||
pub enum FileContent {
|
|
||||||
#[serde(rename = "path")]
|
|
||||||
#[prost(string, tag = "2")]
|
|
||||||
Path(String),
|
|
||||||
#[serde(rename = "data")]
|
|
||||||
#[prost(string, tag = "3")]
|
|
||||||
Data(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for FileContent {
|
|
||||||
fn default() -> Self {
|
|
||||||
FileContent::Data("".to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, prost::Message)]
|
|
||||||
pub struct FileEntry {
|
|
||||||
#[prost(string, tag = "1")]
|
|
||||||
pub path: String,
|
|
||||||
#[prost(oneof = "FileContent", tags = "2, 3")]
|
|
||||||
pub content: Option<FileContent>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, prost::Message)]
|
|
||||||
pub struct EnvironmentEntry {
|
|
||||||
#[prost(string, tag = "1")]
|
|
||||||
pub name: String,
|
|
||||||
#[prost(string, tag = "2")]
|
|
||||||
pub value: String,
|
|
||||||
}
|
|
||||||
#[derive(Clone, Copy, Serialize, Deserialize, prost::Message)]
|
|
||||||
pub struct RestartPolicy {
|
|
||||||
#[prost(uint32, tag = "1")]
|
|
||||||
pub max_retries: u32,
|
|
||||||
#[prost(uint32, tag = "2")]
|
|
||||||
pub delay_seconds: u32,
|
|
||||||
#[prost(oneof = "RestartPolicyType", tags = "3, 4")]
|
|
||||||
pub policy: Option<RestartPolicyType>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Serialize, Deserialize, prost::Oneof)]
|
|
||||||
pub enum RestartPolicyType {
|
|
||||||
#[prost(bool, tag = "3")]
|
|
||||||
Always(bool),
|
|
||||||
#[prost(bool, tag = "4")]
|
|
||||||
OnNonZeroExit(bool),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for RestartPolicyType {
|
|
||||||
fn default() -> Self {
|
|
||||||
RestartPolicyType::Always(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, prost::Message)]
|
|
||||||
pub struct ChildProcess {
|
|
||||||
#[prost(string, tag = "1")]
|
|
||||||
pub path: String,
|
|
||||||
#[prost(string, repeated, tag = "2")]
|
|
||||||
pub arguments: Vec<String>,
|
|
||||||
#[prost(message, optional, tag = "3")]
|
|
||||||
pub restart: Option<RestartPolicy>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, prost::Message)]
|
|
||||||
pub struct Container {
|
|
||||||
#[prost(string, optional, tag = "1")]
|
|
||||||
pub package_url: Option<String>,
|
|
||||||
#[prost(string, tag = "2")]
|
|
||||||
pub node: String,
|
|
||||||
#[prost(message, optional, tag = "3")]
|
|
||||||
pub resource: Option<Resource>,
|
|
||||||
#[serde(default)]
|
|
||||||
#[prost(string, tag = "4")]
|
|
||||||
pub uuid: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, PartialEq, prost::Message)]
|
|
||||||
pub struct Resource {
|
|
||||||
#[prost(uint32, tag = "1")]
|
|
||||||
pub memory_mb: u32,
|
|
||||||
#[prost(uint32, tag = "2")]
|
|
||||||
pub disk_mb: u32,
|
|
||||||
#[prost(uint32, tag = "3")]
|
|
||||||
pub vcpu: u32,
|
|
||||||
#[prost(uint32, repeated, tag = "4")]
|
|
||||||
pub port: Vec<u32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, prost::Message)]
|
|
||||||
pub struct Config {
|
|
||||||
#[prost(message, repeated, tag = "1")]
|
|
||||||
pub filesystem: Vec<FileEntry>,
|
|
||||||
#[prost(message, repeated, tag = "2")]
|
|
||||||
pub environment: Vec<EnvironmentEntry>,
|
|
||||||
#[prost(message, repeated, tag = "3")]
|
|
||||||
pub child_processes: Vec<ChildProcess>,
|
|
||||||
#[prost(message, optional, tag = "4")]
|
|
||||||
pub container: Option<Container>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
|
||||||
pub fn from_path(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
|
||||||
let config_str = std::fs::read_to_string(path)?;
|
|
||||||
Ok(serde_yml::from_str(&config_str)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_data(mut self) -> Result<Self, Box<dyn std::error::Error>> {
|
|
||||||
self.filesystem.iter_mut().for_each(|x| {
|
|
||||||
if let Some(FileContent::Path(path)) = &x.content {
|
|
||||||
let content = std::fs::read(path).expect("Unable to read file {path}");
|
|
||||||
let encoded = BASE64.encode(content);
|
|
||||||
x.content = Some(FileContent::Data(encoded));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
1
src/pb_types.rs
Normal file
1
src/pb_types.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod shared;
|
125
src/pb_types/shared.rs
Normal file
125
src/pb_types/shared.rs
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, prost::Message)]
|
||||||
|
pub struct Config {
|
||||||
|
#[prost(message, repeated, tag = "1")]
|
||||||
|
pub filesystems: Vec<FileEntry>,
|
||||||
|
#[prost(message, repeated, tag = "2")]
|
||||||
|
pub environments: Vec<EnvironmentEntry>,
|
||||||
|
#[prost(message, repeated, tag = "3")]
|
||||||
|
pub child_processes: Vec<ChildProcess>,
|
||||||
|
#[prost(message, optional, tag = "4")]
|
||||||
|
pub container: Option<Container>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, prost::Message)]
|
||||||
|
pub struct FileEntry {
|
||||||
|
#[prost(string, tag = "1")]
|
||||||
|
pub path: String,
|
||||||
|
#[prost(oneof = "FileContent", tags = "2, 3")]
|
||||||
|
pub content: Option<FileContent>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, prost::Oneof)]
|
||||||
|
pub enum FileContent {
|
||||||
|
#[serde(rename = "path")]
|
||||||
|
#[prost(string, tag = "2")]
|
||||||
|
Path(String),
|
||||||
|
#[serde(rename = "data")]
|
||||||
|
#[prost(string, tag = "3")]
|
||||||
|
Data(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for FileContent {
|
||||||
|
fn default() -> Self {
|
||||||
|
FileContent::Data("".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, prost::Message)]
|
||||||
|
pub struct EnvironmentEntry {
|
||||||
|
#[prost(string, tag = "1")]
|
||||||
|
pub name: String,
|
||||||
|
#[prost(string, tag = "2")]
|
||||||
|
pub value: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, prost::Message)]
|
||||||
|
pub struct ChildProcess {
|
||||||
|
#[prost(string, tag = "1")]
|
||||||
|
pub path: String,
|
||||||
|
#[prost(string, repeated, tag = "2")]
|
||||||
|
pub arguments: Vec<String>,
|
||||||
|
#[prost(message, optional, tag = "3")]
|
||||||
|
pub restart: Option<RestartPolicy>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Serialize, Deserialize, prost::Message)]
|
||||||
|
pub struct RestartPolicy {
|
||||||
|
#[prost(uint32, tag = "1")]
|
||||||
|
pub max_retries: u32,
|
||||||
|
#[prost(uint32, tag = "2")]
|
||||||
|
pub delay_seconds: u32,
|
||||||
|
#[prost(oneof = "RestartPolicyType", tags = "3, 4")]
|
||||||
|
pub policy: Option<RestartPolicyType>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Serialize, Deserialize, prost::Oneof)]
|
||||||
|
pub enum RestartPolicyType {
|
||||||
|
#[prost(bool, tag = "3")]
|
||||||
|
Always(bool),
|
||||||
|
#[prost(bool, tag = "4")]
|
||||||
|
OnNonZeroExit(bool),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for RestartPolicyType {
|
||||||
|
fn default() -> Self {
|
||||||
|
RestartPolicyType::Always(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, prost::Message)]
|
||||||
|
pub struct Container {
|
||||||
|
#[prost(string, optional, tag = "1")]
|
||||||
|
pub package_url: Option<String>,
|
||||||
|
#[prost(string, tag = "2")]
|
||||||
|
pub node: String,
|
||||||
|
#[prost(message, optional, tag = "3")]
|
||||||
|
pub resource: Option<Resource>,
|
||||||
|
#[serde(default)]
|
||||||
|
#[prost(string, tag = "4")]
|
||||||
|
pub uuid: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, PartialEq, prost::Message)]
|
||||||
|
pub struct Resource {
|
||||||
|
#[prost(uint32, tag = "1")]
|
||||||
|
pub memory_mb: u32,
|
||||||
|
#[prost(uint32, tag = "2")]
|
||||||
|
pub disk_mb: u32,
|
||||||
|
#[prost(uint32, tag = "3")]
|
||||||
|
pub vcpu: u32,
|
||||||
|
#[prost(uint32, repeated, tag = "4")]
|
||||||
|
pub port: Vec<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn from_path(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
|
let config_str = std::fs::read_to_string(path)?;
|
||||||
|
Ok(serde_yml::from_str(&config_str)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_data(mut self) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
|
self.filesystems.iter_mut().for_each(|x| {
|
||||||
|
if let Some(FileContent::Path(path)) = &x.content {
|
||||||
|
let content =
|
||||||
|
std::fs::read(path).unwrap_or_else(|_| panic!("Unable to read file {path}"));
|
||||||
|
let encoded = BASE64.encode(content);
|
||||||
|
x.content = Some(FileContent::Data(encoded));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user