Archive for November, 2007

Searching for text files

Thursday, November 15th, 2007

For communication between a php application and a ruby I thought it might be handy to use simple plain text files with the words seperated by comma. Its kinda like csv. This way a php frontend can create these csv files while my ruby daemon can handle the background process.

In my case I use it to create a webbased installer for some some software. So inside the php frontend we can choose to install a certain software, next php writes it inside a csv file and ruby picks it up, downloads the software and does what is needed.

First we get the daemon part, which looks like:

#!/opt/csw/bin/ruby
#start.rb
require 'rubygems'
require 'daemons'

if (VERSION < "1.8.0")
  printf "Use Ruby 1.8.0 or later, your Ruby version: %s...bye\n",VERSION
  exit(1)
end

Daemons.run('dir.rb')

Now we get the script itself (which I called dir.rb)


#!/usr/bin/env ruby

loop do

 # Define where the log and which dir to scan for csv's
 log = '/export/home/sander/ruby/test/log.txt'
 tmp = '/export/home/sander/ruby/test/mydir'
 counter = 0
 #Opening log file for writing
 f = File.open(log, "a")

 require 'date'

 Dir.foreach tmp do |dir|
        file = tmp+'/'+dir

        if file == tmp+'/.'
        else
                if file == tmp+'/..'
                else
                        counter = 0
                        f.puts(DateTime.now.to_s + ' Processing' +file)
                        counter = counter + 1
                        dataArray= Array.new
                        n = 0
                        File.new(file).each_line do |line|
                                line.chomp!
                                dataArray[n] = line.split(',')
                                n += 1
                        end
                        i=0

                        while(i < dataArray.length)
                                # go to address query start

                                type = dataArray[i][0].to_s
                                program = dataArray[i][1].to_s
                                domain = dataArray[i][2].to_s

                                case type
                                        when "install"
                                                case program
                                                        when "someprogram"
                                                                #do some things to install someprogram on domain
                                                                f.puts(DateTime.now.to_s + ' Installing someprogram')
                                                                require 'net/ftp'
                                                                server = 'hostname'
                                                                ftpuser = 'username'
                                                                ftppass = 'password'
                                                                ftp = Net::FTP.open(server) do |ftp|
                                                                 ftp.login(ftpuser, ftppass)
                                                                 ftp.getbinaryfile(program +'.tar', '/export/home/sander/ruby/bla/dir2/' + program +'.tar')
                                                                 f.puts(DateTime.now.to_s + ' Downloading ' + program +'.tar')
                                                                end
                                                                system('gtar xfC /export/home/sander/ruby/bla/dir2/' + program +'.tar /export/home/sander/ruby/bla/dir2/')
                                                                f.puts(DateTime.now.to_s + ' Extracting ' + program +'.tar')

                                                        when "someotherprogram"
                                                                f.puts(DateTime.now.to_s + ' Installing Someotherprogram')
                                                                #do some things to install someotherprogram on domain
                                                end
                                                #puts "Installing: "+ program +" on: " + domain
                                        when "remove"
                                                f.puts "Removing: "+ program +" on: " + domain
                                end

                                sleep 2
                                i = i + 1
                                sleep 2
                        end
                File.delete(file)
                end
        end
 end
 sleep 2
 f.close
end

For any questions/remarks, please leave a comment.