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.



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

#pidObject (readonly)

Returns the value of attribute pid.



43
44
45
# File 'lib/ruboty/exec_command/command.rb', line 43

def pid
  @pid
end

#start_atObject (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

.allObject



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

Returns:



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_rootObject



10
11
12
# File 'lib/ruboty/exec_command/command.rb', line 10

def command_root
  "#{ruboty_root}/commands"
end

.filesObject



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_rootObject



14
15
16
# File 'lib/ruboty/exec_command/command.rb', line 14

def log_root
  "#{ruboty_root}/logs/exec_command"
end

.ruboty_rootObject



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_pathObject



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

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

#command2pathObject



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_nameObject



54
55
56
# File 'lib/ruboty/exec_command/command.rb', line 54

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

#helpObject



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

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

#log_dirObject



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_argsObject



77
78
79
# File 'lib/ruboty/exec_command/command.rb', line 77

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

#output_file_nameObject



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_filesObject



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_pathObject



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_logObject



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_logObject



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_monthObject



81
82
83
# File 'lib/ruboty/exec_command/command.rb', line 81

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

#this_timeObject



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