-
Notifications
You must be signed in to change notification settings - Fork 68
feat(rust): add support for the inst.script option #2589
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
459185d
feat(rust): add support for the inst.script option
imobachgs edb41ac
chore(rust): add a comment about disabling SSL checks
imobachgs bf70ba4
docs(rust): update changes file
imobachgs cf5ce0d
fix(rust): avoid inst-scripts name collisions
imobachgs 140a83b
chore(rust): apply suggestions from code review
imobachgs e773431
fix(rust): save inst-script logs
imobachgs f66ffb6
Merge branch 'master' into add-inst-script
imobachgs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // Copyright (c) [2025] SUSE LLC | ||
| // | ||
| // All Rights Reserved. | ||
| // | ||
| // This program is free software; you can redistribute it and/or modify it | ||
| // under the terms of the GNU General Public License as published by the Free | ||
| // Software Foundation; either version 2 of the License, or (at your option) | ||
| // any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, but WITHOUT | ||
| // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| // more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License along | ||
| // with this program; if not, contact SUSE LLC. | ||
| // | ||
| // To contact SUSE LLC about this file by physical or electronic mail, you may | ||
| // find current contact information at www.suse.com. | ||
|
|
||
| use std::{ | ||
| fs::{self, create_dir_all, File}, | ||
| io::Write, | ||
| os::unix::fs::OpenOptionsExt, | ||
| path::{Path, PathBuf}, | ||
| process::Output, | ||
| }; | ||
|
|
||
| use agama_lib::utils::Transfer; | ||
| use url::Url; | ||
|
|
||
| /// Downloads and runs user-defined scripts. | ||
imobachgs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub struct ScriptsRunner { | ||
| pub path: PathBuf, | ||
| insecure: bool, | ||
| idx: usize, | ||
| } | ||
|
|
||
| impl ScriptsRunner { | ||
| /// Creates a new scripts runner. | ||
| /// | ||
| /// * path: working directory for the runner. | ||
| /// * insecure: whether to check certificates when downloading scripts. | ||
| pub fn new<P: AsRef<Path>>(path: P, insecure: bool) -> Self { | ||
| Self { | ||
| path: path.as_ref().to_path_buf(), | ||
| insecure, | ||
| idx: 0, | ||
| } | ||
| } | ||
|
|
||
| /// Downloads and runs the script from the given URL. | ||
| /// | ||
| /// It downloads the script from the given URL to the runner directory. | ||
| /// It saves the stdout, stderr and exit code to separate files. | ||
| /// | ||
| /// * url: script URL. | ||
imobachgs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub fn run(&mut self, url: &str) -> anyhow::Result<()> { | ||
| create_dir_all(&self.path)?; | ||
|
|
||
| let file_name = self.file_name_for(&url)?; | ||
|
|
||
| let path = self.path.join(&file_name); | ||
| self.save_script(url, &path)?; | ||
|
|
||
| let output = std::process::Command::new(&path).output()?; | ||
| self.save_logs(&path, output)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn path(&self) -> &PathBuf { | ||
| &self.path | ||
| } | ||
|
|
||
| fn file_name_for(&mut self, url: &str) -> anyhow::Result<PathBuf> { | ||
| let parsed = Url::parse(&url)?; | ||
|
|
||
| self.idx += 1; | ||
| let unnamed = PathBuf::from(format!("{}-unnamed.sh", self.idx)); | ||
|
|
||
| let Some(path) = parsed.path_segments() else { | ||
| return Ok(unnamed); | ||
| }; | ||
|
|
||
| Ok(path | ||
| .last() | ||
| .map(|p| PathBuf::from(format!("{}-{p}", self.idx))) | ||
| .unwrap_or(unnamed)) | ||
| } | ||
|
|
||
| fn save_script(&self, url: &str, path: &PathBuf) -> anyhow::Result<()> { | ||
| let mut file = Self::create_file(&path, 0o700)?; | ||
| Transfer::get(url, &mut file, self.insecure)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn save_logs(&self, path: &Path, output: Output) -> anyhow::Result<()> { | ||
| if !output.stdout.is_empty() { | ||
| let mut file = Self::create_file(&path.with_extension("stdout"), 0o600)?; | ||
| file.write_all(&output.stdout).unwrap(); | ||
imobachgs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if !output.stderr.is_empty() { | ||
| let mut file = Self::create_file(&path.with_extension("stderr"), 0o600)?; | ||
| file.write_all(&output.stderr)?; | ||
| } | ||
|
|
||
| if let Some(code) = output.status.code() { | ||
| let mut file = Self::create_file(&path.with_extension("exit"), 0o600)?; | ||
| write!(&mut file, "{}", code)?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn create_file(path: &Path, perms: u32) -> std::io::Result<File> { | ||
| fs::OpenOptions::new() | ||
| .create(true) | ||
| .truncate(true) | ||
| .write(true) | ||
| .mode(perms) | ||
| .open(path) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::ScriptsRunner; | ||
|
|
||
| fn script_path(name: &str) -> String { | ||
| let current = std::env::current_dir().unwrap(); | ||
| format!("file://{}/tests/scripts/{name}", current.display()) | ||
| } | ||
|
|
||
| fn script_runner() -> ScriptsRunner { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| ScriptsRunner::new(dir.path(), false) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_run_script() { | ||
| let url = script_path("success.sh"); | ||
| let mut runner = script_runner(); | ||
| runner.run(&url).unwrap(); | ||
|
|
||
| let contents = std::fs::read_to_string(runner.path().join("1-success.stdout")).unwrap(); | ||
| assert_eq!(&contents, "SUCCESS\n"); | ||
| let contents = std::fs::read_to_string(runner.path().join("1-success.exit")).unwrap(); | ||
| assert_eq!(&contents, "0"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_run_script_failed() { | ||
| let url = script_path("error.sh"); | ||
| let mut runner = script_runner(); | ||
| runner.run(&url).unwrap(); | ||
|
|
||
| let contents = std::fs::read_to_string(runner.path().join("1-error.stderr")).unwrap(); | ||
| assert_eq!(&contents, "ERROR\n"); | ||
| let contents = std::fs::read_to_string(runner.path().join("1-error.exit")).unwrap(); | ||
| assert_eq!(&contents, "1"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #!/usr/bin/bash | ||
|
|
||
| echo ERROR 1>&2 | ||
| exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #!/usr/bin/bash | ||
|
|
||
| echo SUCCESS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
| ------------------------------------------------------------------- | ||
| Mon Jul 21 12:11:27 UTC 2025 - Imobach Gonzalez Sosa <[email protected]> | ||
|
|
||
| - Add support for an inst.script that allows to run an arbitrary | ||
| script (bsc#1246702, gh#agama-project/agama#2589). | ||
|
|
||
| ------------------------------------------------------------------- | ||
| Mon Jul 21 10:56:54 UTC 2025 - Knut Anderssen <[email protected]> | ||
|
|
||
| - Fix device type detection when reading a VLAN connection | ||
| (gh#agama-project/agama#2586). | ||
| (gh#agama-project/agama#2586). | ||
|
|
||
| ------------------------------------------------------------------- | ||
| Fri Jul 18 06:54:08 UTC 2025 - Knut Anderssen <[email protected]> | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.