From 5a2a1a3d98271c462b5be4ea42967ba4a86d25f0 Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Wed, 1 Mar 2023 08:13:07 +0000 Subject: [PATCH] 0.2.4: add description and update description --- Cargo.lock | 30 +++++------ core/Cargo.toml | 2 +- core/src/api/mod.rs | 87 ++++++++++++++++++++++++++++--- core/src/state.rs | 1 - frontend/src/types.ts | 6 +++ frontend/src/util/client.ts | 9 +++- frontend/src/util/client_types.ts | 7 ++- lib/db_client/Cargo.toml | 2 +- lib/helpers/Cargo.toml | 2 +- lib/monitor_client/Cargo.toml | 4 +- lib/monitor_client/src/lib.rs | 14 +++++ lib/periphery_client/Cargo.toml | 2 +- lib/types/Cargo.toml | 2 +- lib/types/src/action.rs | 5 ++ lib/types/src/build.rs | 5 ++ lib/types/src/deployment.rs | 5 ++ lib/types/src/group.rs | 5 ++ lib/types/src/procedure.rs | 5 ++ lib/types/src/server.rs | 8 ++- periphery/Cargo.toml | 2 +- 20 files changed, 169 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b637d0664..1b6a5aee1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -734,7 +734,7 @@ dependencies = [ [[package]] name = "core" -version = "0.2.2" +version = "0.2.4" dependencies = [ "anyhow", "async_timing_util", @@ -753,7 +753,7 @@ dependencies = [ "hmac", "jwt", "monitor_helpers", - "monitor_types 0.2.2", + "monitor_types 0.2.4", "mungos", "periphery_client", "serde", @@ -987,10 +987,10 @@ checksum = "23d8666cb01533c39dde32bcbab8e227b4ed6679b2c925eba05feabea39508fb" [[package]] name = "db_client" -version = "0.2.2" +version = "0.2.4" dependencies = [ "anyhow", - "monitor_types 0.2.2", + "monitor_types 0.2.4", "mungos", ] @@ -1853,12 +1853,12 @@ dependencies = [ [[package]] name = "monitor_client" -version = "0.2.2" +version = "0.2.4" dependencies = [ "anyhow", "envy", "futures-util", - "monitor_types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "monitor_types 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest", "serde", "serde_derive", @@ -1870,11 +1870,11 @@ dependencies = [ [[package]] name = "monitor_helpers" -version = "0.2.2" +version = "0.2.4" dependencies = [ "anyhow", "axum", - "monitor_types 0.2.2", + "monitor_types 0.2.4", "rand", "serde", "serde_json", @@ -1883,7 +1883,7 @@ dependencies = [ [[package]] name = "monitor_periphery" -version = "0.2.2" +version = "0.2.4" dependencies = [ "anyhow", "async_timing_util", @@ -1895,7 +1895,7 @@ dependencies = [ "envy", "futures", "monitor_helpers", - "monitor_types 0.2.2", + "monitor_types 0.2.4", "run_command", "serde", "serde_derive", @@ -1908,7 +1908,7 @@ dependencies = [ [[package]] name = "monitor_types" -version = "0.2.2" +version = "0.2.4" dependencies = [ "anyhow", "bollard", @@ -1925,9 +1925,9 @@ dependencies = [ [[package]] name = "monitor_types" -version = "0.2.2" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72e8b62148932ce7c499f16a7f0053dd9bf61ae97f66b62c0a3161203d8ca508" +checksum = "891189d5f49168915fd4c81f4e59d8fdd937008a59431831f28de3d5f8cf2af2" dependencies = [ "anyhow", "bollard", @@ -2183,11 +2183,11 @@ checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" [[package]] name = "periphery_client" -version = "0.2.2" +version = "0.2.4" dependencies = [ "anyhow", "futures-util", - "monitor_types 0.2.2", + "monitor_types 0.2.4", "reqwest", "serde", "serde_json", diff --git a/core/Cargo.toml b/core/Cargo.toml index 1229b778c..ca097c59b 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "core" -version = "0.2.2" +version = "0.2.4" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/core/src/api/mod.rs b/core/src/api/mod.rs index 75f069f30..86c3ba9bf 100644 --- a/core/src/api/mod.rs +++ b/core/src/api/mod.rs @@ -1,20 +1,21 @@ -use anyhow::Context; +use anyhow::{anyhow, Context}; use axum::{ body::Body, extract::Path, http::{Request, StatusCode}, middleware, - routing::get, + routing::{get, post}, Extension, Json, Router, }; use futures_util::Future; use helpers::handle_anyhow_error; -use mungos::Deserialize; -use types::User; +use mungos::{doc, Deserialize}; +use types::{PermissionLevel, UpdateTarget, User}; +use typeshare::typeshare; use crate::{ - auth::{auth_request, JwtExtension, RequestUserExtension}, - state::StateExtension, + auth::{auth_request, JwtExtension, RequestUser, RequestUserExtension}, + state::{State, StateExtension}, }; pub mod build; @@ -27,6 +28,13 @@ pub mod secret; pub mod server; pub mod update; +#[typeshare] +#[derive(Deserialize)] +struct UpdateDescriptionBody { + target: UpdateTarget, + description: String, +} + pub fn router() -> Router { Router::new() .route( @@ -56,6 +64,19 @@ pub fn router() -> Router { .to_string() }), ) + .route( + "/update_description", + post( + |state: StateExtension, + user: RequestUserExtension, + body: Json| async move { + state + .update_description(&body.target, &body.description, &user) + .await + .map_err(handle_anyhow_error) + }, + ), + ) .route("/users", get(get_users)) .nest("/build", build::router()) .nest("/deployment", deployment::router()) @@ -128,3 +149,57 @@ where .map_err(handle_anyhow_error)?; Ok(res) } + +impl State { + pub async fn update_description( + &self, + target: &UpdateTarget, + description: &str, + user: &RequestUser, + ) -> anyhow::Result<()> { + match target { + UpdateTarget::Build(id) => { + self.get_build_check_permissions(id, user, PermissionLevel::Update) + .await?; + self.db + .builds + .update_one::<()>(id, mungos::Update::Set(doc! { "description": description })) + .await?; + } + UpdateTarget::Deployment(id) => { + self.get_deployment_check_permissions(id, user, PermissionLevel::Update) + .await?; + self.db + .builds + .update_one::<()>(id, mungos::Update::Set(doc! { "description": description })) + .await?; + } + UpdateTarget::Server(id) => { + self.get_server_check_permissions(id, user, PermissionLevel::Update) + .await?; + self.db + .builds + .update_one::<()>(id, mungos::Update::Set(doc! { "description": description })) + .await?; + } + UpdateTarget::Group(id) => { + self.get_group_check_permissions(id, user, PermissionLevel::Update) + .await?; + self.db + .builds + .update_one::<()>(id, mungos::Update::Set(doc! { "description": description })) + .await?; + } + UpdateTarget::Procedure(id) => { + self.get_procedure_check_permissions(id, user, PermissionLevel::Update) + .await?; + self.db + .builds + .update_one::<()>(id, mungos::Update::Set(doc! { "description": description })) + .await?; + } + _ => return Err(anyhow!("invalid target: {target:?}")), + } + Ok(()) + } +} diff --git a/core/src/state.rs b/core/src/state.rs index e344511ed..6369e7673 100644 --- a/core/src/state.rs +++ b/core/src/state.rs @@ -78,7 +78,6 @@ impl State { } let futures = servers.unwrap().into_iter().map(|server| async move { let _ = self.periphery.image_prune(&server).await; - let _ = self.periphery.container_prune(&server).await; }); join_all(futures).await; } diff --git a/frontend/src/types.ts b/frontend/src/types.ts index 28dc1d60c..b73eb7440 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -9,6 +9,7 @@ export type PermissionsMap = Record; export interface Action { _id?: string; name: string; + description?: string; path: string; command: string; server_ids?: string[]; @@ -22,6 +23,7 @@ export interface Action { export interface Build { _id?: string; name: string; + description?: string; permissions?: PermissionsMap; server_id?: string; aws_config?: AwsBuilderBuildConfig; @@ -94,6 +96,7 @@ export interface AmiAccounts { export interface Deployment { _id?: string; name: string; + description?: string; server_id: string; permissions?: PermissionsMap; docker_run_args: DockerRunArgs; @@ -164,6 +167,7 @@ export interface DockerContainerStats { export interface Group { _id?: string; name: string; + description?: string; permissions?: PermissionsMap; builds: string[]; deployments: string[]; @@ -192,6 +196,7 @@ export interface UserCredentials { export interface Procedure { _id?: string; name: string; + description?: string; stages?: ProcedureStage[]; webhook_branches?: string[]; permissions?: PermissionsMap; @@ -207,6 +212,7 @@ export interface ProcedureStage { export interface Server { _id?: string; name: string; + description?: string; address: string; permissions?: PermissionsMap; enabled: boolean; diff --git a/frontend/src/util/client.ts b/frontend/src/util/client.ts index 6a6911807..7c7de1132 100644 --- a/frontend/src/util/client.ts +++ b/frontend/src/util/client.ts @@ -43,6 +43,7 @@ import { ModifyUserCreateServerBody, ModifyUserEnabledBody, PermissionsUpdateBody, + UpdateDescriptionBody, } from "./client_types"; import { generateQuery, QueryObject } from "./helpers"; @@ -111,7 +112,7 @@ export class Client { } } - get_username(user_id: string): Promise { + get_username(user_id: string): Promise { return this.get(`/api/username/${user_id}`); } @@ -124,7 +125,11 @@ export class Client { } get_github_webhook_base_url(): Promise { - return this.get("/api/github_webhook_base_url") + return this.get("/api/github_webhook_base_url"); + } + + update_description(body: UpdateDescriptionBody): Promise { + return this.post("/api/update_description", body); } // deployment diff --git a/frontend/src/util/client_types.ts b/frontend/src/util/client_types.ts index 5dbcf04d6..518fe3fda 100644 --- a/frontend/src/util/client_types.ts +++ b/frontend/src/util/client_types.ts @@ -2,7 +2,7 @@ Generated by typeshare 1.0.0 */ -import { PermissionLevel, PermissionsTarget } from "../types"; +import { PermissionLevel, PermissionsTarget, UpdateTarget } from "../types"; export interface CreateBuildBody { name: string; @@ -37,6 +37,11 @@ export interface CreateGroupBody { name: string; } +export interface UpdateDescriptionBody { + target: UpdateTarget; + description: string; +} + export interface PermissionsUpdateBody { user_id: string; permission: PermissionLevel; diff --git a/lib/db_client/Cargo.toml b/lib/db_client/Cargo.toml index be201fd34..bc19a7a84 100644 --- a/lib/db_client/Cargo.toml +++ b/lib/db_client/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "db_client" -version = "0.2.2" +version = "0.2.4" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/lib/helpers/Cargo.toml b/lib/helpers/Cargo.toml index d17661d6b..b300b8914 100644 --- a/lib/helpers/Cargo.toml +++ b/lib/helpers/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "monitor_helpers" -version = "0.2.2" +version = "0.2.4" edition = "2021" authors = ["MoghTech"] description = "helpers used as dependency for mogh tech monitor" diff --git a/lib/monitor_client/Cargo.toml b/lib/monitor_client/Cargo.toml index c5b1e5779..2615fe9bc 100644 --- a/lib/monitor_client/Cargo.toml +++ b/lib/monitor_client/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "monitor_client" -version = "0.2.2" +version = "0.2.4" edition = "2021" authors = ["MoghTech"] description = "a client to interact with the monitor system" @@ -9,7 +9,7 @@ license = "GPL-3.0-or-later" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -monitor_types = "0.2.2" +monitor_types = "0.2.4" # monitor_types = { path = "../types" } reqwest = { version = "0.11", features = ["json"] } tokio-tungstenite = { version = "0.18", features=["native-tls"] } diff --git a/lib/monitor_client/src/lib.rs b/lib/monitor_client/src/lib.rs index 04b2e6daf..4f3e8bbd3 100644 --- a/lib/monitor_client/src/lib.rs +++ b/lib/monitor_client/src/lib.rs @@ -10,6 +10,7 @@ pub use futures_util; pub use tokio_tungstenite; pub use monitor_types as types; +use types::UpdateTarget; mod build; mod deployment; @@ -146,6 +147,19 @@ impl MonitorClient { .context("failed at call to get_github_webhook_base_url") } + pub async fn update_description( + &self, + target: UpdateTarget, + description: &str, + ) -> anyhow::Result<()> { + self.post( + "/api/update_description", + json!({ "target": target, "description": description }), + ) + .await + .context("failed at call to update_description") + } + async fn get( &self, endpoint: &str, diff --git a/lib/periphery_client/Cargo.toml b/lib/periphery_client/Cargo.toml index 7e17cffe4..cf1ddbfd9 100644 --- a/lib/periphery_client/Cargo.toml +++ b/lib/periphery_client/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "periphery_client" -version = "0.2.2" +version = "0.2.4" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/lib/types/Cargo.toml b/lib/types/Cargo.toml index 75808be8d..2048a804b 100644 --- a/lib/types/Cargo.toml +++ b/lib/types/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "monitor_types" -version = "0.2.2" +version = "0.2.4" edition = "2021" authors = ["MoghTech"] description = "types for the mogh tech monitor" diff --git a/lib/types/src/action.rs b/lib/types/src/action.rs index b51936500..af753d819 100644 --- a/lib/types/src/action.rs +++ b/lib/types/src/action.rs @@ -23,6 +23,11 @@ pub struct Action { #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] pub name: String, + #[serde(default)] + #[builder(default)] + #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] + pub description: String, + #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] pub path: String, diff --git a/lib/types/src/build.rs b/lib/types/src/build.rs index d37c3af9d..7a8b94b5a 100644 --- a/lib/types/src/build.rs +++ b/lib/types/src/build.rs @@ -27,6 +27,11 @@ pub struct Build { #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] pub name: String, + #[serde(default)] + #[builder(default)] + #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] + pub description: String, + #[serde(default)] #[diff(attr(#[serde(skip_serializing)]))] #[builder(setter(skip))] diff --git a/lib/types/src/deployment.rs b/lib/types/src/deployment.rs index fb521b6fb..ed130138e 100644 --- a/lib/types/src/deployment.rs +++ b/lib/types/src/deployment.rs @@ -24,6 +24,11 @@ pub struct Deployment { #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] pub name: String, // must be formatted to be compat with docker + #[serde(default)] + #[builder(default)] + #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] + pub description: String, + #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] pub server_id: String, diff --git a/lib/types/src/group.rs b/lib/types/src/group.rs index b46f8783c..33d8b9739 100644 --- a/lib/types/src/group.rs +++ b/lib/types/src/group.rs @@ -23,6 +23,11 @@ pub struct Group { #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] pub name: String, + #[serde(default)] + #[builder(default)] + #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] + pub description: String, + #[serde(default)] #[diff(attr(#[serde(skip_serializing)]))] #[builder(setter(skip))] diff --git a/lib/types/src/procedure.rs b/lib/types/src/procedure.rs index d851005e5..c2f3c1cfe 100644 --- a/lib/types/src/procedure.rs +++ b/lib/types/src/procedure.rs @@ -23,6 +23,11 @@ pub struct Procedure { #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] pub name: String, + #[serde(default)] + #[builder(default)] + #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] + pub description: String, + #[serde(default)] #[builder(default)] #[diff(attr(#[serde(skip_serializing_if = "vec_diff_no_change")]))] diff --git a/lib/types/src/server.rs b/lib/types/src/server.rs index 08e7b773c..5466965cf 100644 --- a/lib/types/src/server.rs +++ b/lib/types/src/server.rs @@ -26,6 +26,11 @@ pub struct Server { #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] pub name: String, + #[serde(default)] + #[builder(default)] + #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] + pub description: String, + #[diff(attr(#[serde(skip_serializing_if = "Option::is_none")]))] pub address: String, @@ -94,7 +99,7 @@ impl Default for Server { address: Default::default(), permissions: Default::default(), enabled: true, - auto_prune: false, + auto_prune: true, to_notify: Default::default(), cpu_alert: default_cpu_alert(), mem_alert: default_mem_alert(), @@ -102,6 +107,7 @@ impl Default for Server { stats_interval: Default::default(), region: Default::default(), instance_id: Default::default(), + description: Default::default(), created_at: Default::default(), updated_at: Default::default(), } diff --git a/periphery/Cargo.toml b/periphery/Cargo.toml index 022f712ab..76a85b5f9 100644 --- a/periphery/Cargo.toml +++ b/periphery/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "monitor_periphery" -version = "0.2.2" +version = "0.2.4" edition = "2021" authors = ["MoghTech"] description = "monitor periphery binary | run monitor periphery as system daemon"