how can i get each java version in each custom facts

154 views
Skip to first unread message

Supriya Uppalapati

unread,
Oct 8, 2014, 2:26:04 PM10/8/14
to puppe...@googlegroups.com
Hi All,

I am trying to get all the java versions and paths where it is located. I got all java_paths in a fact called java_path. Now i need those versions in each custom facts.

I wrote a code but this not executing .

Please help me

require 'rexml/document'
require 'facter'

Facter.add("java_path") do
  setcode do
    Facter::Util::Resolution.exec('/home/suppalapati/java.sh')
    end
end




logfile = '/home/suppalapati/java.txt'

line_num=0

log = Facter::Util::FileRead.read(logfile)
unless log.nil?
  log.each_line do |line|
    if line.include?('java')
       line_num += 1
       javaid, val =  %x[ #{log} ]
    Facter.add("java_#{javaid}") do
      setcode do
         val
        end
       end
     end
   end

 Facter.add("java_version_count") do
  setcode do
    line_num
  end
 end
end


Here is my java.txt file

/usr/java/jre1.7.0_51/bin/java
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java
/opt/CLM-Web-Tools/im/linux.gtk.x86_64/jre_6.0.0.sr9_20110208_03/jre/bin/java
/opt/CLM-Web-Tools/im/linux.gtk.x86/jre_6.0.0.sr9_20110208_03/jre/bin/java
/opt/IBM/TeamConcertBuild/jre/bin/java
/opt/IBM/InstallationManager/eclipse/jre_6.0.0.sr9_20110208_03/jre/bin/java
/opt/IBM/InstallationManager_old/InstallationManager_old/eclipse/jre_6.0.0.sr9_20110208_03/jre/bin/java
/opt/IBM/TeamConcertBuild_old/jre/bin/java
/opt/itm/v6.2.2/JRE/lx8266/bin/java
/var/lib/alternatives/java
/u01/app/oracle/product/jdk1.7.0_25/jre/bin/java
/u01/app/oracle/product/jdk1.7.0_25/bin/java


Please help me

Andy Parker

unread,
Oct 8, 2014, 3:26:16 PM10/8/14
to puppe...@googlegroups.com
On Wed, Oct 8, 2014 at 11:26 AM, Supriya Uppalapati <supriya.u...@gmail.com> wrote:
Hi All,

I am trying to get all the java versions and paths where it is located. I got all java_paths in a fact called java_path. Now i need those versions in each custom facts.

I wrote a code but this not executing .


Can you provide more details about how it isn't  executing? Is it producing an error? Are any of your facts showing up (java_version_count for example)? Are you sure that you've put the custom fact file in the correct location? Have you followed the guide at https://docs.puppetlabs.com/facter/2.2/custom_facts.html for how to write and use custom facts?
 

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/fe7859b3-2eac-4e9c-b495-d4e20dc6dac1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Andrew Parker
Freenode: zaphod42
Twitter: @aparker42
Software Developer

Join us at PuppetConf 2014, September 20-24 in San Francisco - www.puppetconf.com 

Supriya Uppalapati

unread,
Oct 8, 2014, 3:32:05 PM10/8/14
to puppe...@googlegroups.com
Hi,

It isn't executing anything. it means it is not showing individual fact for each java version. yes my facts(java_version_count ) is showing me how many lines(means java) i have in java.txt. yes i placed in wright location.I followed the  link which you provided me.

I feel some thing is wrong here, i think

logfile = '/home/suppalapati/java.txt'

line_num=0

log = Facter::Util::FileRead.read(logfile)
unless log.nil?
  log.each_line do |line|
    if line.include?('java')
       line_num += 1
       javaid, val =  %x[ #{log} ]
    Facter.add("java_#{javaid}") do
      setcode do
         val
 

Can you correct me?


Thank You

--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-dev/P6o7RUvvNFo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/CANhgQXuHw%2BVWX47Knw6tNeY2A0c0y5P%3DpRo1%2BWC5gK_ipvXh1A%40mail.gmail.com.

Andy Parker

unread,
Oct 8, 2014, 3:45:23 PM10/8/14
to puppe...@googlegroups.com
On Wed, Oct 8, 2014 at 12:32 PM, Supriya Uppalapati <supriya.u...@gmail.com> wrote:
Hi,

It isn't executing anything. it means it is not showing individual fact for each java version. yes my facts(java_version_count ) is showing me how many lines(means java) i have in java.txt. yes i placed in wright location.I followed the  link which you provided me.

I feel some thing is wrong here, i think


A useful tool to understand and debug ruby code is irb. With irb you can try out your code interactively. To try out some of your code here you can do:

> irb
1.9.1 :001 > require 'facter'
 => true
1.9.1 :004 > require 'facter/util/file_read'
 => true
1.9.1 :005 > hosts = Facter::Util::FileRead.read("/etc/hosts")
 => ....
1.9.1 :007 > hosts.each_line do |line|
1.9.1 :008 >     puts line
1.9.1 :009?>   end
  ....

logfile = '/home/suppalapati/java.txt'

line_num=0

log = Facter::Util::FileRead.read(logfile)
unless log.nil?
  log.each_line do |line|
    if line.include?('java')
       line_num += 1
       javaid, val =  %x[ #{log} ]

If you try running one of these lines you'll see what is happening:

1.9.1 :012 >   javaid, val = %x{java}
  ...lots of output...
1.9.1 :013 > javaid
  ...same output as above...
1.9.1 :014 > val
  => nil

So you are ending up with nil as the value of your fact and the name of the fact is the output of your command. You can't assign the output of a process execution to a list of variables, only the first variable will receive a value. You'll need to parse the output of that command to get whatever value it is that you are looking for.
 

For more options, visit https://groups.google.com/d/optout.

Supriya Uppalapati

unread,
Oct 8, 2014, 3:59:57 PM10/8/14
to puppe...@googlegroups.com
Hi Andrey,

When i tried your example, It is not returning any values

require 'facter/util/file_read'
=> true
irb(main):005:0> log = Facter::Util::FileRead.read("/home/suppalapati/java.txt")
=> "\n/usr/java/jdk1.7.0_25/jre/bin/java\n/usr/java/jdk1.7.0_25/bin/java\n/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/jre/bin/java\n/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java\n\n"
irb(main):006:0> log.each_line do |line|
irb(main):007:1* javaid, val = %x{line}
irb(main):008:1> javaid, val = %x{java}
irb(main):009:1>  val = %x{java}
irb(main):010:1>  val = %x{line}
irb(main):011:1>  val = %x{log}
irb(main):012:1>  val = %x{i}
irb(main):013:1> value = %x['#{i} -version']
irb(main):014:1> val
irb(main):015:1>


Let me know

Supriya Uppalapati

unread,
Oct 8, 2014, 4:12:54 PM10/8/14
to puppe...@googlegroups.com
Hi,

My question is 

require 'facter/util/file_read'
=> true
irb(main):005:0> log = Facter::Util::FileRead.read("/home/suppalapati/java.txt")
=> "\n/usr/java/jdk1.7.0_25/jre/bin/java\n/usr/java/jdk1.7.0_25/bin/java\n/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/jre/bin/java\n/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java\n\n"
irb(main):006:0> log.each_line do |line|
irb(main):007:1* javaid, val = %x{line}
irb(main):008:1> javaid, val = %x{java}


It is not returning anything. My question is something i am doing wrong here to get the out. I understood the factname and the output of the factname should be different. But it is not showing any output when the running the command itself.  

Help me

Andy Parker

unread,
Oct 8, 2014, 8:18:11 PM10/8/14
to puppe...@googlegroups.com
On Wed, Oct 8, 2014 at 12:59 PM, Supriya Uppalapati <supriya.u...@gmail.com> wrote:
Hi Andrey,

When i tried your example, It is not returning any values

require 'facter/util/file_read'
=> true
irb(main):005:0> log = Facter::Util::FileRead.read("/home/suppalapati/java.txt")
=> "\n/usr/java/jdk1.7.0_25/jre/bin/java\n/usr/java/jdk1.7.0_25/bin/java\n/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/jre/bin/java\n/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java\n\n"
irb(main):006:0> log.each_line do |line|
irb(main):007:1* javaid, val = %x{line}
irb(main):008:1> javaid, val = %x{java}
irb(main):009:1>  val = %x{java}
irb(main):010:1>  val = %x{line}
irb(main):011:1>  val = %x{log}
irb(main):012:1>  val = %x{i}
irb(main):013:1> value = %x['#{i} -version']
irb(main):014:1> val
irb(main):015:1>


You won't get any output until you enter "end". IRB will only execute complete statements. Instead of simply assigning the output of the command to a variable in irb you'll be better off if you print it out as well.

log.each_line do |line|
  val = %{#{line}}
  puts val
end
 

Let me know


For more options, visit https://groups.google.com/d/optout.

Supriya Uppalapati

unread,
Oct 9, 2014, 9:40:10 AM10/9/14
to puppe...@googlegroups.com
Hi,

I did the same way you told me. Still not returning any output
irb
irb(main):001:0> logfile = '/home/suppalapati/java.txt'
=> "/home/suppalapati/java.txt"
irb(main):002:0> line_num=0
=> 0
irb(main):004:0> require 'facter/util/file_read'
=> true
irb(main):005:0> log = Facter::Util::FileRead.read(logfile)
=> "\n/usr/java/jdk1.7.0_25/jre/bin/java -version\n/usr/java/jdk1.7.0_25/bin/java -version\n/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/jre/bin/java -version\n/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java -version\n\n"
irb(main):006:0> unless log.nil?
irb(main):007:1> log.each_line do |line|
irb(main):008:2* if line.include?('java')
irb(main):009:3> line_num += 1
irb(main):010:3> javaid, val = %x[#{log}]
irb(main):011:3>  Facter.add("java_#{javaid}") do
irb(main):012:4* puts val
irb(main):013:4> end
irb(main):014:3> puts val1
irb(main):015:3> end


Let me know where i am doing mistake

Supriya Uppalapati

unread,
Oct 9, 2014, 10:02:05 AM10/9/14
to puppe...@googlegroups.com
Hi,

when i tested my code with irb, it is executing the output. when in facts it is not resulting any output

Here is my output of irb

irb
irb(main):001:0> logfile = '/home/suppalapati/java.txt'
=> "/home/suppalapati/java.txt"
irb(main):002:0> line_num=0
=> 0
irb(main):003:0> require 'facter/util/file_read'
=> true
irb(main):004:0> log = Facter::Util::FileRead.read(logfile)
=> "\n/usr/java/jdk1.7.0_25/jre/bin/java -version\n/usr/java/jdk1.7.0_25/bin/java -version\n/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/jre/bin/java -version\n/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java -version\n\n"
irb(main):005:0> unless log.nil?
irb(main):006:1>  log.each_line do |line|
irb(main):007:2* if line.include?('java')
irb(main):008:3> line_num += 1
irb(main):009:3>  javaid, val = %x[#{log}]
irb(main):010:3> puts val
irb(main):011:3>
irb(main):012:3*
irb(main):013:3* end
irb(main):014:2> end
irb(main):015:1> end
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
java version "1.7.0_45"
OpenJDK Runtime Environment (rhel-2.4.3.2.el6_4-x86_64 u45-b15)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.14) (rhel-1.65.1.11.14.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
java version "1.7.0_45"
OpenJDK Runtime Environment (rhel-2.4.3.2.el6_4-x86_64 u45-b15)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.14) (rhel-1.65.1.11.14.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
java version "1.7.0_45"
OpenJDK Runtime Environment (rhel-2.4.3.2.el6_4-x86_64 u45-b15)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.14) (rhel-1.65.1.11.14.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
java version "1.7.0_45"
OpenJDK Runtime Environment (rhel-2.4.3.2.el6_4-x86_64 u45-b15)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.14) (rhel-1.65.1.11.14.el6_4-x86_64)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

=> "\n/usr/java/jdk1.7.0_25/jre/bin/java -version\n/usr/java/jdk1.7.0_25/bin/java -version\n/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/jre/bin/java -version\n/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java -version\n\n"



Where is in the facts it is not showing anything:
$/opt/puppet/bin/facter -p | grep -i java_*

Here is my code:
logfile = '/home/suppalapati/java.txt'

line_num=0

log = Facter::Util::FileRead.read(logfile)
unless log.nil?
  log.each_line do |line|
    if line.include?('java')
       line_num += 1
       javaid, val = %x[#{log}]
    Facter.add("java_#{javaid}") do
      setcode do
         puts val
        end
       end
     end


Anyone please help me

To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+unsubscribe@googlegroups.com.



--
Andrew Parker
Freenode: zaphod42
Twitter: @aparker42
Software Developer

Join us at PuppetConf 2014, September 20-24 in San Francisco - www.puppetconf.com 

--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-dev/P6o7RUvvNFo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-dev+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+unsubscribe@googlegroups.com.



--
Andrew Parker
Freenode: zaphod42
Twitter: @aparker42
Software Developer

Join us at PuppetConf 2014, September 20-24 in San Francisco - www.puppetconf.com 

--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-dev/P6o7RUvvNFo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-dev+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+unsubscribe@googlegroups.com.



--
Andrew Parker
Freenode: zaphod42
Twitter: @aparker42
Software Developer

Join us at PuppetConf 2014, September 20-24 in San Francisco - www.puppetconf.com 

--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-dev/P6o7RUvvNFo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-dev+unsubscribe@googlegroups.com.

Andy Parker

unread,
Oct 9, 2014, 11:08:05 AM10/9/14
to puppe...@googlegroups.com
Supriya, you don't seem to be having a problem with developing against facter or puppet here, which is the purpose of this mailing list. You seem to be having problems understanding and writing a program in ruby in general. I suggest that you take some time to read some tutorials, try some exercises, and learn how to write, understand, and debug ruby. There are many mailing lists and websites that can help you with that. If you search for "ruby tutorial" or "ruby command execution" you'll find many pages that cover aspects of the language. The ruby language website also has links to a large number of tutorials that others have found helpful (https://www.ruby-lang.org/en/documentation/). Please use this list for suggestions or questions about how to develop on or against puppet or related tools.
 


Where is in the facts it is not showing anything:
$/opt/puppet/bin/facter -p | grep -i java_*

Here is my code:
logfile = '/home/suppalapati/java.txt'

line_num=0

log = Facter::Util::FileRead.read(logfile)
unless log.nil?
  log.each_line do |line|
    if line.include?('java')
       line_num += 1
       javaid, val = %x[#{log}]
    Facter.add("java_#{javaid}") do
      setcode do
         puts val

Facter considers a fact that has a nil return value from the setcode block to not exist. "puts" returns nil. In your previous examples, as well as this one, "val" evaluated to nil. The %x[] command does not return two values, which is why "val" is nil (http://www.ruby-doc.org/core-2.1.3/Kernel.html#method-i-60). You also are executing "log" instead of "line".
 
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/953f3e46-700e-4d72-8889-24661a864697%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Supriya Uppalapati

unread,
Oct 9, 2014, 1:42:45 PM10/9/14
to puppe...@googlegroups.com
Hi Andy,

The way i approach for this, was i took my code and first run it in ruby parser and also in my test server to execute first in ruby.After executing that ruby script then i tried to convert ruby script into puppet facts. like Facter.add to retireve the result.

My question was, when i executed in ruby it is showing me exact result what i need. But in the facts it is not returning any value. even though  i removed puts and replaced log with line 

To unsubscribe from this group and all its topics, send an email to puppet-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/CANhgQXtf05LrCHrtX032H3i7sp_exyLwfobwqas2X0Qq_m32Gw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages