Skip to content

Commit

Permalink
Fixes #16878: Fix generation of parameters and conditionss from cfeng…
Browse files Browse the repository at this point in the history
…ine files
  • Loading branch information
gpoblon authored and peckpeck committed Mar 25, 2020
1 parent 8e88d37 commit d5e196a
Show file tree
Hide file tree
Showing 29 changed files with 142 additions and 241 deletions.
28 changes: 14 additions & 14 deletions rudder-lang/libs/stdlib.rl
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
@format=0

resource sharedfile(p0)
resource package(p0)
resource group(p0)
resource service(p0)
resource variable(p0,p1)
resource http(p0)
resource condition(p0)
resource directory(p0)
resource schedule(p0)
resource file(p0)
resource monitoring(p0)
resource service(p0)
resource http_request(p0,p1)
resource user(p0)
resource environment(p0)
resource group(p0)
resource directory(p0)
resource sharedfile(p0,p1)
resource permissions(p0)
resource condition(p0)
resource schedule(p0)
resource package(p0)
resource command(p0)
resource kernel_module(p0)
resource user(p0)
resource file(p0)

command state execution(){}
command state execution_once(p1,p2,p3){}
Expand Down Expand Up @@ -101,8 +101,8 @@ file state symlink_present_option(p1,p2){}
file state template_expand(p1,p2,p3,p4){}
group state absent(){}
group state present(){}
http state request_check_status_headers(p1,p2,p3){}
http state request_content_headers(p1,p2,p3){}
http_request state check_status_headers(p2,p3){}
http_request state content_headers(p2,p3){}
kernel_module state configuration(p1){}
kernel_module state enabled_at_boot(){}
kernel_module state loaded(){}
Expand Down Expand Up @@ -158,8 +158,8 @@ service state started(){}
service state started_path(p1){}
service state stop(){}
service state stopped(){}
sharedfile state from_node(p1,p2){}
sharedfile state to_node(p1,p2,p3){}
sharedfile state from_node(p2){}
sharedfile state to_node(p2,p3){}
user state absent(){}
user state create(p1,p2,p3,p4,p5){}
user state fullname(p1){}
Expand Down
2 changes: 1 addition & 1 deletion rudder-lang/src/bin/rudderc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn main() {

// Actual action
let result = match action {
Action::Compile => compile_file(&input, &output, true, &libs_dir),
Action::Compile => compile_file(&input, &output, true, &libs_dir, &translate_config),
Action::Translate => translate_file(&input, &output, &translate_config),
};
match &result {
Expand Down
14 changes: 10 additions & 4 deletions rudder-lang/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ impl SourceList {
}
}

pub fn compile_file(source: &Path, dest: &Path, technique: bool, libs_dir: &Path) -> Result<()> {
pub fn compile_file(
source: &Path,
dest: &Path,
technique: bool,
libs_dir: &Path,
translate_config: &Path,
) -> Result<()> {
let sources = SourceList::new();

// read and add files
let oses = libs_dir.join("oslib.rl");
let corelib = libs_dir.join("corelib.rl");
// let cfenginecore = libs_dir.join("cfengine_core.rl");
let cfenginecore = libs_dir.join("cfengine_core.rl");
let stdlib = libs_dir.join("stdlib.rl");
let input_filename = source.to_string_lossy();
let output_filename = dest.to_string_lossy();
Expand All @@ -74,7 +80,7 @@ pub fn compile_file(source: &Path, dest: &Path, technique: bool, libs_dir: &Path
// data
let mut past = PAST::new();
add_file(&mut past, &sources, &corelib, "corelib.rl")?;
// add_file(&mut past, &sources, &cfenginecore, "cfengine_core.rl")?;
add_file(&mut past, &sources, &cfenginecore, "cfengine_core.rl")?;
add_file(&mut past, &sources, &stdlib, "stdlib.rl")?;
add_file(&mut past, &sources, &oses, "oslib.rl")?;
add_file(&mut past, &sources, &source, &input_filename)?;
Expand All @@ -96,5 +102,5 @@ pub fn compile_file(source: &Path, dest: &Path, technique: bool, libs_dir: &Path
} else {
(None, None)
};
cfe.generate(&ast, input_file, output_file, technique)
cfe.generate(&ast, input_file, output_file, translate_config, technique)
}
1 change: 1 addition & 0 deletions rudder-lang/src/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub trait Generator {
gc: &AST,
input_file: Option<&Path>,
output_file: Option<&Path>,
translate_config: &Path,
technique_metadata: bool,
) -> Result<()>;
}
34 changes: 25 additions & 9 deletions rudder-lang/src/generators/cfengine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ impl CFEngine {
})
}

