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

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

Related

chef - how to get path of cookbook in a recipe

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]

sass --interactive returns FormatException: Could not find an option named "interactive"

I've get a FormatException error every time i try to invoke the sass interactive shell. Please does anyone know the reason for this and how it can be resolved. i've tried googling but all i get is syntax errors and nothing about this. please find below the error message returned.
thanks in advance for your help.
my_username$ sass --interactive
/usr/local/lib/node_modules/sass/sass.dart.js:1436
if(this===init.globalState.e)throw u}}finally{this.cy=x
^
FormatException: Could not find an option named "interactive".
at b (/usr/local/lib/node_modules/sass/sass.dart.js:1916:3)
at q (/usr/local/lib/node_modules/sass/sass.dart.js:1922:23)
at iu.fo (/usr/local/lib/node_modules/sass/sass.dart.js:6358:14)
at iu.aC (/usr/local/lib/node_modules/sass/sass.dart.js:6256:9)
at /usr/local/lib/node_modules/sass/sass.dart.js:8058:42
at uM.a (/usr/local/lib/node_modules/sass/sass.dart.js:3050:72)
at uM.dart.uM.$2 (/usr/local/lib/node_modules/sass/sass.dart.js:3146:24)
at tU.dart.tU.$1 (/usr/local/lib/node_modules/sass/sass.dart.js:3142:31)
at tn.fv (/usr/local/lib/node_modules/sass/sass.dart.js:3380:41)
at qv.$0 (/usr/local/lib/node_modules/sass/sass.dart.js:3329:16)
It looks like you're using Dart Sass, which doesn't (currently) support the --interactive flag. Feel free to file a feature request, though!

Ohai thinks my plugin is version 6. Why?

I'm trying to write a plugin for ohai. It seems like a pretty straightforward task:
Ohai.plugin(:Uname) do
provides 'uname'
depends 'kernel'
collect_data do
uname Mash.new
uname[:message] = `uname -a`
end
end
To me this looks like the online examples provided by Opscode, O'Reilly and others. But here's what happens when I try to test it:
% irb -rohai
irb(main):001:0> Ohai::Config[:plugin_path] << "."
=> ["/home/ll0359/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/ohai-8.3.0/lib/ohai/plugins", "."]
irb(main):002:0> o = Ohai::System.new
=> #<Ohai::System:0x007fed82e43078 #plugin_path="", #data={}, #provides_map=#<Ohai::ProvidesMap:0x007fed82e42fd8 #map={}>, #v6_dependency_solver={}, #d82e42f38 #controller=#<Ohai::System:0x007fed82e43078 ...>, #v6_plugin_classes=[], #v7_plugin_classes=[]>, #runner=#<Ohai::Runner:0x007fed82e42ec0 #prp:0x007fed82e42fd8 #map={}>, #safe_run=true>>
irb(main):003:0> o.all_plugins
And here's where the fun begins. I get this output, over and over and over:
[2015-05-20T03:13:09+00:00] WARN: Plugin Definition Error: <./ohai_uname.rb>: collect_data already defined on platform default
[2015-05-20T03:13:09+00:00] WARN: [DEPRECATION] Plugin at ./test_ohai.rb is a version 6 plugin. Version 6 plugins will not be supported in future releases....
your plugin to version 7 plugin syntax. For more information visit here: docs.chef.io/ohai_custom.html
(the text on my second line was clipped by my screen but you get the idea)
I've tried running this code with and without the 'depends' line. Same result.
I've tried running this code with and without the Mash line, substituing 'uname uname -a' for the assignment line. Same result.
I've tried running with and without passing ":linux" as a parameter to collect_data. The only difference is I get a warning about collect_data(:linux) already being defined instead of :default.
I've tried renaming the plugin to a random 8 character identifier just in case it was tripping over being called :Uname. Same result.
I've tried passing "uname" (capital and lower) as a parameter to o.all_plugins. Same result.
So my questions are:
Why does ohai (8.3, running under Ruby 2.2.1) think this is a version 6 plugin? I can't see anything in it that would make it look like it's not version 7.
How can I get this working?
Thanks
Note to self: Next time you do this, don't try to test from the directory your plugin is in and add "." to your plugin_path. Moving to a different directory and adding the absolute path to the plugin solved the problem.
I plan to leave this up in case someone else has this happen to them.

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.

Mongo::ReplSetConnection authenticate fail

This is the simple script.
require 'mongo'
database=Mongo::ReplSetConnection.new(["database1:9900", "database2:9900","database3:9900"]).db("project")
database.authenticate("user","passwd")
I got the following error:
undefined local variable or method `pools' for # <Mongo::ReplSetConnection:0x0000000441e610>
from /home/tommy/.rvm/gems/ruby-1.9.3-p194#myrails3/gems/mongo-1.7.0/lib/mongo/repl_set_connection.rb:309:in `authenticate_pools'
Has anyone encountered such problem before? Is there any suggestions?
The following method works for me. But I don't know why.
con=Mongo::ReplSetConnection.new(["host1:14000", "host2:14000", "host3:14000"])
con.add_auth("databasename","user","passwd")
con.apply_saved_authentication()
This seems to be related to the following bugfix:
https://github.com/mongodb/mongo-ruby-driver/pull/112
If you change that code on your driver it will work.

Resources