Class: Sudo::Wrapper
Defined Under Namespace
Classes: Finalizer, NoValidSocket, NoValidSudoPid, NotRunning, RuntimeError, SocketNotFound, SudoFailed, SudoProcessAlreadyExists, SudoProcessExists, SudoProcessNotFound
Class Method Summary collapse
-
.cleanup!(h) ⇒ Object
Do the actual resources clean-up.
-
.run(ruby_opts = '') ⇒ Object
Yields a new running Sudo::Wrapper, and do all the necessary cleanup when the block exits.
Instance Method Summary collapse
-
#[](object) ⇒ Object
Gives a copy of
object
with root privileges. -
#initialize(ruby_opts = '') ⇒ Wrapper
constructor
ruby_opts
are the command line options to the sudo ruby interpreter; usually you don’t need to specify stuff like “-rmygem/mylib”, libraries will be sorta “inherited”. -
#load_features ⇒ Object
Load needed libraries in the DRb server.
- #running? ⇒ Boolean
- #server_uri ⇒ Object
-
#start! ⇒ Object
Start the sudo-ed Ruby process.
-
#stop! ⇒ Object
Free the resources opened by this Wrapper: e.g.
Constructor Details
#initialize(ruby_opts = '') ⇒ Wrapper
ruby_opts
are the command line options to the sudo ruby interpreter; usually you don’t need to specify stuff like “-rmygem/mylib”, libraries will be sorta “inherited”.
48 49 50 51 52 53 54 55 |
# File 'lib/sudo/wrapper.rb', line 48 def initialize(ruby_opts='') @proxy = nil @socket = "/tmp/rubysu-#{Process.pid}-#{object_id}" @sudo_pid = nil @ruby_opts = ruby_opts @loaded_features = [] # @load_path = [] # currentl unused end |
Class Method Details
.cleanup!(h) ⇒ Object
Do the actual resources clean-up.
Not an instance method, so it may act as a Finalizer (as in ::ObjectSpace::define_finalizer)
38 39 40 41 |
# File 'lib/sudo/wrapper.rb', line 38 def cleanup!(h) Sudo::System.kill h[:pid] Sudo::System.unlink h[:socket] end |
.run(ruby_opts = '') ⇒ Object
Yields a new running Sudo::Wrapper, and do all the necessary cleanup when the block exits.
- ruby_opts
-
is passed to Sudo::Wrapper::new .
27 28 29 30 31 32 |
# File 'lib/sudo/wrapper.rb', line 27 def run(ruby_opts = '') # :yields: sudo sudo = new(ruby_opts).start! retval = yield sudo sudo.stop! retval end |
Instance Method Details
#[](object) ⇒ Object
Gives a copy of object
with root privileges.
114 115 116 117 118 119 120 121 |
# File 'lib/sudo/wrapper.rb', line 114 def [](object) if running? load_features MethodProxy.new object, @proxy else raise NotRunning end end |
#load_features ⇒ Object
Load needed libraries in the DRb server. Usually you don’t need to call this directly.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sudo/wrapper.rb', line 86 def load_features unless $LOADED_FEATURES == @loaded_features new_features = $LOADED_FEATURES - @loaded_features new_features.each do |feature| @proxy.proxy Kernel, :require, feature @loaded_features << feature end #@loaded_features += new_features end end |
#running? ⇒ Boolean
97 98 99 100 101 102 103 |
# File 'lib/sudo/wrapper.rb', line 97 def running? true if ( @sudo_pid and Process.exists? @sudo_pid and @socket and File.exists? @socket and @proxy ) end |
#server_uri ⇒ Object
57 |
# File 'lib/sudo/wrapper.rb', line 57 def server_uri; "drbunix:#{@socket}"; end |
#start! ⇒ Object
Start the sudo-ed Ruby process.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sudo/wrapper.rb', line 60 def start! Sudo::System.check @sudo_pid = spawn( "#{SUDO_CMD} ruby -I#{LIBDIR} #{@ruby_opts} #{SERVER_SCRIPT} #{@socket} #{Process.uid}" ) Process.detach(@sudo_pid) if @sudo_pid # avoid zombies ObjectSpace.define_finalizer self, Finalizer.new( :pid => @sudo_pid, :socket => @socket ) if wait_for(:timeout => 1){File.exists? @socket} @proxy = DRbObject.new_with_uri(server_uri) else raise RuntimeError, "Couldn't create DRb socket #{@socket}" end #set_load_path # apparently, we don't need this load_features self end |
#stop! ⇒ Object
Free the resources opened by this Wrapper: e.g. the sudo-ed ruby process and the Unix-domain socket used to communicate to it via ::DRb.
108 109 110 111 |
# File 'lib/sudo/wrapper.rb', line 108 def stop! self.class.cleanup!(:pid => @sudo_pid, :socket => @socket) @proxy = nil end |