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
use crate::PathDisplayExt;
use crate::config::{ResolvedProfile, ScriptEntry, ScriptShell};
use crate::errors::{ConfigError, Result};
use crate::expand_tilde;
use crate::modules::ResolvedModule;
use crate::output::{Printer, Role};
use super::scripts::{MODULE_SCRIPT_TIMEOUT, build_module_script_env, execute_script};
use super::types::{ModuleAction, ModuleActionKind, ReconcileContext, ScriptPhase};
impl<'a> super::Reconciler<'a> {
#[allow(clippy::too_many_arguments)]
pub(super) fn apply_module_action(
&self,
action: &ModuleAction,
config_dir: &std::path::Path,
printer: &Printer,
apply_id: i64,
context: ReconcileContext,
resolved: &ResolvedProfile,
module_actions: &[ResolvedModule],
shell_override: Option<ScriptShell>,
) -> Result<String> {
// Find the resolved module to obtain its dir and declared env vars.
let resolved_mod = module_actions.iter().find(|m| m.name == action.module_name);
let module_dir = resolved_mod.map(|m| m.dir.clone());
let module_env = resolved_mod.map(|m| m.env.as_slice()).unwrap_or(&[]);
match &action.kind {
ModuleActionKind::InstallPackages { resolved: pkgs } => {
// Packages in each InstallPackages action are already grouped by
// manager in plan_modules(), so just collect names and install.
let pkg_names: Vec<String> = pkgs.iter().map(|p| p.resolved_name.clone()).collect();
if let Some(first) = pkgs.first() {
if first.manager == "script" {
// Script-based install: run each package's script via execute_script
for pkg in pkgs {
if let Some(ref script_content) = pkg.script {
let profile_name = resolved
.layers
.last()
.map(|l| l.profile_name.as_str())
.unwrap_or("unknown");
let env_vars = build_module_script_env(
config_dir,
profile_name,
context,
&ScriptPhase::PostApply,
Some(&action.module_name),
module_dir.as_deref(),
module_env,
);
let script_entry = ScriptEntry::Simple(script_content.clone());
let working = module_dir.as_deref().unwrap_or(config_dir);
execute_script(
&script_entry,
working,
&env_vars,
MODULE_SCRIPT_TIMEOUT,
printer,
shell_override,
)
.map_err(|_| {
crate::errors::CfgdError::Config(ConfigError::Invalid {
message: format!(
"module {} install script for '{}' failed",
action.module_name, pkg.canonical_name
),
})
})?;
}
}
} else {
// Find the manager — check all registered, not just available
let pm = self
.registry
.package_managers
.iter()
.find(|m| m.name() == first.manager);
if let Some(pm) = pm {
// Bootstrap if needed
if !pm.is_available() && pm.can_bootstrap() {
pm.bootstrap(printer)?;
// Persist bootstrapped manager's PATH to ~/.cfgd.env
let path_dirs = pm.path_dirs();
if !path_dirs.is_empty() {
let env_path =
expand_tilde(std::path::Path::new("~/.cfgd.env"));
let existing = match std::fs::read_to_string(&env_path) {
Ok(s) => s,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
String::new()
}
Err(e) => {
tracing::warn!("cannot read {}: {e}", env_path.posix());
String::new()
}
};
let new_dirs: Vec<&str> = path_dirs
.iter()
.filter(|d| !existing.contains(d.as_str()))
.map(|d| d.as_str())
.collect();
if !new_dirs.is_empty() {
let mut content = existing;
if !content.ends_with('\n') && !content.is_empty() {
content.push('\n');
}
content.push_str(&format!(
"export PATH=\"{}:$PATH\"\n",
new_dirs.join(":")
));
crate::atomic_write_str(&env_path, &content)?;
}
}
}
// Update package index before installing
if pm.is_available() {
pm.update(printer)?;
}
pm.install(&pkg_names, printer)?;
}
}
}
Ok(format!(
"module:{}:packages:{}",
action.module_name,
pkg_names.join(",")
))
}
ModuleActionKind::DeployFiles { files } => {
for file in files {
let target = expand_tilde(&file.target);
if let Some(parent) = target.parent() {
std::fs::create_dir_all(parent)?;
}
// Use the per-file strategy override if set, otherwise
// fall back to the global file-strategy from cfgd.yaml (default: symlink).
let strategy = file.strategy.unwrap_or(self.registry.default_file_strategy);
// Backup existing target before overwriting
if let Ok(Some(file_state)) = crate::capture_file_state(&target)
&& let Err(e) = self.state.store_file_backup(
apply_id,
&target.display().to_string(),
&file_state,
)
{
tracing::warn!("failed to backup module file {}: {}", target.posix(), e);
}
// Remove existing target before deploying
if target.symlink_metadata().is_ok() {
if target.is_dir() && !target.is_symlink() {
std::fs::remove_dir_all(&target)?;
} else {
std::fs::remove_file(&target)?;
}
}
if file.source.is_dir() {
match strategy {
crate::config::FileStrategy::Symlink => {
crate::create_symlink(&file.source, &target)?;
}
_ => {
crate::copy_dir_recursive(&file.source, &target)?;
}
}
} else if file.source.exists() {
match strategy {
crate::config::FileStrategy::Symlink => {
crate::create_symlink(&file.source, &target)?;
}
crate::config::FileStrategy::Hardlink => {
std::fs::hard_link(&file.source, &target)?;
}
crate::config::FileStrategy::Copy
| crate::config::FileStrategy::Template => {
let content = std::fs::read(&file.source)?;
crate::atomic_write(&target, &content)?;
}
}
}
// Record in module file manifest
let hash = if target.exists() && !target.is_symlink() {
match std::fs::read(&target) {
Ok(bytes) => crate::sha256_hex(&bytes),
Err(e) => {
tracing::warn!("cannot read {} for hashing: {e}", target.posix());
String::new()
}
}
} else {
String::new()
};
self.state.upsert_module_file(
&action.module_name,
&target.display().to_string(),
&hash,
&format!("{:?}", strategy),
apply_id,
)?;
}
printer.status_simple(
Role::Ok,
format!(
"Module {}: deployed {} file(s)",
action.module_name,
files.len()
),
);
Ok(format!(
"module:{}:files:{}",
action.module_name,
files.len()
))
}
ModuleActionKind::RunScript {
script,
phase: script_phase,
} => {
let profile_name = resolved
.layers
.last()
.map(|l| l.profile_name.as_str())
.unwrap_or("unknown");
let env_vars = build_module_script_env(
config_dir,
profile_name,
context,
script_phase,
Some(&action.module_name),
module_dir.as_deref(),
module_env,
);
let working = module_dir.as_deref().unwrap_or(config_dir);
execute_script(
script,
working,
&env_vars,
MODULE_SCRIPT_TIMEOUT,
printer,
shell_override,
)?;
Ok(format!("module:{}:script", action.module_name))
}
ModuleActionKind::Skip { reason } => {
printer.status_simple(
Role::Warn,
format!("Module {}: skipped — {}", action.module_name, reason),
);
Ok(format!("module:{}:skip", action.module_name))
}
}
}
}