Skip to main content

tuwunel_admin/server/
mod.rs

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