RExec

Copyright (c) 2011 Samuel G. D. Williams. <www.oriontransfer.co.nz>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Copyright (c) 2007, 2009, 2011 Samuel G. D. Williams. <www.oriontransfer.co.nz>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Copyright (c) 2007, 2011 Samuel G. D. Williams. <www.oriontransfer.co.nz>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


RExec is a tool to assist with the execution of other processes.

Constants

CLIENT_CODE

The client code which sets up the connection object and initialises communciation.

CONNECTION_CODE

The connection code which is sent to the client to be used for bi-directional communication.

RD
VERSION
WR

Public Class Methods

change_user(user) click to toggle source

Set the user of the current process. Supply either a user ID or a user name.

Be aware that on Mac OS X / Ruby 1.8 there are bugs when the user id is negative (i.e. it doesn't work). For example "nobody" with uid -2 won't work.

# File lib/rexec/priviledges.rb, line 31
def self.change_user(user)
        if user.kind_of?(String)
                user = Etc.getpwnam(user).uid
        end

        Process::Sys.setuid(user)
end
close_io(except = [$stdin, $stdout, $stderr]) click to toggle source

Cloose all IO other than $stdin, $stdout, $stderr (or those given by the argument except)

# File lib/rexec/task.rb, line 32
def self.close_io(except = [$stdin, $stdout, $stderr])
        # Make sure all file descriptors are closed
        ObjectSpace.each_object(IO) do |io|
                unless except.include?(io)
                        io.close rescue nil
                end
        end
end
current_user() click to toggle source

Get the user of the current process. Returns the user name.

# File lib/rexec/priviledges.rb, line 41
def self.current_user
        uid = Process::Sys.getuid

        Etc.getpwuid(uid).name
end
env(new_env = nil, &block) click to toggle source

Updates the global ENV for the duration of block. Not multi-thread safe.

# File lib/rexec/environment.rb, line 23
def self.env (new_env = nil, &block)
        old_env = ENV.to_hash

        ENV.update(new_env) if new_env

        yield

        ENV.clear
        ENV.update(old_env)
end
start_server(code, command, options = {}, &block) click to toggle source

Start a remote ruby server. This function is a structural cornerstone. This code runs the command you supply (this command should start an instance of ruby somewhere), sends it the code in connection.rb and client.rb as well as the code you supply.

Once the remote ruby instance is set up and ready to go, this code will return (or yield) the connection and pid of the executed command.

From this point, you can send and receive objects, and interact with the code you provided within a remote ruby instance.

For a local shell, you could specify +"ruby"+ as the command. For a remote shell via SSH, you could specify +"ssh example.com ruby"+.

Example

Create a file called client.rb on the server. This file contains code to be executed on the client. This file can assume the existance of an object called +$connection+:

$connection.run do |object|
  case(object[0])
  when :bounce
    $connection.send_object(object[1])
  end
end

Then, on the server, create a new program server.rb which will be used to coordinate the execution of code:

shell = "ssh example.com ruby"
client_code = (Pathname.new(__FILE__).dirname + "./client.rb").read

RExec::start_server(client_code, shell) do |connection, pid|
        connection.send_object([:bounce, "Hello World!"])
        result = connection.receive_object
end
# File lib/rexec/server.rb, line 71
def self.start_server(code, command, options = {}, &block)
        options[:passthrough] = :err unless options[:passthrough]

        send_code = Proc.new do |cin|
                unless options[:raw]
                        cin.puts(CONNECTION_CODE)
                        cin.puts(CLIENT_CODE)
                end
                
                cin.puts(code)
        end

        if block_given?
                Task.open(command, options) do |task|
                        conn = Connection.build(task, options, &send_code)

                        begin
                                yield conn, task
                        ensure
                                conn.stop
                        end
                end
        else
                task = Task.open(command, options)
                conn = Connection.build(task, options, &send_code)

                return conn, task
        end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.