-
-
Notifications
You must be signed in to change notification settings - Fork 829
Expand file tree
/
Copy pathlifecycle.rs
More file actions
335 lines (291 loc) · 9.74 KB
/
Copy pathlifecycle.rs
File metadata and controls
335 lines (291 loc) · 9.74 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use std::net::IpAddr;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
use anyhow::{Context, Result, anyhow, bail};
use crate::{
cli::{
DomainsAction, DomainsAddOpts, DomainsCommand, DomainsEngineArg, DomainsRmOpts, KillOpts,
LifecycleRunOpts, TaskRunOpts,
},
config::{self, Config, LifecycleDomainsConfig},
domains, processes, tasks,
};
static RUNTIME_PREFERRED_URL: OnceLock<Mutex<Option<String>>> = OnceLock::new();
pub fn run_up(opts: LifecycleRunOpts) -> Result<()> {
let project = resolve_project_config(&opts.config)?;
let lifecycle = project.config.lifecycle.clone().unwrap_or_default();
let preferred_url = if let Some(domains_cfg) = lifecycle.domains.as_ref() {
match ensure_domains_up(domains_cfg) {
Ok(()) => lifecycle_preferred_url(domains_cfg),
Err(err) => {
eprintln!(
"WARN lifecycle domains unavailable; continuing without localhost routing"
);
eprintln!("WARN {}", err);
None
}
}
} else {
None
};
let _preferred_url_guard = ScopedPreferredUrl::set(preferred_url);
let ran_task = match lifecycle.up_task.as_deref() {
Some(task) => run_required_task(&project.flow_path, task, opts.args)?,
None => run_optional_task_chain(&project.flow_path, &["up", "dev"], opts.args)?,
};
if !ran_task {
bail!(
"No lifecycle up task found. Define task 'up' or 'dev', or set [lifecycle].up_task in {}",
project.flow_path.display()
);
}
Ok(())
}
pub fn run_down(opts: LifecycleRunOpts) -> Result<()> {
let project = resolve_project_config(&opts.config)?;
let lifecycle = project.config.lifecycle.clone().unwrap_or_default();
let mut task_ran = match lifecycle.down_task.as_deref() {
Some(task) => run_required_task(&project.flow_path, task, opts.args.clone())?,
None => run_optional_task_chain(&project.flow_path, &["down"], opts.args.clone())?,
};
if !task_ran && lifecycle.down_task.is_none() {
processes::kill_processes(KillOpts {
config: project.flow_path.clone(),
task: None,
pid: None,
all: true,
force: false,
timeout: 5,
})?;
task_ran = true;
}
let mut domain_action_ran = false;
if let Some(domains_cfg) = lifecycle.domains.as_ref() {
domain_action_ran = run_domains_down(domains_cfg)?;
}
if !task_ran && !domain_action_ran {
bail!(
"No lifecycle down action found. Define task 'down', set [lifecycle].down_task, or enable [lifecycle.domains] cleanup in {}",
project.flow_path.display()
);
}
Ok(())
}
fn run_required_task(config_path: &Path, task_name: &str, args: Vec<String>) -> Result<bool> {
match run_task(config_path, task_name, args) {
Ok(()) => Ok(true),
Err(err) if is_task_not_found(&err) => {
bail!("lifecycle task '{}' not found", task_name);
}
Err(err) => Err(err),
}
}
fn run_optional_task_chain(
config_path: &Path,
candidates: &[&str],
args: Vec<String>,
) -> Result<bool> {
for name in candidates {
match run_task(config_path, name, args.clone()) {
Ok(()) => return Ok(true),
Err(err) if is_task_not_found(&err) => continue,
Err(err) => return Err(err),
}
}
Ok(false)
}
fn run_task(config_path: &Path, task_name: &str, args: Vec<String>) -> Result<()> {
tasks::run(TaskRunOpts {
config: config_path.to_path_buf(),
delegate_to_hub: false,
hub_host: IpAddr::from([127, 0, 0, 1]),
hub_port: 9050,
name: task_name.to_string(),
args,
})
}
fn ensure_domains_up(cfg: &LifecycleDomainsConfig) -> Result<()> {
let host = lifecycle_domain_host(cfg)?;
let target = lifecycle_domain_target(cfg)?;
let engine = parse_domains_engine(cfg.engine.as_deref())?;
add_lifecycle_route(engine, host, target)?;
for alias in &cfg.aliases {
let alias_host = alias
.host
.as_deref()
.map(str::trim)
.filter(|v| !v.is_empty())
.ok_or_else(|| anyhow!("lifecycle.domains.aliases[].host is required"))?;
let alias_target = alias
.target
.as_deref()
.map(str::trim)
.filter(|v| !v.is_empty())
.ok_or_else(|| anyhow!("lifecycle.domains.aliases[].target is required"))?;
add_lifecycle_route(engine, alias_host, alias_target)?;
}
domains::run(DomainsCommand {
engine,
action: Some(DomainsAction::Up),
})?;
println!("Lifecycle domains ready: http://{}", host);
Ok(())
}
fn lifecycle_preferred_url(cfg: &LifecycleDomainsConfig) -> Option<String> {
let host = lifecycle_domain_host(cfg).ok()?;
Some(format!("http://{}", host))
}
fn run_domains_down(cfg: &LifecycleDomainsConfig) -> Result<bool> {
let mut changed = false;
let engine = parse_domains_engine(cfg.engine.as_deref())?;
if cfg.remove_on_down.unwrap_or(false) {
let host = lifecycle_domain_host(cfg)
.map_err(|_| anyhow!("lifecycle.domains.host is required when remove_on_down=true"))?;
remove_lifecycle_route(engine, host)?;
for alias in &cfg.aliases {
let alias_host = alias
.host
.as_deref()
.map(str::trim)
.filter(|v| !v.is_empty())
.ok_or_else(|| {
anyhow!("lifecycle.domains.aliases[].host is required when remove_on_down=true")
})?;
remove_lifecycle_route(engine, alias_host)?;
}
changed = true;
}
if cfg.stop_proxy_on_down.unwrap_or(false) {
domains::run(DomainsCommand {
engine,
action: Some(DomainsAction::Down),
})?;
changed = true;
}
Ok(changed)
}
fn lifecycle_domain_host(cfg: &LifecycleDomainsConfig) -> Result<&str> {
cfg.host
.as_deref()
.map(str::trim)
.filter(|v| !v.is_empty())
.ok_or_else(|| anyhow!("lifecycle.domains.host is required"))
}
fn lifecycle_domain_target(cfg: &LifecycleDomainsConfig) -> Result<&str> {
cfg.target
.as_deref()
.map(str::trim)
.filter(|v| !v.is_empty())
.ok_or_else(|| anyhow!("lifecycle.domains.target is required"))
}
fn add_lifecycle_route(engine: Option<DomainsEngineArg>, host: &str, target: &str) -> Result<()> {
domains::run(DomainsCommand {
engine,
action: Some(DomainsAction::Add(DomainsAddOpts {
host: host.to_string(),
target: target.to_string(),
replace: true,
})),
})
}
fn remove_lifecycle_route(engine: Option<DomainsEngineArg>, host: &str) -> Result<()> {
domains::run(DomainsCommand {
engine,
action: Some(DomainsAction::Rm(DomainsRmOpts {
host: host.to_string(),
})),
})
}
fn parse_domains_engine(raw: Option<&str>) -> Result<Option<DomainsEngineArg>> {
let Some(raw) = raw else {
return Ok(None);
};
let engine = match raw.trim().to_ascii_lowercase().as_str() {
"docker" => DomainsEngineArg::Docker,
"native" => DomainsEngineArg::Native,
other => bail!(
"invalid lifecycle.domains.engine '{}': expected 'docker' or 'native'",
other
),
};
Ok(Some(engine))
}
fn resolve_project_config(config_arg: &Path) -> Result<ProjectConfig> {
let cwd = std::env::current_dir().context("Failed to read current directory")?;
let flow_path = resolve_flow_path(config_arg, &cwd)?;
let cfg = config::load(&flow_path)
.with_context(|| format!("Failed to load {}", flow_path.display()))?;
Ok(ProjectConfig {
flow_path,
config: cfg,
})
}
fn resolve_flow_path(config_arg: &Path, cwd: &Path) -> Result<PathBuf> {
if config_arg.is_absolute() {
if config_arg.exists() {
return Ok(config_arg.to_path_buf());
}
bail!("config path not found: {}", config_arg.display());
}
let direct = cwd.join(config_arg);
if direct.exists() {
return Ok(direct);
}
if config_arg == Path::new("flow.toml") {
if let Some(found) = find_flow_toml_upwards(cwd) {
return Ok(found);
}
}
bail!("config path not found: {}", direct.display());
}
fn find_flow_toml_upwards(start: &Path) -> Option<PathBuf> {
let mut cur = start.to_path_buf();
loop {
let cand = cur.join("flow.toml");
if cand.exists() {
return Some(cand);
}
if !cur.pop() {
break;
}
}
None
}
fn is_task_not_found(err: &anyhow::Error) -> bool {
let msg = err.to_string().to_ascii_lowercase();
msg.contains("task '") && msg.contains("not found")
}
pub(crate) fn runtime_preferred_url() -> Option<String> {
preferred_url_slot()
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
}
fn preferred_url_slot() -> &'static Mutex<Option<String>> {
RUNTIME_PREFERRED_URL.get_or_init(|| Mutex::new(None))
}
struct ScopedPreferredUrl {
prev: Option<String>,
}
impl ScopedPreferredUrl {
fn set(value: Option<String>) -> Self {
let mut guard = preferred_url_slot()
.lock()
.unwrap_or_else(|e| e.into_inner());
let prev = guard.clone();
*guard = value;
Self { prev }
}
}
impl Drop for ScopedPreferredUrl {
fn drop(&mut self) {
let mut guard = preferred_url_slot()
.lock()
.unwrap_or_else(|e| e.into_inner());
*guard = self.prev.clone();
}
}
struct ProjectConfig {
flow_path: PathBuf,
config: Config,
}