Skip to content

Commit

Permalink
improved tedge-write related logging
Browse files Browse the repository at this point in the history
Put more details into warn and error logs when deploying configuration
files using `tedge-write`:

- before: "WARN: `sudo.enable` set to `true`, but sudo not found in $PATH"
- after:  "WARN: `sudo.enable` set to `true`, but sudo not found in $PATH, using tedge-write directly"

- before: "ERROR: config-manager failed writing updated configuration file: Starting tedge-write process failed"
- after:  "ERROR: config-manager failed writing updated configuration file: failed to start process '/usr/bin/tedge-write': No such file or directory (os error 2)"

- before: "ERROR: config-manager failed writing updated configuration file: sudo: /usr/bin/tedge-write: command not found"
- after:  "ERROR: config-manager failed writing updated configuration file: process returned non-zero exit code (1); stderr="sudo: /usr/bin/tedge-write: command not found""

Signed-off-by: Marcel Guzik <[email protected]>
  • Loading branch information
Bravo555 committed Aug 19, 2024
1 parent a489beb commit f9fc679
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/common/tedge_config/src/sudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl SudoCommandBuilder {
c
}
Err(_) => {
warn!("`sudo.enable` set to `true`, but sudo not found in $PATH");
warn!("`sudo.enable` set to `true`, but sudo not found in $PATH, using tedge-write directly");
Command::new(program)
}
}
Expand Down
20 changes: 13 additions & 7 deletions crates/core/tedge_write/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,30 @@ impl<'a> CopyOptions<'a> {
///
/// Stdin and Stdout are UTF-8.
pub fn copy(self) -> anyhow::Result<()> {
let output = self
.command()?
.output()
.context("Starting tedge-write process failed")?;
let mut command = self.command()?;

let output = command.output();

let program = command.get_program().to_string_lossy();
let output = output.with_context(|| format!("failed to start process '{program}'"))?;

if !output.status.success() {
let stderr = String::from_utf8(output.stderr).expect("output should be utf-8");
let stderr = stderr.trim();
let exit_code = output.status.code().unwrap();
return Err(anyhow!(
String::from_utf8(output.stderr).expect("output should be utf-8")
"process returned non-zero exit code ({exit_code}); stderr=\"{stderr}\""
));
}

Ok(())
}

fn command(&self) -> std::io::Result<Command> {
fn command(&self) -> anyhow::Result<Command> {
let mut command = self.sudo.command(crate::TEDGE_WRITE_PATH);

let from_reader = std::fs::File::open(self.from)?;
let from_reader = std::fs::File::open(self.from)
.with_context(|| format!("could not open file for reading '{}'", self.from))?;
command.stdin(from_reader).arg(self.to);

if let Some(mode) = self.mode {
Expand Down
2 changes: 1 addition & 1 deletion crates/extensions/tedge_config_manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum ConfigManagementError {
#[error(transparent)]
FromAtomFileError(#[from] tedge_utils::fs::AtomFileError),

#[error(transparent)]
#[error("{0:#}")]
Other(#[from] anyhow::Error),
}

Expand Down

0 comments on commit f9fc679

Please sign in to comment.