Class: Sudo::Wrapper
- Inherits:
-
Object
- Object
- Sudo::Wrapper
- Defined in:
- lib/sudo/wrapper.rb
Defined Under Namespace
Classes: Finalizer
Constant Summary collapse
- RuntimeError =
Class.new(RuntimeError)
- NotRunning =
Class.new(RuntimeError)
- SudoFailed =
Class.new(RuntimeError)
- SudoProcessExists =
Class.new(RuntimeError)
- SudoProcessAlreadyExists =
Class.new(SudoProcessExists)
- NoValidSocket =
Class.new(RuntimeError)
- SocketNotFound =
Class.new(NoValidSocket)
- NoValidSudoPid =
Class.new(RuntimeError)
- SudoProcessNotFound =
Class.new(NoValidSudoPid)
Class Method Summary collapse
-
.cleanup!(h) ⇒ Object
Do the actual resources clean-up.
-
.run(ruby_opts: '', load_gems: true) ⇒ 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: '', load_gems: true) ⇒ 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”. - #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: '', load_gems: true) ⇒ 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”.
51 52 53 54 55 56 57 |
# File 'lib/sudo/wrapper.rb', line 51 def initialize(ruby_opts: '', load_gems: true) @proxy = nil @socket = "/tmp/rubysu-#{Process.pid}-#{object_id}" @sudo_pid = nil @ruby_opts = ruby_opts @load_gems = load_gems == true 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)
41 42 43 44 |
# File 'lib/sudo/wrapper.rb', line 41 def cleanup!(h) Sudo::System.kill h[:pid] Sudo::System.unlink h[:socket] end |
.run(ruby_opts: '', load_gems: true) ⇒ 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 .
28 29 30 31 32 33 34 35 |
# File 'lib/sudo/wrapper.rb', line 28 def run(ruby_opts: '', load_gems: true) # :yields: sudo sudo = new(ruby_opts: ruby_opts, load_gems: load_gems).start! yield sudo rescue Exception => e # Bubble all exceptions... raise e ensure # and ensure sudo stops sudo.stop! end |
Instance Method Details
#[](object) ⇒ Object
Gives a copy of object
with root privileges.
100 101 102 103 104 105 106 107 |
# File 'lib/sudo/wrapper.rb', line 100 def [](object) if running? load! MethodProxy.new object, @proxy else raise NotRunning end end |
#running? ⇒ Boolean
83 84 85 86 87 88 89 |
# File 'lib/sudo/wrapper.rb', line 83 def running? true if ( @sudo_pid and Process.exists? @sudo_pid and @socket and File.exist? @socket and @proxy ) end |
#server_uri ⇒ Object
59 |
# File 'lib/sudo/wrapper.rb', line 59 def server_uri; "drbunix:#{@socket}"; end |
#start! ⇒ Object
Start the sudo-ed Ruby process.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/sudo/wrapper.rb', line 62 def start! Sudo::System.check @sudo_pid = spawn( "#{SUDO_CMD} -E #{RUBY_CMD} -I#{LIBDIR} #{@ruby_opts} #{SERVER_SCRIPT} #{@socket} #{Process.uid}" ) Process.detach(@sudo_pid) if @sudo_pid # avoid zombies finalizer = Finalizer.new(pid: @sudo_pid, socket: @socket) ObjectSpace.define_finalizer(self, finalizer) if wait_for(timeout: 1){File.exist? @socket} @proxy = DRbObject.new_with_uri(server_uri) else raise RuntimeError, "Couldn't create DRb socket #{@socket}" end load! 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.
94 95 96 97 |
# File 'lib/sudo/wrapper.rb', line 94 def stop! self.class.cleanup!(pid: @sudo_pid, socket: @socket) @proxy = nil end |