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
use std::{path::Path, sync::Arc};
use anyhow::Context;
use crate::{
config::AutoschematicConfig,
connector::{Connector, VirtToPhyOutput},
connector_cache::ConnectorCache,
keystore::KeyStore,
template::template_config,
report::PlanReport,
util::split_prefix_addr,
};
pub async fn plan_connector(
connector_shortname: &str,
connector: &Box<dyn Connector>,
prefix: &Path,
virt_addr: &Path,
) -> Result<Option<PlanReport>, anyhow::Error> {
let mut plan_report = PlanReport::default();
plan_report.prefix = prefix.into();
plan_report.virt_addr = virt_addr.into();
let phy_addr = match connector.addr_virt_to_phy(virt_addr).await? {
VirtToPhyOutput::NotPresent => None,
VirtToPhyOutput::Deferred(read_outputs) => {
for output in read_outputs {
plan_report.missing_outputs.push(output);
}
return Ok(Some(plan_report));
}
VirtToPhyOutput::Present(phy_addr) => Some(phy_addr),
VirtToPhyOutput::Null(phy_addr) => Some(phy_addr),
};
let current = match phy_addr {
Some(ref phy_addr) => {
match connector.get(&phy_addr.clone()).await.context(format!(
"{}::get({})",
connector_shortname,
&phy_addr.to_str().unwrap_or_default()
))? {
// Existing resource present for this address
Some(get_resource_output) => {
let resource = get_resource_output.resource_definition;
Some(resource)
}
// No existing resource present for this address
None => None,
}
}
None => None,
};
let path = prefix.join(virt_addr);
let connector_ops = if path.is_file() {
// let desired = std::fs::read(&path)?;
let desired_bytes = tokio::fs::read(&path).await?;
match std::str::from_utf8(&desired_bytes) {
Ok(desired) => {
let template_result = template_config(prefix, desired)?;
if !template_result.missing.is_empty() {
for read_output in template_result.missing {
plan_report.missing_outputs.push(read_output);
}
return Ok(Some(plan_report));
} else {
// TODO warning that this phy .unwrap_or( virt )
// may be the most diabolically awful design
// TODO remove awful design
connector
.plan(
&phy_addr.clone().unwrap_or(virt_addr.into()),
current,
Some(template_result.body.into()),
)
.await
.context(format!("{}::plan({}, _, _)", connector_shortname, virt_addr.display()))?
}
}
Err(_) => {
// TODO warning that this phy .unwrap_or( virt )
// may be the most diabolically awful design
// TODO remove awful design
connector
.plan(&phy_addr.clone().unwrap_or(virt_addr.into()), current, Some(desired_bytes))
.await
.context(format!("{}::plan({}, _, _)", connector_shortname, virt_addr.display()))?
}
}
} else {
// The file does not exist, so `desired` is therefore None.
// Generally speaking, this will destroy the given resource if it currently exists.
// TODO warning that this phy .unwrap_or( virt )
// may be the most diabolically awful design
// TODO remove awful design
connector
.plan(&phy_addr.clone().unwrap_or(virt_addr.into()), current, None)
.await
.context(format!(
"{}::plan({}, _, _)",
connector_shortname,
virt_addr.to_str().unwrap_or_default()
))?
};
plan_report.connector_ops = connector_ops;
Ok(Some(plan_report))
}
/// For a given path, attempt to resolve its prefix and Connector impl and return a Vec of ConnectorOps.
/// Note that this, unlike the server implementation, does not handle setting desired = None where files do
/// not exist - it is intended to be used from the command line or from LSPs to quickly modify resources.
pub async fn unbundle(
autoschematic_config: &AutoschematicConfig,
connector_cache: Arc<ConnectorCache>,
keystore: Option<Arc<dyn KeyStore>>,
connector_filter: &Option<String>,
path: &Path,
) -> Result<Option<PlanReport>, anyhow::Error> {
let autoschematic_config = autoschematic_config.clone();
let Some((prefix, virt_addr)) = split_prefix_addr(&autoschematic_config, path) else {
return Ok(None);
};
let Some(prefix_def) = autoschematic_config.prefixes.get(prefix.to_str().unwrap_or_default()) else {
return Ok(None);
};
let prefix_def = prefix_def.clone();
let mut handles = Vec::new();
'connector: for connector_def in prefix_def.connectors {
if let Some(connector_filter) = &connector_filter
&& connector_def.shortname != *connector_filter {
continue 'connector;
}
let connector_cache = connector_cache.clone();
let keystore = keystore.clone();
let prefix = prefix.clone();
handles.push(tokio::spawn(async move {
let (connector, mut inbox) = connector_cache
.get_or_spawn_connector(
&connector_def.shortname,
&connector_def.spec,
&prefix,
&connector_def.env,
keystore,
)
.await
.unwrap();
let _reader_handle = tokio::spawn(async move {
loop {
match inbox.recv().await {
Ok(Some(stdout)) => {
eprintln!("{stdout}");
}
Ok(None) => {}
Err(_) => break,
}
}
});
connector
}));
// if connector_cache.filter(&connector_def.shortname, &prefix, &virt_addr).await? == FilterOutput::Resource {
// let plan_report = plan_connector(&connector_def.shortname, &connector, &prefix, &virt_addr).await?;
// return Ok(plan_report);
// }
}
Ok(None)
}