From 714a7e63a69389a4667b7a267df6b6ecd9109c14 Mon Sep 17 00:00:00 2001 From: Yuichi <913637919@qq.com> Date: Sat, 12 Oct 2024 06:12:25 +0000 Subject: [PATCH] =?UTF-8?q?node=E8=A1=A8=E6=96=B0=E5=A2=9E=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=AD=97=E6=AE=B5=20=E6=B7=BB=E5=8A=A0=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E5=87=BD=E6=95=B0=EF=BC=8Cnanocl=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E6=9C=9F=E9=97=B4=E6=AF=8F=E9=9A=94=E5=8D=81=E7=A7=92=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E7=9B=91=E6=8E=A7=E7=B3=BB=E7=BB=9F=E5=8F=AF=E7=94=A8?= =?UTF-8?q?=E6=8C=87=E6=A0=87=EF=BC=8C=E5=B9=B6=E6=9B=B4=E6=96=B0node?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/dataSources.xml | 12 --- Cargo.lock | 45 +++++++++++ bin/nanocld/.env | 3 - bin/nanocld/Cargo.toml | 1 + .../migrations/2022-08-04-214925_nodes/up.sql | 6 ++ bin/nanocld/src/main.rs | 80 ++++++++++++++++++- bin/nanocld/src/models/node.rs | 6 ++ bin/nanocld/src/repositories/node.rs | 3 + bin/nanocld/src/schema.rs | 3 + diesel.toml | 9 +++ migrations/.keep | 0 src/schema.rs | 2 + 12 files changed, 154 insertions(+), 16 deletions(-) delete mode 100644 .idea/dataSources.xml delete mode 100644 bin/nanocld/.env create mode 100644 diesel.toml create mode 100644 migrations/.keep create mode 100644 src/schema.rs diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml deleted file mode 100644 index 76024611..00000000 --- a/.idea/dataSources.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - sqlite.xerial - true - org.sqlite.JDBC - jdbc:sqlite:$PROJECT_DIR$/database.db - $ProjectFileDir$ - - - \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index bd1094b3..38055e32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2355,6 +2355,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "serde_yaml", + "sysinfo", "thiserror", "tokio", "tokio-util", @@ -2505,6 +2506,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + [[package]] name = "ntex" version = "2.3.0" @@ -3391,6 +3401,26 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redox_syscall" version = "0.5.3" @@ -3962,6 +3992,21 @@ dependencies = [ "futures-core", ] +[[package]] +name = "sysinfo" +version = "0.27.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a902e9050fca0a5d6877550b769abd2bd1ce8c04634b941dbe2809735e1a1e33" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "libc", + "ntapi", + "once_cell", + "rayon", + "winapi", +] + [[package]] name = "tabled" version = "0.16.0" diff --git a/bin/nanocld/.env b/bin/nanocld/.env deleted file mode 100644 index 4519b5ee..00000000 --- a/bin/nanocld/.env +++ /dev/null @@ -1,3 +0,0 @@ -DATABASE_URL=postgres://root:root@store.nanocl.internal:26258/defaultdb? - -#sslmode=verify-full&sslcert=$HOME/.nanocl_dev/state/store/certs/client.root.crt&sslkey=$HOME/.nanocl_dev/state/store/certs/client.root.key&sslrootcert=$HOME/.nanocl_dev/state/store/certs/ca.crt diff --git a/bin/nanocld/Cargo.toml b/bin/nanocld/Cargo.toml index ad8605c7..8e89e1e2 100644 --- a/bin/nanocld/Cargo.toml +++ b/bin/nanocld/Cargo.toml @@ -38,6 +38,7 @@ nanocl_stubs = { version = "0.15", features = ["clap"] } serde_yaml = "0.9" [dependencies] +sysinfo = "0.27" nanocl_error = { version = "0.4", features = [ "io", "http", diff --git a/bin/nanocld/migrations/2022-08-04-214925_nodes/up.sql b/bin/nanocld/migrations/2022-08-04-214925_nodes/up.sql index c683788b..fb0f0aa4 100644 --- a/bin/nanocld/migrations/2022-08-04-214925_nodes/up.sql +++ b/bin/nanocld/migrations/2022-08-04-214925_nodes/up.sql @@ -1,8 +1,11 @@ CREATE TABLE IF NOT EXISTS "nodes" ( "name" VARCHAR NOT NULL UNIQUE PRIMARY KEY, + "role" VARCHAR NOT NULL, "created_at" TEXT NOT NULL DEFAULT (datetime('now')), -- 使用 TEXT 替代 TIMESTAMPTZ + "updated_at" TEXT NOT NULL DEFAULT (datetime('now')), -- 使用 TEXT 替代 TIMESTAMPTZ "ip_address" TEXT NOT NULL UNIQUE, -- SQLite 不支持 INET 类型,因此改为 TEXT "endpoint" VARCHAR NOT NULL, + "master_endpoint" VARCHAR NOT NULL, "version" VARCHAR NOT NULL, "metadata" TEXT -- 使用 TEXT 替代 JSONB ); @@ -17,9 +20,12 @@ CREATE TABLE IF NOT EXISTS "node_group_links" ( ); CREATE INDEX "nodes_name_idx" ON "nodes" ("name"); +CREATE INDEX "nodes_role_idx" ON "nodes" ("role"); CREATE INDEX "nodes_created_at_idx" ON "nodes" ("created_at"); +CREATE INDEX "nodes_updated_at_idx" ON "nodes" ("updated_at"); CREATE INDEX "nodes_ip_address_idx" ON "nodes" ("ip_address"); CREATE INDEX "nodes_endpoint_idx" ON "nodes" ("endpoint"); +CREATE INDEX "nodes_master_endpoint_idx" ON "nodes" ("master_endpoint"); CREATE INDEX "nodes_version_idx" ON "nodes" ("version"); -- 删除不兼容的 GIN 索引,改为普通 TEXT 索引 diff --git a/bin/nanocld/src/main.rs b/bin/nanocld/src/main.rs index d8e198bf..f229c4bb 100644 --- a/bin/nanocld/src/main.rs +++ b/bin/nanocld/src/main.rs @@ -1,8 +1,18 @@ use clap::Parser; - +use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; +use serde_json::json; use nanocl_error::io::FromIo; use nanocl_utils::logger; +use crate::{ + models::{SystemState}, +}; + +use sysinfo::{System, SystemExt, CpuExt}; +use tokio::time::{sleep, Duration}; +use tokio::sync::watch; +use tokio::task; + mod cli; mod config; mod models; @@ -48,6 +58,11 @@ async fn main() -> std::io::Result<()> { } Ok(daemon_state) => daemon_state, }; + + // 启动系统资源监控任务 + let (shutdown_tx, shutdown_rx) = watch::channel(false); + start_monitoring_task(shutdown_rx, daemon_state.clone()).await; + // Start http server match utils::server::gen(daemon_state).await { Err(err) => { @@ -61,6 +76,69 @@ async fn main() -> std::io::Result<()> { } } } + + // 停止监控任务 + shutdown_tx.send(true).unwrap(); + + log::info!("main: shutdown"); Ok(()) } + +// 启动监控任务函数 +async fn start_monitoring_task(mut shutdown_rx: watch::Receiver, daemon_state: SystemState) { + let db_pool = daemon_state.inner.pool.clone(); + let hostname = daemon_state.inner.config.hostname.clone(); + + tokio::spawn(async move { + loop { + // 检查是否需要停止监控任务 + if *shutdown_rx.borrow() { + break; + } + + // 获取系统信息 + let mut system = System::new_all(); + system.refresh_all(); + + let cpu_core_count = system.cpus().len(); + let cpu_usage = system.global_cpu_info().cpu_usage(); + let total_memory = system.total_memory() / 1024; + let available_memory = system.available_memory() / 1024; + + // 创建JSON格式的元数据 + let meta_data = json!({ + "cpu_core_count": cpu_core_count, + "cpu_usage": cpu_usage, + "total_memory_mb": total_memory, + "available_memory_mb": available_memory, + }); + + // 使用Diesel将数据写入数据库 + let update_result = { + let db_pool = db_pool.clone(); + let hostname = hostname.clone(); + let meta_data = meta_data.to_string(); + task::spawn_blocking(move || { + use crate::schema::nodes::dsl::*; + let mut conn = utils::store::get_pool_conn(&db_pool).unwrap(); + diesel::update(nodes.find(hostname)) + .set(( + metadata.eq(meta_data), + updated_at.eq(chrono::Utc::now().naive_utc().format("%Y-%m-%d %H:%M:%S").to_string()), + )) + .execute(&mut conn) + }) + .await + }; + + // 检查更新结果是否成功 + if let Err(e) = update_result { + eprintln!("Failed to update database: {}", e); + } + + // 每十秒钟更新一次数据 + sleep(Duration::from_secs(10)).await; + } + }); +} \ No newline at end of file diff --git a/bin/nanocld/src/models/node.rs b/bin/nanocld/src/models/node.rs index 30cdfada..b8fca9d4 100644 --- a/bin/nanocld/src/models/node.rs +++ b/bin/nanocld/src/models/node.rs @@ -14,12 +14,18 @@ use crate::schema::nodes; pub struct NodeDb { /// The name of the node pub name: String, + /// The role of the node + pub role: String, /// The created at date pub created_at: String, + /// The updated date + pub updated_at: String, /// The ip address of the node pub ip_address: String, /// Endpoint to connect to the node pub endpoint: String, + /// The endpoint of the master node + pub master_endpoint: String, /// Version of the node pub version: String, /// User defined metadata diff --git a/bin/nanocld/src/repositories/node.rs b/bin/nanocld/src/repositories/node.rs index c249f4d0..f987e7cd 100644 --- a/bin/nanocld/src/repositories/node.rs +++ b/bin/nanocld/src/repositories/node.rs @@ -93,9 +93,12 @@ impl NodeDb { let ip_address = ipnet::IpNet::from(ip_address).to_string(); let node = NodeDb { name: state.inner.config.hostname.clone(), + role: "master".parse().unwrap(), ip_address, endpoint: state.inner.config.advertise_addr.clone(), + master_endpoint: state.inner.config.advertise_addr.clone(), created_at: chrono::Utc::now().naive_utc().format("%Y-%m-%d %H:%M:%S").to_string(), + updated_at: chrono::Utc::now().naive_utc().format("%Y-%m-%d %H:%M:%S").to_string(), version: vars::VERSION.to_owned(), metadata: None, }; diff --git a/bin/nanocld/src/schema.rs b/bin/nanocld/src/schema.rs index 6b07e1e4..14354dc0 100644 --- a/bin/nanocld/src/schema.rs +++ b/bin/nanocld/src/schema.rs @@ -76,9 +76,12 @@ diesel::table! { diesel::table! { nodes (name) { name -> Text, + role -> Text, created_at -> Text, + updated_at -> Text, ip_address -> Text, endpoint -> Text, + master_endpoint -> Text, version -> Text, metadata -> Nullable, } diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 00000000..f438be4f --- /dev/null +++ b/diesel.toml @@ -0,0 +1,9 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" +custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] + +[migrations_directory] +dir = "/home/yuichi/fleet-core/migrations" diff --git a/migrations/.keep b/migrations/.keep new file mode 100644 index 00000000..e69de29b diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 00000000..4dc25e28 --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,2 @@ +// @generated automatically by Diesel CLI. + -- Gitee