def test_canonify(self): result = ncf.canonify("ascii @&_ string") self.assertEqual(result, "ascii_____string") # python/ncf reads UTF-8 files and produces u'' strings in python2 and '' strings in python3 # python2 tests if sys.version_info[0] == 2: # unicode in source file -> interpreted as unicode with u'' -> correct iso in python string (ncf builder use case) result = ncf.canonify(u'héhé') self.assertEqual(result, 'h__h__') # python3 tests if sys.version_info[0] == 3: # unicode in source file -> correct unicode in python string (ncf builder use case) result = ncf.canonify('héhé') self.assertEqual(result, "h__h__")
def test_canonify(self): result = ncf.canonify("ascii @&_ string") self.assertEquals(result, "ascii_____string") # python/ncf reads UTF-8 files and produces u'' strings in python2 and '' strings in python3 # python2 tests if sys.version_info[0] == 2: # unicode in source file -> interpreted as unicode with u'' -> correct iso in python string (ncf builder use case) result = ncf.canonify(u'héhé') self.assertEquals(result, 'h__h__') # python3 tests if sys.version_info[0] == 3: # unicode in source file -> correct unicode in python string (ncf builder use case) result = ncf_rudder.canonify('héhé') self.assertEquals(result, "h__h__")
def canonify_expected_reports(expected_reports, dest): # Open file containing original expected_reports source_file = codecs.open(expected_reports, encoding="utf-8") # Create destination file dest_file = codecs.open(dest, 'w', encoding="utf-8") # Iterate over each line (this does *not* read the whole file into memory) for line in source_file: # Just output comments as they are if re.match("^\s*#", line, flags=re.UNICODE): dest_file.write(line) continue # Replace the second field with a canonified version of itself (a la CFEngine) fields = line.strip().split(";;") fields[1] = ncf.canonify(fields[1]) dest_file.write(";;".join(fields) + "\n")
def generate_rudder_reporting(technique): """Generate complementary reporting needed for Rudder in rudder_reporting.st file""" # Get all generic methods generic_methods = ncf.get_all_generic_methods_metadata( alt_path='/var/rudder/configuration-repository/ncf' )['data']['generic_methods'] bundle_param = "" if len(technique["parameter"]) > 0: bundle_param = "(" + ", ".join( [ncf.canonify(param["name"]) for param in technique["parameter"]]) + ")" content = [] content.append('bundle agent ' + technique['bundle_name'] + '_rudder_reporting' + bundle_param) content.append('{') content.append(' vars:') content.append( ' "promisers" slist => { @{this.callers_promisers}, cf_null }, policy => "ifdefined";' ) content.append( ' "class_prefix" string => canonify(join("_", "promisers"));') content.append(' "args" slist => { };') content.append('') content.append(' methods:') for method_call in technique["method_calls"]: method_name = method_call['method_name'] if method_call['method_name'].startswith("_"): continue generic_method = generic_methods[method_name] key_value = ncf.get_key_value(method_call, generic_method) class_prefix = ncf.get_class_prefix(key_value, generic_method) if not "cfengine-community" in generic_method["agent_support"]: message = "'" + generic_method[ "name"] + "' method is not available on cfengine based agent, skip" logger_call = ncf.get_logger_call(message, class_prefix) content.append(' any::') content.append(' "dummy_report" usebundle => _classes_noop("' + class_prefix + '");') content.append(' ' + logger_call + ';') elif method_call['class_context'] != "any": # escape double quote in key value regex_quote = re.compile(r'(?<!\\)"', flags=re.UNICODE) escaped_key_value = regex_quote.sub('\\"', key_value) message = generic_method[ 'name'] + ' ' + escaped_key_value + ' if ' + method_call[ 'class_context'] logger_rudder_call = ncf.get_logger_call(message, class_prefix) # Always add an empty line for readability content.append('') if not "$" in method_call['class_context']: content.append(' !(' + method_call['class_context'] + ')::') content.append( ' "dummy_report" usebundle => _classes_noop("' + class_prefix + '");') content.append(' ' + logger_rudder_call + ';') else: class_context = ncf.canonify_class_context( method_call['class_context']) content.append( ' "dummy_report" usebundle => _classes_noop("' + class_prefix + '"),') content.append(' ifvarclass => concat("!(' + class_context + ')");') content.append(' ' + logger_rudder_call + ',') content.append(' ifvarclass => concat("!(' + class_context + ')");') content.append('}') # Join all lines with \n to get a pretty CFEngine file result = '\n'.join(content) + "\n" return result
def generate_rudder_reporting(technique): """Generate complementary reporting needed for Rudder in rudder_reporting.st file""" # Get all generic methods generic_methods = ncf.get_all_generic_methods_metadata(alt_path='/var/rudder/configuration-repository/ncf')['data']['generic_methods'] args = "" bundle_param = "" if len(technique["parameter"]) > 0: bundle_param = "("+", ".join([ncf.canonify(param["name"]) for param in technique["parameter"] ])+")" args = ", ".join('"${' + ncf.canonify(param["name"]) + '}"' for param in technique["parameter"] ) bundle_name = technique['bundle_name']+'_rudder_reporting' content = [] content.append('bundle agent '+ technique['bundle_name']+'_rudder_reporting'+ bundle_param) content.append('{') content.append(' vars:') content.append(' "args" slist => { '+ args +' };') content.append(' "report_param" string => join("_", args);') content.append(' "full_class_prefix" string => canonify("' + bundle_name + '_${report_param}");') content.append(' "class_prefix" string => string_head("${full_class_prefix}", "1000");') content.append('') content.append(' methods:') report_unique_id = 0 for method_call in technique["method_calls"]: method_name = method_call['method_name'] if method_call['method_name'].startswith("_"): continue generic_method = generic_methods[method_name] key_value = ncf.get_key_value(method_call, generic_method) class_prefix = '${class_prefix}_'+ncf.get_class_prefix(key_value, generic_method) method_reporting = '"dummy_report_' + str(report_unique_id) + '" usebundle => ' + ncf.generate_reporting_context(generic_method, method_call) report_unique_id += 1 class_parameter = ncf.generate_reporting_class_parameter(generic_method, method_call) if not "cfengine-community" in generic_method["agent_support"]: message = "'"+generic_method["name"]+"' method is not available on cfengine based agent, skip" logger_call = get_logger_call(message, class_prefix, class_parameter) content.append(' any::') content.append(' "dummy_report" usebundle => _classes_noop("'+class_prefix+'");') content.append(' ' + method_reporting + ';') content.append(' ' + logger_call+';') elif method_call['class_context'] != "any": # escape double quote in key value regex_quote = re.compile(r'(?<!\\)"', flags=re.UNICODE ) escaped_key_value = regex_quote.sub('\\"', key_value) message = generic_method['name'] + ' ' + escaped_key_value + ' if ' + method_call['class_context'] logger_rudder_call = get_logger_call(message, class_prefix, class_parameter) # Always add an empty line for readability content.append('') if not "$" in method_call['class_context']: content.append(' !('+method_call['class_context']+')::') content.append(' "dummy_report" usebundle => _classes_noop("'+class_prefix+'");') content.append(' ' + method_reporting + ';') content.append(' ' + logger_rudder_call + ';') else: class_context = ncf.canonify_class_context(method_call['class_context']) content.append(' "dummy_report" usebundle => _classes_noop("'+class_prefix+'"),') content.append(' ifvarclass => concat("!('+class_context+')");') content.append(' ' + method_reporting + ',') content.append(' ifvarclass => concat("!('+class_context+')");') content.append(' ' + logger_rudder_call + ',') content.append(' ifvarclass => concat("!('+class_context+')");') content.append('}') # Join all lines with \n to get a pretty CFEngine file result = '\n'.join(content)+"\n" return result