impl procedure monrun

This commit is contained in:
mbecker20
2024-03-27 00:48:43 -07:00
parent bfcdf011c4
commit 1100e160de
20 changed files with 176 additions and 42 deletions

1
Cargo.lock generated
View File

@@ -2206,7 +2206,6 @@ name = "monrun"
version = "1.0.1"
dependencies = [
"anyhow",
"async-trait",
"clap",
"futures",
"monitor_client",

View File

@@ -19,5 +19,4 @@ toml.workspace = true
clap.workspace = true
futures.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
async-trait.workspace = true
tracing-subscriber.workspace = true

View File

@@ -2,7 +2,7 @@
name = "monitor-core-v1"
description = "the prod build for monitor"
tags = ["monitor", "prod", "api"]
config.builder_id = "mogh-builder"
config.builder = "mogh-builder"
config.repo = "mbecker20/monitor"
config.branch = "next"
config.build_path = "."
@@ -13,7 +13,7 @@ config.docker_account = "mbecker2020"
name = "monitor-frontend-v1"
description = "the prod frontend build for monitor"
tags = ["monitor", "prod", "frontend"]
config.builder_id = "mogh-builder"
config.builder = "mogh-builder"
config.repo = "mbecker20/monitor"
config.branch = "next"
config.build_path = "."
@@ -24,7 +24,7 @@ config.docker_account = "mbecker2020"
name = "monitor-core-v1-dev"
description = "the dev build for monitor"
tags = ["monitor", "dev", "api"]
config.builder_id = "mogh-builder"
config.builder = "mogh-builder"
config.repo = "mbecker20/monitor"
config.branch = "next"
config.build_path = "."
@@ -35,7 +35,7 @@ config.docker_account = "mbecker2020"
name = "monitor-frontend-v1-dev"
description = "the prod frontend build for monitor"
tags = ["monitor", "prod", "frontend"]
config.builder_id = "mogh-builder"
config.builder = "mogh-builder"
config.repo = "mbecker20/monitor"
config.branch = "next"
config.build_path = "."

View File

@@ -1,8 +1,8 @@
[[deployment]]
name = "monitor-core-v1-dev"
description = "v1 dev core api"
tags = ["mogh", "dev"]
config.server_id = "mogh-server"
tags = ["monitor", "v1", "dev"]
config.server = "mogh-server"
[[deployment.config.environment]]
variable = "TEST_1"

View File

@@ -0,0 +1,59 @@
[[procedure]]
name = "redeploy-v1"
description = "builds v1 core / frontend, and redeploys them"
config.type = "Sequence"
[[procedure.config.data]]
id = ""
enabled = true
# Deploys
[[procedure]]
name = "deploy-v1"
description = "deploys v1 core / frontend"
config.type = "Parallel"
[[procedure.config.data]]
id = "deploy-v1-core"
enabled = true
[[procedure.config.data]]
id = "deploy-v1-frontend"
enabled = true
[[procedure]]
name = "deploy-v1-core"
config.type = "Execution"
config.data.type = "Deploy"
config.data.params.deployment = "monitor-core-v1"
[[procedure]]
name = "deploy-v1-frontend"
config.type = "Execution"
config.data.type = "Deploy"
config.data.params.deployment = "monitor-frontend-v1"
# Builds
[[procedure]]
name = "build-v1"
description = "builds v1 core / frontend"
config.type = "Parallel"
[[procedure.config.data]]
id = "build-v1-core"
enabled = true
[[procedure.config.data]]
id = "build-v1-frontend"
enabled = true
[[procedure]]
name = "build-v1-core"
config.type = "Execution"
config.data.type = "RunBuild"
config.data.params.build = "monitor-core-v1"
[[procedure]]
name = "build-v1-frontend"
config.type = "Execution"
config.data.type = "RunBuild"
config.data.params.build = "monitor-frontend-v1"

View File

@@ -6,8 +6,8 @@ use monitor_client::{
entities::{
alerter::AlerterListItem, build::BuildListItem,
builder::BuilderListItem, deployment::DeploymentListItem,
repo::RepoListItem, resource::ResourceListItem,
server::ServerListItem,
procedure::ProcedureListItem, repo::RepoListItem,
resource::ResourceListItem, server::ServerListItem,
},
};
@@ -115,3 +115,19 @@ pub fn name_to_repo() -> &'static HashMap<String, RepoListItem> {
.collect()
})
}
pub fn name_to_procedure(
) -> &'static HashMap<String, ProcedureListItem> {
static NAME_TO_PROCEDURE: OnceLock<
HashMap<String, ProcedureListItem>,
> = OnceLock::new();
NAME_TO_PROCEDURE.get_or_init(|| {
futures::executor::block_on(
monitor_client().read(read::ListProcedures::default()),
)
.expect("failed to get procedures from monitor")
.into_iter()
.map(|procedure| (procedure.name.clone(), procedure))
.collect()
})
}

View File

