Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

puppet template remove the last comma

Tags:

ruby

erb

puppet

I have the following puppet example template:

{
  "servers" : [ {
    "port" : 9200,
    "host" : "localhost",

    "queries" : [
      <% @markets.each do |market| -%>
      {
      "outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ],
      "obj" : "solr/market_<%= market %>:type=queryResultCache,id=org.apache.solr.search.LRUCache",
      "attr" : [ "hits","hitratio" ]
    },
    <% end -%>
    ],
    "numQueryThreads" : 2
  } ],
}

Applying it with market=['UK','FR','IT'], I get the following:

{
  "servers" : [ {
    "port" : 9200,
    "host" : "localhost",

    "queries" : [
            {
      "outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ],
      "obj" : "solr/market_UK:type=queryResultCache,id=org.apache.solr.search.LRUCache",
      "attr" : [ "hits","hitratio" ]
    },
          {
      "outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ],
      "obj" : "solr/market_FR:type=queryResultCache,id=org.apache.solr.search.LRUCache",
      "attr" : [ "hits","hitratio" ]
    },
          {
      "outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ],
      "obj" : "solr/market_IT:type=queryResultCache,id=org.apache.solr.search.LRUCache",
      "attr" : [ "hits","hitratio" ]
    },
        ],
    "numQueryThreads" : 2
  } ],
}

The problem is the last comma, which makes it an invalid solr config.

Instead of applying markets.each do, I could use market.map and join(','). but how to use map in this case?

I can use map as follows:

<%= @markets.map{ |market| "hello_"+market }.join(',') -%>

this would print hello_UK,hello_FR,hello_IT (note that we don't have a comma after hello_IT), but I would need something like this:

{
  "servers" : [ {
    "port" : 9200,
    "host" : "localhost",

    "queries" : [
      <% @markets.map |market| -%>
      {
      "outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ],
      "obj" : "solr/market_<%= market %>:type=queryResultCache,id=org.apache.solr.search.LRUCache",
      "attr" : [ "hits","hitratio" ]
    },
    <% }.join(',') -%>
    ],
    "numQueryThreads" : 2
  } ],
}

this does not work.

so, how to make it work? or how to modify my puppet template to remove the last comma?

like image 561
David Portabella Avatar asked Aug 13 '13 09:08

David Portabella


2 Answers

This is actually a Ruby problem rather than a Puppet problem. Since this is an array, just change the .each to .each_with_index. Then you can wrap the final comma in a check to see whether the current index value is one less than the size of the index. For example, the following code adds a comma only if the current market is not the last one in the array:

<% @markets.each_with_index do |market, i| -%>

and then

}<%= ',' if i < (@markets.size - 1) %>
like image 177
itsbruce Avatar answered Oct 24 '22 12:10

itsbruce


I saw on a puppet labs site (https://ask.puppetlabs.com/question/4195/joining-array-from-hieraconcat-other-value-in-erb/) this form of solution.

<%= quorum.map{ |srv| "#{srv}:#{portNum}" }.join(',') %>

This code lets you take an array and create a variation of each element then join them all together with the appropriate join text (not including a spurious comma at end).

like image 37
Ben Avatar answered Oct 24 '22 11:10

Ben