tuwunel_service/admin/
execute.rs1use tokio::task::yield_now;
2use tuwunel_core::{Err, Result, debug, debug_info, error, implement, info};
3#[cfg(feature = "console")]
4use tuwunel_core::{log::is_terminal_mode, warn};
5
6use super::CommandOutput;
7
8pub(super) const SIGNAL: &str = "SIGUSR2";
9
10#[implement(super::Service)]
13#[cfg_attr(not(feature = "console"), expect(clippy::unused_async))]
14pub(super) async fn console_auto_start(&self) {
15 #[cfg(feature = "console")]
16 if self
17 .services
18 .server
19 .config
20 .admin
21 .console_automatic
22 {
23 if !is_terminal_mode() {
24 warn!("Not starting the admin console: standard input is not a terminal");
25 return;
26 }
27
28 yield_now().await;
30 self.console.start();
31 }
32}
33
34#[implement(super::Service)]
36#[cfg_attr(not(feature = "console"), expect(clippy::unused_async))]
37pub(super) async fn console_auto_stop(&self) {
38 #[cfg(feature = "console")]
39 self.console.close().await;
40}
41
42#[implement(super::Service)]
44pub async fn startup_execute(&self) -> Result {
45 let commands = &self.services.server.config.admin.execute;
47
48 let smoketest = self.services.server.config.test.contains("smoke");
50
51 let errors = !smoketest
53 && self
54 .services
55 .server
56 .config
57 .admin
58 .execute_errors_ignore;
59
60 for (i, command) in commands.iter().enumerate() {
61 if let Err(e) = self.execute_command(i, command.clone()).await
62 && !errors
63 {
64 return Err(e);
65 }
66
67 yield_now().await;
68 }
69
70 if smoketest {
73 debug_info!("Smoketest mode. All commands complete. Shutting down now...");
74 self.services
75 .server
76 .shutdown()
77 .inspect_err(error::inspect_log)
78 .expect("Error shutting down from smoketest");
79 }
80
81 Ok(())
82}
83
84#[implement(super::Service)]
86pub(super) async fn signal_execute(&self) -> Result {
87 let commands = self
89 .services
90 .server
91 .config
92 .admin
93 .signal_execute
94 .clone();
95
96 let ignore_errors = self
98 .services
99 .server
100 .config
101 .admin
102 .execute_errors_ignore;
103
104 for (i, command) in commands.iter().enumerate() {
105 if let Err(e) = self.execute_command(i, command.clone()).await
106 && !ignore_errors
107 {
108 return Err(e);
109 }
110
111 yield_now().await;
112 }
113
114 Ok(())
115}
116
117#[implement(super::Service)]
119async fn execute_command(&self, i: usize, command: String) -> Result {
120 debug!("Execute command #{i}: executing {command:?}");
121
122 match self.command_in_place(command.into()).await {
123 | Ok(Some(output)) => Self::execute_command_output(i, &output),
124 | Err(output) => Self::execute_command_error(i, &output),
125 | Ok(None) => {
126 info!("Execute command #{i} completed (no output).");
127 Ok(())
128 },
129 }
130}
131
132#[cfg(feature = "console")]
133#[implement(super::Service)]
134fn execute_command_output(i: usize, content: &CommandOutput) -> Result {
135 debug_info!("Execute command #{i} completed:");
136 super::console::print(content.as_str());
137 Ok(())
138}
139
140#[cfg(feature = "console")]
141#[implement(super::Service)]
142fn execute_command_error(i: usize, content: &CommandOutput) -> Result {
143 super::console::print_err(content.as_str());
144 Err!(debug_error!("Execute command #{i} failed."))
145}
146
147#[cfg(not(feature = "console"))]
148#[implement(super::Service)]
149fn execute_command_output(i: usize, content: &CommandOutput) -> Result {
150 info!("Execute command #{i} completed:\n{:#}", content.as_str());
151 Ok(())
152}
153
154#[cfg(not(feature = "console"))]
155#[implement(super::Service)]
156fn execute_command_error(i: usize, content: &CommandOutput) -> Result {
157 Err!(error!("Execute command #{i} failed:\n{:#}", content.as_str()))
158}