Skip to content

Commit b575d89

Browse files
committed
Squashed 'crates/raster/' changes from 95b6ee0..4a3227c
4a3227c VCUE-1417 - parallax imagestore creation hook for Lustre PFL setup (#15) git-subtree-dir: crates/raster git-subtree-split: 4a3227cc93ab6dfb4b3d6ecbf5443bdf4cfc236e
1 parent 0c91139 commit b575d89

5 files changed

Lines changed: 169 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ toml = "0.9.5"
1313
regex = "1.12.2"
1414
serial_test = "3.2.0"
1515
nix = { version = "0.30.1", features = ["user","fs","signal"] }
16+
is_executable = "1.0.5"

src/config.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const CONFIG_PATH: &str = "/etc/sarus-suite";
99
#[derive(Serialize, Deserialize, Clone, Default)]
1010
pub struct RawConfig {
1111
edf_system_search_path: Option<String>,
12+
hooks: Option<RawConfigHooks>,
1213
parallax_imagestore: Option<String>,
1314
parallax_mount_program: Option<String>,
1415
parallax_path: Option<String>,
@@ -26,10 +27,17 @@ pub struct RawConfig {
2627
tracking_tool: Option<String>,
2728
}
2829

30+
#[derive(Serialize, Deserialize, Clone, Default)]
31+
pub struct RawConfigHooks {
32+
parallax_imagestore_create: Option<String>,
33+
}
34+
2935
#[derive(Serialize, Deserialize, Clone, Default)]
3036
pub struct Config {
3137
#[serde(default = "get_default_edf_system_search_path")]
3238
pub edf_system_search_path: String,
39+
#[serde(default = "get_default_hooks")]
40+
pub hooks: ConfigHooks,
3341
#[serde(default = "get_default_parallax_imagestore")]
3442
pub parallax_imagestore: String,
3543
#[serde(default = "get_default_parallax_mount_program")]
@@ -62,6 +70,12 @@ pub struct Config {
6270
pub tracking_tool: String,
6371
}
6472

73+
#[derive(Serialize, Deserialize, Clone, Default)]
74+
pub struct ConfigHooks {
75+
#[serde(default = "get_default_hook_parallax_imagestore_create")]
76+
pub parallax_imagestore_create: String,
77+
}
78+
6579
#[derive(Clone, Copy)]
6680
pub enum VarExpand {
6781
Never, // Do not expand variables.
@@ -134,13 +148,27 @@ fn get_default_tracking_tool() -> String {
134148
return String::from("");
135149
}
136150

151+
fn get_default_hook_parallax_imagestore_create() -> String {
152+
return String::from("");
153+
}
154+
155+
fn get_default_hooks() -> ConfigHooks {
156+
return ConfigHooks {
157+
parallax_imagestore_create: get_default_hook_parallax_imagestore_create(),
158+
}
159+
}
160+
137161
impl From<RawConfig> for Config {
138162
fn from(r: RawConfig) -> Self {
139163
Config {
140164
edf_system_search_path: match r.edf_system_search_path {
141165
Some(s) => s,
142166
None => get_default_edf_system_search_path(),
143167
},
168+
hooks: match r.hooks {
169+
Some(s) => ConfigHooks::from(s),
170+
None => get_default_hooks(),
171+
},
144172
parallax_imagestore: match r.parallax_imagestore {
145173
Some(s) => s,
146174
None => get_default_parallax_imagestore(),
@@ -211,6 +239,9 @@ impl RawConfig {
211239
if i.edf_system_search_path.is_some() {
212240
self.edf_system_search_path = i.edf_system_search_path;
213241
}
242+
if i.hooks.is_some() {
243+
self.hooks = i.hooks;
244+
}
214245
if i.parallax_imagestore.is_some() {
215246
self.parallax_imagestore = i.parallax_imagestore;
216247
}
@@ -259,6 +290,17 @@ impl RawConfig {
259290
}
260291
}
261292

293+
impl From<RawConfigHooks> for ConfigHooks {
294+
fn from(r: RawConfigHooks) -> Self {
295+
ConfigHooks {
296+
parallax_imagestore_create: match r.parallax_imagestore_create {
297+
Some(s) => s,
298+
None => get_default_hook_parallax_imagestore_create(),
299+
},
300+
}
301+
}
302+
}
303+
262304
fn validate_configfile(path: String) -> SarusResult<()> {
263305
// Embedding schema file
264306
let schema_content = include_str!("schema/config.json");
@@ -303,6 +345,7 @@ fn load_raw_config_from_file(
303345
};
304346

305347
let mut r: RawConfig = toml_value;
348+
306349
//let mut r: RawConfig = toml_read(path_str)?;
307350

308351
// Expand variables in the fields
@@ -427,6 +470,14 @@ fn load_raw_config_from_dir(
427470
}
428471

429472
pub fn update_config_by_user(config: &mut Config, edf: EDF) -> SarusResult<()> {
473+
let parallax_imagestore_create = edf.annotations.get("com.sarus.hooks.parallax_imagestore_create");
474+
if parallax_imagestore_create.is_some() {
475+
match parallax_imagestore_create.unwrap().as_str() {
476+
"false" => { config.hooks.parallax_imagestore_create = String::from("") },
477+
_ => {},
478+
}
479+
}
480+
430481
let parallax_imagestore = edf.annotations.get("com.sarus.parallax_imagestore");
431482
if parallax_imagestore.is_some() {
432483
config.parallax_imagestore = parallax_imagestore.unwrap().to_string();

src/hooks.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use std::path::Path;
2+
use std::process::{Command, Output};
3+
use is_executable::IsExecutable;
4+
5+
use crate::error::{SarusError, SarusResult};
6+
use crate::Config;
7+
8+
pub struct ExecutedCommand {
9+
pub command: String,
10+
pub output: Output,
11+
}
12+
13+
pub fn hook_run(config: &Config, name: &str, args: Vec<&str>) -> SarusResult<Option<ExecutedCommand>> {
14+
15+
let hook = match name {
16+
"parallax_imagestore_create" => &config.hooks.parallax_imagestore_create,
17+
_ => return Err(SarusError {
18+
code: 26,
19+
file_path: None,
20+
msg: format!("unknown hook name: \"{name}\""),
21+
}),
22+
};
23+
24+
if hook == "" {
25+
return Ok(None);
26+
}
27+
28+
let hook_path = Path::new(&hook);
29+
30+
if ! hook_path.exists() {
31+
return Err(SarusError {
32+
code: 27,
33+
file_path: None,
34+
msg: format!("config.hooks.{name} file \"{hook}\" doesn't exist"),
35+
});
36+
}
37+
38+
if ! hook_path.is_executable() {
39+
return Err(SarusError {
40+
code: 28,
41+
file_path: None,
42+
msg: format!("config.hooks.{name} file \"{hook}\" isn't executable"),
43+
});
44+
}
45+
46+
let output = hook_run_path(hook_path, &args)?;
47+
//let outstr = hook_output_to_string(output);
48+
49+
let ec = ExecutedCommand {
50+
command: format!("{hook} {}", args.concat()),
51+
output: output,
52+
};
53+
54+
//Ok(format!("{name} hook executed: {hook} {}\n{name} hook {outstr}", args.concat()))
55+
Ok(Some(ec))
56+
}
57+
58+
fn hook_run_path(path: &Path, args: &Vec<&str>) -> SarusResult<Output> {
59+
60+
match Command::new(path)
61+
.args(args)
62+
.output() {
63+
Ok(output) => Ok(output),
64+
Err(err) => return Err(SarusError {
65+
code: 29,
66+
file_path: None,
67+
msg: format!("Running command \"{} {}\" error: {err}", path.display(), args.concat()),
68+
}),
69+
}
70+
}
71+
/*
72+
fn hook_output_to_string(output: Output) -> String {
73+
74+
let ret_result = match output.status.success() {
75+
true => "succeeded",
76+
false => "failed",
77+
};
78+
79+
let ret_code = match output.status.code() {
80+
Some(s) => s.to_string(),
81+
None => String::from("UNKNOWN"),
82+
};
83+
84+
let mut ret_stdout = match String::from_utf8(output.stdout) {
85+
Ok(stdout) => String::from(stdout.strip_suffix("\n").unwrap_or(&stdout)),
86+
Err(err) => format!("translation stdout error: \"{err}\""),
87+
};
88+
89+
if ret_stdout != "" {
90+
ret_stdout = format!("\nstdout:\n{ret_stdout}");
91+
}
92+
93+
let mut ret_stderr = match String::from_utf8(output.stderr) {
94+
Ok(stderr) => String::from(stderr.strip_suffix("\n").unwrap_or(&stderr)),
95+
Err(err) => format!("translation stderr error: \"{err}\""),
96+
};
97+
98+
if ret_stderr != "" {
99+
ret_stderr = format!("\nstderr:\n{ret_stderr}");
100+
}
101+
102+
return format!("{ret_result} with exit code: {ret_code}{ret_stdout}{ret_stderr}");
103+
}
104+
*/

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ use crate::mount::{SarusMounts, sarus_mounts_from_strings};
1515
pub mod common;
1616
pub mod config;
1717
pub mod error;
18+
pub mod hooks;
1819
pub mod mount;
1920

2021
pub use crate::common::expand_vars_string;
2122
pub use crate::config::{Config, VarExpand, load_config, load_config_path, update_config_by_user};
23+
pub use crate::hooks::{hook_run, ExecutedCommand};
2224

2325
#[allow(dead_code)]
2426
#[derive(Derivative, Serialize, Deserialize, Clone, Default)]

src/schema/config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
"description": "filesystem path where to load EDF files from",
1111
"type": "string"
1212
},
13+
"hooks": {
14+
"description": "Sarus Suite hooks table",
15+
"type": "object",
16+
"additionalProperties": false,
17+
"properties": {
18+
"parallax_imagestore_create": {
19+
"description": "hook for parallax imagestore creation",
20+
"type": "string"
21+
}
22+
}
23+
},
1324
"parallax_imagestore": {
1425
"description": "shared filesystem path where to store/load images",
1526
"type": "string"

0 commit comments

Comments
 (0)