rubychef-infracookbook

chef : enable service based on a "grep" result


I had a first question, but apparently, I was doing things in the wrong way, so trying this new approach. I'm creating a systemd service file to run multiple instances of the service :

if ( (node[:platform] == "redhat" &&  node[:platform_version] >= "7"))
  cookbook_file "/usr/lib/systemd/system/sec@.service" do 
    source "usr/lib/systemd/system/sec@.service"
    owner "root"; group "root"; mode "0644"
  end
end

The number of instances (and names) is configured into a file, with one or multiple entries like :

OPTIONS_0="--conf=/etc/sec/oracle.sec --input=/var/log/oracle.log "
OPTIONS_1="--conf=/etc/sec/jboss.sec --input=/var/log/jboss.log "

Now, I need to enable as many services as OPTIONS_X exist in this file. As a "bash-man", I would grep, but I suppose there must exist a right way to do this in ruby, so that in the end, it will run :

service "sec@0" do action [:enable, :start]  end
service "sec@1" do action [:enable, :start]  end
[...]
service "sec@X" do action [:enable, :start]  end

Thank you.


Solution

  • Since the basis for the list of services is from a file, bash/shell utilities would be more efficient in getting the required details, i.e. the count of OPTIONS. This also means that this file would need to be maintained/updated correctly for this logic to work.

    Something like:

    grep OPTIONS_ path/to/file | wc -l
    

    This count can be stored in a variable and services can be enabled/started based on this.

    count = `grep OPTIONS_ path/to/file | wc -l`
    
    count.to_i.times do |num|
      service "sec@#{num}" do
        action [:enable, :start]
      end
    end