Class: Vines::Agent::Shell

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/vines/agent/shell.rb

Overview

Provides a shell session to execute commands as a particular user. All commands are forked and executed in a child process to isolate them from the agent process. Keeping the same session open between commands allows stateful commands like ‘cd’ to work properly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jid, permissions) ⇒ Shell

Create a new shell session to asynchronously execute commands for this JID. The JID is validated in the permissions Hash before executing commands.



18
19
20
21
22
23
# File 'lib/vines/agent/shell.rb', line 18

def initialize(jid, permissions)
  @jid, @permissions = jid, permissions
  @user = allowed_users.first if allowed_users.size == 1
  @commands = EM::Queue.new
  process_command_queue
end

Instance Attribute Details

#permissions=(value) ⇒ Object (writeonly)

Sets the attribute permissions

Parameters:

  • value

    the value to set the attribute permissions to.



13
14
15
# File 'lib/vines/agent/shell.rb', line 13

def permissions=(value)
  @permissions = value
end

Instance Method Details

#run(command, &callback) ⇒ Object

Queue the shell command to run as soon as the currently executing tasks complete. Yields the shell output to the callback block.



27
28
29
30
31
32
33
# File 'lib/vines/agent/shell.rb', line 27

def run(command, &callback)
  if reset?(command)
    callback.call(run_built_in(command))
  else
    @commands.push({command: command.strip, callback: callback})
  end
end