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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
// SPDX-FileCopyrightText: 2020 Serokell <https://serokell.io/>
// SPDX-FileCopyrightText: 2020 Andreas Fuchs <asf@boinkor.net>
// SPDX-FileCopyrightText: 2021 Yannik Sander <contact@ysndr.de>
//
// SPDX-License-Identifier: MPL-2.0
use rnix::{types::*, SyntaxKind::*};
use merge::Merge;
use thiserror::Error;
use flexi_logger::*;
pub fn make_lock_path(temp_path: &str, closure: &str) -> String {
let lock_hash =
&closure["/nix/store/".len()..closure.find('-').unwrap_or_else(|| closure.len())];
format!("{}/deploy-rs-canary-{}", temp_path, lock_hash)
}
const fn make_emoji(level: log::Level) -> &'static str {
match level {
log::Level::Error => "❌",
log::Level::Warn => "⚠️",
log::Level::Info => "ℹ️",
log::Level::Debug => "❓",
log::Level::Trace => "🖊️",
}
}
pub fn logger_formatter_activate(
w: &mut dyn std::io::Write,
_now: &mut DeferredNow,
record: &Record,
) -> Result<(), std::io::Error> {
let level = record.level();
write!(
w,
"⭐ {} [activate] [{}] {}",
make_emoji(level),
style(level, level.to_string()),
record.args()
)
}
pub fn logger_formatter_wait(
w: &mut dyn std::io::Write,
_now: &mut DeferredNow,
record: &Record,
) -> Result<(), std::io::Error> {
let level = record.level();
write!(
w,
"👀 {} [wait] [{}] {}",
make_emoji(level),
style(level, level.to_string()),
record.args()
)
}
pub fn logger_formatter_revoke(
w: &mut dyn std::io::Write,
_now: &mut DeferredNow,
record: &Record,
) -> Result<(), std::io::Error> {
let level = record.level();
write!(
w,
"↩️ {} [revoke] [{}] {}",
make_emoji(level),
style(level, level.to_string()),
record.args()
)
}
pub fn logger_formatter_deploy(
w: &mut dyn std::io::Write,
_now: &mut DeferredNow,
record: &Record,
) -> Result<(), std::io::Error> {
let level = record.level();
write!(
w,
"🚀 {} [deploy] [{}] {}",
make_emoji(level),
style(level, level.to_string()),
record.args()
)
}
pub enum LoggerType {
Deploy,
Activate,
Wait,
Revoke,
}
pub fn init_logger(
debug_logs: bool,
log_dir: Option<&str>,
logger_type: &LoggerType,
) -> Result<(), FlexiLoggerError> {
let logger_formatter = match &logger_type {
LoggerType::Deploy => logger_formatter_deploy,
LoggerType::Activate => logger_formatter_activate,
LoggerType::Wait => logger_formatter_wait,
LoggerType::Revoke => logger_formatter_revoke,
};
if let Some(log_dir) = log_dir {
let mut logger = Logger::with_env_or_str("debug")
.log_to_file()
.format_for_stderr(logger_formatter)
.set_palette("196;208;51;7;8".to_string())
.directory(log_dir)
.duplicate_to_stderr(match debug_logs {
true => Duplicate::Debug,
false => Duplicate::Info,
})
.print_message();
match logger_type {
LoggerType::Activate => logger = logger.discriminant("activate"),
LoggerType::Wait => logger = logger.discriminant("wait"),
LoggerType::Revoke => logger = logger.discriminant("revoke"),
LoggerType::Deploy => (),
}
logger.start()?;
} else {
Logger::with_env_or_str(match debug_logs {
true => "debug",
false => "info",
})
.log_target(LogTarget::StdErr)
.format(logger_formatter)
.set_palette("196;208;51;7;8".to_string())
.start()?;
}
Ok(())
}
pub mod data;
pub mod deploy;
pub mod push;
pub mod cli;
#[derive(Debug)]
pub struct CmdOverrides {
pub ssh_user: Option<String>,
pub profile_user: Option<String>,
pub ssh_opts: Option<String>,
pub fast_connection: Option<bool>,
pub auto_rollback: Option<bool>,
pub hostname: Option<String>,
pub magic_rollback: Option<bool>,
pub temp_path: Option<String>,
pub confirm_timeout: Option<u16>,
pub sudo: Option<String>,
pub dry_activate: bool,
pub remote_build: bool,
}
#[derive(PartialEq, Debug)]
pub struct DeployFlake<'a> {
pub repo: &'a str,
pub node: Option<String>,
pub profile: Option<String>,
}
#[derive(Error, Debug)]
pub enum ParseFlakeError {
#[error("The given path was too long, did you mean to put something in quotes?")]
PathTooLong,
#[error("Unrecognized node or token encountered")]
Unrecognized,
}
pub fn parse_flake(flake: &str) -> Result<DeployFlake, ParseFlakeError> {
let flake_fragment_start = flake.find('#');
let (repo, maybe_fragment) = match flake_fragment_start {
Some(s) => (&flake[..s], Some(&flake[s + 1..])),
None => (flake, None),
};
let mut node: Option<String> = None;
let mut profile: Option<String> = None;
if let Some(fragment) = maybe_fragment {
let ast = rnix::parse(fragment);
let first_child = match ast.root().node().first_child() {
Some(x) => x,
None => {
return Ok(DeployFlake {
repo,
node: None,
profile: None,
})
}
};
let mut node_over = false;
for entry in first_child.children_with_tokens() {
let x: Option<String> = match (entry.kind(), node_over) {
(TOKEN_DOT, false) => {
node_over = true;
None
}
(TOKEN_DOT, true) => {
return Err(ParseFlakeError::PathTooLong);
}
(NODE_IDENT, _) => Some(entry.into_node().unwrap().text().to_string()),
(TOKEN_IDENT, _) => Some(entry.into_token().unwrap().text().to_string()),
(NODE_STRING, _) => {
let c = entry
.into_node()
.unwrap()
.children_with_tokens()
.nth(1)
.unwrap();
Some(c.into_token().unwrap().text().to_string())
}
_ => return Err(ParseFlakeError::Unrecognized),
};
if !node_over {
node = x;
} else {
profile = x;
}
}
}
Ok(DeployFlake {
repo,
node,
profile,
})
}
#[test]
fn test_parse_flake() {
assert_eq!(
parse_flake("../deploy/examples/system").unwrap(),
DeployFlake {
repo: "../deploy/examples/system",
node: None,
profile: None,
}
);
assert_eq!(
parse_flake("../deploy/examples/system#").unwrap(),
DeployFlake {
repo: "../deploy/examples/system",
node: None,
profile: None,
}
);
assert_eq!(
parse_flake("../deploy/examples/system#computer.\"something.nix\"").unwrap(),
DeployFlake {
repo: "../deploy/examples/system",
node: Some("computer".to_string()),
profile: Some("something.nix".to_string()),
}
);
assert_eq!(
parse_flake("../deploy/examples/system#\"example.com\".system").unwrap(),
DeployFlake {
repo: "../deploy/examples/system",
node: Some("example.com".to_string()),
profile: Some("system".to_string()),
}
);
assert_eq!(
parse_flake("../deploy/examples/system#example").unwrap(),
DeployFlake {
repo: "../deploy/examples/system",
node: Some("example".to_string()),
profile: None
}
);
assert_eq!(
parse_flake("../deploy/examples/system#example.system").unwrap(),
DeployFlake {
repo: "../deploy/examples/system",
node: Some("example".to_string()),
profile: Some("system".to_string())
}
);
assert_eq!(
parse_flake("../deploy/examples/system").unwrap(),
DeployFlake {
repo: "../deploy/examples/system",
node: None,
profile: None,
}
);
}
#[derive(Debug, Clone)]
pub struct DeployData<'a> {
pub node_name: &'a str,
pub node: &'a data::Node,
pub profile_name: &'a str,
pub profile: &'a data::Profile,
pub cmd_overrides: &'a CmdOverrides,
pub merged_settings: data::GenericSettings,
pub debug_logs: bool,
pub log_dir: Option<&'a str>,
}
#[derive(Debug)]
pub struct DeployDefs {
pub ssh_user: String,
pub profile_user: String,
pub profile_path: String,
pub sudo: Option<String>,
}
#[derive(Error, Debug)]
pub enum DeployDataDefsError {
#[error("Neither `user` nor `sshUser` are set for profile {0} of node {1}")]
NoProfileUser(String, String),
}
impl<'a> DeployData<'a> {
pub fn defs(&'a self) -> Result<DeployDefs, DeployDataDefsError> {
let ssh_user = match self.merged_settings.ssh_user {
Some(ref u) => u.clone(),
None => whoami::username(),
};
let profile_user = self.get_profile_user()?;
let profile_path = self.get_profile_path()?;
let sudo: Option<String> = match self.merged_settings.user {
Some(ref user) if user != &ssh_user => Some(format!("{} {}", self.get_sudo(), user)),
_ => None,
};
Ok(DeployDefs {
ssh_user,
profile_user,
profile_path,
sudo,
})
}
fn get_profile_path(&'a self) -> Result<String, DeployDataDefsError> {
let profile_user = self.get_profile_user()?;
let profile_path = match self.profile.profile_settings.profile_path {
None => match &profile_user[..] {
"root" => format!("/nix/var/nix/profiles/{}", self.profile_name),
_ => format!(
"/nix/var/nix/profiles/per-user/{}/{}",
profile_user, self.profile_name
),
},
Some(ref x) => x.clone(),
};
Ok(profile_path)
}
fn get_profile_user(&'a self) -> Result<String, DeployDataDefsError> {
let profile_user = match self.merged_settings.user {
Some(ref x) => x.clone(),
None => match self.merged_settings.ssh_user {
Some(ref x) => x.clone(),
None => {
return Err(DeployDataDefsError::NoProfileUser(
self.profile_name.to_owned(),
self.node_name.to_owned(),
))
}
},
};
Ok(profile_user)
}
fn get_sudo(&'a self) -> String {
match self.merged_settings.sudo {
Some(ref x) => x.clone(),
None => "sudo -u".to_string(),
}
}
}
pub fn make_deploy_data<'a, 's>(
top_settings: &'s data::GenericSettings,
node: &'a data::Node,
node_name: &'a str,
profile: &'a data::Profile,
profile_name: &'a str,
cmd_overrides: &'a CmdOverrides,
debug_logs: bool,
log_dir: Option<&'a str>,
) -> DeployData<'a> {
let mut merged_settings = profile.generic_settings.clone();
merged_settings.merge(node.generic_settings.clone());
merged_settings.merge(top_settings.clone());
// build all machines remotely when the command line flag is set
if cmd_overrides.remote_build {
merged_settings.remote_build = Some(cmd_overrides.remote_build);
}
if cmd_overrides.ssh_user.is_some() {
merged_settings.ssh_user = cmd_overrides.ssh_user.clone();
}
if cmd_overrides.profile_user.is_some() {
merged_settings.user = cmd_overrides.profile_user.clone();
}
if let Some(ref ssh_opts) = cmd_overrides.ssh_opts {
merged_settings.ssh_opts = ssh_opts.split(' ').map(|x| x.to_owned()).collect();
}
if let Some(fast_connection) = cmd_overrides.fast_connection {
merged_settings.fast_connection = Some(fast_connection);
}
if let Some(auto_rollback) = cmd_overrides.auto_rollback {
merged_settings.auto_rollback = Some(auto_rollback);
}
if let Some(magic_rollback) = cmd_overrides.magic_rollback {
merged_settings.magic_rollback = Some(magic_rollback);
}
DeployData {
node_name,
node,
profile_name,
profile,
cmd_overrides,
merged_settings,
debug_logs,
log_dir,
}
}
|