How to set an environment variable on a server with puppet? - ruby

I'm starting to use puppet in my current project and I'm having some issues.
I'm using a recipe to install jruby, but I want to set a environment variable (in this case, JRUBY_HOME and modify the PATH to include JRUBY_HOME/bin) after it finishes installing jruby.
Here's the recipe:
class jruby {
$jruby_home = "/opt/jruby"
exec { "download_jruby":
command => "wget http://jruby.org.s3.amazonaws.com/downloads/1.7.0.RC2/jruby-bin-1.7.0.RC2.tar.gz",
path => $path,
timeout => 0,
unless => "ls /opt | grep jruby-1.7.0",
require => Package["openjdk-7-jre-headless"]
}
exec { "unpack_jruby" :
command => "tar -zxf jruby-bin-1.7.0.RC2.tar.gz -C /opt",
path => $path,
creates => "${jruby_home}-1.7.0.RC2",
require => Exec["download_jruby"]
}
file { $jruby_home:
ensure => link,
target => "${jruby_home}-1.7.0.RC2",
require => Exec["unpack_jruby"]
}
}
So, what's the best way to add /opt/jruby as JRUBY_HOME and then add JRUBY_HOME/bin to PATH?

Solved it:
# init.pp
$jruby_sh = "/etc/profile.d/jruby.sh"
file { $jruby_sh:
ensure => present,
source => "puppet:///modules/jruby/jruby.sh",
owner => "root",
group => "root",
mode => 644,
require => File[$jruby_home]
}
# jruby.sh
export JRUBY_HOME=/opt/jruby
export PATH=$PATH:$JRUBY_HOME/bin

Related

Parse Perl Data::Dumper output with ruby

I've a directory full of state files which were printed by Perl's Data::Dumper. The content looks like this:
$VAR1 = {
'localtime' => 'Tue Jun 6 11:48:20 2017',
'lookback_history' => {
'ifHCOutOctets' => {
'1496742350.42394' => '74365529910',
'1496742455.72943' => '74366309899',
'1496742446.38562' => '74366309114',
'1496742500.42388' => '74372744112'
},
'ifHCInOctets' => {
'1496742350.42394' => '13198364950',
'1496742455.72943' => '13198718163',
'1496742446.38562' => '13198705712',
'1496742500.42388' => '13199010183'
}
},
'timestamp' => '1496742500.42388',
'ifHCOutOctets' => '74372744112',
'ifHCInOctets' => '13199010183'
};
I've to analyze if the files contain unreproducible information.
Is there a way in ruby to parse those perl dumps?
If this data is yours and you are positive it contains no unexpected/harmful strings in it, the simplest way would be (assuming the content of the file is what you have posted):
eval(File.read(file))
my_local_var = $VAR1
Since you already have the files and can't change them, one way would be to use a utility that converts them to a format your Ruby understands, like JSON. If you have the JSON module in your Perl distribution (you probably have a system Perl), you also have the json_pp utility.
So you could shell out to that, and let it convert your Perl data structure (Data::Dumper is nothing else than that) to JSON:
$ cat data.pl | json_pp -f eval -t json > data.json
and then use Ruby to convert that JSON to a Ruby data structure:
require 'json'
JSON.parse(File.read('data.json'))
#=> {
# "localtime" => "Tue Jun 6 11:48:20 2017",
# "ifHCOutOctets" => "74372744112",
# "ifHCInOctets" => "13199010183",
# # ...
# }

Gem not found when I have just installed it

I am creating a Ruby script which reads data from an Excel sheet and put its data on a MySQL database. I have written it and installed the necessary gems. However, when I try to run it via my cPanel host and I get the following error:
Array ( [0] => /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- ruby-mysql (LoadError) [1] => from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' [2] => from ../ruby/InsertarFaltantesExcel.rb:2 )
Ruby Code:
require 'rubygems'
require 'ruby-mysql'
require 'spreadsheet'
#load './spreadsheet.rb'
con = Mysql.connect('xx', 'xx', 'xx','xx')
ARGV = "--help" if ARGV.empty?
workbook = Spreadsheet.open(ARGV[0])
sheet = workbook.worksheet(0)
sheet.each do |row|
#faltantes = {
"id_verificador" => "#{row[0]}",
"order_id" => "#{row[1]}",
"id_proveedor" => "#{row[28]}",
"shipping" => "#{row[10]}",
"ean" => "#{row[4]}",
"isbn" => "#{row[5]}",
"description" => "#{row[8]}",
"sku" => "#{row[9]}",
"cost" => "#{row[40]}",
"order_price" => "#{row[14]}",
"master" => "#{row[39]}",
"quantity_purchased" => "#{row[11]}",
"total_price" => "#{row[12]}",
"condition" => "#{row[33]}",
"tracking" => "#{row[29]}"
}
insertar_faltantes(#faltantes, con)
end
def insertar_faltantes(hash, con)
statement - con.prepare("INSERT INTO articulos(art_id_verificador, art_id_orden, art_id_proveedor, art_shipping, art_N13, art_ISBN, art_titulo, art_SKU, art_cost, art_precio, art_master, art_cantidad, art_total, art_condition,
art_tracking) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?);")
statement.execute "#{hash['id_verificador']}", "#{hash['order_id']}", "#{hash['id_proveedor']}", "#{hash['shipping']}", "#{hash['ean']}", "#{hash['isbn']}", "#{hash['sku']}", "#{hash['cost']}", "#{hash['order_price']}","#{hash['master']}",
"#{hash['quantity_purchased']}", "#{hash['condition']}", "#{hash['tracking']}"
end
The gems in ~/ruby/gems/gems/ are not being recognised by your Ruby executable. Find where all the other gems are being kept and move these ones into there.
Alternatively, try using a different package manager. If you have installed gems successfully in the past, use the manager you used for that.

