Class: FireAndForget::Command::Fire
- Inherits:
-
CommandBase
- Object
- CommandBase
- FireAndForget::Command::Fire
- Defined in:
- lib/fire_and_forget/command/fire.rb
Instance Attribute Summary collapse
-
#niceness ⇒ Object
readonly
Returns the value of attribute niceness.
-
#task_gid ⇒ Object
readonly
Returns the value of attribute task_gid.
-
#task_uid ⇒ Object
readonly
Returns the value of attribute task_uid.
Attributes inherited from CommandBase
Instance Method Summary collapse
- #binary ⇒ Object
- #binary_file ⇒ Object
- #cmd ⇒ Object
- #env ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(task, params = {}) ⇒ Fire
constructor
A new instance of Fire.
- #permitted? ⇒ Boolean
- #run ⇒ Object
- #valid? ⇒ Boolean
Methods inherited from CommandBase
Constructor Details
#initialize(task, params = {}) ⇒ Fire
Returns a new instance of Fire.
9 10 11 12 13 |
# File 'lib/fire_and_forget/command/fire.rb', line 9 def initialize(task, params={}) super @task_uid = Process.euid @task_gid = Process.egid end |
Instance Attribute Details
#niceness ⇒ Object (readonly)
Returns the value of attribute niceness.
7 8 9 |
# File 'lib/fire_and_forget/command/fire.rb', line 7 def niceness @niceness end |
#task_gid ⇒ Object (readonly)
Returns the value of attribute task_gid.
7 8 9 |
# File 'lib/fire_and_forget/command/fire.rb', line 7 def task_gid @task_gid end |
#task_uid ⇒ Object (readonly)
Returns the value of attribute task_uid.
7 8 9 |
# File 'lib/fire_and_forget/command/fire.rb', line 7 def task_uid @task_uid end |
Instance Method Details
#binary ⇒ Object
19 20 21 |
# File 'lib/fire_and_forget/command/fire.rb', line 19 def binary @task.binary end |
#binary_file ⇒ Object
23 24 25 |
# File 'lib/fire_and_forget/command/fire.rb', line 23 def binary_file @task.binary.split(" ").first end |
#cmd ⇒ Object
27 28 29 |
# File 'lib/fire_and_forget/command/fire.rb', line 27 def cmd %(#{binary} #{FireAndForget.to_arguments(@params)}) end |
#env ⇒ Object
45 46 47 48 49 50 |
# File 'lib/fire_and_forget/command/fire.rb', line 45 def env @task.env.merge({ FireAndForget::ENV_SOCKET => FireAndForget.socket, FireAndForget::ENV_TASK_NAME => @task.name.to_s }) end |
#exists? ⇒ Boolean
40 41 42 43 |
# File 'lib/fire_and_forget/command/fire.rb', line 40 def exists? raise FileNotFoundError, "'#{binary_file}'" unless File.exists?(binary_file) true end |
#permitted? ⇒ Boolean
35 36 37 38 |
# File 'lib/fire_and_forget/command/fire.rb', line 35 def permitted? raise PermissionsError, "'#{binary_file}' does not belong to user '#{ENV["USER"]}'" unless File.stat(binary_file).uid == task_uid true end |
#run ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fire_and_forget/command/fire.rb', line 52 def run if valid? pid = fork do # set up the environment so that the task can access the F&F server env.each { | k, v | ENV[k] = v } Daemons.daemonize(:backtrace => true) Process.setpriority(Process::PRIO_PROCESS, 0, niceness) if niceness > 0 # change to the UID of the originating thread if necessary Process::UID.change_privilege(task_uid) unless Process.euid == task_uid exec(cmd) end Process.detach(pid) if pid # don't return the PID as it's actually wrong (the daemonize call forks again so our original # PID is at least 1 out) "OK" end end |
#valid? ⇒ Boolean
31 32 33 |
# File 'lib/fire_and_forget/command/fire.rb', line 31 def valid? exists? && permitted? end |