Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make nix.conf changes deterministic #620

Merged
merged 10 commits into from Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -56,7 +56,6 @@ uuid = { version = "1.2.2", features = ["serde"] }
os-release = { version = "0.1.0", default-features = false }
is_ci = { version = "1.1.1", default-features = false, optional = true }
strum = { version = "0.25.0", features = ["derive"] }
nix-config-parser = { version = "0.1.2", features = ["serde"] }
which = "4.4.0"
sysctl = "0.5.4"
walkdir = "2.3.3"
Expand Down
15 changes: 8 additions & 7 deletions src/action/base/create_file.rs
Expand Up @@ -27,7 +27,7 @@ pub struct CreateFile {
group: Option<String>,
mode: Option<u32>,
buf: String,
force: bool,
replace: bool,
}

impl CreateFile {
Expand All @@ -38,7 +38,7 @@ impl CreateFile {
group: impl Into<Option<String>>,
mode: impl Into<Option<u32>>,
buf: String,
force: bool,
replace: bool,
) -> Result<StatefulAction<Self>, ActionError> {
let path = path.as_ref().to_path_buf();
let mode = mode.into();
Expand All @@ -50,7 +50,7 @@ impl CreateFile {
group,
mode,
buf,
force,
replace,
};

if this.path.exists() {
Expand Down Expand Up @@ -128,7 +128,7 @@ impl CreateFile {
.map_err(|e| ActionErrorKind::Read(this.path.clone(), e))
.map_err(Self::error)?;

if discovered_buf != this.buf {
if discovered_buf != this.buf && !this.replace {
return Err(Self::error(ActionErrorKind::DifferentContent(
this.path.clone(),
)));
Expand Down Expand Up @@ -183,7 +183,8 @@ impl Action for CreateFile {
group,
mode,
buf,
force: _,
// If `replace` was not set, and the file existed, the `plan` step would have errored
replace: _,
} = self;

if tracing::enabled!(tracing::Level::TRACE) {
Expand Down Expand Up @@ -247,7 +248,7 @@ impl Action for CreateFile {
group: _,
mode: _,
buf: _,
force: _,
replace: _,
} = &self;

vec![ActionDescription::new(
Expand All @@ -264,7 +265,7 @@ impl Action for CreateFile {
group: _,
mode: _,
buf: _,
force: _,
replace: _,
} = self;

remove_file(&path)
Expand Down
102 changes: 81 additions & 21 deletions src/action/base/create_or_insert_into_file.rs
Expand Up @@ -19,6 +19,10 @@ use tracing::{span, Span};
pub enum Position {
Beginning,
End,
Before {
index: usize,
expected_content: String,
},
}

/** Create a file at the given location with the provided `buf` as
Expand Down Expand Up @@ -140,6 +144,20 @@ impl CreateOrInsertIntoFile {
return Ok(StatefulAction::completed(this));
}

if let Position::Before {
index,
expected_content,
} = &this.position
{
if discovered_buf.lines().nth(*index) != Some(expected_content.as_str()) {
return Err(ActionErrorKind::DifferentLineContent(
this.path.clone(),
this.position.clone(),
))
.map_err(Self::error);
}
}

// If not, we can't skip this, so we still do it
}

Expand Down Expand Up @@ -221,33 +239,75 @@ impl Action for CreateOrInsertIntoFile {
ActionErrorKind::Open(temp_file_path.clone(), e)
}).map_err(Self::error)?;

if *position == Position::End {
if let Some(ref mut orig_file) = orig_file {
tokio::io::copy(orig_file, &mut temp_file)
match position {
Position::End => {
if let Some(ref mut orig_file) = orig_file {
tokio::io::copy(orig_file, &mut temp_file)
.await
.map_err(|e| {
ActionErrorKind::Copy(path.to_owned(), temp_file_path.to_owned(), e)
})
.map_err(Self::error)?;
}

temp_file
.write_all(buf.as_bytes())
.await
.map_err(|e| {
ActionErrorKind::Copy(path.to_owned(), temp_file_path.to_owned(), e)
})
.map_err(|e| ActionErrorKind::Write(temp_file_path.clone(), e))
.map_err(Self::error)?;
},
Position::Beginning => {
temp_file
.write_all(buf.as_bytes())
.await
.map_err(|e| ActionErrorKind::Write(temp_file_path.clone(), e))
.map_err(Self::error)?;
}
}

temp_file
.write_all(buf.as_bytes())
.await
.map_err(|e| ActionErrorKind::Write(temp_file_path.clone(), e))
.map_err(Self::error)?;
if let Some(ref mut orig_file) = orig_file {
tokio::io::copy(orig_file, &mut temp_file)
.await
.map_err(|e| {
ActionErrorKind::Copy(path.to_owned(), temp_file_path.to_owned(), e)
})
.map_err(Self::error)?;
}
},
Position::Before {
index,
expected_content,
} => {
let mut original_content_buf = Vec::new();
if let Some(ref mut orig_file) = orig_file {
tokio::io::copy(orig_file, &mut original_content_buf)
.await
.map_err(|e| {
ActionErrorKind::Copy(path.to_owned(), temp_file_path.to_owned(), e)
})
.map_err(Self::error)?;
}
let original_content = String::from_utf8(original_content_buf)
.map_err(ActionErrorKind::FromUtf8)
.map_err(Self::error)?;
let mut original_content_lines = original_content.lines().collect::<Vec<_>>();
// The last line should match expected
if original_content_lines.get(*index).copied() != Some(expected_content.as_str()) {
return Err(ActionErrorKind::DifferentLineContent(
path.clone(),
position.clone(),
))
.map_err(Self::error);
}

original_content_lines.insert(*index, buf);
let new_content = original_content_lines.join("\n");

if *position == Position::Beginning {
if let Some(ref mut orig_file) = orig_file {
tokio::io::copy(orig_file, &mut temp_file)
temp_file
.write_all(new_content.as_bytes())
.await
.map_err(|e| {
ActionErrorKind::Copy(path.to_owned(), temp_file_path.to_owned(), e)
})
.map_err(|e| ActionErrorKind::Write(temp_file_path.clone(), e))
.map_err(Self::error)?;
}
}
},
};

let gid = if let Some(group) = group {
Some(
Expand Down