forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.rs
279 lines (245 loc) · 9.13 KB
/
service.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#![allow(missing_docs)]
use std::{ffi::OsString, path::PathBuf, time::Duration};
use clap::Parser;
use crate::{cli::handle_config_errors, config};
const DEFAULT_SERVICE_NAME: &str = crate::built_info::PKG_NAME;
#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
pub struct Opts {
#[command(subcommand)]
sub_command: Option<SubCommand>,
}
#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
struct InstallOpts {
/// The name of the service to install.
#[arg(long)]
name: Option<String>,
/// The display name to be used by interface programs to identify the service like Windows Services App
#[arg(long)]
display_name: Option<String>,
/// Vector config files in TOML format to be used by the service.
#[arg(name = "config-toml", long, value_delimiter(','))]
config_paths_toml: Vec<PathBuf>,
/// Vector config files in JSON format to be used by the service.
#[arg(name = "config-json", long, value_delimiter(','))]
config_paths_json: Vec<PathBuf>,
/// Vector config files in YAML format to be used by the service.
#[arg(name = "config-yaml", long, value_delimiter(','))]
config_paths_yaml: Vec<PathBuf>,
/// The configuration files that will be used by the service.
/// If no configuration file is specified, will target default configuration file.
#[arg(name = "config", short, long, value_delimiter(','))]
config_paths: Vec<PathBuf>,
/// Read configuration from files in one or more directories.
/// File format is detected from the file name.
///
/// Files not ending in .toml, .json, .yaml, or .yml will be ignored.
#[arg(
id = "config-dir",
short = 'C',
long,
env = "VECTOR_CONFIG_DIR",
value_delimiter(',')
)]
config_dirs: Vec<PathBuf>,
}
impl InstallOpts {
fn service_info(&self) -> ServiceInfo {
let service_name = self.name.as_deref().unwrap_or(DEFAULT_SERVICE_NAME);
let display_name = self.display_name.as_deref().unwrap_or("Vector Service");
let description = crate::built_info::PKG_DESCRIPTION;
let current_exe = ::std::env::current_exe().unwrap();
let config_paths = self.config_paths_with_formats();
let arguments = create_service_arguments(&config_paths).unwrap();
ServiceInfo {
name: OsString::from(service_name),
display_name: OsString::from(display_name),
description: OsString::from(description),
executable_path: current_exe,
launch_arguments: arguments,
}
}
fn config_paths_with_formats(&self) -> Vec<config::ConfigPath> {
config::merge_path_lists(vec![
(&self.config_paths, None),
(&self.config_paths_toml, Some(config::Format::Toml)),
(&self.config_paths_json, Some(config::Format::Json)),
(&self.config_paths_yaml, Some(config::Format::Yaml)),
])
.map(|(path, hint)| config::ConfigPath::File(path, hint))
.chain(
self.config_dirs
.iter()
.map(|dir| config::ConfigPath::Dir(dir.to_path_buf())),
)
.collect()
}
}
#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
struct RestartOpts {
/// The name of the service.
#[arg(long)]
name: Option<String>,
/// How long to wait for the service to stop before starting it back, in seconds.
#[arg(default_value = "60", long)]
stop_timeout: u32,
}
impl RestartOpts {
fn service_info(&self) -> ServiceInfo {
let mut default_service = ServiceInfo::default();
let service_name = self.name.as_deref().unwrap_or(DEFAULT_SERVICE_NAME);
default_service.name = OsString::from(service_name);
default_service
}
}
#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
struct StandardOpts {
/// The name of the service.
#[arg(long)]
name: Option<String>,
}
impl StandardOpts {
fn service_info(&self) -> ServiceInfo {
let mut default_service = ServiceInfo::default();
let service_name = self.name.as_deref().unwrap_or(DEFAULT_SERVICE_NAME);
default_service.name = OsString::from(service_name);
default_service
}
}
#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
enum SubCommand {
/// Install the service.
Install(InstallOpts),
/// Uninstall the service.
Uninstall(StandardOpts),
/// Start the service.
Start(StandardOpts),
/// Stop the service.
Stop(StandardOpts),
/// Restart the service.
Restart(RestartOpts),
}
struct ServiceInfo {
name: OsString,
display_name: OsString,
description: OsString,
executable_path: std::path::PathBuf,
launch_arguments: Vec<OsString>,
}
impl Default for ServiceInfo {
fn default() -> Self {
let current_exe = ::std::env::current_exe().unwrap();
ServiceInfo {
name: OsString::from(DEFAULT_SERVICE_NAME),
display_name: OsString::from("Vector Service"),
description: OsString::from(crate::built_info::PKG_DESCRIPTION),
executable_path: current_exe,
launch_arguments: vec![],
}
}
}
#[derive(Debug, Clone, PartialEq)]
enum ControlAction {
Install,
Uninstall,
Start,
Stop,
Restart { stop_timeout: Duration },
}
pub fn cmd(opts: &Opts) -> exitcode::ExitCode {
let sub_command = &opts.sub_command;
match sub_command {
Some(s) => match s {
SubCommand::Install(opts) => {
control_service(&opts.service_info(), ControlAction::Install)
}
SubCommand::Uninstall(opts) => {
control_service(&opts.service_info(), ControlAction::Uninstall)
}
SubCommand::Start(opts) => control_service(&opts.service_info(), ControlAction::Start),
SubCommand::Stop(opts) => control_service(&opts.service_info(), ControlAction::Stop),
SubCommand::Restart(opts) => {
let stop_timeout = Duration::from_secs(opts.stop_timeout as u64);
control_service(
&opts.service_info(),
ControlAction::Restart { stop_timeout },
)
}
},
None => {
error!("You must specify a sub command. Valid sub commands are [start, stop, restart, install, uninstall].");
exitcode::USAGE
}
}
}
fn control_service(service: &ServiceInfo, action: ControlAction) -> exitcode::ExitCode {
use crate::vector_windows;
let service_definition = vector_windows::service_control::ServiceDefinition {
name: service.name.clone(),
display_name: service.display_name.clone(),
description: service.description.clone(),
executable_path: service.executable_path.clone(),
launch_arguments: service.launch_arguments.clone(),
};
let res = match action {
ControlAction::Install => vector_windows::service_control::control(
&service_definition,
vector_windows::service_control::ControlAction::Install,
),
ControlAction::Uninstall => vector_windows::service_control::control(
&service_definition,
vector_windows::service_control::ControlAction::Uninstall,
),
ControlAction::Start => vector_windows::service_control::control(
&service_definition,
vector_windows::service_control::ControlAction::Start,
),
ControlAction::Stop => vector_windows::service_control::control(
&service_definition,
vector_windows::service_control::ControlAction::Stop,
),
ControlAction::Restart { stop_timeout } => vector_windows::service_control::control(
&service_definition,
vector_windows::service_control::ControlAction::Restart { stop_timeout },
),
};
match res {
Ok(()) => exitcode::OK,
Err(error) => {
error!(message = "Error controlling service.", %error);
exitcode::SOFTWARE
}
}
}
fn create_service_arguments(config_paths: &[config::ConfigPath]) -> Option<Vec<OsString>> {
let config_paths = config::process_paths(config_paths)?;
match config::load_from_paths(&config_paths) {
Ok(_) => Some(
config_paths
.iter()
.flat_map(|config_path| match config_path {
config::ConfigPath::File(path, format) => {
let key = match format {
None => "--config",
Some(config::Format::Toml) => "--config-toml",
Some(config::Format::Json) => "--config-json",
Some(config::Format::Yaml) => "--config-yaml",
};
vec![OsString::from(key), path.as_os_str().into()]
}
config::ConfigPath::Dir(path) => {
vec![OsString::from("--config-dir"), path.as_os_str().into()]
}
})
.collect::<Vec<OsString>>(),
),
Err(errs) => {
handle_config_errors(errs);
None
}
}
}