raykit 0.0.301 → 0.0.305

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ca8445d21165e3877630c9cd2a91b04af3057db3b95de7a333ec1a7f7350573
4
- data.tar.gz: 2cec72aec2c5df07bdd300530c70829f1523bf532bd1710aa9d5860a2cd12819
3
+ metadata.gz: 7ce7014fb0f09e6200f197f0af6d67c51dbce58df476d440d8cb1dc39a99f44d
4
+ data.tar.gz: ae8b9b47b88762e6778d594bb34a1d6da36f20e5c69f2dbd3ee7fc9c861d09f0
5
5
  SHA512:
6
- metadata.gz: f986d3ac57b0710ce7132358a96c1c8c1cd24e68ad02bc2cf75b34860eb0a474d3a727ab80438d23b211e51040c6b75a6f734286545e1e368ac2719ff7c85348
7
- data.tar.gz: 5a9d8f4c04f090a1089a5f0d2c6fef0b28cd1aeb691b289ca778195065313b478e6efec4c280aaae89f94804b70e7369c860d212bd30a75cf16af64e0ca048a3
6
+ metadata.gz: 073f890fd9f91ddccf1c3a264865f2b5359b6f315f9434823d6683c5b3d99e0dc4b56dffa1d9e2a7c2cee41a4fa74892c0321318ff466f7237e42c8ddb09c0db
7
+ data.tar.gz: 6631387820d5366183c50a0f8f0b6fb31eccff7b9c778dab3ea0e89c13b59c66c6ec6c31c86dc4bbc82fc8352ba318444776192905f9bb1b37052c68e36b8b1f
data/bin/raykit CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
- require_relative '../lib/raykit.rb'
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/raykit'
3
5
  console = Raykit::Console.new
4
- console.run
6
+ console.run
@@ -1,258 +1,262 @@
1
- require 'open3'
2
- require 'timeout'
3
- require 'json'
4
- require 'yaml'
5
- require 'logger'
6
- require 'securerandom'
1
+ # frozen_string_literal: true
7
2
 
8
- BUFFER_SIZE=1024 if(!defined?(BUFFER_SIZE))
9
- module Raykit
10
- # Functionality to support executing and logging system commands
11
- class Command
12
- @@commands=Array.new
13
- # The timeout in seconds, defaults to 0 if not specified
14
- attr_accessor :timeout
15
- # The working directory, defaults the current directory if not specified
16
- attr_accessor :directory
17
- # The start time
18
- attr_accessor :start_time
19
- # The execution time in seconds
20
- attr_accessor :elapsed
21
- attr_accessor :command,:output,:error,:exitstatus
22
- attr_accessor :user,:machine
23
- attr_accessor :logging_enabled
24
- attr_accessor :success_log_level
3
+ require "open3"
4
+ require "timeout"
5
+ require "json"
6
+ require "yaml"
7
+ require "logger"
8
+ require "securerandom"
25
9
 
26
- def init_defaults
27
- @timeout=0
28
- @directory = Dir.pwd
29
- @output = ''
30
- @error = ''
31
- @exitstatus = 0
32
- @user = Environment.user
33
- @machine=Environment.machine
34
- @logging_enabled = true
35
- end
10
+ BUFFER_SIZE = 1024 unless defined?(BUFFER_SIZE)
36
11
 
37
- def initialize(command,timeout=0,success_log_level=Logger::DEBUG,logging_enabled=true)
38
- @@commands=Array.new
39
- init_defaults
40
- @success_log_level=success_log_level
41
- @logging_enabled=logging_enabled
42
- @command=command
43
- @timeout=timeout
44
- if(@command.length > 0)
45
- run
46
- end
47
- self
48
- end
12
+ module Raykit
13
+ # Functionality to support executing and logging system commands
14
+ class Command
15
+ @@commands = []
16
+ # The timeout in seconds, defaults to 0 if not specified
17
+ attr_accessor :timeout
18
+ # The working directory, defaults the current directory if not specified
19
+ attr_accessor :directory
20
+ # The start time
21
+ attr_accessor :start_time
22
+ # The execution time in seconds
23
+ attr_accessor :elapsed
24
+ attr_accessor :command, :output, :error, :exitstatus, :user, :machine, :logging_enabled, :success_log_level
49
25
 
