raykit 0.0.302 → 0.0.306

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: 1e31c9155319544ec3235de4545a56cb22d31fdaa4bd7dd29ce886b5aa9abba4
4
- data.tar.gz: 72c06e1332d3b1c261a0668a76b2d6708da83352f77872b69ddb1d4f007c75d3
3
+ metadata.gz: ee9ddaa4700fb58d7f4819286b17752c1313498a9e3d159c59f75ec424be92fe
4
+ data.tar.gz: 2ebd78110747cbccdbca74111dc9eb95c6e560ebdb3270ca08775641c3632210
5
5
  SHA512:
6
- metadata.gz: adb8ce5adeeea98305c56a232983a503a9ba2a1a0e8e473a48b4b2a8598b75cf9989d5a86a33242a2f139ffaa41dcb22f053f7b94b5d40bd1d64a7d0713c1b6c
7
- data.tar.gz: abefe06850b65f1968e4f5023747b3cd807c68644f8e21df2aaa4ae1122048352dc348cec6454f71a7d38faba41b5c55257ff31675ebaf38f2b997a74e4746ab
6
+ metadata.gz: feebb5d35660642dc0523065b96e0763dc77b1c3c43d8a35cdf1883cf5dfb8c691eb547c095d926ebe7c9d0de8bbb4a458e5ddd6becc7ee5e7d5156d3b901e62
7
+ data.tar.gz: a5c0a6ad0539af06cfd5b224623053804ff7780e60e78ab5256bb554aa3628716dd1050d014acfa3a3a3bc459245c3663fe6ba2507ca4207d1ef4b50bf20b621
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,263 +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
- output=@output
111
- error=@error
112
- output = @output.last(1000) if(@output.length > 1200)
113
- error = @error.last(1000) if(@output.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
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
126
47
 
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
- log.info event.to_json
145
- # ---------------------------
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)
146
63
  begin
147
- json=JSON.generate(to_hash)
148
- log_filename = Environment.get_dev_dir('log') + '/Raykit.Command/' + SecureRandom.uuid + '.json'
149
- log_dir = File.dirname(log_filename)
150
- FileUtils.mkdir_p(log_dir) if(!Dir.exist?(log_dir))
151
- File.open(log_filename, 'w') {|f| f.write(json) }
152
- if(@exitstatus == 0)
153
- LOG.log('Raykit.Command',Logger::Severity::INFO,json)
154
- else
155
- LOG.log('Raykit.Command',Logger::Severity::ERROR,json)
156
- end
157
- rescue JSON::GeneratorError => ge
158
- puts to_hash.to_s
159
- puts ge.to_s
160
- 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
161
70
  end
162
- end
163
-
164
- def elapsed_str()
165
- if(elapsed < 1.0)
166
- "%.0f" % (elapsed) + "s"
71
+ elapsed = Time.now - start
72
+ end
73
+ if thread.alive?
74
+ if Gem.win_platform?
75
+ `taskkill /f /pid #{pid}`
167
76
  else
168
- "%.0f" % (elapsed) + "s"
77
+ Process.kill("TERM", pid)
169
78
  end
79
+ @exitstatus = 5
80
+ @error = "timed out"
81
+ else
82
+ @exitstatus = thread.value.exitstatus
83
+ end
170
84
  end
171
-
172
- def summary(show_directory=false)
173
- checkmark="\u2713"
174
- #warning="\u26A0"
175
- error="\u0058"
176
- symbol=Rainbow(checkmark.encode('utf-8')).green
177
- symbol=Rainbow(error.encode('utf-8')).red if(@exitstatus!=0)
178
- if(show_directory)
179
- puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow + " (#{@directory})"
180
- else
181
- puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow# + " (#{@directory})"
182
- end
183
- #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
184
101
  end
102
+ end
103
+ end
185
104
 
186
- def details
187
- #summary
188
- puts @output
189
- puts @error
190
- 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)
191
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
192
164
 
193
- def save()
194
- filename = Environment.get_dev_dir('log') + '/Commands/' + SecureRandom.uuid
195
- log_dir=File.dirname(filename)
196
- 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
197
170
 
198
- File.open(filename,'w'){|f|
199
- f.write(JSON.pretty_generate(self.to_hash))
200
- }
201
- self
202
- 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
203
184
 
204
- def to_hash()
205
- hash = Hash.new
206
- hash[:command] = @command
207
- hash[:directory] = @directory
208
- hash[:timeout] = @timeout
209
- hash[:start_time] = @start_time
210
- hash[:elapsed] = @elapsed
211
- hash[:output] = @output.force_encoding("ISO-8859-1").encode("UTF-8")
212
- hash[:error] = @error.force_encoding("ISO-8859-1").encode("UTF-8")
213
- hash[:exitstatus] = @exitstatus
214
- hash[:user] = @user
215
- hash[:machine] = @machine
216
- hash[:context] = "Raykit.Command"
217
- hash
218
- end
185
+ def details
186
+ # summary
187
+ puts @output
188
+ puts @error
189
+ puts
190
+ end
219
191
 
220
- def from_hash(hash)
221
- @command = hash["command"]
222
- @directory=hash["directory"]
223
- @timeout=hash["timeout"]
224
- @start_time=hash["start_time"]
225
- @elapsed=hash["elapsed"]
226
- @output=hash["output"]
227
- @error=hash["error"]
228
- @exitstatus=hash["exitstatus"]
229
- @user=hash["user"] if(hash.include?("user"))
230
- @machine=hash["machine"] if(hash.include?("machine"))
231
- 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)
232
196
 
233
- def self.parse(json)
234
- cmd=Command.new('')
235
- cmd.from_hash(JSON.parse(json))
236
- cmd
237
- end
197
+ File.open(filename, "w") do |f|
198
+ f.write(JSON.pretty_generate(to_hash))
199
+ end
200
+ self
201
+ end
238
202
 
239
- def self.parse_yaml_commands(yaml)
240
- #commands=Array.new()
241
- data = YAML.load(yaml)
242
- 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
243
249
  end
250
+ end
251
+ hash.each do |_k, v|
252
+ next unless v.is_a?(Hash)
244
253
 
245
- def self.get_script_commands(hash)
246
- commands=Array.new
247
- if(hash.key?('script'))
248
- hash['script'].each{|cmd|
249
- commands << cmd
250
- }
251
- end
252
- hash.each{|k,v|
253
- if(v.is_a?(Hash))
254
- subcommands=get_script_commands(v)
255
- subcommands.each{|c|
256
- commands << c
257
- }
258
- end
259
- }
260
- commands
254
+ subcommands = get_script_commands(v)
255
+ subcommands.each do |c|
256
+ commands << c
261
257
  end
258
+ end
259
+ commands
262
260
  end
263
- end
261
+ end
262
+ end