-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
pulumi.rs
502 lines (454 loc) · 16.6 KB
/
pulumi.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#![warn(missing_docs)]
use serde::Deserialize;
use sha1::{Digest, Sha1};
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use yaml_rust2::{Yaml, YamlLoader};
use super::{Context, Module, ModuleConfig};
use crate::configs::pulumi::PulumiConfig;
use crate::formatter::{StringFormatter, VersionFormatter};
static PULUMI_HOME: &str = "PULUMI_HOME";
#[derive(Deserialize)]
struct Credentials {
current: Option<String>,
accounts: Option<HashMap<String, Account>>,
}
#[derive(Deserialize)]
struct Account {
username: Option<String>,
}
/// Creates a module with the current Pulumi version and stack name.
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("pulumi");
let config = PulumiConfig::try_load(module.config);
let project_file_in_cwd = context
.try_begin_scan()?
.set_files(&["Pulumi.yaml", "Pulumi.yml"])
.is_match();
if !project_file_in_cwd && !config.search_upwards {
return None;
}
let project_file = find_package_file(&context.logical_dir)?;
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {
"symbol" => Some(config.symbol),
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.map(|variable| match variable {
"version" => {
let stdout = context.exec_cmd("pulumi", &["version"])?.stdout;
VersionFormatter::format_module_version(
module.get_name(),
parse_version(&stdout),
config.version_format,
)
}
.map(Ok),
"username" => get_pulumi_username(context).map(Ok),
"stack" => stack_name(&project_file, context).map(Ok),
_ => None,
})
.parse(None, Some(context))
});
match parsed {
Ok(x) => {
module.set_segments(x);
Some(module)
}
Err(e) => {
log::warn!("Error in module `pulumi`:\n{}", e);
None
}
}
}
/// Parse and sanitize the output of `pulumi version` into just the version string.
///
/// Normally, this just means returning it. When Pulumi is being developed, it
/// can return results like `3.12.0-alpha.1630554544+f89e9a29.dirty`, which we
/// don't want to see. Instead we display that as `3.12.0-alpha`.
fn parse_version(version: &str) -> &str {
let new_version = version.strip_prefix('v').unwrap_or(version);
let sanitized_version = new_version.trim_end();
let mut periods = 0;
for (i, c) in sanitized_version.as_bytes().iter().enumerate() {
if *c == b'.' {
if periods == 2 {
return &sanitized_version[0..i];
} else {
periods += 1;
}
}
}
// We didn't hit 3 periods, so we just return the whole string.
sanitized_version
}
/// Find a file describing a Pulumi package in the current directory (or any parent directory).
fn find_package_file(path: &Path) -> Option<PathBuf> {
for path in path.ancestors() {
log::trace!("Looking for package file in {:?}", path);
let dir = std::fs::read_dir(path).ok()?;
let goal = dir.filter_map(Result::ok).find(|path| {
path.file_name() == OsStr::new("Pulumi.yaml")
|| path.file_name() == OsStr::new("Pulumi.yml")
});
if let Some(goal) = goal {
return Some(goal.path());
}
}
log::trace!("Did not find a Pulumi package file");
None
}
/// We get the name of the current stack.
///
/// Pulumi has no CLI option that is fast enough to get this for us, but finding
/// the location is simple. We get it ourselves.
fn stack_name(project_file: &Path, context: &Context) -> Option<String> {
let mut file = File::open(project_file).ok()?;
let mut contents = String::new();
file.read_to_string(&mut contents).ok()?;
let name = YamlLoader::load_from_str(&contents).ok().and_then(
|yaml| -> Option<Option<String>> {
log::trace!("Parsed {:?} into yaml", project_file);
let yaml = yaml.into_iter().next()?;
yaml.into_hash().map(|mut hash| -> Option<String> {
hash.remove(&Yaml::String("name".to_string()))?
.into_string()
})
},
)??;
log::trace!("Found project name: {:?}", name);
let workspace_file = get_pulumi_workspace(context, &name, project_file)
.map(File::open)?
.ok()?;
log::trace!("Trying to read workspace_file: {:?}", workspace_file);
let workspace: serde_json::Value = match serde_json::from_reader(workspace_file) {
Ok(k) => k,
Err(e) => {
log::debug!("Failed to parse workspace file: {}", e);
return None;
}
};
log::trace!("Read workspace_file: {:?}", workspace);
workspace
.as_object()?
.get("stack")?
.as_str()
.map(ToString::to_string)
}
/// Calculates the path of the workspace settings file for a given pulumi stack.
fn get_pulumi_workspace(context: &Context, name: &str, project_file: &Path) -> Option<PathBuf> {
let project_file = if cfg!(test) {
// Because this depends on the absolute path of the file, it changes in
// each test run. We thus mock it.
"test".to_string()
} else {
let mut hasher = Sha1::new();
hasher.update(project_file.to_str()?.as_bytes());
crate::utils::encode_to_hex(&hasher.finalize())
};
let unique_file_name = format!("{name}-{project_file}-workspace.json");
let mut path = pulumi_home_dir(context)?;
path.push("workspaces");
path.push(unique_file_name);
Some(path)
}
/// Get the Pulumi home directory. We first check `PULUMI_HOME`. If that isn't
/// set, we return `$HOME/.pulumi`.
fn pulumi_home_dir(context: &Context) -> Option<PathBuf> {
if let Some(k) = context.get_env(PULUMI_HOME) {
std::path::PathBuf::from_str(&k).ok()
} else {
context.get_home().map(|p| p.join(".pulumi"))
}
}
fn get_pulumi_username(context: &Context) -> Option<String> {
let home_dir = pulumi_home_dir(context)?;
let creds_path = home_dir.join("credentials.json");
let file = File::open(creds_path).ok()?;
let reader = BufReader::new(file);
// Read the JSON contents of the file as an instance of `User`.
let creds: Credentials = serde_json::from_reader(reader).ok()?;
let current_api_provider = creds.current?;
creds.accounts?.remove(¤t_api_provider)?.username
}
#[cfg(test)]
mod tests {
use std::io;
use super::*;
use crate::context::Target;
use crate::test::ModuleRenderer;
use nu_ansi_term::Color;
#[test]
fn pulumi_version_release() {
let expected = "3.12.0";
let inputs: [&str; 6] = [
"v3.12.0\r\n",
"v3.12.0\n",
"v3.12.0",
"3.12.0\r\n",
"3.12.0\n",
"3.12.0",
];
for input in &inputs {
assert_eq!(parse_version(input), expected);
}
}
#[test]
fn pulumi_version_prerelease() {
let expected = "3.12.0-alpha";
let inputs: [&str; 6] = [
"v3.12.0-alpha\r\n",
"v3.12.0-alpha\n",
"v3.12.0-alpha",
"3.12.0-alpha\r\n",
"3.12.0-alpha\n",
"3.12.0-alpha",
];
for input in &inputs {
assert_eq!(parse_version(input), expected);
}
}
#[test]
fn pulumi_version_dirty() {
let expected = "3.12.0-alpha";
let inputs: [&str; 6] = [
"v3.12.0-alpha.1630554544+f89e9a29.dirty\r\n",
"v3.12.0-alpha.1630554544+f89e9a29.dirty\n",
"v3.12.0-alpha.1630554544+f89e9a29.dirty",
"3.12.0-alpha.1630554544+f89e9a29.dirty\r\n",
"3.12.0-alpha.1630554544+f89e9a29.dirty\n",
"3.12.0-alpha.1630554544+f89e9a29.dirty",
];
for input in &inputs {
assert_eq!(parse_version(input), expected);
}
}
#[test]
fn get_home_dir() {
let mut context = Context::new(Default::default(), Target::Main);
context.env.insert("HOME", "/home/sweet/home".to_string());
assert_eq!(
pulumi_home_dir(&context),
Some(PathBuf::from("/home/sweet/home/.pulumi"))
);
context.env.insert("PULUMI_HOME", "/a/dir".to_string());
assert_eq!(pulumi_home_dir(&context), Some(PathBuf::from("/a/dir")))
}
#[test]
fn test_get_pulumi_workspace() {
let mut context = Context::new(Default::default(), Target::Main);
context.env.insert("HOME", "/home/sweet/home".to_string());
let name = "foobar";
let project_file = PathBuf::from("/hello/Pulumi.yaml");
assert_eq!(
get_pulumi_workspace(&context, name, &project_file),
Some(PathBuf::from(
"/home/sweet/home/.pulumi/workspaces/foobar-test-workspace.json"
))
);
}
#[test]
fn version_render() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let pulumi_file = File::create(dir.path().join("Pulumi.yaml"))?;
pulumi_file.sync_all()?;
let rendered = ModuleRenderer::new("pulumi")
.path(dir.path())
.config(toml::toml! {
[pulumi]
format = "with [$version]($style) "
})
.collect();
dir.close()?;
let expected = format!("with {} ", Color::Fixed(5).bold().paint("v1.2.3-ver"));
assert_eq!(expected, rendered.expect("a result"));
Ok(())
}
#[test]
/// This test confirms a full render. This means finding a Pulumi.yml file,
/// tracing back to the backing workspace settings file, and printing the
/// stack name.
fn render_valid_paths() -> io::Result<()> {
use io::Write;
let dir = tempfile::tempdir()?;
let root = dunce::canonicalize(dir.path())?;
let mut yaml = File::create(root.join("Pulumi.yml"))?;
yaml.write_all("name: starship\nruntime: nodejs\ndescription: A thing\n".as_bytes())?;
yaml.sync_all()?;
let workspace_path = root.join(".pulumi").join("workspaces");
std::fs::create_dir_all(&workspace_path)?;
let workspace_path = &workspace_path.join("starship-test-workspace.json");
let mut workspace = File::create(workspace_path)?;
serde_json::to_writer_pretty(
&mut workspace,
&serde_json::json!(
{
"stack": "launch"
}
),
)?;
workspace.sync_all()?;
let credential_path = root.join(".pulumi");
std::fs::create_dir_all(&credential_path)?;
let credential_path = &credential_path.join("credentials.json");
let mut credential = File::create(credential_path)?;
serde_json::to_writer_pretty(
&mut credential,
&serde_json::json!(
{
"current": "https://api.example.com",
"accessTokens": {
"https://api.example.com": "redacted",
"https://api.pulumi.com": "redacted"
},
"accounts": {
"https://api.example.com": {
"accessToken": "redacted",
"username": "test-user",
"lastValidatedAt": "2022-01-12T00:00:00.000000000-08:00"
}
}
}
),
)?;
credential.sync_all()?;
let rendered = ModuleRenderer::new("pulumi")
.path(root.clone())
.logical_path(root.clone())
.config(toml::toml! {
[pulumi]
format = "via [$symbol($username@)$stack]($style) "
})
.env("HOME", root.to_str().unwrap())
.collect();
let expected = format!(
"via {} ",
Color::Fixed(5).bold().paint(" test-user@launch")
);
assert_eq!(expected, rendered.expect("a result"));
dir.close()?;
Ok(())
}
#[test]
/// This test confirms a render when the account information incomplete, i.e.: no username for
/// the current API.
fn partial_login() -> io::Result<()> {
use io::Write;
let dir = tempfile::tempdir()?;
let root = dunce::canonicalize(dir.path())?;
let mut yaml = File::create(root.join("Pulumi.yml"))?;
yaml.write_all("name: starship\nruntime: nodejs\ndescription: A thing\n".as_bytes())?;
yaml.sync_all()?;
let workspace_path = root.join(".pulumi").join("workspaces");
std::fs::create_dir_all(&workspace_path)?;
let workspace_path = &workspace_path.join("starship-test-workspace.json");
let mut workspace = File::create(workspace_path)?;
serde_json::to_writer_pretty(
&mut workspace,
&serde_json::json!(
{
"stack": "launch"
}
),
)?;
workspace.sync_all()?;
let credential_path = root.join(".pulumi");
std::fs::create_dir_all(&credential_path)?;
let credential_path = &credential_path.join("starship-test-credential.json");
let mut credential = File::create(credential_path)?;
serde_json::to_writer_pretty(
&mut credential,
&serde_json::json!(
{
"current": "https://api.example.com",
"accessTokens": {
"https://api.example.com": "redacted",
"https://api.pulumi.com": "redacted"
},
"accounts": {
}
}
),
)?;
credential.sync_all()?;
let rendered = ModuleRenderer::new("pulumi")
.path(root.clone())
.logical_path(root.clone())
.config(toml::toml! {
[pulumi]
format = "via [$symbol($username@)$stack]($style) "
})
.env("HOME", root.to_str().unwrap())
.collect();
let expected = format!("via {} ", Color::Fixed(5).bold().paint(" launch"));
assert_eq!(expected, rendered.expect("a result"));
dir.close()?;
Ok(())
}
#[test]
fn empty_config_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let yaml = File::create(dir.path().join("Pulumi.yaml"))?;
yaml.sync_all()?;
let rendered = ModuleRenderer::new("pulumi")
.path(dir.path())
.logical_path(dir.path())
.config(toml::toml! {
[pulumi]
format = "in [$symbol($stack)]($style) "
})
.collect();
let expected = format!("in {} ", Color::Fixed(5).bold().paint(" "));
assert_eq!(expected, rendered.expect("a result"));
dir.close()?;
Ok(())
}
#[test]
fn do_not_search_upwards() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let child_dir = dir.path().join("child");
std::fs::create_dir(&child_dir)?;
let yaml = File::create(dir.path().join("Pulumi.yaml"))?;
yaml.sync_all()?;
let actual = ModuleRenderer::new("pulumi")
.path(&child_dir)
.logical_path(&child_dir)
.config(toml::toml! {
[pulumi]
format = "in [$symbol($stack)]($style) "
search_upwards = false
})
.collect();
let expected = None;
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn search_upwards_default() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let child_dir = dir.path().join("child");
std::fs::create_dir(&child_dir)?;
let yaml = File::create(dir.path().join("Pulumi.yaml"))?;
yaml.sync_all()?;
let actual = ModuleRenderer::new("pulumi")
.path(&child_dir)
.logical_path(&child_dir)
.config(toml::toml! {
[pulumi]
format = "in [$symbol($stack)]($style) "
})
.collect();
let expected = Some(format!("in {} ", Color::Fixed(5).bold().paint(" ")));
assert_eq!(expected, actual);
dir.close()
}
}