Skip to content

Commit b3fb5ab

Browse files
matthewgrossmanelezar
authored andcommitted
refactor(tmachine): simplify the layer-key command
Print the setup-layer hash for every environment instead of `none` for environments without setup playbooks, and drop the workflow checks for it. Expose the existing hash function directly, and drop the tests and the Ansible hashing split that only they needed. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent d1c5b7d commit b3fb5ab

5 files changed

Lines changed: 17 additions & 107 deletions

File tree

‎.github/workflows/e2e-suites.yml‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ jobs:
147147
with:
148148
cachix-auth-token: ${{ secrets.CACHIX_AUTH_TOKEN }}
149149

150-
# The hash tmachine names the environment's setup layer after, or `none`
151-
# when the environment has no setup layer. It reads the base image, which
152-
# the run step needs anyway, so this mostly moves that download earlier.
150+
# The hash tmachine names the environment's setup layer after. It reads
151+
# the base image, which the run step needs anyway, so this mostly moves
152+
# that download earlier.
153153
- name: Compute the tmachine setup layer key
154154
id: layer-key
155155
env:
@@ -161,7 +161,6 @@ jobs:
161161
echo "key=${key}" >> "$GITHUB_OUTPUT"
162162
163163
- name: Cache tmachine disks
164-
if: steps.layer-key.outputs.key != 'none'
165164
uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5.1.0
166165
with:
167166
# Only the setup layers, which depend on the environment alone. The

‎.github/workflows/integration-runner.yml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ jobs:
6464
6565
- uses: ./.github/actions/setup-nix
6666

