examples/jexec2.rb
changeset 7 4460fc10c6a3
equal deleted inserted replaced
6:2d52adc4adcc 7:4460fc10c6a3
       
     1 #!/usr/bin/env ruby
       
     2 #
       
     3 # A 'smarter' jexec in ruby.
       
     4 #
       
     5 # Run a command in multiple jails in parallel.
       
     6 # Jails can be selected via host, ip, or jid.
       
     7 #
       
     8 #   "Run 'ls' in all jails that match the hostname 'trac':
       
     9 #       ./jexec2.rb trac ls
       
    10 #
       
    11 
       
    12 BEGIN {
       
    13         require 'pathname'
       
    14         basedir = Pathname.new( __FILE__ ).dirname.parent
       
    15 
       
    16         $LOAD_PATH.unshift basedir + "ext" unless 
       
    17                 $LOAD_PATH.include? basedir + "ext"
       
    18 }
       
    19 
       
    20 require 'jail'
       
    21 
       
    22 jarg = ARGV.shift
       
    23 args = ARGV
       
    24 
       
    25 jails = BSD::Jail.find_all do |j|
       
    26     j.jid     == jarg.to_i ||
       
    27     j.ip.to_s == jarg      ||
       
    28     j.host    =~ /#{jarg}/
       
    29 end or raise "Unable to find jails that match '#{jarg}'."
       
    30 
       
    31 
       
    32 jails.each do |j|
       
    33     $deferr.puts "Parent #{Process.pid} about to attach to #{j.host} in a block!"
       
    34     childpid = j.attach do
       
    35         $deferr.puts "Child #{Process.pid} exec()ing:", "  " + args.join(" ")
       
    36         exec( *args )
       
    37     end
       
    38 
       
    39     $deferr.puts "Parent: waiting on imprisoned child #{childpid}"
       
    40     Process.waitpid( childpid )
       
    41     $deferr.puts "Child exited with exit code: %d" % [ $?.exitstatus ]
       
    42 end
       
    43