@@ -1,6 +1,5 @@
use std::{collections::HashMap, path::Path};
use async_trait::async_trait;
use monitor_client::{
api::{
read::ListTags,
@@ -11,6 +10,7 @@ use monitor_client::{
build::Build,
builder::Builder,
deployment::Deployment,
procedure::Procedure,
repo::Repo,
resource::{Resource, ResourceListItem},
server::Server,
@@ -40,6 +40,8 @@ pub async fn run_sync(path: &Path) -> anyhow::Result<()> {
Alerter::get_updates(resources.alerters)?;
let (repo_updates, repo_creates) =
Repo::get_updates(resources.repos)?;
let (procedure_updates, procedure_creates) =
Procedure::get_updates(resources.procedures)?;
wait_for_enter("CONTINUE")?;
@@ -52,6 +54,7 @@ pub async fn run_sync(path: &Path) -> anyhow::Result<()> {
Deployment::run_updates(deployment_updates, deployment_creates)
.await;
Repo::run_updates(repo_updates, repo_creates).await;
Procedure::run_updates(procedure_updates, procedure_creates).await;
Ok(())
}
@@ -60,7 +63,6 @@ type ToUpdate<T> = Vec<(String, Resource<T>)>;
type ToCreate<T> = Vec<Resource<T>>;
type UpdatesResult<T> = (ToUpdate<T>, ToCreate<T>);
#[async_trait]
pub trait ResourceSync {
type PartialConfig: Clone + Send + 'static;
type ListItemInfo: 'static;

View File

@@ -4,7 +4,7 @@ use anyhow::{anyhow, Context};
use monitor_client::entities::{
alerter::PartialAlerterConfig, build::PartialBuildConfig,
builder::PartialBuilderConfig, deployment::PartialDeploymentConfig,
repo::PartialRepoConfig, resource::Resource,
procedure::Procedure, repo::PartialRepoConfig, resource::Resource,
server::PartialServerConfig,
};
use serde::Deserialize;
@@ -24,6 +24,8 @@ pub struct ResourceFile {
pub repos: Vec<Resource<PartialRepoConfig>>,
#[serde(default, rename = "alerter")]
pub alerters: Vec<Resource<PartialAlerterConfig>>,
#[serde(default, rename = "procedure")]
pub procedures: Vec<Procedure>,
}
pub fn read_resources(path: &Path) -> anyhow::Result<ResourceFile> {

View File

@@ -1,6 +1,5 @@
use std::collections::HashMap;
use async_trait::async_trait;
use monitor_client::{
api::write::{CreateAlerter, UpdateAlerter},
entities::{
@@ -14,7 +13,6 @@ use crate::{
maps::name_to_alerter, monitor_client, sync::ResourceSync,
};
#[async_trait]
impl ResourceSync for Alerter {
type PartialConfig = PartialAlerterConfig;
type ListItemInfo = AlerterListItemInfo;
@@ -34,9 +32,7 @@ impl ResourceSync for Alerter {
name_to_alerter()
}
async fn init_lookup_data() -> Self::ExtLookup {
()
}
async fn init_lookup_data() -> Self::ExtLookup {}
async fn create(
resource: Resource<Self::PartialConfig>,

View File

@@ -1,6 +1,5 @@
use std::collections::HashMap;
use async_trait::async_trait;
use monitor_client::{
api::{
read::ListBuilders,
@@ -17,7 +16,6 @@ use crate::{
maps::name_to_build, monitor_client, sync::ResourceSync,
};
#[async_trait]
impl ResourceSync for Build {
type PartialConfig = PartialBuildConfig;
type ListItemInfo = BuildListItemInfo;

View File

@@ -1,6 +1,5 @@
use std::collections::HashMap;
use async_trait::async_trait;
use monitor_client::{
api::write::{CreateBuilder, UpdateBuilder},
entities::{
@@ -14,7 +13,6 @@ use crate::{
maps::name_to_builder, monitor_client, sync::ResourceSync,
};
#[async_trait]
impl ResourceSync for Builder {
type PartialConfig = PartialBuilderConfig;
type ListItemInfo = BuilderListItemInfo;
@@ -34,9 +32,7 @@ impl ResourceSync for Builder {
name_to_builder()
}
async fn init_lookup_data() -> Self::ExtLookup {
()
}
async fn init_lookup_data() -> Self::ExtLookup {}
async fn create(
resource: Resource<Self::PartialConfig>,

View File

@@ -1,6 +1,5 @@
use std::collections::HashMap;
use async_trait::async_trait;
use monitor_client::{
api::{
read::{ListBuilds, ListServers},
@@ -25,7 +24,6 @@ pub struct DeploymentExtLookup {
pub builds: HashMap<String, String>,
}
#[async_trait]
impl ResourceSync for Deployment {
type PartialConfig = PartialDeploymentConfig;
type ListItemInfo = DeploymentListItemInfo;

View File

@@ -0,0 +1,63 @@
use std::collections::HashMap;
use monitor_client::{
api::write::{CreateProcedure, UpdateProcedure},
entities::{
procedure::{Procedure, ProcedureConfig, ProcedureListItemInfo},
resource::{Resource, ResourceListItem},
update::ResourceTarget,
},
};
use crate::{
maps::name_to_procedure, monitor_client, sync::ResourceSync,
};
impl ResourceSync for Procedure {
type PartialConfig = ProcedureConfig;
type ListItemInfo = ProcedureListItemInfo;
type ExtLookup = ();
fn display() -> &'static str {
"procedure"
}
fn resource_target(id: String) -> ResourceTarget {
ResourceTarget::Procedure(id)
}
fn name_to_resource(
) -> &'static HashMap<String, ResourceListItem<Self::ListItemInfo>>
{
name_to_procedure()
}
async fn init_lookup_data() -> Self::ExtLookup {}
async fn create(
resource: Resource<Self::PartialConfig>,
_: &Self::ExtLookup,
) -> anyhow::Result<String> {
monitor_client()
.write(CreateProcedure {
name: resource.name,
config: resource.config,
})
.await
.map(|p| p.id)
}
async fn update(
id: String,
resource: Resource<Self::PartialConfig>,
_: &Self::ExtLookup,
) -> anyhow::Result<()> {
monitor_client()
.write(UpdateProcedure {
id,
config: resource.config,
})
.await?;
Ok(())
}
}

View File

@@ -1,6 +1,5 @@
use std::collections::HashMap;
use async_trait::async_trait;
use monitor_client::{
api::write::{CreateRepo, UpdateRepo},
entities::{
@@ -12,7 +11,6 @@ use monitor_client::{
use crate::{maps::name_to_repo, monitor_client, sync::ResourceSync};
#[async_trait]
impl ResourceSync for Repo {
type PartialConfig = PartialRepoConfig;
type ListItemInfo = RepoInfo;
@@ -32,9 +30,7 @@ impl ResourceSync for Repo {
name_to_repo()
}
async fn init_lookup_data() -> Self::ExtLookup {
()
}
async fn init_lookup_data() -> Self::ExtLookup {}
async fn create(
resource: Resource<Self::PartialConfig>,

View File

@@ -1,6 +1,5 @@
use std::collections::HashMap;
use async_trait::async_trait;
use monitor_client::{
api::write::{CreateServer, UpdateServer},
entities::{
@@ -14,7 +13,6 @@ use crate::{
maps::name_to_server, monitor_client, sync::ResourceSync,
};
#[async_trait]
impl ResourceSync for Server {
type ListItemInfo = ServerListItemInfo;
type PartialConfig = PartialServerConfig;
@@ -34,9 +32,7 @@ impl ResourceSync for Server {
name_to_server()
}
async fn init_lookup_data() -> Self::ExtLookup {
()
}
async fn init_lookup_data() -> Self::ExtLookup {}
async fn create(
resource: Resource<Self::PartialConfig>,

View File

@@ -16,6 +16,7 @@ use super::MonitorExecuteRequest;
#[empty_traits(MonitorExecuteRequest)]
#[response(Update)]
pub struct RunBuild {
#[serde(alias = "build")]
pub build_id: String,
}

View File

@@ -18,6 +18,7 @@ use super::MonitorExecuteRequest;
#[empty_traits(MonitorExecuteRequest)]
#[response(Update)]
pub struct Deploy {
#[serde(alias = "deployment")]
pub deployment_id: String,
pub stop_signal: Option<TerminationSignal>,
pub stop_time: Option<i32>,

View File

@@ -40,7 +40,7 @@ pub type _PartialBuildConfig = PartialBuildConfig;
#[skip_serializing_none]
#[partial_from]
pub struct BuildConfig {
#[serde(default)]
#[serde(default, alias = "builder")]
#[builder(default)]
pub builder_id: String,

View File

@@ -38,7 +38,7 @@ pub type _PartialDeploymentConfig = PartialDeploymentConfig;
#[skip_serializing_none]
#[partial_from]
pub struct DeploymentConfig {
#[serde(default)]
#[serde(default, alias = "server")]
#[builder(default)]
pub server_id: String,
@@ -140,8 +140,16 @@ fn default_network() -> String {
)]
#[serde(tag = "type", content = "params")]
pub enum DeploymentImage {
Image { image: String },
Build { build_id: String, version: Version },
Image {
#[serde(default)]
image: String,
},
Build {
#[serde(default, alias = "build")]
build_id: String,
#[serde(default)]
version: Version,
},
}
impl Default for DeploymentImage {

View File

@@ -33,8 +33,12 @@ pub type _PartialRepoConfig = PartialRepoConfig;
#[skip_serializing_none]
#[partial_from]
pub struct RepoConfig {
#[serde(default, alias = "server")]
#[builder(default)]
pub server_id: String,
#[serde(default)]
#[builder(default)]
pub repo: String,
#[serde(default = "default_branch")]