chef - how to get path of cookbook in a recipe - ruby

From default.rb, I want to access the relative path of the cookbook.
I tried doing:
print "cookbook path: " + run_context.cookbook_collection[cookbook_name].root_dir
but I get
TypeError
---------
no implicit conversion of nil into String
I tried replacing cookbook_name with my cookbook name, but get the same error.
Any help would be appreciated.

This is not a supported API, we do not offer it in an official capacity. Any use of such APIs is at your own risk and will break in the future so you should be comfortable enough with reading the code to find things yourself.

The following worked for me: Chef::Config[:cookbook_path]

Related

What is the proper way to get FQDN of current host?

For some time I was using code:
hostname = Socket.gethostbyname(Socket.gethostname).first
taken from here.
But, as the comment said - it's deprecated, and now both rubocop and ruby itself are complaining:
=$ ./z.rb
./z.rb:5: warning: Socket.gethostbyname is deprecated; use Addrinfo.getaddrinfo instead.
=$ rubocop z.rb
...
z.rb:5:13: W: Lint/DeprecatedClassMethods: Socket.gethostbyname is deprecated in favor of Addrinfo#getaddrinfo.
puts Socket.gethostbyname(Socket.gethostname).first
^^^^^^^^^^^^^
The problem is that I don't know how to use getaddrinfo to get the same information. Anyone could show an example?
For now, I figured I'll just use:
`hostname --fqdn`
which works, as I only run it on Linux, but it's not really nice approach.
Got help in some other way, but figured others might benefit too.
Current way to do it is:
require 'socket'
Addrinfo.getaddrinfo(Socket.gethostname, nil).first.getnameinfo.first

Ruby returns uninitialized constant error when trying to include a module within a chef recipe

I have a java/recipes/windows recipe that uses a method called win_friendly_path and it doesn't work because win_friendly_path is not yet defined.
win_friendly_path is however defined in the ../windows/libraries/windows_helper.rb as follows:
module Windows
module Helper
def win_friendly_path(path)
path.gsub(::File::SEPARATOR, ::File::ALT_SEPARATOR || '\\') if path
end
I already have my berksfile and metadata.rb setup in the java (./) recipe to depend on the windows cookbook.
I'm not sure how to include this module so right now I am trying to just use include WindowsHelper in the java/cookbook/windows recipe and receiving this error:
uninitialized constant #<Class:#<Chef::Recipe:0x00000000029a2188>>::WindowsHelper
I have tried several variations of this and now feel like I have spent way too much time troubleshooting the issue so any help is appreciated.
UPDATE: plugging in this line ::Chef::Resource.send(:include, Windows::Helper) to my java/recipes/windows recipe gives me the following error:
Chef::Exceptions::ValidationFailed
----------------------------------
value is a required property
Try this
include Windows::Helper
Inserting the following line resolved this issue for me:
::Chef::Recipe.send(:include, Windows::Helper)
This allows me to use the variables following module from the windows cookbook:
module Windows
module Helper
...
{Variable}
{Other_variable}
...

Print vars with Test Kitchen - Chef

I'm working with Chef and Test Kitchen, I have some problems installing Apache 2.2 and I was reading the cookbook with my recipe (https://github.com/sous-chefs/apache2/blob/master/recipes/default.rb), I would like to know how this recipe is working when installed Apache and I want print some variables when I launch my recipe:
node['platform_family']
node['platform_version']
An example from Chef official doc:
Chef::Log.fatal('You did not accept the license (set node["splunk"]["accept_license"] to true)')
But I write in my recipe:
Chef::Log.info('PERSONAL-LOG,node["platform_family"]')
I get the log, but exactly with the same text I write, I cant concatenate or call the var directly.
Can someone help me?
Its a case of ruby string interpolation, please use it as:-
Chef::Log.info("PERSONAL-LOG, #{node['platform_family']}")
http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html

Ruby code on Chef as a "ruby_block" not working

I have a question for the Ruby and Chef hackers.
I have very limited knowledge of Chef and even less on Ruby programming language, however, I need to implement on Chef (chef-solo) something similar to "augeas" (which works with Puppet, but here I need a solution for Chef).
I got the example code below but it's not working and I am now for a few days trying to figure out what is wrong.
Basically I need to be able to select specific strings in a text file and modify these values. I could use sed but perhaps I can do it in a more elegant way using the ruby_block from Chef.
Please let me know what can be possibly wrong with the code below. Why is my /etc/hosts not being updated with new values?
Always when I re-run chef-solo, I get the following error:
NoMethodError
-------------
undefined method `chef' for Chef::Resource::RubyBlock
Thanks for your help.
Follows my default.rb file:
ruby_block "edit etc hosts" do
block do
rc = Chef::Util::FileEdit.new("/etc/hosts")
rc.search_file_replace_line(
/^127\.0\.0\.1 localhost$/,
"127.0.0.1 #{new_fqdn} #{new_hostname} localhost"
)
rc.write_file
end
end
Add this line as the first line of your ruby block:
require 'chef/util/file_edit'
According to your case, you should use the cookbook hostsfile:
hostsfile_entry '127.0.0.1' do
hostname new_hostname
aliases [new_fqdn]
comment 'Append by Recipe X'
action :append
end
It shouldn't be too hard to get Augeas to work with Chef. Augeas is a C library, and Puppet simply uses its Ruby bindings. You just need to make use of these bindings in Chef.
There is a PoC Augeas resource provider for Chef here: https://github.com/craigtracey/augeas.
Note: http://lists.opscode.com/sympa/arc/chef/2013-02/msg00337.html mentions Augeas integration into Chef, but apparently the participants misunderstand Augeas as they mention idempotency issues and deltas. Most uses of Augeas don't lead to managing deltas, but desired states.

How can I define PATH for Phusion Passenger under nginx with Bundler?

Please forgive my near total ignorance of Ruby, but I am having an issue with Phusion Passenger in that it attempts to run something that relies on $PATH (and blindly assumes it is defined and a string). However, nginx evidently clears out the variable.
Error message:
private method `split' called for nil:NilClass
...
Backtrace:
# File Line Location
0 /usr/lib64/ruby/gems/1.8/gems/bundler-1.1.0/lib/bundler.rb 254 in `which'
That line reads:
path = ENV['PATH'].split(File::PATH_SEPARATOR).find do |p|
If there is a workaround for this, I would greatly appreciate it, as I would like to avoid using Apache if at all possible.
have you tried setting the path within your nginx config? Something to the effect:
env PATH=/some/path/expected:/another/path;
See: http://wiki.nginx.org/CoreModule#env

Resources