examples/jexec2.rb
author Mahlon E. Smith <mahlon@martini.nu>
Tue, 03 Mar 2009 22:23:45 +0000
changeset 7 4460fc10c6a3
permissions -rwxr-xr-x
* Now with 87% more hot jail action! * Predeclared all C methods in jail.h, so they could be arranged in logical order in jail.c * Fixed the extconf namespace. * Added rdoc. * Added usage examples, demonstrating jls, jexec, and jail ruby equivalents. * Re-added the "attach and execute within a block" code. * Added Enumerable and Comparable support. * Return 'path' as a Pathname object. TODO: * Create the actual 'jParallel' shell binary, now that we have a good backend framework. * Tests? How? * Add support for recently committed (will be part of 7.2-RELEASE) multiple IPs per jail, and jail labels.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
#!/usr/bin/env ruby
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
#
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
# A 'smarter' jexec in ruby.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
#
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
# Run a command in multiple jails in parallel.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
# Jails can be selected via host, ip, or jid.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
#
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
#   "Run 'ls' in all jails that match the hostname 'trac':
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
#       ./jexec2.rb trac ls
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
#
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
BEGIN {
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
        require 'pathname'
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
        basedir = Pathname.new( __FILE__ ).dirname.parent
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
        $LOAD_PATH.unshift basedir + "ext" unless 
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
                $LOAD_PATH.include? basedir + "ext"
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
require 'jail'
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
jarg = ARGV.shift
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
args = ARGV
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
jails = BSD::Jail.find_all do |j|
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
    j.jid     == jarg.to_i ||
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
    j.ip.to_s == jarg      ||
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
    j.host    =~ /#{jarg}/
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
end or raise "Unable to find jails that match '#{jarg}'."
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
jails.each do |j|
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
    $deferr.puts "Parent #{Process.pid} about to attach to #{j.host} in a block!"
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
    childpid = j.attach do
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
        $deferr.puts "Child #{Process.pid} exec()ing:", "  " + args.join(" ")
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
        exec( *args )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
    end
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
    $deferr.puts "Parent: waiting on imprisoned child #{childpid}"
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
    Process.waitpid( childpid )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
    $deferr.puts "Child exited with exit code: %d" % [ $?.exitstatus ]
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
end
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43