Finding which OS?

hi -

is there a way to find which OS Ruby is running on?

also, searching for ruby clues, i usually resort to google, wondering
how other people find stuff?

/dc


  David "DC" Collier

mailto:[email protected]
+81 (0)80 6521 9559
skype: callto://d3ntaku

On 16/11/06, dc [email protected] wrote:

  David "DC" Collier

mailto:[email protected]
+81 (0)80 6521 9559
skype: callto://d3ntaku

  Pikkle e$B3t<02q<Re(B
  http://www.pikkle.com

C:\Documents and Settings\flifson>irb
irb(main):001:0> PLATFORM
=> “i386-mswin32”
irb(main):002:0>

Farrel

On Nov 16, 2006, at 3:17 AM, dc wrote:

hi -

is there a way to find which OS Ruby is running on?

Facter[1] does this and much more:

luke@midden(0) $ irb
irb(main):001:0> require ‘facter’
=> true
irb(main):002:0> Facter.value(:operatingsystem)
=> “Darwin”
irb(main):003:0> Facter.macaddress
=> “00:0a:95:96:5f:64”
irb(main):004:0> Facter.to_hash.each do |p, v| next if p =~ /key/;
puts “%s => %s” % [p, v] end
kernel => Darwin
rubysitedir => /usr/lib/ruby/site_ruby/1.8
operatingsystemrelease => 8.8.0
hardwaremodel => Power Macintosh
ipaddress => 192.168.0.4
kernelrelease => 8.8.0
fqdn => midden.madstop.com
ps => ps -auxwww
domain => madstop.com
rubyversion => 1.8.2
puppetversion => 0.20.1
hostname => midden
facterversion => 1.3.3
operatingsystem => Darwin
macaddress => 00:0a:95:96:5f:64
home => /Users/luke

Note that Facter distinguishes the kernel from the operatingsystem;
you’ll correctly get ‘Debian’ or ‘Fedora’ as your operating system,
for instance, not ‘Linux’.

Also, FWIW, Facter is pretty darn easy to extend:

Facter.add(:myfact) do setcode { …find your data } end

1 - http://reductivelabs.com/projects/facter

C:\Documents and Settings\flifson>irb
irb(main):001:0> PLATFORM
=> “i386-mswin32”
irb(main):002:0>

… or from inside a Ruby script:

puts RUBY_PLATFORM

Where to find resources:

http://wiki.rubygarden.org/Ruby
Ruby Programming Language (down just now?)

Documentation:
http://www.ruby-doc.org/docs/ProgrammingRuby/

Some code snippets:
http://pleac.sourceforge.net/pleac_ruby/index.html

Search for help
http://groups.google.com/group/comp.lang.ruby?hl=en&lr=&ie=UTF-8&num=50

Libraries:
http://raa.ruby-lang.org/
http://rubyforge.org/ (down just now?)

Book with a lot of “How Tos”:
“The Ruby Way” by Hal F.

Have a nice day!

Axel

Luke K. wrote:

=> true
ipaddress => 192.168.0.4
home => /Users/luke

–Luke K.
http://madstop.com | http://reductivelabs.com | 615-594-8199

I use this:
myhome = ENV[‘HOME’]
path = 'load path: '+$:.to_s
script = 'Script location = ‘+Dir.pwd+$0+’ pid id '+$$.to_s
rubyinfo = 'OS = ‘+RUBY_PLATFORM+’ and Ruby version = '+VERSION
puts ‘Below is script location’
puts script
puts ‘Below is rubyinfo’
puts rubyinfo
puts ‘Below is load path’
puts path
puts ’ My Shell enviroment = ’ + ENV[‘SHELL’]
puts ’ My Home dir = ’ + ENV[‘HOME’]
puts ’ My username = ’ + ENV[‘USER’]


Regards
Dave Ashmore
SOHO IT Solutions llc
1934 S. Delaware Dr.
Easton, PA 18042
610-258-7128
http://callsohoit.com
Sometimes I can be reached via Skype as djash34
Disclaimer: The below quote is randomly generated and I may or
may not agree with the quote.
################################################################
HELP! MY TYPEWRITER IS BROKEN!
– E. E. CUMMINGS
################################################################

On Fri, 17 Nov 2006, Luke K. wrote:

=> true
irb(main):002:0> Facter.value(:operatingsystem)
=> “Darwin”
irb(main):003:0> Facter.macaddress
=> “00:0a:95:96:5f:64”
irb(main):004:0> Facter.to_hash.each do |p, v| next if p =~ /key/; puts "%s
^^^^^^^^^^^^^
^^^^^^^^^^^^^

can you explain that? custom .to_s?

seems like you should be able to do

y Facter.table(‘key’) # or :key

for similar effect, or some such

looks very cool btw - i didn’t know about Facter.

cheers.

-a

On Fri, 17 Nov 2006, Luke K. wrote:

their values.

I’m skipping any fact that matches /key/ here because Facter knows how to
retrieve SSH keys, which are huge and would have polluted the output. I had
to pick between confusing people by skipping those values, or confusing
people by including them. :slight_smile:

okay - it makes perfect sense then

seems like you should be able to do

y Facter.table(‘key’) # or :key

for similar effect, or some such

What would be the intent of that?

pls ignore - in light of above explanation…

I originally wrote it a few years ago as an inventorying mechanism –
there’s a simple tool[1] that stores these facts into LDAP – but now Puppet
uses it heavily. Every time a Puppet client connects to the server, it
collects all of its known facts and passes them to the server as a hash.
The server then sets these facts as variables in the top-level scope. Thus,
you can use $operatingsystem or $ipaddress in your Puppet configurations and
use this data to make decisions in your configuratin.

well, i happen to be working on resource requests for ruby queue, eg

rq queue submit --requirements=“cpu > 3.0mhz and mem > 64gb” –
job.rb

so you can see what’s applealing about it!

cheers.

-a

On Nov 16, 2006, at 2:43 PM, [email protected] wrote:

irb(main):004:0> Facter.to_hash.each do |p, v| next if p =~ /key/;
puts "%s
^^^^^^^^^^^^^
^^^^^^^^^^^^^

can you explain that? custom .to_s?

Facter is meant to be a collection of mechanisms for retrieving a
named piece of data; e.g., ‘uname -s’ is the most common way of
retrieving the operating system name, but it’s useless on any Linux
distro so instead I look for /etc/redhat-release or use lsbrelease.
Thus, it’s pretty easy to think of Facter as a big hash, but instead
of simple values it has one or more ways of retrieving each value.

So, I just implemented to_hash as a way of returning all known facts
and their values.

I’m skipping any fact that matches /key/ here because Facter knows
how to retrieve SSH keys, which are huge and would have polluted the
output. I had to pick between confusing people by skipping those
values, or confusing people by including them. :slight_smile:

seems like you should be able to do

y Facter.table(‘key’) # or :key

for similar effect, or some such

What would be the intent of that? I think we’re each missing
something here. Facter.to_hash just returns has hash of all of the
facts it knows about. Everything after that is normal, non-Facter
ruby. What would ‘table’ be used for?

looks very cool btw - i didn’t know about Facter.

I originally wrote it a few years ago as an inventorying mechanism –
there’s a simple tool[1] that stores these facts into LDAP – but now
Puppet uses it heavily. Every time a Puppet client connects to the
server, it collects all of its known facts and passes them to the
server as a hash. The server then sets these facts as variables in
the top-level scope. Thus, you can use $operatingsystem or
$ipaddress in your Puppet configurations and use this data to make
decisions in your configuratin.

1 - http://reductivelabs.com/projects/enhost

On Fri, 17 Nov 2006, Luke K. wrote:

well, i happen to be working on resource requests for ruby queue, eg

rq queue submit --requirements=“cpu > 3.0mhz and mem > 64gb” – job.rb

Yeah. There are currently very limited facts for cpu and memory information,
but they could be expanded pretty easily.

By the way, I’ve been using the term ‘confine’ to describe matching these

yes! i’ll take it.

thanks.

-a

On Nov 16, 2006, at 4:38 PM, [email protected] wrote:

On Fri, 17 Nov 2006, Luke K. wrote:

I’m skipping any fact that matches /key/ here because Facter knows
how to retrieve SSH keys, which are huge and would have polluted
the output. I had to pick between confusing people by skipping
those values, or confusing people by including them. :slight_smile:

okay - it makes perfect sense then

Okay. whew

well, i happen to be working on resource requests for ruby queue, eg

rq queue submit --requirements=“cpu > 3.0mhz and mem > 64gb” –
job.rb

Yeah. There are currently very limited facts for cpu and memory
information, but they could be expanded pretty easily.

By the way, I’ve been using the term ‘confine’ to describe matching
these requirements; as in, ‘confine this code to hosts that match
these requirements’. Facter uses this internally to confine a
resolution mechanism to specific platforms or organizations. E.g.,
here’s how you set up one of the ways to retrieve the hostname on OS
X (darwin):

     Facter.add("Hostname") do
         confine :kernel => :darwin, :kernelrelease => "R7"
         setcode do
             %x{/usr/sbin/scutil --get LocalHostName}
         end
     end

You can confine resolutions based on any other fact values.

Puppet providers (the low-level code that handles things like
different package types or different user management tools) use a
similar mechanism. Mostly I confine providers based on available
binaries:

Puppet::Type.type(:package).provide(:dpkg) do
confine :exists => “/usr/bin/dpkg”

end

But you can also confine just like in Facter:

Puppet::Type.type(:package).provide(:darwinports) do
confine :operatingsystem => :darwin

end

So, not saying you have to adopt my terminology or something, but
I’ve been pretty happy with it.