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
//! `ryra upgrade` — re-render an installed service against the current
//! registry, back up files that change, and restart the unit. The render
//! itself happens in `ryra-core::upgrade_service`; this module owns the
//! user-facing flow: print the diff, confirm, apply.
use std::collections::{BTreeMap, BTreeSet};
use anyhow::Result;
use console::style;
use dialoguer::{Confirm, Input};
use ryra_core::registry::service_def::EnvKind;
use ryra_core::{DiffKind, EnvAddition, GeneratedFile, Step, UpgradeResult};
use super::apply;
pub async fn run(services: &[String], yes: bool, force: bool, dry_run: bool) -> Result<()> {
let targets = resolve_targets(services)?;
if targets.is_empty() {
println!("No services installed.");
return Ok(());
}
// Plan every service first. Aborting early on a hand-edit (without
// --force) means we never partially upgrade some services and refuse
// others.
let mut plans: Vec<UpgradeResult> = Vec::with_capacity(targets.len());
for service in &targets {
match ryra_core::ops::plan_upgrade(&ryra_core::ops::UpgradeRequest {
service: service.to_string(),
force,
})
.await
{
Ok(plan) => plans.push(plan),
Err(ryra_core::error::Error::HandEditedFiles { service, paths }) => {
eprintln!(
"{} {service}: {} hand-edited file(s):",
style("error").red().bold(),
paths.len()
);
for p in &paths {
eprintln!(" {} {}", style("!").red().bold(), p.display());
}
eprintln!();
eprintln!(
"Re-run with {} to overwrite (backups land in {}), or back up the changes first.",
style("--force").bold(),
style("~/.local/state/ryra/backups/").dim()
);
anyhow::bail!("upgrade aborted");
}
Err(e) => return Err(e.into()),
}
}
let any_change = plans.iter().any(|p| !p.diff.is_clean() || p.force_apply);
if !any_change {
println!("Everything up to date.");
return Ok(());
}
print_summary(&plans);
if dry_run {
println!("Dry run — no changes made. Remove --dry-run to apply.\n");
return Ok(());
}
// Prompt for any env additions that the registry marks as Prompted /
// Required — these are user-facing values (admin email, OAuth client
// ids, etc.) where the registry's literal default is a placeholder
// ("admin@example.com") and silently appending it would be wrong.
// Default-kind additions are appended as-is. Non-interactive runs
// accept defaults for Prompted but bail on Required (no value to
// use, and we don't want to silently write nothing).
for plan in plans.iter_mut() {
prompt_and_patch_env_additions(plan)?;
}
if !yes {
if super::is_interactive() {
let proceed = Confirm::new()
.with_prompt("Apply upgrade?")
.default(true)
.interact()?;
if !proceed {
println!("Cancelled.");
return Ok(());
}
} else {
// No TTY and no --yes: refuse to apply silently. `ryra upgrade`
// restarts services (brief downtime) and edits running quadlets,
// so silently proceeding from a script is the wrong default.
anyhow::bail!(
"non-interactive run without --yes — re-run with `ryra upgrade --yes` (or --dry-run to preview)"
);
}
}
for plan in &plans {
if plan.diff.is_clean() && !plan.force_apply {
continue;
}
println!();
println!("Upgrading {}…", style(&plan.service).bold());
apply::execute_all(&plan.steps).await?;
if let Some(backup) = &plan.backup_dir {
println!(
" {} {}",
style("Backed up to").dim(),
style(backup.display()).dim()
);
}
// Cap the backup tree to the most recent N snapshots per
// service so a long history of upgrades doesn't accumulate
// forever. Best-effort — a prune failure doesn't fail the
// upgrade (the new backup is already on disk).
match ryra_core::prune_backups(&plan.service, ryra_core::DEFAULT_BACKUP_KEEP) {
Ok(pruned) if !pruned.is_empty() => {
println!(
" {} pruned {} older backup(s) (keep={})",
style("⌫").dim(),
pruned.len(),
ryra_core::DEFAULT_BACKUP_KEEP
);
}
Ok(_) => {}
Err(e) => {
eprintln!(" warning: backup prune failed: {e}");
}
}
}
println!();
println!("Done.");
Ok(())
}
/// Walk `plan.diff.env_additions`, prompt for any Prompted / Required
/// entries, and rewrite the plan's `.env` write step to use the
/// user-chosen values. Default-kind entries flow through unchanged.
fn prompt_and_patch_env_additions(plan: &mut UpgradeResult) -> Result<()> {
if plan.diff.env_additions.is_empty() {
return Ok(());
}
let needs_prompt: Vec<&EnvAddition> = plan
.diff
.env_additions
.iter()
.filter(|a| matches!(a.kind, EnvKind::Prompted | EnvKind::Required))
.collect();
if needs_prompt.is_empty() {
return Ok(());
}
let interactive = super::is_interactive();
let has_required = needs_prompt
.iter()
.any(|a| matches!(a.kind, EnvKind::Required));
if !interactive && has_required {
anyhow::bail!(
"{}: registry adds required env var(s); re-run interactively or pre-populate them in `.env`:\n {}",
plan.service,
needs_prompt
.iter()
.filter(|a| matches!(a.kind, EnvKind::Required))
.map(|a| a.key.as_str())
.collect::<Vec<_>>()
.join("\n ")
);
}
let mut overrides: BTreeMap<String, String> = BTreeMap::new();
if interactive {
println!();
println!("Confirm env values for {}:", style(&plan.service).bold());
for add in &needs_prompt {
let label = add.prompt.as_deref().unwrap_or(&add.key);
let value = match add.kind {
EnvKind::Required => Input::<String>::new()
.with_prompt(format!(" {label} (required)"))
.interact_text()?,
EnvKind::Prompted => Input::<String>::new()
.with_prompt(format!(" {label}"))
.default(add.value.clone())
.interact_text()?,
EnvKind::Default => continue,
};
if value != add.value {
overrides.insert(add.key.clone(), value);
}
}
}
if overrides.is_empty() {
// User accepted every default — nothing to patch.
return Ok(());
}
// Apply overrides to the addition list (so the summary the user
// already saw remains coherent with what gets written).
for add in plan.diff.env_additions.iter_mut() {
if let Some(v) = overrides.get(&add.key) {
add.value = v.clone();
}
}
// Rebuild the .env write step with the new values. The old step's
// content was a `read existing + append` so we redo that with the
// patched additions.
let env_path = ryra_core::service_home(&plan.service)?.join(".env");
let mut content = match std::fs::read_to_string(&env_path) {
Ok(c) => c,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(e) => anyhow::bail!("read {}: {e}", env_path.display()),
};
if !content.is_empty() && !content.ends_with('\n') {
content.push('\n');
}
for add in &plan.diff.env_additions {
content.push_str(&format!("{}={}\n", add.key, add.value));
}
// Find and replace the env step in plan.steps. Add a fresh step at
// the end of the file-write region if it isn't there (defensive —
// shouldn't happen).
let mut replaced = false;
for step in plan.steps.iter_mut() {
if let Step::WriteFile(file) = step
&& file.path == env_path
{
file.content = content.clone();
replaced = true;
break;
}
}
if !replaced {
plan.steps.push(Step::WriteFile(GeneratedFile {
path: env_path,
content,
}));
}
Ok(())
}
fn print_summary(plans: &[UpgradeResult]) {
for plan in plans {
if plan.diff.is_clean() {
// Native services rebuild from source even with a clean config
// diff; say so rather than showing an empty entry.
if plan.force_apply {
println!(
"{} {}",
style(&plan.service).bold(),
style("(rebuild from source)").dim()
);
}
continue;
}
println!("{}", style(&plan.service).bold());
for entry in &plan.diff.entries {
match entry.kind {
DiffKind::Unchanged => {}
DiffKind::Added => println!(
" {} {} {}",
style("+").green().bold(),
entry.path.display(),
style("added").green()
),
DiffKind::Modified => println!(
" {} {} {}",
style("~").yellow(),
entry.path.display(),
style("modified").yellow()
),
DiffKind::Removed => println!(
" {} {} {}",
style("-").red(),
entry.path.display(),
style("removed").red()
),
DiffKind::Drift => println!(
" {} {} {}",
style("!").red().bold(),
entry.path.display(),
style("drift (overwriting via --force)").red().bold()
),
}
}
for add in &plan.diff.env_additions {
println!(
" {} env: {}={} {}",
style("+").green().bold(),
add.key,
add.value,
style("appended to .env (registry-added)").green()
);
}
// Surface the side effects the user is consenting to. Restart
// means a brief downtime; backup path tells them where to look
// if something goes sideways; the revert hint is the get-out-of-jail
// card so they don't have to figure out how to roll back manually.
println!(
" {} systemctl --user daemon-reload + restart {} (brief downtime)",
style("→").cyan(),
plan.service
);
if let Some(backup) = &plan.backup_dir {
println!(
" {} backup of replaced files: {}",
style("→").cyan(),
style(backup.display()).dim()
);
println!(
" {} undo with: {}",
style("→").cyan(),
style(format!("ryra revert {}", plan.service)).dim()
);
}
}
println!();
}
fn resolve_targets(services: &[String]) -> Result<Vec<String>> {
if !services.is_empty() {
for s in services {
if !ryra_core::is_service_installed(s) {
anyhow::bail!("service '{s}' is not installed");
}
}
let mut out: Vec<String> = services.to_vec();
let mut seen = BTreeSet::new();
out.retain(|s| seen.insert(s.clone()));
return Ok(out);
}
let installed = ryra_core::list_installed()?;
Ok(installed.into_iter().map(|s| s.name).collect())
}