Skip to main content

tuwunel_admin/server/
mod.rs

1mod admin_notice;
2mod backup_database;
3mod clear_caches;
4mod delete_backups;
5mod list_backups;
6mod list_features;
7mod memory_usage;
8mod regenerate_config;
9mod reload_config;
10mod reload_mods;
11#[cfg(unix)]
12mod restart;
13mod show_config;
14mod shutdown;
15mod uptime;
16mod verify_backup;
17
18use std::{path::PathBuf, sync::Arc};
19
20use clap::Subcommand;
21use tuwunel_core::{Result, implement};
22use tuwunel_database::Database;
23
24use crate::admin_command_dispatch;
25
26#[admin_command_dispatch]
27#[derive(Debug, Subcommand)]
28pub(super) enum ServerCommand {
29	/// - Time elapsed since startup
30	Uptime,
31
32	/// - Show configuration values
33	ShowConfig,
34
35	/// - Reload configuration values, layering an optional extra file over the
36	///   ones the server started with
37	ReloadConfig {
38		path: Option<PathBuf>,
39	},
40
41	#[command(about = "Regenerate configuration into a server-local file.")]
42	RegenerateConfig {
43		#[arg(help = "Absolute destination path on this server.")]
44		path: PathBuf,
45
46		#[arg(
47			long,
48			help = "Replace an existing destination after saving a backup."
49		)]
50		force: bool,
51
52		#[arg(
53			long,
54			help = "Materialize values currently supplied by the environment."
55		)]
56		include_env: bool,
57
58		#[arg(
59			long,
60			help = "Comment out keys that are not recognized by this binary."
61		)]
62		strip_unknown: bool,
63	},
64
65	/// - List the features built into the server
66	ListFeatures {
67		#[arg(short, long)]
68		available: bool,
69
70		#[arg(short, long)]
71		enabled: bool,
72
73		#[arg(short, long)]
74		comma: bool,
75	},
76
77	/// - Print database memory usage statistics
78	MemoryUsage,
79
80	/// - Clears all of Tuwunel's caches
81	ClearCaches,
82
83	/// - Performs an online backup of the database (only available for RocksDB
84	///   at the moment)
85	BackupDatabase,
86
87	/// - List database backups
88	ListBackups,
89
90	/// - Verify the files of a database backup are present with their expected
91	///   sizes
92	VerifyBackup {
93		/// Backup ID as listed by list-backups; the most recent backup when
94		/// omitted.
95		backup_id: Option<u32>,
96	},
97
98	/// - Delete database backups, retaining the most recent `keep`
99	DeleteBackups {
100		/// Number of most-recent backups to retain; zero deletes every backup.
101		keep: usize,
102	},
103
104	/// - Send a message to the admin room.
105	AdminNotice {
106		message: Vec<String>,
107	},
108
109	/// - Hot-reload the server
110	#[clap(alias = "reload")]
111	ReloadMods,
112
113	#[cfg(unix)]
114	/// - Restart the server
115	Restart {
116		#[arg(short, long)]
117		force: bool,
118	},
119
120	/// - Shutdown the server
121	Shutdown,
122}
123
124/// Run blocking database work off the async runtime.
125///
126/// Shared by the admin command groups (`server`, `query raw`); the closure
127/// receives the `Database` handle on a `spawn_blocking` worker.
128#[implement(crate::Context, params = "<'_>")]
129pub(crate) async fn blocking_db<F, T>(&self, f: F) -> Result<T>
130where
131	F: FnOnce(Arc<Database>) -> Result<T> + Send + 'static,
132	T: Send + 'static,
133{
134	let db = Arc::clone(&self.services.db);
135
136	self.services
137		.server
138		.runtime()
139		.spawn_blocking(move || f(db))
140		.await?
141}