Class: Raykit::Command
- Inherits:
-
Object
- Object
- Raykit::Command
- Defined in:
- lib/raykit/command.rb
Overview
Functionality to support executing and logging system commands
Constant Summary collapse
- @@commands =
Array.new
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
-
#directory ⇒ Object
The working directory, defaults the current directory if not specified.
-
#elapsed ⇒ Object
The execution time in seconds.
-
#error ⇒ Object
Returns the value of attribute error.
-
#exitstatus ⇒ Object
Returns the value of attribute exitstatus.
-
#machine ⇒ Object
Returns the value of attribute machine.
-
#output ⇒ Object
Returns the value of attribute output.
-
#start_time ⇒ Object
The start time.
-
#timeout ⇒ Object
The timeout in seconds, defaults to 0 if not specified.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #details ⇒ Object
- #elapsed_str ⇒ Object
- #from_hash(hash) ⇒ Object
- #init_defaults ⇒ Object
-
#initialize(command, timeout = 0) ⇒ Command
constructor
A new instance of Command.
- #log ⇒ Object
- #run ⇒ Object
- #save ⇒ Object
- #summary(show_directory = false) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(command, timeout = 0) ⇒ Command
Returns a new instance of Command.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/raykit/command.rb', line 34 def initialize(command,timeout=0) @@commands=Array.new init_defaults @command=command @timeout=timeout if(@command.length > 0) run end self end |
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
21 22 23 |
# File 'lib/raykit/command.rb', line 21 def command @command end |
#directory ⇒ Object
The working directory, defaults the current directory if not specified
16 17 18 |
# File 'lib/raykit/command.rb', line 16 def directory @directory end |
#elapsed ⇒ Object
The execution time in seconds
20 21 22 |
# File 'lib/raykit/command.rb', line 20 def elapsed @elapsed end |
#error ⇒ Object
Returns the value of attribute error.
21 22 23 |
# File 'lib/raykit/command.rb', line 21 def error @error end |
#exitstatus ⇒ Object
Returns the value of attribute exitstatus.
21 22 23 |
# File 'lib/raykit/command.rb', line 21 def exitstatus @exitstatus end |
#machine ⇒ Object
Returns the value of attribute machine.
22 23 24 |
# File 'lib/raykit/command.rb', line 22 def machine @machine end |
#output ⇒ Object
Returns the value of attribute output.
21 22 23 |
# File 'lib/raykit/command.rb', line 21 def output @output end |
#start_time ⇒ Object
The start time
18 19 20 |
# File 'lib/raykit/command.rb', line 18 def start_time @start_time end |
#timeout ⇒ Object
The timeout in seconds, defaults to 0 if not specified
14 15 16 |
# File 'lib/raykit/command.rb', line 14 def timeout @timeout end |
#user ⇒ Object
Returns the value of attribute user.
22 23 24 |
# File 'lib/raykit/command.rb', line 22 def user @user end |
Class Method Details
.get_script_commands(hash) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/raykit/command.rb', line 185 def self.get_script_commands(hash) commands=Array.new if(hash.key?('script')) hash['script'].each{|cmd| commands << cmd } end hash.each{|k,v| if(v.is_a?(Hash)) subcommands=get_script_commands(v) subcommands.each{|c| commands << c } end } commands end |
.parse(json) ⇒ Object
173 174 175 176 177 |
# File 'lib/raykit/command.rb', line 173 def self.parse(json) cmd=Command.new('') cmd.from_hash(JSON.parse(json)) cmd end |
.parse_yaml_commands(yaml) ⇒ Object
179 180 181 182 183 |
# File 'lib/raykit/command.rb', line 179 def self.parse_yaml_commands(yaml) #commands=Array.new() data = YAML.load(yaml) commands = get_script_commands(data) end |
Instance Method Details
#details ⇒ Object
126 127 128 129 130 131 |
# File 'lib/raykit/command.rb', line 126 def details #summary puts @output puts @error puts end |
#elapsed_str ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/raykit/command.rb', line 104 def elapsed_str() if(elapsed < 1.0) "%.0f" % (elapsed) + "s" else "%.0f" % (elapsed) + "s" end end |
#from_hash(hash) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/raykit/command.rb', line 160 def from_hash(hash) @command = hash["command"] @directory=hash["directory"] @timeout=hash["timeout"] @start_time=hash["start_time"] @elapsed=hash["elapsed"] @output=hash["output"] @error=hash["error"] @exitstatus=hash["exitstatus"] @user=hash["user"] if(hash.include?("user")) @machine=hash["machine"] if(hash.include?("machine")) end |
#init_defaults ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/raykit/command.rb', line 24 def init_defaults @timeout=0 @directory = Dir.pwd @output = '' @error = '' @exitstatus = 0 @user = Environment.user @machine=Environment.machine end |
#log ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/raykit/command.rb', line 85 def log begin json=JSON.generate(to_hash) log_filename = Environment.get_dev_dir('log') + '/Raykit.Command/' + SecureRandom.uuid + '.json' log_dir = File.dirname(log_filename) FileUtils.mkdir_p(log_dir) if(!Dir.exist?(log_dir)) File.open(log_filename, 'w') {|f| f.write(json) } if(@exitstatus == 0) LOG.log('Raykit.Command',Logger::Severity::INFO,json) else LOG.log('Raykit.Command',Logger::Severity::ERROR,json) end rescue JSON::GeneratorError => ge puts to_hash.to_s puts ge.to_s json=JSON.generate(to_hash) end end |
#run ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/raykit/command.rb', line 45 def run() @start_time = Time.now timer = Timer.new if(@timeout == 0) @output,@error,process_status = Open3.capture3(@command) @exitstatus=process_status.exitstatus else Open3.popen3(@command, :chdir=>@directory) { |stdin,stdout,stderr,thread| tick=2 pid = thread.pid start = Time.now elapsed = Time.now-start while (elapsed) < @timeout and thread.alive? Kernel.select([stdout,stderr], nil, nil, tick) begin @output << stdout.read_nonblock(BUFFER_SIZE) @error << stderr.read_nonblock(BUFFER_SIZE) rescue IO::WaitReadable rescue EOFError @exitstatus=thread.value.exitstatus break end elapsed = Time.now-start end if thread.alive? if(Gem.win_platform?) `taskkill /f /pid #{pid}` else Process.kill("TERM", pid) end @exitstatus=5 else @exitstatus=thread.value.exitstatus end } end @elapsed = timer.elapsed log end |
#save ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/raykit/command.rb', line 133 def save() filename = Environment.get_dev_dir('log') + '/Commands/' + SecureRandom.uuid log_dir=File.dirname(filename) FileUtils.mkdir_p(log_dir) if(!Dir.exist?(log_dir)) File.open(filename,'w'){|f| f.write(JSON.pretty_generate(self.to_hash)) } self end |
#summary(show_directory = false) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/raykit/command.rb', line 112 def summary(show_directory=false) checkmark="\u2713" #warning="\u26A0" error="\u0058" symbol=Rainbow(checkmark.encode('utf-8')).green symbol=Rainbow(error.encode('utf-8')).red if(@exitstatus!=0) if(show_directory) puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow + " (#{@directory})" else puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow# + " (#{@directory})" end #puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow# + " (#{@directory})" end |
#to_hash ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/raykit/command.rb', line 144 def to_hash() hash = Hash.new hash[:command] = @command hash[:directory] = @directory hash[:timeout] = @timeout hash[:start_time] = @start_time hash[:elapsed] = @elapsed hash[:output] = @output.force_encoding("ISO-8859-1").encode("UTF-8") hash[:error] = @error.force_encoding("ISO-8859-1").encode("UTF-8") hash[:exitstatus] = @exitstatus hash[:user] = @user hash[:machine] = @machine hash[:context] = "Raykit.Command" hash end |