Ruby i skrypt startowy do SVN

Posted by Jarosław Zabiełło Thu, 05 Oct 2006 14:37:00 GMT

Subversion to świetny, darmowy system wersjonowania kodu. Jest znacznie lepszy od starego CVS’a. Niestety, nigdzie nie mogłem się doszukać skryptu startującego do serwera. Co gorsze, skrytp svnserve w ogóle nie ma opcji zapisu numeru swojego procesu co utrudnia pisanie kodu który go “zabije”. Na szczęście Linux posiada komendę pidof. Poniżej gotowy skrypt napisany w Ruby. Należy go umieścić w /etc/init.d, dodać atrybuty wykonywalności (chmod a+x subversion.rb) i dodać linki symboliczne aby startowała utomatycznie razem ze startem serwera (pod Debianem/Ubuntu robi to komenda update-rc.d -f subversion.rb defaults, pod RedHatem/CentOS/Fedorą jest do tego komenda chkconfig)

#!/usr/bin/env ruby

$USER = "nobody"
$APP = "/usr/bin/svnserve"
$DIR = "/home/SVN"
$PID = "/var/run/svnserve.pid"

def script action
  def start 
    system(%Q|su -c "#{$APP} -d -r #{$DIR}" #{$USER}|)
    system(%Q|echo `pidof -o %PPID #{$APP}` > #{$PID}|)
  end
  def stop
    return false if not File.exists?($PID)
    system(%Q|/bin/kill -TERM #{File.read($PID)}|)
    true
  end
  print "#{$APP} #{action}..."
  $defout.flush # unbuffered output
  case action 
    when :start 
      start()
      puts "done"
    when :stop
      if not stop()
        puts "#{File.basename($APP)} cannot be stopped because it cannot find #{$PID}"
      else
        puts "done"
      end 
    when :restart
      stop()
      start()
      puts "done"
  end
end

case ARGV.first
  when "start": script :start
  when "stop": script :stop
  when "restart": script :restart
end

unless %w{start stop restart}.include? ARGV.first
  puts "Usage: sudo #{__FILE__} {start|stop|restart}"
  exit
end

Posted in  | Tags , ,  | 2 comments

Uniksowy skrypt /etc/init.d w Ruby

Posted by Jarosław Zabiełło Fri, 29 Sep 2006 12:04:00 GMT

Ruby całkiem dobrze nadaje się do pisania skryptów systemowych. W sumie nie wiem dlaczego do tej pory pisałem skrypty tego typu w uniksowym bash’u. :)

#!/usr/bin/env ruby

MONGREL = "/usr/bin/mongrel_rails"
PROJECT = "/home/httpd/somedomain.com/rails_project"

def execute(action, project)
  Dir.chdir(project)
  print "#{MONGREL} #{action}..."
  $defout.flush # unbuffered output
  `#{MONGREL} cluster::#{action}`
  puts 'done'
end

case ARGV.first
  when 'start': execute 'start', PROJECT
  when 'stop': execute 'stop', PROJECT
  when 'restart': execute 'restart', PROJECT
end

unless %w{start stop restart}.include? ARGV.first
  puts "Usage: sudo #{__FILE__} {start|stop|restart}"
  exit
end

Posted in  | Tags , ,  | 1 comment