Пример #1
0
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']

    content = []
    content.append('bundle agent ' + technique['bundle_name'] +
                   '_rudder_reporting')
    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
Пример #2
0
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