-
Notifications
You must be signed in to change notification settings - Fork 315
/
main.rs
223 lines (177 loc) · 6.17 KB
/
main.rs
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
use std::os::unix::fs::PermissionsExt;
use std::{
assert,
fs::{self, File, Permissions},
io::Write,
net::Ipv6Addr,
path::{Path, PathBuf},
};
use anyhow::{Context, Error};
use clap::{Args, Parser};
use tempfile::NamedTempFile;
use url::Url;
use partition_tools::{ext::ExtPartition, fat::FatPartition, Partition};
use utils::deployment::DeploymentJson;
const SERVICE_NAME: &str = "setupos-inject-configuration";
#[derive(Parser)]
#[command(name = SERVICE_NAME)]
struct Cli {
#[arg(long, default_value = "disk.img")]
image_path: PathBuf,
#[command(flatten)]
network: NetworkConfig,
#[arg(long)]
private_key_path: Option<PathBuf>,
#[arg(long, value_delimiter = ',')]
public_keys: Option<Vec<String>>,
#[command(flatten)]
deployment: DeploymentConfig,
}
#[derive(Args)]
struct NetworkConfig {
#[arg(long)]
ipv6_prefix: Option<String>,
#[arg(long)]
ipv6_gateway: Option<Ipv6Addr>,
#[arg(long)]
mgmt_mac: Option<String>,
}
#[derive(Args)]
struct DeploymentConfig {
#[arg(long)]
nns_url: Option<Url>,
#[arg(long, allow_hyphen_values = true)]
nns_public_key: Option<String>,
#[arg(long)]
memory_gb: Option<u32>,
#[arg(long)]
cpu_mode: Option<String>,
}
#[tokio::main]
async fn main() -> Result<(), Error> {
let cli = Cli::parse();
// Open config partition
let mut config = FatPartition::open(cli.image_path.clone(), 3).await?;
// Print previous config.ini
println!("Previous config.ini:\n---");
let previous_config = config
.read_file(Path::new("/config.ini"))
.await
.context("failed to print previous config")?;
println!("{previous_config}");
// Update config.ini
let config_ini = NamedTempFile::new()?;
write_config(config_ini.path(), &cli.network)
.await
.context("failed to write config file")?;
config
.write_file(config_ini.path(), Path::new("/config.ini"))
.await
.context("failed to copy config file")?;
// Update node-provider private-key
if let Some(private_key_path) = cli.private_key_path {
config
.write_file(
&private_key_path,
Path::new("/node_operator_private_key.pem"),
)
.await
.context("failed to copy private-key")?;
}
// Print previous public keys
println!("Previous ssh_authorized_keys/admin:\n---");
let previous_admin_keys = config
.read_file(Path::new("ssh_authorized_keys/admin"))
.await
.context("failed to print previous config")?;
println!("{previous_admin_keys}");
// Update SSH keys
if let Some(ks) = cli.public_keys {
let public_keys = NamedTempFile::new()?;
write_public_keys(public_keys.path(), ks)
.await
.context("failed to write public keys")?;
config
.write_file(public_keys.path(), Path::new("/ssh_authorized_keys/admin"))
.await
.context("failed to copy public keys")?;
}
// Close config partition
config.close().await?;
// Open data partition
let mut data = ExtPartition::open(cli.image_path.clone(), 4).await?;
// Print previous deployment.json
println!("Previous deployment.json:\n---");
let previous_deployment = data
.read_file(Path::new("/deployment.json"))
.await
.context("failed to print previous deployment config")?;
println!("{previous_deployment}");
// Update deployment.json
let mut deployment_json = NamedTempFile::new()?;
deployment_json.write_all(previous_deployment.as_bytes())?;
fs::set_permissions(deployment_json.path(), Permissions::from_mode(0o644))?;
update_deployment(deployment_json.path(), &cli.deployment)
.await
.context("failed to write deployment config file")?;
data.write_file(deployment_json.path(), Path::new("/deployment.json"))
.await
.context("failed to copy deployment config file")?;
// Update NNS key
if let Some(public_key) = cli.deployment.nns_public_key {
let mut nns_key = NamedTempFile::new()?;
write!(&mut nns_key, "{public_key}")?;
fs::set_permissions(nns_key.path(), Permissions::from_mode(0o644))?;
data.write_file(nns_key.path(), Path::new("/nns_public_key.pem"))
.await
.context("failed to copy nns key file")?;
}
// Close data partition
data.close().await?;
Ok(())
}
async fn write_config(path: &Path, cfg: &NetworkConfig) -> Result<(), Error> {
let mut f = File::create(path).context("failed to create config file")?;
let NetworkConfig {
ipv6_prefix,
ipv6_gateway,
mgmt_mac,
} = cfg;
if let (Some(ipv6_prefix), Some(ipv6_gateway)) = (ipv6_prefix, ipv6_gateway) {
// Always write 4 segments, even if our prefix is less.
assert!(format!("{ipv6_prefix}::").parse::<Ipv6Addr>().is_ok());
writeln!(&mut f, "ipv6_prefix={}", ipv6_prefix)?;
writeln!(&mut f, "ipv6_gateway={}", ipv6_gateway)?;
}
if let Some(mgmt_mac) = mgmt_mac {
writeln!(&mut f, "mgmt_mac={}", mgmt_mac)?;
}
Ok(())
}
async fn write_public_keys(path: &Path, ks: Vec<String>) -> Result<(), Error> {
let mut f = File::create(path).context("failed to create public keys file")?;
for k in ks {
writeln!(&mut f, "{k}")?;
}
Ok(())
}
async fn update_deployment(path: &Path, cfg: &DeploymentConfig) -> Result<(), Error> {
let mut deployment_json = {
let f = File::open(path).context("failed to open deployment config file")?;
let deployment_json: DeploymentJson = serde_json::from_reader(f)?;
deployment_json
};
if let Some(nns_url) = &cfg.nns_url {
deployment_json.nns.url = nns_url.clone();
}
if let Some(memory) = cfg.memory_gb {
deployment_json.resources.memory = memory;
}
if let Some(cpu_mode) = &cfg.cpu_mode {
deployment_json.resources.cpu = Some(cpu_mode.to_owned());
}
let mut f = File::create(path).context("failed to open deployment config file")?;
let output = serde_json::to_string_pretty(&deployment_json)?;
write!(&mut f, "{output}")?;
Ok(())
}