67-
# The hash tmachine names the environment's setup layer after, or `none`
68-
# when the environment has no setup layer. See e2e-suites.yml.
67+
# The hash tmachine names the environment's setup layer after. See
68+
# e2e-suites.yml.
6969
- name: Compute the tmachine setup layer key
7070
id: layer-key
7171
env:
@@ -79,7 +79,6 @@ jobs:
7979
# Only the setup layers: install layers under disks/install hold this
8080
# commit's build, so no later run could use them.
8181
- name: Cache tmachine disks
82-
if: steps.layer-key.outputs.key != 'none'
8382
uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5.1.0
8483
with:
8584
path: ~/.cache/tmachine/disks/setup/*.qcow2

‎tests/tmachine/src/main.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ enum Command {
2323
Setup {
2424
environment: String,
2525
},
26-
/// Print the key of the environment's setup layer, or `none` when it has
27-
/// no setup playbooks, without starting a VM.
26+
/// Print the hash that names the environment's setup layer, without
27+
/// starting a VM.
2828
LayerKey {
2929
environment: String,
3030
},
@@ -58,8 +58,7 @@ async fn run(cli: Cli) -> Result<()> {
5858
}
5959
Command::LayerKey { environment } => {
6060
let (machine, environment) = find_environment(&config, &environment)?;
61-
let key = setup_layer_key(&machine, &environment)?;
62-
println!("{}", key.as_deref().unwrap_or("none"));
61+
println!("{}", setup_layer_key(&machine, &environment)?);
6362
}
6463
Command::Install {
6564
environment,

‎tests/tmachine/src/qemu/ansible_hash.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ use super::layer::hash_file;
1212

1313
pub(super) fn hash_sources(hasher: &mut Hasher) -> Result<()> {
1414
let requirements = crate::ansible::requirements_path();
15-
hash_sources_in(hasher, requirements.parent().unwrap())
16-
}
17-
18-
pub(super) fn hash_sources_in(hasher: &mut Hasher, root: &Path) -> Result<()> {
15+
let root = requirements.parent().unwrap();
1916
hash_tree(hasher, root, root)
2017
}
2118

‎tests/tmachine/src/qemu/setup.rs‎

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::path::{Path, PathBuf};
4+
use std::path::PathBuf;
55

66
use anyhow::{Context, Result};
77
use blake3::Hasher;
88

99
use crate::config::{Environment, Machine};
1010

11-
use super::ansible_hash::hash_sources_in;
11+
use super::ansible_hash::hash_sources;
1212
use super::layer::{SETUP_DISKS, cached_layer, hash_file, hash_files, hash_inputs};
1313

1414
const SETUP_CACHE_VERSION: &[u8] = b"tmachine-disk-blake3-v1";
1515

1616
pub async fn setup(machine: &Machine, environment: &Environment) -> Result<PathBuf> {
17-
let Some(hash) = setup_layer_key(machine, environment)? else {
17+
if environment.setup.playbooks.is_empty() {
1818
return Ok(machine.base_image.clone());
19-
};
19+
}
2020

21+
let hash = setup_layer_key(machine, environment)?;
2122
cached_layer(
2223
SETUP_DISKS,
2324
&machine.base_image,
@@ -29,23 +30,14 @@ pub async fn setup(machine: &Machine, environment: &Environment) -> Result<PathB
2930
.await
3031
}
3132

32-
/// The hash that names the environment's setup layer (`disks/<hash>.qcow2`),
33-
/// or `None` when it has no setup playbooks and so runs on the base image.
34-
pub fn setup_layer_key(machine: &Machine, environment: &Environment) -> Result<Option<String>> {
35-
if environment.setup.playbooks.is_empty() {
36-
return Ok(None);
37-
}
38-
let requirements = crate::ansible::requirements_path();
39-
setup_hash(machine, environment, requirements.parent().unwrap()).map(Some)
40-
}
41-
42-
fn setup_hash(machine: &Machine, environment: &Environment, ansible: &Path) -> Result<String> {
33+
/// The hash that names the environment's setup layer (`disks/<hash>.qcow2`).
34+
pub fn setup_layer_key(machine: &Machine, environment: &Environment) -> Result<String> {
4335
let mut hasher = Hasher::new();
4436
hasher.update(SETUP_CACHE_VERSION);
4537
hash_file(&mut hasher, &machine.base_image)
4638
.with_context(|| format!("failed to hash base image for machine {:?}", machine.name))?;
4739
hasher.update(&[u8::from(environment.setup.use_galaxy)]);
48-
hash_sources_in(&mut hasher, ansible).context("failed to hash Ansible sources")?;
40+
hash_sources(&mut hasher).context("failed to hash Ansible sources")?;
4941
hash_files(&mut hasher, &environment.setup.playbooks)
5042
.context("failed to hash setup playbooks")?;
5143
// Skipped when empty so environments without inputs keep their cache keys.
@@ -54,79 +46,3 @@ fn setup_hash(machine: &Machine, environment: &Environment, ansible: &Path) -> R
5446
}
5547
Ok(hasher.finalize().to_hex().to_string())
5648
}
57-
58-
#[cfg(test)]
59-
mod tests {
60-
use std::collections::BTreeMap;
61-
use std::fs;
62-
63-
use super::*;
64-
use crate::config::Setup;
65-
66-
fn environment(playbooks: Vec<PathBuf>, inputs: BTreeMap<String, PathBuf>) -> Environment {
67-
Environment {
68-
name: "test".into(),
69-
machine: "test".into(),
70-
setup: Setup {
71-
use_galaxy: false,
72-
playbooks,
73-
inputs,
74-
},
75-
}
76-
}
77-
78-
#[test]
79-
fn an_environment_without_setup_playbooks_has_no_layer() {
80-
let machine = Machine {
81-
name: "test".into(),
82-
base_image: "/nonexistent/base.qcow2".into(),
83-
};
84-
let key = setup_layer_key(&machine, &environment(Vec::new(), BTreeMap::new())).unwrap();
85-
assert_eq!(key, None);
86-
}
87-
88-
#[test]
89-
fn the_setup_hash_follows_every_setup_input() {
90-
let dir = tempfile::tempdir().unwrap();
91-
let ansible = dir.path().join("ansible");
92-
fs::create_dir(&ansible).unwrap();
93-
fs::write(ansible.join("requirements.yaml"), "[]").unwrap();
94-
let base_image = dir.path().join("base.qcow2");
95-
fs::write(&base_image, "base").unwrap();
96-
let playbook = dir.path().join("setup.yaml");
97-
fs::write(&playbook, "- hosts: all").unwrap();
98-
let input = dir.path().join("realm.json");
99-
fs::write(&input, "{}").unwrap();
100-
101-
let machine = Machine {
102-
name: "test".into(),
103-
base_image: base_image.clone(),
104-
};
105-
let environment = environment(
106-
vec![playbook.clone()],
107-
BTreeMap::from([("realm".into(), input.clone())]),
108-
);
109-
let hash = || setup_hash(&machine, &environment, &ansible).unwrap();
110-
111-
let original = hash();
112-
assert_eq!(original.len(), 64);
113-
assert_eq!(hash(), original);
114-
115-
let mut seen = vec![original];
116-
for (file, contents) in [
117-
(&input, "{\"realm\": 1}"),
118-
(&playbook, "- hosts: guest"),
119-
(&ansible.join("requirements.yaml"), "[role]"),
120-
(&base_image, "rebuilt base"),
121-
] {
122-
fs::write(file, contents).unwrap();
123-
let changed = hash();
124-
assert!(
125-
!seen.contains(&changed),
126-
"{} did not change the hash",
127-
file.display()
128-
);
129-
seen.push(changed);
130-
}
131-
}
132-
}

0 commit comments

Comments
 (0)