Archive for the ‘Programming / Scripting’ Category

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.

Ugly mail backup script

Tuesday, June 5th, 2007

Created a small script with ruby thats being executed by a cronjob to automatically backup a mailserver thats uses the Maildir format on Solaris.

The script backups the user home dir’s (This is where the mail is located) to a mounted NFS partition from the backup server. The script also should delete all backups older then 7 days. This is where I had a problem because I am not a real programmer “yet” and I didn’t want to search how to store all in a text file or in a db. So here is what I did (I know it’s a pretty ugly way of doing it, if anyone wants to give me a better code, please do so :- ) )

#!/opt/csw/bin/ruby
#Ruby Mail Backup
#Created by Sander on 05-06-2007

#
require 'date'

class Date
   def Date.now
      return Date.jd(DateTime.now.jd)
   end
end

dateBack = Date.now.to_s
dateDel1 = Date.now - 7
dateDel2 = Date.now - 50

#Deleting old backups (ugly workaround)
while dateDel2 < dateDel1
   delstr = dateDel2.to_s
    puts 'removing ' + delstr
    system('rm /mnt/backup/mail/home' + delstr +'.tar')
    dateDel2 = dateDel2 + 1
end

#Making backup of all home dir's
system('tar cf /mnt/backup/mail/home' + dateBack + '.tar /export/home/')

Writing Ruby Daemons

Wednesday, May 9th, 2007

We got some bash scripts running via a cronjob to set permissions of certain directories. To improve this process I wrote a very simple ruby daemon process to handle this.

First I installed the daemons gem (#gem install daemons), next I created the following:

#control.rb
require ‘rubygems’
require ‘daemons’

Daemons.run(’permissions.rb’)

———————————————————————-
#permissions.rb
loop do
system(’<some unix commands’>
system(’<some unix commands’>
sleep 15
end

As you can see, I use the standard unix tools by invoking the system(”) command. I am still trying to find out if it would be benefitial to use File.chmod and File.chown from the fileutils package.