Add logs in different directory using daemon in ruby

I am using daemon to wrap my script and has specified logs location into that :
Script looks like this :
#!/usr/local/bin/ruby
require 'rubygems'
require 'daemons'
Daemons.run_proc(
'script_test', # name of daemon
:log_output => true,
:output_logfilename => "script-test.log",
:logfilename => "script-test.log"
) do
exec 'ruby /opt/script-test/script-test.rb'
end
Problem is my logs are storing in same directory where my script is present. I have to add my logs to different directory such as /var/log/script-test and later have to rotate those logs weekly.
Provide me with a solution so that i can store the logs of script in /var/log directory.
Make sure you are using an absolute path instead of a relative path
For example:
:output_logfilename => "/var/log/script-test.log",
:logfilename => "/var/log/script-test.log"
In order to logrotate your logs, (assuming Linux) add the following to your logrotate config to rotate on a weekly basis:
/var/log/script-test.log {
weekly
missingok
compress
notifempty
copytruncate
}
It worked for me with this configuration as :
Daemons.run_proc(
'script-test', # name of daemon
:log_output => true,
:dir_mode => :normal,
:dir => "/var/log",
:output_logfilename => "script-test.log",
:logfilename => "script-test.log"
) do
exec 'ruby /opt/script-test/script-test.rb'
end

Conditionally add an extra parameter to an associative hash

I want to conditionally add an extra parameter to an associative hash.
The existing code looks like this:
:env => {
"ANSIBLE_FORCE_COLOR" => "true",
"ANSIBLE_HOST_KEY_CHECKING" => "#{config.host_key_checking}",
# Ensure Ansible output isn't buffered so that we receive ouput
# on a task-by-task basis.
"PYTHONUNBUFFERED" => 1
},
I want to conditionally add another variable "ANSIBLE_SSH_ARGS" => "-o ForwardAgent=yes" if config.ssh.forward_agent is true.
I could just copy paste, and create an if/else block but surely Ruby has something more elegant?
I solved it like so:
env = {
"ANSIBLE_FORCE_COLOR" => "true",
"ANSIBLE_HOST_KEY_CHECKING" => "#{config.host_key_checking}",
# Ensure Ansible output isn't buffered so that we receive ouput
# on a task-by-task basis.
"PYTHONUNBUFFERED" => 1
}
env["ANSIBLE_SSH_ARGS"] ="-o ForwardAgent=yes" if config.ssh.forward_agent
command << {
:env => env,
:notify => [:stdout, :stderr],
:workdir => #machine.env.root_path.to_s
}
Not sure this is idiomatic Ruby but it worked for me.

Chef template and using it in a loop

How to write this in a loop.
I am very new to ruby so struggling with the approach.
I am trying to create a response file and then update the fixpack for IHS (IBM HTTP Server)
#Install the fix pack for IHS
template "/tmp/ihs-fixpack-response1.txt" do
source "ihs-fixpack-response.erb"
mode 0755
owner "root"
group "root"
variables({
:fixpack => "7.0.0-WS-IHS-LinuxX32-FP0000019.pak",
:product_path => node[:websphere][:ihs][:ihs_path]
})
end
# code for installing Fixpack
bash "ihs/was-updateinstaller" do
user "root"
code %(#{node[:websphere][:ihs][:ihs_updi_path]}/update.sh -options "/tmp/ihs-fixpack-response1.txt" -silent)
end
#Install the fix pack for the plugin.
template "/tmp/ihs-fixpack-response2.txt" do
source "ihs-fixpack-response.erb"
mode 0755
owner "root"
group "root"
variables({
:fixpack => "7.0.0-WS-PLG-LinuxX32-FP0000019.pak",
:product_path => node[:websphere][:ihs][:ihs_wasPluginPath]
})
end
# code for installing Fixpack
bash "ihs/was-updateinstaller" do
user "root"
code %(#{node[:websphere][:ihs][:ihs_updi_path]}/update.sh -options "/tmp/ihs-fixpack-response2.txt" -silent)
end
I believe this will do what you want:
[ [ "7.0.0-WS-IHS-LinuxX32-FP0000019.pak", node[:websphere][:ihs][:ihs_path] ],
[ "7.0.0-WS-PLG-LinuxX32-FP0000019.pak", node[:websphere][:ihs][:ihs_wasPluginPath] ]
].zip(1..2).each do |vars, i|
template "/tmp/ihs-fixpack-response#{i}.txt" do
source "ihs-fixpack-response.erb"
mode 0755
owner "root"
group "root"
variables({
:fixpack => vars.first,
:product_path => vars.last
})
end
bash "ihs/was-updateinstaller" do
user "root"
code %(#{node[:websphere][:ihs][:ihs_updi_path]}/update.sh -options "/tmp/ihs-fixpack-response#{i}.txt" -silent)
end
end

Resources