diff --git a/proto/shared.proto b/proto/shared.proto index 9abc99f..0ab4116 100644 --- a/proto/shared.proto +++ b/proto/shared.proto @@ -23,10 +23,7 @@ message ManagerConfigPB { // Represents a file entry with a path and content message FileEntry { string path = 1; - oneof content { - string data = 2; - string file_path = 3; // For the `Path` variant in FileContent - } + string content = 2; } // Represents an environment variable entry diff --git a/src/lib.rs b/src/lib.rs index 567c78d..0b77d13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,4 +7,4 @@ pub mod pb { } } -pub mod pb_types; +pub mod types; diff --git a/src/pb_types/shared.rs b/src/pb_types/shared.rs deleted file mode 100644 index beee565..0000000 --- a/src/pb_types/shared.rs +++ /dev/null @@ -1,133 +0,0 @@ -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, - #[prost(message, repeated, tag = "2")] - pub environments: Vec, - #[prost(message, repeated, tag = "3")] - pub child_processes: Vec, - #[prost(message, optional, tag = "4")] - pub container: Option, -} - -#[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, -} - -#[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, - #[prost(message, optional, tag = "3")] - pub restart: Option, -} - -#[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, -} - -#[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, - #[prost(string, tag = "2")] - pub node: String, - #[prost(message, optional, tag = "3")] - pub resource: Option, - #[prost(message, tag = "4")] - pub uuid: Option, - #[serde(default)] - #[prost(string, tag = "5")] - pub admin_pubkey: String, -} - -#[derive(Clone, Serialize, Deserialize, prost::Message)] -pub struct Uuid { - #[prost(string, tag = "1")] - 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, -} - -impl Config { - pub fn from_path(path: &str) -> Result> { - let config_str = std::fs::read_to_string(path)?; - Ok(serde_yml::from_str(&config_str)?) - } - - pub fn load_data(mut self) -> Result> { - 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) - } -} diff --git a/src/pb_types.rs b/src/types.rs similarity index 100% rename from src/pb_types.rs rename to src/types.rs diff --git a/src/types/shared.rs b/src/types/shared.rs new file mode 100644 index 0000000..ef3e5e9 --- /dev/null +++ b/src/types/shared.rs @@ -0,0 +1,290 @@ +use base64::{engine::general_purpose::STANDARD as BASE64, Engine}; +use serde::{Deserialize, Serialize}; + +use crate::pb::shared as pb_shared; + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct Config { + pub filesystems: Vec, + pub environments: Vec, + pub child_processes: Vec, + pub container: Option, +} + +impl From for Config { + fn from(pb_val: pb_shared::ManagerConfigPb) -> Self { + Config { + filesystems: pb_val + .filesystems + .into_iter() + .map(FileEntry::from) + .collect(), + environments: pb_val + .environments + .into_iter() + .map(EnvironmentEntry::from) + .collect(), + child_processes: pb_val + .child_processes + .into_iter() + .map(ChildProcess::from) + .collect(), + container: pb_val.container.map(Container::from), + } + } +} + +impl From for pb_shared::ManagerConfigPb { + fn from(val: Config) -> pb_shared::ManagerConfigPb { + pb_shared::ManagerConfigPb { + filesystems: val.filesystems.into_iter().map(Into::into).collect(), + environments: val.environments.into_iter().map(Into::into).collect(), + child_processes: val.child_processes.into_iter().map(Into::into).collect(), + container: val.container.map(Into::into), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct FileEntry { + pub path: String, + pub content: Option, +} + +impl From for FileEntry { + fn from(pb_val: pb_shared::FileEntry) -> Self { + FileEntry { + path: pb_val.path, + content: Some(FileContent::Data(pb_val.content)), + } + } +} +impl From for pb_shared::FileEntry { + fn from(val: FileEntry) -> pb_shared::FileEntry { + pb_shared::FileEntry { + path: val.path, + content: match val.content { + Some(FileContent::Data(data)) => data, + Some(FileContent::Path(path)) => path, + None => String::new(), + }, + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum FileContent { + #[serde(rename = "path")] + Path(String), + #[serde(rename = "data")] + Data(String), +} + +impl Default for FileContent { + fn default() -> Self { + FileContent::Data("".to_string()) + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct EnvironmentEntry { + pub name: String, + pub value: String, +} + +impl From for EnvironmentEntry { + fn from(pb_val: pb_shared::EnvironmentEntry) -> Self { + EnvironmentEntry { + name: pb_val.name, + value: pb_val.value, + } + } +} + +impl From for pb_shared::EnvironmentEntry { + fn from(val: EnvironmentEntry) -> pb_shared::EnvironmentEntry { + pb_shared::EnvironmentEntry { + name: val.name, + value: val.value, + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct ChildProcess { + pub path: String, + pub arguments: Vec, + pub restart: Option, +} + +impl From for ChildProcess { + fn from(pb_val: pb_shared::ChildProcess) -> Self { + ChildProcess { + path: pb_val.path, + arguments: pb_val.arguments, + restart: pb_val.restart.map(RestartPolicy::from), + } + } +} + +impl From for pb_shared::ChildProcess { + fn from(val: ChildProcess) -> pb_shared::ChildProcess { + pb_shared::ChildProcess { + path: val.path, + arguments: val.arguments, + restart: val.restart.map(Into::into), + } + } +} + +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +pub struct RestartPolicy { + pub max_retries: u32, + pub delay_seconds: u32, + pub policy: Option, +} + +#[derive(Debug, Clone, Copy, Serialize, Deserialize)] +pub enum RestartPolicyType { + Always(bool), + OnNonZeroExit(bool), +} + +impl Default for RestartPolicyType { + fn default() -> Self { + RestartPolicyType::Always(true) + } +} +impl From for RestartPolicy { + fn from(pb_val: pb_shared::RestartPolicy) -> Self { + RestartPolicy { + max_retries: pb_val.max_retries, + delay_seconds: pb_val.delay_seconds, + policy: match pb_val.policy_type { + Some(pb_shared::restart_policy::PolicyType::Always(_)) => { + Some(RestartPolicyType::Always(true)) + } + Some(pb_shared::restart_policy::PolicyType::OnNonZeroExit(_)) => { + Some(RestartPolicyType::OnNonZeroExit(true)) + } + None => None, + }, + } + } +} + +impl From for pb_shared::RestartPolicy { + fn from(val: RestartPolicy) -> pb_shared::RestartPolicy { + pb_shared::RestartPolicy { + max_retries: val.max_retries, + delay_seconds: val.delay_seconds, + policy_type: match val.policy { + Some(RestartPolicyType::Always(_)) => { + Some(pb_shared::restart_policy::PolicyType::Always(true)) + } + Some(RestartPolicyType::OnNonZeroExit(_)) => { + Some(pb_shared::restart_policy::PolicyType::OnNonZeroExit(true)) + } + None => None, + }, + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct Container { + pub package_url: Option, + pub node: String, + pub resource: Option, + pub uuid: Option, + #[serde(default)] + pub admin_pubkey: String, +} + +impl From for Container { + fn from(pb_val: pb_shared::Container) -> Self { + Self { + package_url: pb_val.package_url, + node: pb_val.node, + resource: pb_val.resource.map(Resource::from), + uuid: pb_val.uuid.map(Uuid::from), + admin_pubkey: pb_val.admin_pubkey, + } + } +} + +impl From for pb_shared::Container { + fn from(val: Container) -> pb_shared::Container { + pb_shared::Container { + package_url: val.package_url, + node: val.node, + resource: val.resource.map(Into::into), + uuid: val.uuid.map(Into::into), + admin_pubkey: val.admin_pubkey, + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Uuid { + pub uuid: String, +} + +impl From for Uuid { + fn from(pb_val: pb_shared::Uuid) -> Self { + Self { uuid: pb_val.uuid } + } +} +impl From for pb_shared::Uuid { + fn from(val: Uuid) -> pb_shared::Uuid { + pb_shared::Uuid { uuid: val.uuid } + } +} +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Resource { + pub memory_mb: u32, + pub disk_mb: u32, + pub vcpu: u32, + pub port: Vec, +} + +impl From for Resource { + fn from(pb_val: pb_shared::Resource) -> Self { + Self { + memory_mb: pb_val.memory_mb, + disk_mb: pb_val.disk_mb, + vcpu: pb_val.vcpu, + port: pb_val.ports, + } + } +} +impl From for pb_shared::Resource { + fn from(val: Resource) -> pb_shared::Resource { + pb_shared::Resource { + memory_mb: val.memory_mb, + disk_mb: val.disk_mb, + vcpu: val.vcpu, + ports: val.port, + } + } +} + +impl Config { + pub fn from_path(path: &str) -> Result> { + let config_str = std::fs::read_to_string(path)?; + Ok(serde_yml::from_str(&config_str)?) + } + + pub fn load_data(mut self) -> Result> { + 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) + } +}