Beispiel #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:')

  # Handle method calls
  technique_name = technique['bundle_name']
  # Filter calls so we only have those who are not using a any class_context
  filter_calls = [ method_call for method_call in technique["method_calls"] if method_call['class_context'] != "any" ]

  for method_call in filter_calls:
     
    method_name = method_call['method_name']
    generic_method = generic_methods[method_name]

    key_value = method_call["args"][generic_method["class_parameter_id"]-1].replace("\\'", "\'")
    regex = re.compile("[^\$\{\}a-zA-Z0-9_](?![^{}]+})|\$(?!{)")
    # to match cfengine behaviour we need to treat utf8 as if it was ascii (see #7195)
    # string should be unicode string (ie u'') which is the case if they are read from files opened with encoding="utf-8"
    key_value = key_value.encode("utf-8").decode("iso-8859-1") 
    key_value_canonified = regex.sub("_", key_value)

    class_prefix = generic_method["class_prefix"]+"_"+key_value_canonified
    logger_rudder_call = '"dummy_report" usebundle => log_rudder("' + generic_method['name'] + ' ' + key_value + ' if ' + method_call['class_context'] + '", "' + class_prefix +'", "${class_prefix}", @{args})'
    logger_rudder_call = logger_rudder_call.replace("&", "\\&")

    # 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
Beispiel #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']

  content = []
  content.append('bundle agent '+ technique['bundle_name']+'_rudder_reporting')
  content.append('{')
  content.append('  methods:')

  # Handle method calls
  technique_name = technique['bundle_name']
  # Filter calls so we only have those who are not using a any class_context
  filter_calls = [ method_call for method_call in technique["method_calls"] if method_call['class_context'] != "any" ]

  for method_call in filter_calls:

    method_name = method_call['method_name']
    generic_method = generic_methods[method_name]

    key_value = method_call["args"][generic_method["class_parameter_id"]-1].replace("\\'", "\'")
    regex = re.compile("[^\$\{\}\w](?![^{}]+})|\$(?!{)", flags=re.UNICODE)
    key_value_canonified = regex.sub("_", key_value)


    class_prefix = generic_method["class_prefix"]+"_"+key_value_canonified

    # 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('      "dummy_report" usebundle => logger_rudder("Not applicable", "'+class_prefix+'");')

    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('      "dummy_report" usebundle => logger_rudder("Not applicable", "'+class_prefix+'"),')
      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
Beispiel #3
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:')

    # Handle method calls
    technique_name = technique['bundle_name']
    # Filter calls so we only have those who are not using a any class_context
    filter_calls = [
        method_call for method_call in technique["method_calls"]
        if method_call['class_context'] != "any"
    ]

    for method_call in filter_calls:

        method_name = method_call['method_name']
        generic_method = generic_methods[method_name]

        key_value = method_call["args"][generic_method["class_parameter_id"] -
                                        1].replace("\\'", "\'")
        regex = re.compile("[^\$\{\}a-zA-Z0-9_](?![^{}]+})|\$(?!{)")
        # to match cfengine behaviour we need to treat utf8 as if it was ascii (see #7195)
        # string should be unicode string (ie u'') which is the case if they are read from files opened with encoding="utf-8"
        key_value = key_value.encode("utf-8").decode("iso-8859-1")
        key_value_canonified = regex.sub("_", key_value)

        class_prefix = generic_method[
            "class_prefix"] + "_" + key_value_canonified
        logger_rudder_call = '"dummy_report" usebundle => log_rudder("' + generic_method[
            'name'] + ' ' + key_value + ' if ' + method_call[
                'class_context'] + '", "' + class_prefix + '", "${class_prefix}", @{args})'
        logger_rudder_call = logger_rudder_call.replace("&", "\\&")

        # 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
Beispiel #4
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
Beispiel #5
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