how to execute paths in a text file in puppet ruby

132 views
Skip to first unread message

Spriya

unread,
Oct 7, 2014, 1:31:12 PM10/7/14
to puppet...@googlegroups.com
Hi,

I have a text file where i get all the java version.

cat java.txt
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.65.x86_64/jre/bin/java -version
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java -version
/opt/oracle/agent12c/core/12.1.0.4.0/jdk/bin/java -version
/opt/oracle/agent12c/core/12.1.0.4.0/jdk/jre/bin/java -version
/var/lib/alternatives/java -version
/u01/java/jdk1.7.0_65/jre/bin/java -version
/u01/java/jdk1.7.0_65/bin/java -version


Now i want to run commands in each line and place it in facts.

How can i do that.

I have just started doing the code.

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

line_num=0

log = Facter::Util::FileRead.read(logfile)
unless log.nil?
  log.each_line do |line|
  output =  Facter::Util::Resoloution.exec


How can i can continue that


Please help me

Corey Osman

unread,
Oct 8, 2014, 3:22:22 PM10/8/14
to puppet...@googlegroups.com
I would probably do something like this where we store all the java versions in a comma separated string.
Although if using facter >= 2.0 you can probably remove the join statement and store as an array.

I also want to point out that java -version returns about 3 lines of text which may not be desirable. 

I am also not sure if Facter has a readlines  method (guessing it does), so you might have to swap out with ruby's file reading code.

Facter.add(:java_versions) do
     setcode do
        log = Facter::Util::FileRead.readlines( '/home/weblogic/java.txt')
        if ! log.nil?
           versions = log.collect {|command| `#{command}`}
        else
           versions = `java -version`.to_a
        end 
        versions.join(",")
     end
end


Corey

Spriya

unread,
Oct 8, 2014, 3:33:55 PM10/8/14
to puppet...@googlegroups.com
Hi,

I used the same command which you provided me

Here is the code:

Facter.add(:java_versions1) do
     setcode do
        log = Facter::Util::FileRead.readlines( '/home/suppalapati/java.txt')
        if ! log.nil?
           versions = log.collect {|command| `#{command}`}
        else
           versions = `java -version`.to_a
        end
        versions.join(",")
     end
end


Here is my 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

The fact is not returning anything.

Let me know

Corey Osman

unread,
Oct 8, 2014, 5:08:28 PM10/8/14
to puppet...@googlegroups.com
Yea looks like the Facter readlines method doesn't exist.


You will need to use ruby's readlines and probably clean up the lines as well with chop.

Revised edition:  (untested, and probably full of syntax errors)

Facter.add(:java_versions1) do
     setcode do
        file = '/home/suppalapati/java.txt'
        if File.exists?('/home/suppalapati/java.txt')
               lines = File.readlines( file, "\n")
               if ! lines.nil?
                 versions = lines.collect {|command| `#{command.chop}`}
               else
                 versions = `java -version`.to_a
              end
              versions.join(",")
       else
             # poor mans debugger
             versions = "Can't find file: #{file}"
        end
       
     end
end


See section about loading custom facts. This will give you the chance to test on your machine.  

check syntax ruby -c <facter_file.rb> 

Spriya

unread,
Oct 9, 2014, 9:46:12 AM10/9/14
to puppet...@googlegroups.com
Hi,

When i executed your facts. It is throwing me error

 irb
irb(main):001:0> require 'facter'
=> true
irb(main):002:0> Facter.add(:java_versions1) do
irb(main):003:1* setcode do
irb(main):004:2*  file = '/home/suppalapati/java.txt'
irb(main):005:2> if File.exists?('/home/suppalapati/java.txt')
irb(main):006:3> lines = File.readlines( file, "\n")
irb(main):007:3>  if ! lines.nil?
irb(main):008:4> versions = lines.collect {|command| `#{command.chop}`}
irb(main):009:4> else
irb(main):010:4*  versions = `java -version`.to_a
irb(main):011:4> end
irb(main):012:3> versions.join(",")
irb(main):013:3>  else
irb(main):014:3* versions = "Can't find file: #{file}"
irb(main):015:3>  end
irb(main):016:2>  end
irb(main):017:1>  end
=> #<Facter::Util::Fact:0x00000000a50320 @name=:java_versions1, @ldapname="java_versions1", @resolves=[], @searching=false, @value=nil>


Please help me

Felix Frank

unread,
Oct 31, 2014, 2:52:23 PM10/31/14
to puppet...@googlegroups.com
On 10/09/2014 03:46 PM, Spriya wrote:
> Hi,
>
> When i executed your facts. It is throwing me error

...

> *=> #<Facter::Util::Fact:0x00000000a50320 @name=:java_versions1,
> @ldapname="java_versions1", @resolves=[], @searching=false, @value=nil>*

Uh, that's not an error. That's a fact if I ever saw one (well, I
didn't, but it's recognizable regardless ;-)

Granted, the fact value is nil, so the code is not exactly working, but
it looks salvageable.

Hint: Move the content of the setcode do ... end block out of the Facter
boilerplate and let it run as a standalone scriptlet. Add print
debugging ad libitum.

HTH,
Felix
Reply all
Reply to author
Forward
0 new messages