Class: Simultaneous::Command::Fire

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

Instance Attribute Summary collapse

Attributes inherited from CommandBase

#params, #tag, #task

Instance Method Summary collapse

Methods inherited from CommandBase

#domain, #domain=, #dump, #merge_params, #namespaced_task_name, #task_name

Constructor Details

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

Returns a new instance of Fire.



10
11
12
13
14
# File 'lib/simultaneous/command/fire.rb', line 10

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

Instance Attribute Details

#task_gidObject (readonly)

Returns the value of attribute task_gid.



8
9
10
# File 'lib/simultaneous/command/fire.rb', line 8

def task_gid
  @task_gid
end

#task_uidObject (readonly)

Returns the value of attribute task_uid.



8
9
10
# File 'lib/simultaneous/command/fire.rb', line 8

def task_uid
  @task_uid
end

Instance Method Details

#binaryObject



20
21
22
# File 'lib/simultaneous/command/fire.rb', line 20

def binary
  @task.binary
end

#binary_fileObject



24
25
26
# File 'lib/simultaneous/command/fire.rb', line 24

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

#cmdObject



28
29
30
# File 'lib/simultaneous/command/fire.rb', line 28

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

#debugObject



77
78
79
# File 'lib/simultaneous/command/fire.rb', line 77

def debug
  "Fire :#{namespaced_task_name}: #{cmd}\n"
end

#envObject



46
47
48
49
50
51
52
# File 'lib/simultaneous/command/fire.rb', line 46

def env
  @task.env.merge({
    Simultaneous::ENV_CONNECTION => Simultaneous.connection,
    Simultaneous::ENV_TASK_NAME => task_name,
    Simultaneous::ENV_DOMAIN => domain
  })
end

#exists?Boolean

Returns:

  • (Boolean)

Raises:



41
42
43
44
# File 'lib/simultaneous/command/fire.rb', line 41

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

#nicenessObject



16
17
18
# File 'lib/simultaneous/command/fire.rb', line 16

def niceness
  @task.niceness
end

#permitted?Boolean

Returns:

  • (Boolean)

Raises:



36
37
38
39
# File 'lib/simultaneous/command/fire.rb', line 36

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

#runObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/simultaneous/command/fire.rb', line 54

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(cmd, @task.logfile)
      Process.setpriority(Process::PRIO_PROCESS, 0, niceness) if niceness > 0
      ## change to the UID & GID of the originating thread if necessary
      Process::GID.change_privilege(task_gid) unless Process.egid == task_gid
      Process::UID.change_privilege(task_uid) unless Process.euid == task_uid
      File.umask(0022)
      redirect_io(@task.logfile)
      Dir.chdir(@task.pwd)
      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)


32
33
34
# File 'lib/simultaneous/command/fire.rb', line 32

def valid?
  exists? && permitted?
end