Repository navigation
Expand file tree
/
Copy pathapply.rs
More file actions
317 lines (265 loc) · 10.6 KB
/
Copy pathapply.rs
File metadata and controls
317 lines (265 loc) · 10.6 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
use super::ComtryaCommand;
use crate::Runtime;
use clap::Parser;
use comfy_table::{Cell, ContentArrangement, Table};
use comtrya_lib::contexts::to_rhai;
use comtrya_lib::manifests::{load, Manifest};
use core::panic;
use petgraph::{visit::DfsPostOrder, Graph};
use rhai::Engine;
use std::path::PathBuf;
use std::{collections::HashMap, ops::Deref};
use tracing::{debug, error, info, instrument, span, trace, warn};
#[derive(Parser, Debug)]
pub(crate) struct Apply {
/// Run a subset of your manifests, comma separated list.
/// This should be a list of manifest names. No paths.
#[arg(short, long, value_delimiter = ',')]
manifests: Vec<String>,
/// Performs a dry-run without changing the system
#[arg(long)]
dry_run: bool,
/// Define label selector
#[arg(short, long)]
pub label: Option<String>,
}
impl Apply {
fn manifest_path(&self, runtime: &Runtime) -> anyhow::Result<PathBuf> {
for manifest in &self.manifests {
if manifest.contains(std::path::MAIN_SEPARATOR) {
return Err(anyhow::anyhow!(
"Found a path, expected only names in the manifests list!"
));
}
}
let first_manifest_path = runtime.config.manifest_paths.first().ok_or_else(|| {
anyhow::anyhow!(
"No manifest paths found in config file, please add at least one path to your manifests"
)
})?;
let manifest_path = match crate::manifests::resolve(first_manifest_path) {
Some(path) => path,
None => {
return Err(anyhow::anyhow!(
"Manifest location, {:?}, could be resolved",
first_manifest_path
))
}
};
trace!(manifests = self.manifests.join(",").deref(),);
Ok(manifest_path)
}
#[instrument(skip(self, runtime))]
pub fn status(&self, runtime: &Runtime) -> anyhow::Result<()> {
let contexts = &runtime.contexts;
let manifest_path = self.manifest_path(&runtime)?;
println!("Load manifests from path: {:#?}", manifest_path);
let manifests = load(manifest_path, contexts);
let mut table = Table::new();
table
.set_content_arrangement(ContentArrangement::Dynamic)
.set_width(40)
.set_header(vec!["Manifest", "Count of Actions"]);
for (name, manifest) in manifests.iter() {
table.add_row(vec![
Cell::new(format!("{name}")),
Cell::new(format!("{}", manifest.actions.len())),
]);
}
println!("{table}");
Ok(())
}
}
impl ComtryaCommand for Apply {
#[instrument(skip(self, runtime))]
fn execute(&self, runtime: &Runtime) -> anyhow::Result<()> {
let contexts = &runtime.contexts;
let manifest_path = self.manifest_path(&runtime)?;
let manifests = load(manifest_path, contexts);
// Build DAG
let mut dag: Graph<Manifest, u32, petgraph::Directed> = Graph::new();
let manifest_root = Manifest {
r#where: None,
root_dir: None,
dag_index: None,
name: None,
depends: vec![],
actions: vec![],
..Default::default()
};
let root_index = dag.add_node(manifest_root);
let manifests: HashMap<String, Manifest> = manifests
.into_iter()
.map(|(name, mut manifest)| {
let abc = dag.add_node(manifest.clone());
manifest.dag_index = Some(abc);
dag.add_edge(root_index, abc, 0);
(name, manifest)
})
.collect();
for (name, manifest) in manifests.iter() {
manifest.depends.iter().for_each(|dependency| {
let (local_dependency_prefix, _) = name.rsplit_once('.').unwrap_or((name, ""));
let resolved_dependency_name =
dependency.replace("./", format!("{}.", local_dependency_prefix).as_str());
let m1 = match manifests.get(&resolved_dependency_name) {
Some(manifest) => manifest,
None => {
error!(
message = "Unresolved dependency",
dependency = resolved_dependency_name.as_str()
);
return;
}
};
trace!(
message = "Dependency Registered",
from = name.as_str(),
to = m1.name.as_deref().unwrap_or("cannot extract name"),
);
if let (Some(from), Some(to)) = (manifest.dag_index, m1.dag_index) {
dag.add_edge(from, to, 0);
} else {
error!(message = "Cannot add dependency, missing dag index");
}
});
}
let clone_m = self.manifests.clone();
let run_manifests = if self.manifests.is_empty() {
// No manifests specified on command line, so run everything
vec![String::from("")]
} else {
// Run subset
manifests
.keys()
.filter(|z| clone_m.contains(z))
.cloned()
.collect::<Vec<String>>()
};
let dry_run = self.dry_run;
let engine = Engine::new();
let mut scope = to_rhai(contexts);
run_manifests.iter().for_each(|manifest| {
let start = if manifest.eq(&String::from("")) {
root_index
} else if let Some(dag_index) = manifests
.get(manifest)
.and_then(|manifest| manifest.dag_index)
{
dag_index
} else {
// FIXME: Don't panic here. Find a better way to handle this.
panic!("Cannot find manifest in DAG");
};
let mut dfs = DfsPostOrder::new(&dag, start);
while let Some(visited) = dfs.next(&dag) {
if dag.node_weight(visited).is_none() {
info!(
message = "Skipping manifest, not found in DAG",
index = visited.index()
);
}
// .unwrap() is safe here, because we just checked that the node exists
let m1 = dag.node_weight(visited).unwrap();
// Root manifest, nothing to do.
if m1.name.is_none() {
continue;
}
let span_manifest = span!(
tracing::Level::INFO,
"",
manifest = m1.name.as_deref().unwrap_or("Cannot extract name"),
)
.entered();
let mut successful = true;
if let Some(label) = self.label.as_ref() {
if !m1.labels.contains(label) {
info!(
message = "Skipping manifest, label not found",
label = label.as_str()
);
continue;
}
}
if let Some(where_condition) = &m1.r#where {
let where_result =
match engine.eval_with_scope::<bool>(&mut scope, where_condition) {
Ok(result) => {
debug!(
"Result of 'where' condition '{}' -> '{}'",
where_condition, result
);
result
}
Err(err) => {
warn!("'where' condition '{}' failed: {}", where_condition, err);
false
}
};
if !where_result {
info!("Skip manifest, because 'where' conditions were false!");
span_manifest.exit();
continue;
}
}
for action in m1.actions.iter() {
let span_action = span!(tracing::Level::INFO, "", %action).entered();
let action = action.inner_ref();
let plan = match action.plan(m1, contexts) {
Ok(steps) => steps,
Err(err) => {
info!("Action failed to get plan: {:?}", err);
successful = false;
continue;
}
};
let mut steps = plan
.into_iter()
.filter(|step| step.do_initializers_allow_us_to_run())
.filter(|step| match step.atom.plan() {
Ok(outcome) => outcome.should_run,
Err(_) => false,
})
.peekable();
if steps.peek().is_none() {
info!("nothing to be done to reconcile action");
span_action.exit();
continue;
}
for mut step in steps {
if dry_run {
continue;
}
match step.atom.execute() {
Ok(_) => (),
Err(err) => {
debug!("Atom failed to execute: {:?}", err);
successful = false;
break;
}
}
if !step.do_finalizers_allow_us_to_continue() {
debug!("Finalizers won't allow us to continue with this action");
successful = false;
break;
}
}
info!("{}", action.summarize());
span_action.exit();
}
if dry_run {
span_manifest.exit();
continue;
}
if !successful {
error!("Failed");
span_manifest.exit();
break;
}
info!("Completed");
span_manifest.exit();
}
});
Ok(())
}
}