Ruby: How can I group multiple blocks? - ruby

I have this code where execute is a Chef resource:
execute 'phpbrew_0' do
command 'phpbrew --verbose install 5.6 +apxs2'
end
execute 'phpbrew_1' do
command 'phpbrew --verbose install 7.0 +apxs2'
end
execute 'phpbrew_2' do
command 'phpbrew --verbose install 7.1 +apxs2'
end
Does ruby have this feature where I can provide a collection of command and have them all be passed to the execute resource. Something like Javascript's Array.map?

Yep, Hash#each, for example: https://ruby-doc.org/core-2.4.1/Hash.html#method-i-each
resource_to_command = {
'phpbrew_14' => 'phpbrew --verbose install 5.6 +apxs2',
'foo' => 'bar'
}
resource_to_command.each do |resource, cmd_text|
execute resource do
command cmd_text
end
end

Related

Running bash scripts in Homebrew Formulae

I have created a brew formulae for one of my github repos. Now I want run 2 bash scripts that are present in the project.
My formulae looks likes this :
class UnifaiCore < Formula
##branch_name = "brew-installer"
desc ""
homepage "homepage_url"
url "project_url",
:branch => ##branch_name
version "v0.1.0"
license ""
def install()
puts "inside install"
puts %x(whoami)
venv_setup_script = "unifai-core-venv-setup.sh"
installer_script = "unifai-core-installer.sh"
bin.install venv_setup_script
exec("#{prefix}/bin/#{venv_setup_script}")
bin.install installer_script
exec("#{prefix}/bin/#{installer_script}")
# I have also tried system, %x() to run these bash scripts, have also tried adding source in front.
end
end
Now when ever I am trying to run this using brew install command. I am getting permission denied error.
Error: An exception occurred within a child process:
Errno::EACCES: Permission denied - /usr/local/Cellar/unifai-core/v0.1.0/bin/unifai-core-venv-setup.sh
I have tried following things till now :
How to fix homebrew permissions?
giving chmod 775 to the folder
I also checked which user it's using to execute the scripts but printing whoami. It's the same user that I have used to run these bash scripts in my terminal.

Install a particular Ruby version using chef-run

I have been trying to install a particular (latest) version of Ruby using Chef Workstation and its included chef-run CLI.
This is the recipe I'm using for Ruby:
package 'ruby' do
version '2.5.3'
action :install
end
Which, running with the command line
chef-run -i /path-to/private_key user#host ruby.rb
Produces the not very helpful message:
[✔] Packaging cookbook... done!
[✔] Generating local policyfile... exporting... done!
[✖] Applying ruby from ruby.rb to target.
└── [✖] [127.0.0.1] Failed to converge ruby.
The converge of the remote host failed for the
following reason:
Expected process to exit with [0], but received '100'
I have tried to run it with the -V flag, or look for a log file, but I can't seem to find it. Any idea?
raise the log_level by setting it to debug in the chef-workstation configuration
$ cat ~/.chef-workstation/config.toml
[log]
level="debug"

Set environment variables for current chef-client run

I'm trying to use chef to install OpenJDK, as well as downoad Eclipse and install a few plugins using p2 director on a Windows 2008 node. OpenJDK installs and I set my environment variables JAVA_HOME and add it to the path.
However, this change does not take affect until I close and re-open PowerShell. The chef-client run needs these in the current session to run the eclipse p2 director. Is there any way to do this so that I can run chef-client only once?
In my recipe for installing openJDK I included:
env "JAVA_HOME" do
value 'C:\\Program Files\\Zulu\\zulu-8'
end
env "path" do
delim ";"
value '%JAVA_HOME%\\bin'
action :modify
end
#For Command Prompt
execute "setPathCMD" do
command "set PATH=#{node['java']['path']}\\bin;%PATH%"
end
#For PowerShell
powershell_script "setPathPS" do
code <<-EOH
$env:Path="#{node['java']['path']}\\bin;$env:Path"
EOH
end
ENV['Path'] += ";C:\\Program Files\\Zulu\\zulu-8\\bin"
And in the recipe for installing the eclipse plugins I have:
if not node['eclipse']['plugins'].empty?
node['eclipse']['plugins'].each do |plugin_group|
repo, id = plugin_group.first
execute "eclipse plugin install" do
command "#{node['eclipse']['unzip_location']}/eclipse/eclipse.exe -application org.eclipse.equinox.p2.director -noSplash -repository #{repo} -installIUs #{id}"
action :run
end
end
end
Try using setx:
execute 'set java_home' do
command "setx -m JAVA_HOME \"C:\\Program Files\\Zulu\\zulu-8\""
only_if { ENV['JAVA_HOME'] != 'C:\\Program Files\\Zulu\\zulu-8' }
end
# Set JAVA_HOME for this process
ENV['JAVA_HOME'] = 'C:\\Program Files\\Zulu\\zulu-8'
# do something similar for path...
Adapted from the visualstudio cookbook for enabling NuGet package restore:
https://github.com/daptiv/visualstudio/blob/master/recipes/nuget.rb
Put on your client.rb or solo.rb:
ENV['VAR'] = 'value'