50
- def run()
51
- @start_time = Time.now
52
- timer = Timer.new
53
- if(@timeout == 0)
54
- @output,@error,process_status = Open3.capture3(@command)
55
- @exitstatus=process_status.exitstatus
56
- else
57
- Open3.popen3(@command, :chdir=>@directory) { |stdin,stdout,stderr,thread|
58
- tick=2
59
- pid = thread.pid
60
- start = Time.now
61
- elapsed = Time.now-start
62
- while ((elapsed) < @timeout) and thread.alive?
63
- Kernel.select([stdout,stderr], nil, nil, tick)
64
- begin
65
- @output << stdout.read_nonblock(BUFFER_SIZE)
66
- @error << stderr.read_nonblock(BUFFER_SIZE)
67
- rescue IO::WaitReadable
68
- rescue EOFError
69
- @exitstatus=thread.value.exitstatus
70
- break
71
- end
72
- elapsed = Time.now-start
73
- end
74
- if thread.alive?
75
- if(Gem.win_platform?)
76
- `taskkill /f /pid #{pid}`
77
- else
78
- Process.kill("TERM", pid)
79
- end
80
- @exitstatus=5
81
- @error="timed out"
82
- else
83
- @exitstatus=thread.value.exitstatus
84
- end
85
- }
86
- end
87
- @elapsed = timer.elapsed
88
- if @logging_enabled
89
- log
90
- if @exitstatus != 0
91
- to_log_event.to_seq
92
- else
93
- if !@success_log_level.nil?
94
- e = to_log_event
95
- e["Level"] = "Verbose" if @success_log_level == "Verbose"
96
- e["Level"]= "Debug" if @success_log_level == Logger::DEBUG
97
- e["Level"] = "Information" if @success_log_level == Logger::INFO
98
- e["Level"] = "Warning" if @elapsed > 60*2
99
- e.to_seq
100
- end
101
- end
102
- end
103
- end
26
+ def init_defaults
27
+ @timeout = 0
28
+ @directory = Dir.pwd
29
+ @output = ""
30
+ @error = ""
31
+ @exitstatus = 0
32
+ @user = Environment.user
33
+ @machine = Environment.machine
34
+ @logging_enabled = true
35
+ end
104
36
 
105
- def to_log_event
106
- secrets=Secrets.new()
107
- msg =secrets.hide(@command)
108
- level="Verbose"
109
- level="Warning" if @exitstatus != 0
110
- Raykit::LogEvent.new(level,msg,{
111
- "SourceContext"=>"Raykit::Command",
112
- "Category"=>"Command",
113
- "Timeout"=>@timeout,
114
- "Directory"=>@directory,
115
- "Output"=>@output,
116
- "Error"=>@error,
117
- "ExitStatus"=>@exitstatus,
118
- "Elapsed"=>elapsed_str
119
- })
120
- end
37
+ def initialize(command, timeout = 0, success_log_level = Logger::DEBUG, logging_enabled = true)
38
+ @@commands = []
39
+ init_defaults
40
+ @success_log_level = success_log_level
41
+ @logging_enabled = logging_enabled
42
+ @command = command
43
+ @timeout = timeout
44
+ run if @command.length.positive?
45
+ self
46
+ end
121
47
 
