-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtool_cache.rs
438 lines (378 loc) · 13.7 KB
/
tool_cache.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
use std::{
collections::{BTreeSet, HashMap},
env::consts::EXE_SUFFIX,
io::Cursor,
path::PathBuf,
};
use semver::Version;
use serde::{Deserialize, Serialize};
use zip::ZipArchive;
use crate::{
artifact_choosing::platform_keywords,
ci_string::CiString,
config::ToolSpec,
error::{ForemanError, ForemanResult},
fs,
paths::ForemanPaths,
process,
tool_provider::{Release, ToolProvider},
};
fn choose_asset(release: &Release, platform_keywords: &[&str]) -> Option<usize> {
log::trace!(
"Checking for name with compatible os/arch pair from platform-derived list: {:?}",
platform_keywords
);
let asset_index = platform_keywords.iter().find_map(|keyword| {
release
.assets
.iter()
.position(|asset| asset.name.contains(keyword))
})?;
log::debug!(
"Found matching artifact: {}",
release.assets[asset_index].name
);
Some(asset_index)
}
/// Contains the current state of all of the tools that Foreman manages.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ToolCache {
pub tools: HashMap<CiString, ToolEntry>,
#[serde(skip)]
paths: ForemanPaths,
}
impl ToolCache {
pub fn new(paths: &ForemanPaths) -> Self {
Self {
tools: Default::default(),
paths: paths.clone(),
}
}
pub fn run(&self, tool: &ToolSpec, version: &Version, args: Vec<String>) -> ForemanResult<i32> {
let tool_path = self.get_tool_exe_path(tool, version);
log::debug!("Running tool {} ({})", tool, tool_path.display());
let code = process::run(&tool_path, args).map_err(|err| {
ForemanError::io_error_with_context(
err,
format!(
"an error happened trying to run `{}` at `{}` (this is an error in Foreman)",
tool,
tool_path.display()
),
)
})?;
Ok(code)
}
pub fn download_if_necessary(
&mut self,
tool: &ToolSpec,
providers: &ToolProvider,
) -> ForemanResult<Version> {
if let Some(tool_entry) = self.tools.get(&tool.cache_key()) {
log::debug!("Tool has some versions installed");
let matching_version = tool_entry
.versions
.iter()
.rev()
.find(|version| tool.version().matches(version));
if let Some(version) = matching_version {
return Ok(version.clone());
}
}
self.download(tool, providers)
}
pub fn download(
&mut self,
tool: &ToolSpec,
providers: &ToolProvider,
) -> ForemanResult<Version> {
log::info!("Downloading {}", tool);
let provider = providers.get(&tool.provider());
let releases = provider.get_releases(tool.path(), tool.host())?;
// Filter down our set of releases to those that are valid versions and
// have release assets for our current platform.
let mut semver_releases: Vec<_> = releases
.into_iter()
.filter_map(|release| {
log::trace!("Evaluating tag {}", release.tag_name);
let version = Version::parse(&release.tag_name).ok().or_else(|| {
if !release.tag_name.starts_with('v') {
log::debug!(
"Release tag name did not start with 'v'! {}",
release.tag_name
);
return None;
}
Version::parse(&release.tag_name[1..]).ok()
})?;
let asset_index = choose_asset(&release, platform_keywords())?;
Some((version, asset_index, release))
})
.collect();
// Releases should come back chronological, but we want strictly
// descending version numbers.
semver_releases.sort_by(|a, b| b.0.cmp(&a.0));
let version_req = tool.version();
let matching_release = semver_releases
.iter()
.find(|(version, _asset_index, _release)| version_req.matches(version));
if let Some((version, asset_index, release)) = matching_release {
log::trace!("Picked version {}", version);
let url = &release.assets[*asset_index].url;
let buffer = provider.download_asset(url)?;
log::trace!("Extracting downloaded artifact");
let mut archive = ZipArchive::new(Cursor::new(&buffer)).map_err(|err| {
ForemanError::invalid_release_asset(
tool,
version,
format!("unable to open zip archive ({})", err),
)
})?;
let mut file = archive.by_index(0).map_err(|err| {
ForemanError::invalid_release_asset(
tool,
version,
format!("unable to obtain file from zip archive ({})", err),
)
})?;
let tool_path = self.get_tool_exe_path(tool, version);
fs::copy_from_reader(&mut file, &tool_path)?;
// On Unix systems, mark the tool as executable.
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(&tool_path, fs::Permissions::from_mode(0o777))?;
}
log::trace!("Updating tool cache");
let tool_entry = self.tools.entry(tool.cache_key()).or_default();
tool_entry.versions.insert(version.clone());
self.save()?;
Ok(version.clone())
} else {
Err(ForemanError::no_compatible_version_found(
tool,
semver_releases
.into_iter()
.map(|(version, _asset_index, _release)| version)
.collect(),
))
}
}
pub fn load(paths: &ForemanPaths) -> ForemanResult<Self> {
let path = paths.index_file();
log::debug!("load tool cache from {}", path.display());
let mut tool_cache = fs::try_read(&path)?
.map(|contents| {
serde_json::from_slice(&contents)
.map_err(|err| ForemanError::tool_cache_parsing(&path, err.to_string()))
})
.unwrap_or_else(|| Ok(Self::new(paths)))?;
tool_cache.paths = paths.clone();
Ok(tool_cache)
}
fn save(&self) -> ForemanResult<()> {
let serialized =
serde_json::to_string_pretty(self).expect("unable to serialize tool cache");
fs::write(self.paths.index_file(), serialized)
}
fn get_tool_exe_path(&self, tool: &ToolSpec, version: &Version) -> PathBuf {
let mut tool_path = self.paths.tools_dir();
let exe_name = tool_identifier_to_exe_name(tool, version);
tool_path.push(exe_name);
tool_path
}
}
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ToolEntry {
pub versions: BTreeSet<Version>,
}
fn tool_identifier_to_exe_name(tool: &ToolSpec, version: &Version) -> String {
let mut name = format!("{}-{}{}", tool.cache_key().0, version, EXE_SUFFIX);
name = name.replace('/', "__");
name.replace('\\', "__")
}
#[cfg(test)]
mod test {
use tempfile::tempdir;
use crate::tool_provider::ReleaseAsset;
use super::*;
// Regression test for LUAFDN-1041, based on the release that surfaced it
#[test]
fn select_correct_asset() {
let release = Release {
prerelease: false,
tag_name: "v0.5.2".to_string(),
assets: vec![
ReleaseAsset {
name: "tool-linux.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/1".to_string(),
},
ReleaseAsset {
name: "tool-macos-arm64.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/2".to_string(),
},
ReleaseAsset {
name: "tool-macos-x86_64.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/3".to_string(),
},
ReleaseAsset {
name: "tool-win64.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/4".to_string(),
},
],
};
assert_eq!(
choose_asset(&release, &["win32", "win64", "windows"]),
Some(3)
);
assert_eq!(
choose_asset(
&release,
&["macos-x86_64", "darwin-x86_64", "macos", "darwin"]
),
Some(2)
);
assert_eq!(
choose_asset(
&release,
&[
"macos-arm64",
"darwin-arm64",
"macos-x86_64",
"darwin-x86_64",
"macos",
"darwin",
]
),
Some(1)
);
assert_eq!(choose_asset(&release, &["linux"]), Some(0));
}
#[test]
fn select_correct_asset_macos() {
let release = Release {
prerelease: false,
tag_name: "v0.5.2".to_string(),
assets: vec![
ReleaseAsset {
name: "tool-linux.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/1".to_string(),
},
ReleaseAsset {
name: "tool-macos-x86_64.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/2".to_string(),
},
ReleaseAsset {
name: "tool-macos-aarch64.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/3".to_string(),
},
],
};
assert_eq!(
choose_asset(
&release,
&[
"macos-arm64",
"darwin-arm64",
"macos-aarch64",
"darwin-aarch64",
"macos",
"darwin",
]
),
Some(2)
);
}
#[test]
fn select_correct_asset_linux() {
let release = Release {
prerelease: false,
tag_name: "v0.5.2".to_string(),
assets: vec![
ReleaseAsset {
name: "tool-linux-aarch64.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/1".to_string(),
},
ReleaseAsset {
name: "tool-linux-x86_64.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/2".to_string(),
},
ReleaseAsset {
name: "tool-macos-x86_64.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/3".to_string(),
},
ReleaseAsset {
name: "tool-win64.zip".to_string(),
url: "https://example.com/some/repo/releases/assets/4".to_string(),
},
],
};
assert_eq!(choose_asset(&release, &["linux"]), Some(0));
assert_eq!(choose_asset(&release, &["linux-x86_64", "linux"]), Some(1));
assert_eq!(
choose_asset(&release, &["linux-arm64", "linux-aarch64", "linux"]),
Some(0)
);
}
mod load {
use super::*;
#[test]
fn use_default_when_tool_cache_file_does_not_exist() {
let foreman_root = tempdir().expect("unable to create temporary directory");
let paths = ForemanPaths::new(foreman_root.into_path());
let cache = ToolCache::load(&paths).unwrap();
assert_eq!(cache, ToolCache::new(&paths));
}
#[test]
fn reads_the_content_from_the_cache_file() {
let foreman_root = tempdir().expect("unable to create temporary directory");
let paths = ForemanPaths::new(foreman_root.into_path());
fs::write(
paths.index_file(),
r#"
{
"tools": {
"username/toolname": {
"versions": [
"0.1.0"
]
}
}
}
"#,
)
.unwrap();
let cache = ToolCache::load(&paths).unwrap();
let mut expected_cache = ToolCache::new(&paths);
expected_cache.tools.insert(
"username/toolname".into(),
ToolEntry {
versions: {
let mut tree = BTreeSet::new();
tree.insert(Version::parse("0.1.0").unwrap());
tree
},
},
);
assert_eq!(cache, expected_cache);
}
}
mod save {
use super::*;
#[test]
fn snapshot_default_tool_cache() {
let foreman_root = tempdir().expect("unable to create temporary directory");
let paths = ForemanPaths::new(foreman_root.into_path());
let cache = ToolCache::new(&paths);
cache.save().unwrap();
let content = fs::try_read_to_string(paths.index_file())
.unwrap()
.expect("unable to find tool cache file");
insta::assert_snapshot!(&content, @r###"
{
"tools": {}
}
"###);
}
}
}