Error executing Ruby in Jenkins

I'm running Jenkins ver. 1.537 on Win7 64.
I'm trying to run a Ruby script in Jenkins using the "Execute Ruby script" build function.
The command is C:/autotest/Ultrapos_FX2-3/lib/testout.rb.
The script I'm trying to run is a simple hello world type thing:-
begin
puts 'TEST OUTPUT'
end
I can run this sucessfully from the command prompt, but Jenkins gives me the following error:-
Started by user anonymous
Building in workspace C:\Program Files (x86)\Jenkins\jobs\ruby\workspace
[workspace] $ ruby -v C:\Users\ithomp\AppData\Local\Temp\hudson8549990801552818192.rb
ruby 2.0.0p247 (2013-06-27) [i386-mingw32]
C:/Users/ithomp/AppData/Local/Temp/hudson8549990801552818192.rb:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input
C:/autotest/Ultrapos_FX2-3/lib/testout.rb
^
Build step 'Execute Ruby script' marked build as failure
Finished: FAILURE
It seems that Jenkins is not executing the script that I'm specifing and instead calling some temp file.
Any ideas as to what the issue is?.
Thanks.
Ian.

How do I uninstall DMG's with puppet?

So I have a local DMG that I'm installing with puppet (VirtualBox-4.2.18-88780-OSX.dmg), and I run it with
sudo puppet resource package virtualbox ensure=present provider=pkgdmg source=puppet:///virtualbox/VirtualBox-4.2.18-88780-OSX.dmg,
and everything works fine. But when I try to remove it with sudo puppet resource package virtualbox ensure=absent, I get an error
Error: Could not set 'absent' on ensure: undefined method 'uninstall' for #<Puppet::Type::Package::ProviderPkgdmg:0x107cb8218>
I have a vague idea of why this is happening, it doesn't look like puppet is recognizing the virtualbox uninstall tool. How do I fix this?
millmouse is correct OS X packages can't be uninstalled, at least by this method. Puppet doesn't support 'absent' on appdmg or apppkg providers.
You can however trick Puppet to reinstall a package by removing the 'cookie' like file it creates to track the package was installed. Puppet creates a file in /var/db with a pattern like .puppet_<provider>_installed_<package_name>-<version> on OS X; for example you'll have a file like /var/db/.puppet_pkgdmg_installed_VirtualBox-4.2.18-88780
You could do something like the following, but it won't actually uninstall the app only trick Puppet into allowing it to be installed again:
exec {'rm -f .puppet_pkgdmg_installed_VirtualBox-4.2.18-88780':
cwd => /var/db/',
user => 'root',
onlyif => 'test -f /var/db/.puppet_pkgdmg_installed_VirtualBox-4.2.18-88780',
}
or
file {'/var/db/.puppet_pkgdmg_installed_VirtualBox-4.2.18-88780':
ensure => 'absent',
force => true,
}
Otherwise the version number or name of the package needs to change in order to install again.
I would use an exec resource to do the uninstall rather than the package resource.
exec { "uninstall_mypkg" :
command => "uninstall mypkg",
onlyif => "check if the package is installed",
path => "/path/to/command/",
}

Resources