122
- def log
123
- # --- Rolling File JSON -----
124
- log = Logger.new("#{Raykit::Environment::log_dir}/Raykit.Commands.txt",'daily')
125
- log.formatter = proc do |severity,datetime,progname,msg|
126
- "#{msg}\n"
127
- end
128
- secrets=Secrets.new()
129
- msg =secrets.hide(@command)#"?"# self.summary(false)
130
- level="Verbose"
131
- level="Warning" if @exitstatus != 0
132
- event = Raykit::LogEvent.new(level,msg,{
133
- "Timeout"=>@timeout,
134
- "Directory"=>@directory,
135
- "Output"=>@output,
136
- "Error"=>@error,
137
- "ExitStatus"=>@exitstatus,
138
- "Elapsed"=>elapsed_str})
139
- log.info event.to_json
140
- # ---------------------------
48
+ def run
49
+ # puts '---running---'
50
+ @start_time = Time.now
51
+ timer = Timer.new
52
+ if @timeout.zero?
53
+ @output, @error, process_status = Open3.capture3(@command)
54
+ @exitstatus = process_status.exitstatus
55
+ else
56
+ Open3.popen3(@command, chdir: @directory) do |_stdin, stdout, stderr, thread|
57
+ tick = 2
58
+ pid = thread.pid
59
+ start = Time.now
60
+ elapsed = Time.now - start
61
+ while ((elapsed) < @timeout) && thread.alive?
62
+ Kernel.select([stdout, stderr], nil, nil, tick)
141
63
  begin
142
- json=JSON.generate(to_hash)
143
- log_filename = Environment.get_dev_dir('log') + '/Raykit.Command/' + SecureRandom.uuid + '.json'
144
- log_dir = File.dirname(log_filename)
145
- FileUtils.mkdir_p(log_dir) if(!Dir.exist?(log_dir))
146
- File.open(log_filename, 'w') {|f| f.write(json) }
147
- if(@exitstatus == 0)
148
- LOG.log('Raykit.Command',Logger::Severity::INFO,json)
149
- else
150
- LOG.log('Raykit.Command',Logger::Severity::ERROR,json)
151
- end
152
- rescue JSON::GeneratorError => ge
153
- puts to_hash.to_s
154
- puts ge.to_s
155
- json=JSON.generate(to_hash)
64
+ @output << stdout.read_nonblock(BUFFER_SIZE)
65
+ @error << stderr.read_nonblock(BUFFER_SIZE)
66
+ rescue IO::WaitReadable
67
+ rescue EOFError
68
+ @exitstatus = thread.value.exitstatus
69
+ break
156
70
  end
157
- end
158
-
159
- def elapsed_str()
160
- if(elapsed < 1.0)
161
- "%.0f" % (elapsed) + "s"
71
+ elapsed = Time.now - start
72
+ end
73
+ if thread.alive?
74
+ if Gem.win_platform?
75
+ `taskkill /f /pid #{pid}`
162
76
  else
163
- "%.0f" % (elapsed) + "s"
77
+ Process.kill("TERM", pid)
164
78
  end
79
+ @exitstatus = 5
80
+ @error = "timed out"
81
+ else
82
+ @exitstatus = thread.value.exitstatus
83
+ end
165
84
  end
166
-
167
- def summary(show_directory=false)
168
- checkmark="\u2713"
169
- #warning="\u26A0"
170
- error="\u0058"
171
- symbol=Rainbow(checkmark.encode('utf-8')).green
172
- symbol=Rainbow(error.encode('utf-8')).red if(@exitstatus!=0)
173
- if(show_directory)
174
- puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow + " (#{@directory})"
175
- else
176
- puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow# + " (#{@directory})"
177
- end
178
- #puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow# + " (#{@directory})"
85
+ end
86
+ @elapsed = timer.elapsed
87
+ if @logging_enabled
88
+ log
89
+ if @exitstatus != 0
90
+ to_log_event.to_seq
91
+ else
92
+ # puts '---logging---'
93
+ unless @success_log_level.nil?
94
+ e = to_log_event
95
+ e["Level"] = "Verbose" if @success_log_level == "Verbose"
96
+ e["Level"] = "Debug" if @success_log_level == Logger::DEBUG
97
+ e["Level"] = "Information" if @success_log_level == Logger::INFO
98
+ e["Level"] = "Warning" if @elapsed > 60 * 2
99
+ e.to_seq
100
+ end
179
101
  end
102
+ end
103
+ end
180
104
 
