Class: Ruboty::ExecCommand::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/ruboty/exec_command/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Command

Returns a new instance of Command.



37
38
39
40
41
42
43
# File 'lib/ruboty/exec_command/command.rb', line 37

def initialize(args={})
  args = { absolute_path: nil, command_args: nil }.merge(args)
  @absolute_path = args[:absolute_path]
  @command_args = args[:command_args].split if not args[:command_args].nil?
  @pid = nil
  @start_at = nil
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



45
46
47
# File 'lib/ruboty/exec_command/command.rb', line 45

def pid
  @pid
end

#start_atObject (readonly)

Returns the value of attribute start_at.



46
47
48
# File 'lib/ruboty/exec_command/command.rb', line 46

def start_at
  @start_at
end

Class Method Details

.allObject



30
31
32
33
34
# File 'lib/ruboty/exec_command/command.rb', line 30

def all
  files.map do |e|
    Command.new(absolute_path: e)
  end
end

.command?(path) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/ruboty/exec_command/command.rb', line 20

def command?(path)
  File.executable?(path) and not File.directory?(path)
end

.command_rootObject



12
13
14
# File 'lib/ruboty/exec_command/command.rb', line 12

def command_root
  "#{ruboty_root}/commands"
end

.filesObject



24
25
26
27
28
# File 'lib/ruboty/exec_command/command.rb', line 24

def files
  Dir[command_root+'/**/*'].select do |path|
    command?(path)
  end
end

.output_rootObject



16
17
18
# File 'lib/ruboty/exec_command/command.rb', line 16

def output_root
  ENV['EXEC_COMMAND_OUTPUT_ROOT'] || "#{ruboty_root}/logs/exec_command"
end

.ruboty_rootObject



8
9
10
# File 'lib/ruboty/exec_command/command.rb', line 8

def ruboty_root
  "#{ENV['RUBOTY_ROOT'] || Dir.pwd}"
end

Instance Method Details

#__command2path(path, args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruboty/exec_command/command.rb', line 60

def __command2path(path, args)
  if self.class.command?(path)
    [path, args]
  else
    if args == []
      # command not found
      return ["", ""]
    else
      __command2path("#{path}/#{args[0]}", args.slice(1, args.length))
    end
  end
end

#absolute_pathObject



48
49
50
# File 'lib/ruboty/exec_command/command.rb', line 48

def absolute_path
  @absolute_path ||= command2path[0]
end

#command2pathObject



73
74
75
76
77
# File 'lib/ruboty/exec_command/command.rb', line 73

def command2path
  path = self.class.command_root
  __command2path "#{path}/#{@command_args[0]}",
                @command_args.slice(1, @command_args.length)
end

#command_nameObject



56
57
58
# File 'lib/ruboty/exec_command/command.rb', line 56

def command_name
  @command_name ||= relative_path.gsub('/', ' ')
end

#helpObject



144
145
146
# File 'lib/ruboty/exec_command/command.rb', line 144

def help
  run(args=['-h']).chomp
end

#opt_argsObject



79
80
81
# File 'lib/ruboty/exec_command/command.rb', line 79

def opt_args
  @opt_args ||= command2path[1]
end

#output_dirObject



91
92
93
94
95
# File 'lib/ruboty/exec_command/command.rb', line 91

def output_dir
  d = ENV['EXEC_COMMAND_OUTPUT_DIR'] || "#{self.class.output_root}/#{this_month}"
  FileUtils.mkdir_p(d) if not Dir.exists?(d)
  d
end

#output_file_nameObject



102
103
104
# File 'lib/ruboty/exec_command/command.rb', line 102

def output_file_name
  %Q(#{output_dir}/#{command_name.gsub(" ", "_")}-#{start_at})
end

#output_filesObject

return temporary output file name [stdout, stderr]



112
113
114
# File 'lib/ruboty/exec_command/command.rb', line 112

def output_files
  ["#{output_file_name}.out", "#{output_file_name}.err"]
end

#relative_pathObject



52
53
54
# File 'lib/ruboty/exec_command/command.rb', line 52

def relative_path
  @relative_path ||= absolute_path.sub(/^#{self.class.command_root}\//,"")
end

#run(args = []) ⇒ Object



126
127
128
# File 'lib/ruboty/exec_command/command.rb', line 126

def run(args=[])
  `#{absolute_path} #{args.join(" ")}`
end

#run_bg(args = []) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ruboty/exec_command/command.rb', line 130

def run_bg(args=[])
  @start_at = this_time
  stdout, stderr = output_files
  stdout_link, stderr_link = symlink_files
  FileUtils.ln_sf(stdout, stdout_link)
  FileUtils.ln_sf(stderr, stderr_link)
  cmd = %Q(#{absolute_path} #{args.join(" ")})
  with_clean_env do
    @pid = Process.spawn(cmd, pgroup: true, out: stdout, err: stderr)
  end
  Ruboty.logger.debug { "[EXEC_COMMAND] Invoked `#{cmd}`. PID: #{@pid}" }
  @pid
end

#stderr_logObject

return contents of stderr



122
123
124
# File 'lib/ruboty/exec_command/command.rb', line 122

def stderr_log
  File.open(output_files[1]).read
end

#stdout_logObject

return contents of stdout



117
118
119
# File 'lib/ruboty/exec_command/command.rb', line 117

def stdout_log
  File.open(output_files[0]).read
end

symlink to output_file_name so that we can easily tail -F



98
99
100
# File 'lib/ruboty/exec_command/command.rb', line 98

def symlink_file_name
  %Q(#{output_dir}/#{command_name.gsub(" ", "_")})
end

return symlink output file name [stdout, stderr]



107
108
109
# File 'lib/ruboty/exec_command/command.rb', line 107

def symlink_files
  ["#{symlink_file_name}.out", "#{symlink_file_name}.err"]
end

#this_monthObject



83
84
85
# File 'lib/ruboty/exec_command/command.rb', line 83

def this_month
  Time.now.strftime "%Y-%m"
end

#this_timeObject



87
88
89
# File 'lib/ruboty/exec_command/command.rb', line 87

def this_time
  Time.now.strftime "%Y-%m-%d_%H:%M:%S"
end

#with_clean_env(&block) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/ruboty/exec_command/command.rb', line 148

def with_clean_env(&block)
  if defined?(::Bundler)
    ::Bundler.with_clean_env do
      yield
    end
  else
    yield
  end
end