complete refactor pb types derive prost to impl From trait
changed FileEntry protobuff type into simple one
This commit is contained in:
parent
b162dd99c0
commit
73eac4cb04
@ -23,10 +23,7 @@ message ManagerConfigPB {
|
|||||||
// Represents a file entry with a path and content
|
// Represents a file entry with a path and content
|
||||||
message FileEntry {
|
message FileEntry {
|
||||||
string path = 1;
|
string path = 1;
|
||||||
oneof content {
|
string content = 2;
|
||||||
string data = 2;
|
|
||||||
string file_path = 3; // For the `Path` variant in FileContent
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Represents an environment variable entry
|
// Represents an environment variable entry
|
||||||
|
@ -7,4 +7,4 @@ pub mod pb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod pb_types;
|
pub mod types;
|
||||||
|
@ -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<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>,
|
|
||||||
#[prost(message, tag = "4")]
|
|
||||||
pub uuid: Option<Uuid>,
|
|
||||||
#[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<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)
|
|
||||||
}
|
|
||||||
}
|
|
290
src/types/shared.rs
Normal file
290
src/types/shared.rs
Normal file
@ -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<FileEntry>,
|
||||||
|
pub environments: Vec<EnvironmentEntry>,
|
||||||
|
pub child_processes: Vec<ChildProcess>,
|
||||||
|
pub container: Option<Container>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<pb_shared::ManagerConfigPb> 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<Config> 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<FileContent>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<pb_shared::FileEntry> for FileEntry {
|
||||||
|
fn from(pb_val: pb_shared::FileEntry) -> Self {
|
||||||
|
FileEntry {
|
||||||
|
path: pb_val.path,
|
||||||
|
content: Some(FileContent::Data(pb_val.content)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<FileEntry> 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<pb_shared::EnvironmentEntry> for EnvironmentEntry {
|
||||||
|
fn from(pb_val: pb_shared::EnvironmentEntry) -> Self {
|
||||||
|
EnvironmentEntry {
|
||||||
|
name: pb_val.name,
|
||||||
|
value: pb_val.value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<EnvironmentEntry> 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<String>,
|
||||||
|
pub restart: Option<RestartPolicy>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<pb_shared::ChildProcess> 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<ChildProcess> 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<RestartPolicyType>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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<pb_shared::RestartPolicy> 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<RestartPolicy> 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<String>,
|
||||||
|
pub node: String,
|
||||||
|
pub resource: Option<Resource>,
|
||||||
|
pub uuid: Option<Uuid>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub admin_pubkey: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<pb_shared::Container> 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<Container> 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<pb_shared::Uuid> for Uuid {
|
||||||
|
fn from(pb_val: pb_shared::Uuid) -> Self {
|
||||||
|
Self { uuid: pb_val.uuid }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<Uuid> 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<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<pb_shared::Resource> 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<Resource> 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<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