181
- def details
182
- #summary
183
- puts @output
184
- puts @error
185
- puts
105
+ def to_log_event
106
+ secrets = Secrets.new
107
+ msg = secrets.hide(@command)
108
+ level = "Verbose"
109
+ level = "Warning" if @exitstatus != 0
110
+ output = @output
111
+ error = @error
112
+ output = @output[-1000..-1] if @output.length > 1200
113
+ error = @error[-1000..-1] if @error.length > 1200
114
+ Raykit::LogEvent.new(level, msg, {
115
+ "SourceContext" => "Raykit::Command",
116
+ "Category" => "Command",
117
+ "Timeout" => @timeout,
118
+ "Directory" => @directory,
119
+ "Output" => output,
120
+ "Error" => error,
121
+ "ExitStatus" => @exitstatus,
122
+ "Elapsed" => elapsed_str,
123
+ "ElapsedSeconds" => @elapsed,
124
+ })
125
+ end
126
+
127
+ def log
128
+ # --- Rolling File JSON -----
129
+ log = Logger.new("#{Raykit::Environment.log_dir}/Raykit.Commands.txt", "daily")
130
+ log.formatter = proc do |_severity, _datetime, _progname, msg|
131
+ "#{msg}\n"
132
+ end
133
+ secrets = Secrets.new
134
+ msg = secrets.hide(@command) # "?"# self.summary(false)
135
+ level = "Verbose"
136
+ level = "Warning" if @exitstatus != 0
137
+ event = Raykit::LogEvent.new(level, msg, {
138
+ "Timeout" => @timeout,
139
+ "Directory" => @directory,
140
+ "Output" => @output,
141
+ "Error" => @error,
142
+ "ExitStatus" => @exitstatus,
143
+ "Elapsed" => elapsed_str,
144
+ })
145
+ log.info event.to_json
146
+ # ---------------------------
147
+ begin
148
+ json = JSON.generate(to_hash)
149
+ log_filename = "#{Environment.get_dev_dir("log")}/Raykit.Command/#{SecureRandom.uuid}.json"
150
+ log_dir = File.dirname(log_filename)
151
+ FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
152
+ File.open(log_filename, "w") { |f| f.write(json) }
153
+ if @exitstatus.zero?
154
+ LOG.log("Raykit.Command", Logger::Severity::INFO, json)
155
+ else
156
+ LOG.log("Raykit.Command", Logger::Severity::ERROR, json)
186
157
  end
158
+ rescue JSON::GeneratorError => e
159
+ puts to_hash.to_s
160
+ puts e.to_s
161
+ json = JSON.generate(to_hash)
162
+ end
163
+ end
187
164
 
188
- def save()
189
- filename = Environment.get_dev_dir('log') + '/Commands/' + SecureRandom.uuid
190
- log_dir=File.dirname(filename)
191
- FileUtils.mkdir_p(log_dir) if(!Dir.exist?(log_dir))
165
+ def elapsed_str
166
+ if elapsed < 1.0
167
+ end
168
+ "#{format("%.0f", elapsed)}s"
169
+ end
192
170
 
193
- File.open(filename,'w'){|f|
194
- f.write(JSON.pretty_generate(self.to_hash))
195
- }
196
- self
197
- end
171
+ def summary(show_directory = false)
172
+ checkmark = "\u2713"
173
+ # warning="\u26A0"
174
+ error = "\u0058"
175
+ symbol = Rainbow(checkmark.encode("utf-8")).green
176
+ symbol = Rainbow(error.encode("utf-8")).red if @exitstatus != 0
177
+ if show_directory
178
+ puts "#{symbol} #{elapsed_str.rjust(4)} #{Rainbow(SECRETS.hide(@command)).yellow} (#{@directory})"
179
+ else
180
+ puts "#{symbol} #{elapsed_str.rjust(4)} #{Rainbow(SECRETS.hide(@command)).yellow}" # + " (#{@directory})"
181
+ end
182
+ # puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow# + " (#{@directory})"
183
+ end
198
184
 
199
- def to_hash()
200
- hash = Hash.new
201
- hash[:command] = @command
202
- hash[:directory] = @directory
203
- hash[:timeout] = @timeout
204
- hash[:start_time] = @start_time
205
- hash[:elapsed] = @elapsed
206
- hash[:output] = @output.force_encoding("ISO-8859-1").encode("UTF-8")
207
- hash[:error] = @error.force_encoding("ISO-8859-1").encode("UTF-8")
208
- hash[:exitstatus] = @exitstatus
209
- hash[:user] = @user
210
- hash[:machine] = @machine
211
- hash[:context] = "Raykit.Command"
212
- hash
213
- end
185
+ def details
186
+ # summary
187
+ puts @output
188
+ puts @error
189
+ puts
190
+ end
214
191
 
