Class: FireAndForget::Command::Fire

Inherits:
CommandBase show all
Defined in:
lib/fire_and_forget/command/fire.rb

Instance Attribute Summary collapse

Attributes inherited from CommandBase

#params, #tag, #task

Instance Method Summary collapse

Methods inherited from CommandBase

#dump, #merge_params

Constructor Details

#initialize(task, params = {}) ⇒ Fire

Returns a new instance of Fire.



8
9
10
11
12
# File 'lib/fire_and_forget/command/fire.rb', line 8

def initialize(task, params={})
  super
  @task_uid = Process.euid
  @task_gid = Process.egid
end

Instance Attribute Details

#nicenessObject (readonly)

Returns the value of attribute niceness.



6
7
8
# File 'lib/fire_and_forget/command/fire.rb', line 6

def niceness
  @niceness
end

#task_gidObject (readonly)

Returns the value of attribute task_gid.



6
7
8
# File 'lib/fire_and_forget/command/fire.rb', line 6

def task_gid
  @task_gid
end

#task_uidObject (readonly)

Returns the value of attribute task_uid.



6
7
8
# File 'lib/fire_and_forget/command/fire.rb', line 6

def task_uid
  @task_uid
end

Instance Method Details

#binaryObject



18
19
20
# File 'lib/fire_and_forget/command/fire.rb', line 18

def binary
  @task.binary
end

#binary_fileObject



22
23
24
# File 'lib/fire_and_forget/command/fire.rb', line 22

def binary_file
  @task.binary.split(" ").first
end

#cmdObject



26
27
28
# File 'lib/fire_and_forget/command/fire.rb', line 26

def cmd
  %(#{binary} #{FireAndForget.to_arguments(@params)})
end

#debugObject



71
72
73
# File 'lib/fire_and_forget/command/fire.rb', line 71

def debug
  "Fire :#{@task.name}: #{cmd}\n"
end

#envObject



44
45
46
47
48
49
# File 'lib/fire_and_forget/command/fire.rb', line 44

def env
  @task.env.merge({
    FireAndForget::ENV_SOCKET => FireAndForget.socket,
    FireAndForget::ENV_TASK_NAME => @task.name.to_s
  })
end

#exists?Boolean

Returns:

  • (Boolean)

Raises:



39
40
41
42
# File 'lib/fire_and_forget/command/fire.rb', line 39

def exists?
  raise FileNotFoundError, "'#{binary_file}'" unless File.exists?(binary_file)
  true
end

#permitted?Boolean

Returns:

  • (Boolean)

Raises:



34
35
36
37
# File 'lib/fire_and_forget/command/fire.rb', line 34

def permitted?
  raise PermissionsError, "'#{binary_file}' does not belong to user '#{ENV["USER"]}'" unless File.stat(binary_file).uid == task_uid
  true
end

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fire_and_forget/command/fire.rb', line 51

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 }
      # TODO: figure out how to pass a logfile path to this
      daemonize
      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
      File.umask(0022)
      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

Returns:

  • (Boolean)


30
31
32
# File 'lib/fire_and_forget/command/fire.rb', line 30

def valid?
  exists? && permitted?
end