diff --git a/bin/migrator/src/legacy/v1_6/build.rs b/bin/migrator/src/legacy/v1_6/build.rs index bd042ddf0..300ea1ab6 100644 --- a/bin/migrator/src/legacy/v1_6/build.rs +++ b/bin/migrator/src/legacy/v1_6/build.rs @@ -1,3 +1,7 @@ +use monitor_client::entities::{ + build::{CloudRegistryConfig, ImageRegistry}, + NoData, +}; use serde::{Deserialize, Serialize}; use super::{ @@ -6,31 +10,20 @@ use super::{ pub type Build = Resource; -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct BuildListItemInfo { - /// Unix timestamp in milliseconds of last build - pub last_built_at: i64, - /// The current version of the build - pub version: Version, - /// The Github repo used as the source of the build - pub repo: String, - /// The branch of the repo - pub branch: String, - /// State of the build. Reflects whether most recent build successful. - pub state: BuildState, -} - -#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)] -pub enum BuildState { - /// Last build successful (or never built) - Ok, - /// Last build failed - Failed, - /// Currently building - Building, - /// Other case - #[default] - Unknown, +impl From for monitor_client::entities::build::Build { + fn from(value: Build) -> Self { + monitor_client::entities::build::Build { + id: value.id, + name: value.name, + description: value.description, + updated_at: value.updated_at, + tags: value.tags, + info: monitor_client::entities::build::BuildInfo { + last_built_at: value.info.last_built_at, + }, + config: value.config.into(), + } + } } #[derive(Serialize, Deserialize, Debug, Clone, Default)] @@ -114,6 +107,49 @@ pub struct BuildConfig { pub webhook_enabled: bool, } +impl From + for monitor_client::entities::build::BuildConfig +{ + fn from(value: BuildConfig) -> Self { + monitor_client::entities::build::BuildConfig { + builder_id: value.builder_id, + skip_secret_interp: value.skip_secret_interp, + version: monitor_client::entities::Version { + major: value.version.major, + minor: value.version.minor, + patch: value.version.patch, + }, + repo: value.repo, + branch: value.branch, + commit: value.commit, + github_account: value.github_account, + pre_build: monitor_client::entities::SystemCommand { + path: value.pre_build.path, + command: value.pre_build.command, + }, + build_path: value.build_path, + dockerfile_path: value.dockerfile_path, + build_args: value + .build_args + .into_iter() + .map(Into::into) + .collect(), + labels: value.labels.into_iter().map(Into::into).collect(), + extra_args: value.extra_args, + use_buildx: value.use_buildx, + image_registry: if value.docker_account.is_empty() { + ImageRegistry::None(NoData {}) + } else { + ImageRegistry::DockerHub(CloudRegistryConfig { + account: value.docker_account, + organization: value.docker_organization, + }) + }, + webhook_enabled: value.webhook_enabled, + } + } +} + fn default_branch() -> String { String::from("main") } diff --git a/bin/migrator/src/legacy/v1_6/deployment.rs b/bin/migrator/src/legacy/v1_6/deployment.rs index 0810ada6d..510155ffa 100644 --- a/bin/migrator/src/legacy/v1_6/deployment.rs +++ b/bin/migrator/src/legacy/v1_6/deployment.rs @@ -1,11 +1,33 @@ -use std::collections::HashMap; - +use monitor_client::entities::{ + build::{CloudRegistryConfig, ImageRegistry}, + deployment::{ + Conversion, DeploymentImage, RestartMode, TerminationSignal, + TerminationSignalLabel, + }, + NoData, +}; use serde::{Deserialize, Serialize}; -use super::{resource::Resource, EnvironmentVar, Version}; +use super::{resource::Resource, EnvironmentVar}; pub type Deployment = Resource; +impl From + for monitor_client::entities::deployment::Deployment +{ + fn from(value: Deployment) -> Self { + Self { + id: value.id, + name: value.name, + description: value.description, + updated_at: value.updated_at, + tags: value.tags, + info: value.info, + config: value.config.into(), + } + } +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct DeploymentConfig { /// The id of server the deployment is deployed on. @@ -36,7 +58,7 @@ pub struct DeploymentConfig { /// Labels attached to various termination signal options. /// Used to specify different shutdown functionality depending on the termination signal. - #[serde(default = "default_term_signal_labels")] + #[serde(default)] pub term_signal_labels: Vec, /// The default termination signal to use to stop the deployment. Defaults to SigTerm (default docker signal). @@ -88,12 +110,45 @@ pub struct DeploymentConfig { pub extra_args: Vec, } -fn default_send_alerts() -> bool { - true +impl From + for monitor_client::entities::deployment::DeploymentConfig +{ + fn from(value: DeploymentConfig) -> Self { + Self { + server_id: value.server_id, + send_alerts: value.send_alerts, + image: value.image, + image_registry: if value.docker_account.is_empty() { + ImageRegistry::None(NoData {}) + } else { + ImageRegistry::DockerHub(CloudRegistryConfig { + account: value.docker_account, + ..Default::default() + }) + }, + skip_secret_interp: value.skip_secret_interp, + redeploy_on_build: value.redeploy_on_build, + term_signal_labels: value.term_signal_labels, + termination_signal: value.termination_signal, + termination_timeout: value.termination_timeout, + ports: value.ports, + volumes: value.volumes, + environment: value + .environment + .into_iter() + .map(Into::into) + .collect(), + labels: value.labels.into_iter().map(Into::into).collect(), + network: value.network, + restart: value.restart, + command: value.command, + extra_args: value.extra_args, + } + } } -fn default_term_signal_labels() -> Vec { - vec![TerminationSignalLabel::default()] +fn default_send_alerts() -> bool { + true } fn default_termination_timeout() -> i32 { @@ -103,197 +158,3 @@ fn default_termination_timeout() -> i32 { fn default_network() -> String { String::from("host") } - -impl Default for DeploymentConfig { - fn default() -> Self { - Self { - server_id: Default::default(), - send_alerts: default_send_alerts(), - image: Default::default(), - docker_account: Default::default(), - skip_secret_interp: Default::default(), - redeploy_on_build: Default::default(), - term_signal_labels: default_term_signal_labels(), - termination_signal: Default::default(), - termination_timeout: default_termination_timeout(), - ports: Default::default(), - volumes: Default::default(), - environment: Default::default(), - labels: Default::default(), - network: default_network(), - restart: Default::default(), - command: Default::default(), - extra_args: Default::default(), - } - } -} - -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] -#[serde(tag = "type", content = "params")] -pub enum DeploymentImage { - /// Deploy any external image. - Image { - /// The docker image, can be from any registry that works with docker and that the host server can reach. - #[serde(default)] - image: String, - }, - - /// Deploy a monitor build. - Build { - /// The id of the build - #[serde(default, alias = "build")] - build_id: String, - /// Use a custom / older version of the image produced by the build. - /// if version is 0.0.0, this means `latest` image. - #[serde(default)] - version: Version, - }, -} - -impl Default for DeploymentImage { - fn default() -> Self { - Self::Image { - image: Default::default(), - } - } -} - -#[derive( - Serialize, Deserialize, Debug, Clone, Default, PartialEq, -)] -pub struct Conversion { - /// reference on the server. - pub local: String, - /// reference in the container. - pub container: String, -} - -/// A summary of a docker container on a server. -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct ContainerSummary { - /// Name of the container. - pub name: String, - /// Id of the container. - pub id: String, - /// The image the container is based on. - pub image: String, - /// The docker labels on the container. - pub labels: HashMap, - /// The state of the container, like `running` or `not_deployed` - pub state: DeploymentState, - /// The status string of the docker container. - pub status: Option, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct DockerContainerStats { - #[serde(alias = "Name")] - pub name: String, - #[serde(alias = "CPUPerc")] - pub cpu_perc: String, - #[serde(alias = "MemPerc")] - pub mem_perc: String, - #[serde(alias = "MemUsage")] - pub mem_usage: String, - #[serde(alias = "NetIO")] - pub net_io: String, - #[serde(alias = "BlockIO")] - pub block_io: String, - #[serde(alias = "PIDs")] - pub pids: String, -} - -/// Variants de/serialized from/to snake_case. -/// -/// Eg. -/// - NotDeployed -> not_deployed -/// - Restarting -> restarting -/// - Running -> running. -#[derive( - Serialize, - Deserialize, - Debug, - PartialEq, - Hash, - Eq, - Clone, - Copy, - Default, -)] -#[serde(rename_all = "snake_case")] -pub enum DeploymentState { - #[default] - Unknown, - NotDeployed, - Created, - Restarting, - Running, - Removing, - Paused, - Exited, - Dead, -} - -#[derive( - Serialize, - Deserialize, - Debug, - PartialEq, - Hash, - Eq, - Clone, - Copy, - Default, -)] -pub enum RestartMode { - #[default] - #[serde(rename = "no")] - NoRestart, - #[serde(rename = "on-failure")] - OnFailure, - #[serde(rename = "always")] - Always, - #[serde(rename = "unless-stopped")] - UnlessStopped, -} - -#[derive( - Serialize, - Deserialize, - Debug, - PartialEq, - Hash, - Eq, - Clone, - Copy, - Default, -)] -#[serde(rename_all = "UPPERCASE")] -pub enum TerminationSignal { - #[serde(alias = "1")] - SigHup, - #[serde(alias = "2")] - SigInt, - #[serde(alias = "3")] - SigQuit, - #[default] - #[serde(alias = "15")] - SigTerm, -} - -#[derive( - Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, -)] -pub struct TerminationSignalLabel { - pub signal: TerminationSignal, - pub label: String, -} - -#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)] -pub struct DeploymentActionState { - pub deploying: bool, - pub stopping: bool, - pub starting: bool, - pub removing: bool, - pub renaming: bool, -} diff --git a/bin/migrator/src/legacy/v1_6/mod.rs b/bin/migrator/src/legacy/v1_6/mod.rs index 6ea07a7b3..93bf833fa 100644 --- a/bin/migrator/src/legacy/v1_6/mod.rs +++ b/bin/migrator/src/legacy/v1_6/mod.rs @@ -1,8 +1,8 @@ use serde::{Deserialize, Serialize}; pub mod build; -pub mod resource; pub mod deployment; +pub mod resource; #[derive( Serialize, Deserialize, Debug, Clone, Default, PartialEq, @@ -29,4 +29,15 @@ pub struct SystemCommand { pub struct EnvironmentVar { pub variable: String, pub value: String, -} \ No newline at end of file +} + +impl From + for monitor_client::entities::EnvironmentVar +{ + fn from(value: EnvironmentVar) -> Self { + monitor_client::entities::EnvironmentVar { + variable: value.variable, + value: value.value, + } + } +}