215
- def from_hash(hash)
216
- @command = hash["command"]
217
- @directory=hash["directory"]
218
- @timeout=hash["timeout"]
219
- @start_time=hash["start_time"]
220
- @elapsed=hash["elapsed"]
221
- @output=hash["output"]
222
- @error=hash["error"]
223
- @exitstatus=hash["exitstatus"]
224
- @user=hash["user"] if(hash.include?("user"))
225
- @machine=hash["machine"] if(hash.include?("machine"))
226
- end
192
+ def save
193
+ filename = "#{Environment.get_dev_dir("log")}/Commands/#{SecureRandom.uuid}"
194
+ log_dir = File.dirname(filename)
195
+ FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
227
196
 
228
- def self.parse(json)
229
- cmd=Command.new('')
230
- cmd.from_hash(JSON.parse(json))
231
- cmd
232
- end
197
+ File.open(filename, "w") do |f|
198
+ f.write(JSON.pretty_generate(to_hash))
199
+ end
200
+ self
201
+ end
233
202
 
234
- def self.parse_yaml_commands(yaml)
235
- #commands=Array.new()
236
- data = YAML.load(yaml)
237
- commands = get_script_commands(data)
203
+ def to_hash
204
+ hash = {}
205
+ hash[:command] = @command
206
+ hash[:directory] = @directory
207
+ hash[:timeout] = @timeout
208
+ hash[:start_time] = @start_time
209
+ hash[:elapsed] = @elapsed
210
+ hash[:output] = @output.force_encoding("ISO-8859-1").encode("UTF-8")
211
+ hash[:error] = @error.force_encoding("ISO-8859-1").encode("UTF-8")
212
+ hash[:exitstatus] = @exitstatus
213
+ hash[:user] = @user
214
+ hash[:machine] = @machine
215
+ hash[:context] = "Raykit.Command"
216
+ hash
217
+ end
218
+
219
+ def from_hash(hash)
220
+ @command = hash["command"]
221
+ @directory = hash["directory"]
222
+ @timeout = hash["timeout"]
223
+ @start_time = hash["start_time"]
224
+ @elapsed = hash["elapsed"]
225
+ @output = hash["output"]
226
+ @error = hash["error"]
227
+ @exitstatus = hash["exitstatus"]
228
+ @user = hash["user"] if hash.include?("user")
229
+ @machine = hash["machine"] if hash.include?("machine")
230
+ end
231
+
232
+ def self.parse(json)
233
+ cmd = Command.new("")
234
+ cmd.from_hash(JSON.parse(json))
235
+ cmd
236
+ end
237
+
238
+ def self.parse_yaml_commands(yaml)
239
+ # commands=Array.new()
240
+ data = YAML.safe_load(yaml)
241
+ commands = get_script_commands(data)
242
+ end
243
+
244
+ def self.get_script_commands(hash)
245
+ commands = []
246
+ if hash.key?("script")
247
+ hash["script"].each do |cmd|
248
+ commands << cmd
238
249
  end
250
+ end
251
+ hash.each do |_k, v|
252
+ next unless v.is_a?(Hash)
239
253
 
240
- def self.get_script_commands(hash)
241
- commands=Array.new
242
- if(hash.key?('script'))
243
- hash['script'].each{|cmd|
244
- commands << cmd
245
- }
246
- end
247
- hash.each{|k,v|
248
- if(v.is_a?(Hash))
249
- subcommands=get_script_commands(v)
250
- subcommands.each{|c|
251
- commands << c
252
- }
253
- end
254
- }
255
- commands
254
+ subcommands = get_script_commands(v)
255
+ subcommands.each do |c|
256
+ commands << c
256
257
  end
258
+ end
259
+ commands
257
260
  end
258
- end
261
+ end
262
+ end