fn get_config_from_file() -> Result<toml::Value> {
let config_filename = "libs/translate_config.toml";
fn get_config_from_file(translate_config: &Path) -> Result<toml::Value> {
let fname = translate_config.to_str().unwrap();
let file_error =
|filename: &str, err| err!(Token::new(&filename.to_owned(), ""), "{}", err);
let config_data =
std::fs::read_to_string(config_filename).map_err(|e| file_error(config_filename, e))?;
toml::from_str(&config_data).map_err(|e| err!(Token::new(config_filename, ""), "{}", e))
std::fs::read_to_string(translate_config).map_err(|e| file_error(fname, e))?;
toml::from_str(&config_data).map_err(|e| err!(Token::new(fname, ""), "{}", e))
}
fn get_class_parameter_index(method_name: String) -> Result<usize> {
fn get_class_parameter_index(method_name: String, translate_config: &Path) -> Result<usize> {
// outcome detection and formating
let config = Self::get_config_from_file()?;
let config = Self::get_config_from_file(translate_config)?;
let mconf = match config.get("methods") {
None => return Err(Error::User("No methods section in config.toml".into())),
Some(m) => m,
Expand Down Expand Up @@ -177,6 +177,7 @@ impl CFEngine {
st: &Statement,
id: usize,
in_class: String,
translate_config: &Path,
) -> Result<String> {
match st {
Statement::StateDeclaration(sd) => {
Expand All @@ -198,7 +199,7 @@ impl CFEngine {
", ",
)?;
let method_name = format!("{}_{}", sd.resource.fragment(), sd.state.fragment());
let index = Self::get_class_parameter_index(method_name)?;
let index = Self::get_class_parameter_index(method_name, translate_config)?;
let class = self.format_class(in_class)?;
let state_param = if sd.resource_params.len() > 0 {
if let Ok(param) = self.parameter_to_cfengine(&sd.resource_params[0]) {
Expand Down Expand Up @@ -245,7 +246,15 @@ impl CFEngine {
let case_exp = self.format_case_expr(gc, case)?;
map_strings_results(
vst.iter(),
|st| self.format_statement(gc, st, id, case_exp.clone()),
|st| {
self.format_statement(
gc,
st,
id,
case_exp.clone(),
translate_config,
)
},
"",
)
},
Expand Down Expand Up @@ -387,6 +396,7 @@ impl Generator for CFEngine {
gc: &AST,
input_file: Option<&Path>,
output_file: Option<&Path>,
translate_config: &Path,
technique_metadata: bool,
) -> Result<()> {
let mut files: HashMap<&str, String> = HashMap::new();
Expand Down Expand Up @@ -443,7 +453,13 @@ impl Generator for CFEngine {
);
content.push_str(" methods:\n");
for (i, st) in state.statements.iter().enumerate() {
content.push_str(&self.format_statement(gc, st, i, "any".to_string())?);
content.push_str(&self.format_statement(
gc,
st,
i,
"any".to_string(),
translate_config,
)?);
}
content.push_str("}\n");
files.insert(file_to_create, content);
Expand Down
6 changes: 2 additions & 4 deletions rudder-lang/src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,11 @@ fn translate(config: &toml::Value, technique: &Technique) -> Result<String> {
let out = format!(
r#"# This file has been generated with rltranslate
@format=0
@name="{name}"
@description="{description}"
@version="{version}"
@parameters= [{newline}{parameters_meta}]
resource {bundle_name}({parameters})
{bundle_name} state technique() {{
{calls}
}}
Expand Down Expand Up @@ -394,7 +391,7 @@ fn translate_condition(_config: &toml::Value, cond: &str) -> Result<String> {
static ref OS_RE: Regex = Regex::new(
// OS part: debian_9_0 \ And optional condition: (!(ubuntu|otheros).something)
// r"^\(?(?P<os>([a-zA-Z\d]+)(_([a-zA-Z\d]+))*(\|([a-zA-Z\d]+)(_([a-zA-Z\d]+))*)*)\)?(.(?P<cdt>\(\(*!*\(*\w+\)*([.|]\(*!*\(*\w+\)*)*\)))?$"
r"^(\(*\w+\(*\)*(\.|\|)\(*\w+\)*)*$"
r"^(\(*\w+\(*\)*[.|]\(*\w+\)*)*$"
).unwrap();
}

Expand All @@ -414,6 +411,7 @@ fn translate_condition(_config: &toml::Value, cond: &str) -> Result<String> {
{
return Ok(format!("{} =~ {}", method, status));
}
println!("CAPS = {:?}", caps);
};

// detect system classes
Expand Down
2 changes: 1 addition & 1 deletion rudder-lang/tests/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use test_case::test_case;
#[test_case("s_basic")]
#[test_case("s_enum_mapping")]
#[test_case("s_state_case")]
#[test_case("s_declare")]
// #[test_case("s_declare")]
// #[test_case("s_does_not_exist")] // supposed to fail as the file does not exist
fn real_files(filename: &str) {
// manually define here the output folder (direct parent folder is /tests/)
Expand Down
118 changes: 0 additions & 118 deletions rudder-lang/tests/helpers/cfjson_tester

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# This file has been generated with rltranslate
@format=0

@name="condition_errorprone"
@description=""
@version="1.0"
@parameters=[]
@parameters= [
]

resource condition_errorprone()

condition_errorprone state technique() {
@component = "Command execution"
if dragonfly|openbsd => command("pwd").execution() as command_execution_pwd
if (dragonfly|openbsd) => command("pwd").execution() as command_execution_pwd
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ bundle agent condition_errorprone_technique
vars:
"resources_dir" string => "${this.promise_dirname}/resources";
methods:
"Command execution_${report_data.directive_id}_0" usebundle => _method_reporting_context("Command execution"),
if => concat("(dragonfly)|(openbsd)");
"Command execution_${report_data.directive_id}_0" usebundle => _method_reporting_context("Command execution", "pwd"),
if => concat("dragonfly|openbsd");
"Command execution_${report_data.directive_id}_0" usebundle => command_execution("pwd"),
if => concat("(dragonfly)|(openbsd)");
if => concat("dragonfly|openbsd");
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"parameter": [],
"method_calls": [
{
"class_context": "(dragonfly)|(openbsd)",
"class_context": "dragonfly|openbsd",
"component": "Command execution",
"method_name": "command_execution",
"args": [
"pwd"
]
}
]
}
}

0 comments on commit d5e196a

Please sign in to comment.