Skip to content

Commit

Permalink
Merge branch 'master' into users_mandatory_step
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Dec 1, 2024
2 parents 6c09486 + f83615d commit 95e50c6
Show file tree
Hide file tree
Showing 41 changed files with 536 additions and 286 deletions.
6 changes: 1 addition & 5 deletions rust/agama-cli/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
// find current contact information at www.suse.com.

use agama_lib::base_http_client::BaseHTTPClient;
use agama_lib::logs::set_archive_permissions;
use agama_lib::manager::http_client::ManagerHTTPClient as HTTPClient;
use clap::Subcommand;
use std::io;
Expand Down Expand Up @@ -51,10 +50,7 @@ pub async fn run(client: BaseHTTPClient, subcommand: LogsCommands) -> anyhow::Re
let result = client
.store(dst_file.as_path())
.await
.map_err(|_| anyhow::Error::msg("Downloading of logs failed"))?;

set_archive_permissions(result.clone())
.map_err(|_| anyhow::Error::msg("Cannot store the logs"))?;
.map_err(|e| anyhow::Error::new(e))?;

println!("{}", result.clone().display());

Expand Down
25 changes: 25 additions & 0 deletions rust/agama-lib/share/examples/post-script.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
user: {
fullName: 'Jane Doe',
userName: 'jane.doe',
password: 'nots3cr3t',
},
root: {
password: 'nots3cr3t',
},
product: {
id: 'Tumbleweed',
},
scripts: {
post: [
{
name: 'enable-sshd',
chroot: true,
body: |||
#!/bin/bash
systemctl enable sshd
|||,
},
],
},
}
59 changes: 54 additions & 5 deletions rust/agama-lib/share/profile.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
"description": "User-defined scripts to run before the installation starts",
"type": "array",
"items": {
"$ref": "#/$defs/script"
"$ref": "#/$defs/preScript"
}
},
"post": {
"title": "Post-installation scripts",
"description": "User-defined scripts to run after the installation finishes",
"type": "array",
"items": {
"$ref": "#/$defs/script"
"$ref": "#/$defs/postScript"
}
},
"init": {
"title": "Init scripts",
"description": "User-defined scripts to run booting the installed system",
"type": "array",
"items": {
"$ref": "#/$defs/script"
"$ref": "#/$defs/initScript"
}
}
}
Expand Down Expand Up @@ -1418,8 +1418,57 @@
"title": "Stripe size",
"$ref": "#/$defs/sizeValue"
},
"script": {
"title": "User-defined installation script",
"preScript": {
"title": "User-defined installation script that runs before the installation starts",
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"description": "Script name, to be used as file name",
"type": "string"
},
"body": {
"title": "Script content",
"description": "Script content, starting with the shebang",
"type": "string"
},
"url": {
"title": "Script URL",
"description": "URL to fetch the script from"
}
},
"required": ["name"],
"oneOf": [{ "required": ["body"] }, { "required": ["url"] }]
},
"postScript": {
"title": "User-defined installation script that runs after the installation finishes",
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"description": "Script name, to be used as file name",
"type": "string"
},
"body": {
"title": "Script content",
"description": "Script content, starting with the shebang",
"type": "string"
},
"url": {
"title": "Script URL",
"description": "URL to fetch the script from"
},
"chroot": {
"title": "Whether it should run in the installed system using a chroot environment",
"description": "whether to chroot to the target system (default: yes) or not",
"type": "boolean"
}
},
"required": ["name"],
"oneOf": [{ "required": ["body"] }, { "required": ["url"] }]
},
"initScript": {
"title": "User-defined installation script that runs during the first boot of the target system, once the installation is finished",
"type": "object",
"additionalProperties": false,
"properties": {
Expand Down
12 changes: 10 additions & 2 deletions rust/agama-lib/src/base_http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,19 @@ impl BaseHTTPClient {
/// Returns raw reqwest::Response. Use e.g. in case when response content is not
/// JSON body but e.g. binary data
pub async fn get_raw(&self, path: &str) -> Result<Response, ServiceError> {
self.client
let raw: Result<_, ServiceError> = self
.client
.get(self.url(path))
.send()
.await
.map_err(|e| e.into())
.map_err(|e| e.into());
let response = raw?;

if response.status().is_success() {
Ok(response)
} else {
Err(self.build_backend_error(response).await)
}
}

/// POST/PUT/PATCH an object to a given path and returns server response.
Expand Down
39 changes: 25 additions & 14 deletions rust/agama-lib/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,33 @@ use std::process::Command;
use tempfile::TempDir;
use utoipa::ToSchema;

const DEFAULT_COMMANDS: [(&str, &str); 3] = [
const DEFAULT_COMMANDS: [(&str, &str); 6] = [
// (<command to be executed>, <file name used for storing result of the command>)
("journalctl -u agama", "agama"),
("journalctl -u agama-auto", "agama-auto"),
("journalctl -u agama-web-server", "agama-web-server"),
("journalctl -u agama-dbus-monitor", "agama-dbus-monitor"),
("journalctl --dmesg", "dmesg"),
("rpm -qa", "rpm-qa"),
];

const DEFAULT_PATHS: [&str; 14] = [
// logs
"/var/log/YaST2",
"/var/log/zypper.log",
"/var/log/zypper/history*",
"/var/log/zypper/pk_backend_zypp",
"/var/log/pbl.log",
"/var/log/linuxrc.log",
"/var/log/wickedd.log",
"/var/log/NetworkManager",
"/var/log/messages",
"/var/log/boot.msg",
"/var/log/udev.log",
"/run/agama/dbus.log",
// config
"/etc/install.inf",
"/etc/os-release",
"/linuxrc.config",
"/.packages.root",
];

const DEFAULT_RESULT: &str = "/run/agama/agama-logs";
Expand Down Expand Up @@ -141,14 +144,16 @@ impl LogItem for LogPath {
fs::create_dir_all(dst_path)?;

let options = CopyOptions::new();
// fs_extra's own Error doesn't implement From trait so ? operator is unusable
match copy_items(&[self.src_path.as_str()], dst_path, &options) {
Ok(_p) => Ok(()),
Err(_e) => Err(io::Error::new(
io::ErrorKind::Other,
"Copying of a file failed",
)),

copy_items(&[self.src_path.as_str()], dst_path, &options)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Copying of a file failed"))?;

if let Some(name) = dst_file.file_name().and_then(|fname| fname.to_str()) {
let dst_name = name.trim_start_matches(".");
let _ = fs::rename(dst_file.clone(), dst_file.with_file_name(dst_name));
}

Ok(())
}
}

Expand All @@ -172,11 +177,17 @@ impl LogItem for LogCmd {
let output = Command::new(cmd_parts[0])
.args(cmd_parts[1..].iter())
.output()?;
let mut file_stdout = File::create(format!("{}.out.log", file_path.display()))?;
let mut file_stderr = File::create(format!("{}.err.log", file_path.display()))?;

file_stdout.write_all(&output.stdout)?;
file_stderr.write_all(&output.stderr)?;
if output.stdout.len() > 0 {
let mut file_stdout = File::create(format!("{}.out.log", file_path.display()))?;

file_stdout.write_all(&output.stdout)?;
}
if output.stderr.len() > 0 {
let mut file_stderr = File::create(format!("{}.err.log", file_path.display()))?;

file_stderr.write_all(&output.stderr)?;
}

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion rust/agama-lib/src/scripts/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl ScriptsClient {
/// Adds a script to the given group.
///
/// * `script`: script's definition.
pub async fn add_script(&self, script: &Script) -> Result<(), ServiceError> {
pub async fn add_script(&self, script: Script) -> Result<(), ServiceError> {
self.client.post_void("/scripts", &script).await
}

Expand Down
2 changes: 2 additions & 0 deletions rust/agama-lib/src/scripts/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ pub enum ScriptError {
Unreachable(#[from] TransferError),
#[error("I/O error: '{0}'")]
InputOutputError(#[from] io::Error),
#[error("Wrong script type")]
WrongScriptType,
}
Loading

0 comments on commit 95e50c6

Please sign in to comment.