Class: Ruboty::ExecCommand::Command
- Inherits:
-
Object
- Object
- Ruboty::ExecCommand::Command
- Defined in:
- lib/ruboty/exec_command/command.rb
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#start_at ⇒ Object
readonly
Returns the value of attribute start_at.
Class Method Summary collapse
- .all ⇒ Object
- .command?(path) ⇒ Boolean
- .command_root ⇒ Object
- .files ⇒ Object
- .log_root ⇒ Object
- .ruboty_root ⇒ Object
Instance Method Summary collapse
- #__command2path(path, args) ⇒ Object
- #absolute_path ⇒ Object
- #command2path ⇒ Object
- #command_name ⇒ Object
- #help ⇒ Object
-
#initialize(args = {}) ⇒ Command
constructor
A new instance of Command.
- #log_dir ⇒ Object
- #opt_args ⇒ Object
- #output_file_name ⇒ Object
- #output_files ⇒ Object
- #relative_path ⇒ Object
- #run(args = []) ⇒ Object
- #run_bg(args = []) ⇒ Object
- #stderr_log ⇒ Object
- #stdout_log ⇒ Object
- #this_month ⇒ Object
- #this_time ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Command
Returns a new instance of Command.
35 36 37 38 39 40 41 |
# File 'lib/ruboty/exec_command/command.rb', line 35 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
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
43 44 45 |
# File 'lib/ruboty/exec_command/command.rb', line 43 def pid @pid end |
#start_at ⇒ Object (readonly)
Returns the value of attribute start_at.
44 45 46 |
# File 'lib/ruboty/exec_command/command.rb', line 44 def start_at @start_at end |
Class Method Details
.all ⇒ Object
28 29 30 31 32 |
# File 'lib/ruboty/exec_command/command.rb', line 28 def all files.map do |e| Command.new(absolute_path: e) end end |
.command?(path) ⇒ Boolean
18 19 20 |
# File 'lib/ruboty/exec_command/command.rb', line 18 def command?(path) File.executable?(path) and not File.directory?(path) end |
.command_root ⇒ Object
10 11 12 |
# File 'lib/ruboty/exec_command/command.rb', line 10 def command_root "#{ruboty_root}/commands" end |
.files ⇒ Object
22 23 24 25 26 |
# File 'lib/ruboty/exec_command/command.rb', line 22 def files Dir[command_root+'/**/*'].select do |path| command?(path) end end |
.log_root ⇒ Object
14 15 16 |
# File 'lib/ruboty/exec_command/command.rb', line 14 def log_root "#{ruboty_root}/logs/exec_command" end |
.ruboty_root ⇒ Object
6 7 8 |
# File 'lib/ruboty/exec_command/command.rb', line 6 def ruboty_root "#{ENV['RUBOTY_ROOT'] || Dir.pwd}" end |
Instance Method Details
#__command2path(path, args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ruboty/exec_command/command.rb', line 58 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_path ⇒ Object
46 47 48 |
# File 'lib/ruboty/exec_command/command.rb', line 46 def absolute_path @absolute_path ||= command2path[0] end |
#command2path ⇒ Object
71 72 73 74 75 |
# File 'lib/ruboty/exec_command/command.rb', line 71 def command2path path = self.class.command_root __command2path "#{path}/#{@command_args[0]}", @command_args.slice(1, @command_args.length) end |
#command_name ⇒ Object
54 55 56 |
# File 'lib/ruboty/exec_command/command.rb', line 54 def command_name @command_name ||= relative_path.gsub('/', ' ') end |
#help ⇒ Object
125 126 127 |
# File 'lib/ruboty/exec_command/command.rb', line 125 def help run(args=['-h']).chomp end |
#log_dir ⇒ Object
89 90 91 92 93 |
# File 'lib/ruboty/exec_command/command.rb', line 89 def log_dir d = "#{self.class.log_root}/#{this_month}" FileUtils.mkdir_p(d) if not Dir.exists?(d) d end |
#opt_args ⇒ Object
77 78 79 |
# File 'lib/ruboty/exec_command/command.rb', line 77 def opt_args @opt_args ||= command2path[1] end |
#output_file_name ⇒ Object
95 96 97 |
# File 'lib/ruboty/exec_command/command.rb', line 95 def output_file_name %Q(#{log_dir}/#{command_name.gsub(" ", "_")}-#{this_time}) end |
#output_files ⇒ Object
99 100 101 102 |
# File 'lib/ruboty/exec_command/command.rb', line 99 def output_files # return temporary output file IO objects [stdout, stderr] ["#{output_file_name}.out", "#{output_file_name}.err"] end |
#relative_path ⇒ Object
50 51 52 |
# File 'lib/ruboty/exec_command/command.rb', line 50 def relative_path @relative_path ||= absolute_path.sub(/^#{self.class.command_root}\//,"") end |
#run(args = []) ⇒ Object
114 115 116 |
# File 'lib/ruboty/exec_command/command.rb', line 114 def run(args=[]) `#{absolute_path} #{args.join(" ")}` end |
#run_bg(args = []) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/ruboty/exec_command/command.rb', line 118 def run_bg(args=[]) stdout, stderr = output_files @start_at = this_time @pid = Process.spawn(%Q(#{absolute_path} #{args.join(" ")}), pgroup: true, out: stdout, err: stderr) end |
#stderr_log ⇒ Object
109 110 111 112 |
# File 'lib/ruboty/exec_command/command.rb', line 109 def stderr_log # return contents of stderr File.open(output_files[1]).read end |
#stdout_log ⇒ Object
104 105 106 107 |
# File 'lib/ruboty/exec_command/command.rb', line 104 def stdout_log # return contents of stdout File.open(output_files[0]).read end |
#this_month ⇒ Object
81 82 83 |
# File 'lib/ruboty/exec_command/command.rb', line 81 def this_month Time.now.strftime "%Y-%m" end |
#this_time ⇒ Object
85 86 87 |
# File 'lib/ruboty/exec_command/command.rb', line 85 def this_time Time.now.strftime "%Y-%m-%d_%H:%M:%S" end |