agentmux_srv_core/backend/slug_migration/
steps.rs1use std::path::PathBuf;
13
14use serde_json::Value;
15
16use super::home::{Home, Install};
17use super::journal::{BackupEntry, Step};
18use super::{steps_db, steps_files};
19
20pub(super) struct Ctx<'a> {
22 pub home: &'a Home,
23 pub installs: &'a [Install],
24 pub run_dir: PathBuf,
25 pub backups: &'a [BackupEntry],
26 pub retired_at: i64,
28}
29
30impl Ctx<'_> {
31 pub fn objects_dbs(&self) -> Vec<PathBuf> {
32 self.installs.iter().flat_map(|i| i.versions.iter().map(|v| v.objects_db())).collect()
33 }
34 pub fn wan_dbs(&self) -> Vec<PathBuf> {
35 self.installs.iter().filter_map(|i| i.wan_db()).collect()
36 }
37 pub fn host_stores(&self) -> Vec<PathBuf> {
38 self.home.host_stores(self.installs)
39 }
40 pub fn backup_of(&self, original: &std::path::Path) -> Result<PathBuf, String> {
42 super::backup::copy_of(&self.run_dir, self.backups, original)
43 }
44}
45
46#[derive(Debug, Clone)]
48pub(super) struct Agent {
49 pub uid: String,
50 pub name: String,
51 pub old: String,
53 pub new: String,
54}
55
56pub(super) fn before(ctx: &Ctx, a: &Agent, step: Step) -> Result<Value, String> {
57 match step {
58 Step::S1 => steps_files::s1_before(ctx, a),
59 Step::S2 => steps_db::s2_before(ctx, a),
60 Step::S3 => steps_db::s3_before(ctx, a),
61 Step::S4 => steps_db::s4_before(ctx, a),
62 Step::S5 => steps_db::s5_before(ctx, a),
63 Step::S6 => steps_db::s6_before(ctx, a),
64 Step::S7 => {
65 let mut v = steps_db::s7_before(ctx, a)?;
66 v["files"] = steps_files::s7_files_before(ctx, a)?;
67 Ok(v)
68 }
69 Step::S8 => steps_files::s8_before(ctx, a),
70 }
71}
72
73pub(super) fn apply(ctx: &Ctx, a: &Agent, step: Step, before: &Value) -> Result<Vec<String>, String> {
74 match step {
75 Step::S1 => steps_files::s1_apply(ctx, a, before),
76 Step::S2 => steps_db::s2_apply(ctx, a, before),
77 Step::S3 => steps_db::s3_apply(ctx, a, before),
78 Step::S4 => steps_db::s4_apply(ctx, a, before),
79 Step::S5 => steps_db::s5_apply(before),
80 Step::S6 => steps_db::s6_apply(ctx, a, before),
81 Step::S7 => {
82 let mut changes = steps_db::s7_apply(ctx, a, before)?;
83 changes.extend(steps_files::s7_files_apply(a, &before["files"])?);
84 Ok(changes)
85 }
86 Step::S8 => steps_files::s8_apply(before),
87 }
88}
89
90pub(super) fn undo(ctx: &Ctx, a: &Agent, step: Step, before: &Value) -> Result<Vec<String>, String> {
91 match step {
92 Step::S1 => steps_files::s1_undo(ctx, a, before),
93 Step::S2 => steps_db::s2_undo(a, before),
94 Step::S3 => steps_db::s3_undo(ctx, a, before),
95 Step::S4 => steps_db::s4_undo(ctx, a, before),
96 Step::S5 => Ok(Vec::new()),
97 Step::S6 => steps_db::s6_undo(a, before),
98 Step::S7 => {
99 let mut changes = steps_db::s7_undo(a, before)?;
100 changes.extend(steps_files::s7_files_undo(ctx, a, &before["files"])?);
101 Ok(changes)
102 }
103 Step::S8 => steps_files::s8_undo(ctx, before),
104 }
105}
106
107pub(super) fn parse<T: serde::de::DeserializeOwned>(before: &Value) -> Result<T, String> {
109 serde_json::from_value(before.clone()).map_err(|e| format!("the journal's before-image doesn't read: {e}"))
110}
111
112pub(super) fn to_value<T: serde::Serialize>(v: &T) -> Result<Value, String> {
113 serde_json::to_value(v).map_err(|e| e.to_string())
114}