raykit 0.0.304 → 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: af677bf68d15ed6aeabaf70b1523e4e350a127d9b836d5a183702584045fa65b
4
- data.tar.gz: 2690c79f04897a80b5c5f9d300eb8c41df3d57b7b7f25547910f9eeea340bad1
3
+ metadata.gz: 7ce7014fb0f09e6200f197f0af6d67c51dbce58df476d440d8cb1dc39a99f44d
4
+ data.tar.gz: ae8b9b47b88762e6778d594bb34a1d6da36f20e5c69f2dbd3ee7fc9c861d09f0
5
5
  SHA512:
6
- metadata.gz: 9844953716e273bd5593c8aea8e4085e2ddf7279bf47e8fc51b37a128b654ab92f901ae98c665f337def725a2cddadefe151bf260d33e1251fff8da38dbc7173
7
- data.tar.gz: 8e5b9e5be341b7bedc27f022afe5cd687276d424c0f1cffe8945e7f174908b6e8f74e466c7a460cc60442d373b7107d1adc865a81e15d9921cfdd04d62fc7098
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,265 +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
- #puts '---running---'
52
- @start_time = Time.now
53
- timer = Timer.new
54
- if(@timeout == 0)
55
- @output,@error,process_status = Open3.capture3(@command)
56
- @exitstatus=process_status.exitstatus
57
- else
58
- Open3.popen3(@command, :chdir=>@directory) { |stdin,stdout,stderr,thread|
59
- tick=2
60
- pid = thread.pid
61
- start = Time.now
62
- elapsed = Time.now-start
63
- while ((elapsed) < @timeout) and thread.alive?
64
- Kernel.select([stdout,stderr], nil, nil, tick)
65
- begin
66
- @output << stdout.read_nonblock(BUFFER_SIZE)
67
- @error << stderr.read_nonblock(BUFFER_SIZE)
68
- rescue IO::WaitReadable
69
- rescue EOFError
70
- @exitstatus=thread.value.exitstatus
71
- break
72
- end
73
- elapsed = Time.now-start
74
- end
75
- if thread.alive?
76
- if(Gem.win_platform?)
77
- `taskkill /f /pid #{pid}`
78
- else
79
- Process.kill("TERM", pid)
80
- end
81
- @exitstatus=5
82
- @error="timed out"
83
- else
84
- @exitstatus=thread.value.exitstatus
85
- end
86
- }
87
- end
88
- @elapsed = timer.elapsed
89
- if @logging_enabled
90
- log
91
- if @exitstatus != 0
92
- to_log_event.to_seq
93
- else
94
- #puts '---logging---'
95
- if !@success_log_level.nil?
96
- e = to_log_event
97
- e["Level"] = "Verbose" if @success_log_level == "Verbose"
98
- e["Level"]= "Debug" if @success_log_level == Logger::DEBUG
99
- e["Level"] = "Information" if @success_log_level == Logger::INFO
100
- e["Level"] = "Warning" if @elapsed > 60*2
101
- e.to_seq
102
- end
103
- end
104
- end
105
- 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
106
36
 
107
- def to_log_event
108
- secrets=Secrets.new()
109
- msg =secrets.hide(@command)
110
- level="Verbose"
111
- level="Warning" if @exitstatus != 0
112
- output=@output
113
- error=@error
114
- output = @output[-1000..-1] if(@output.length > 1200)
115
- error = @error[-1000..-1] if(@error.length > 1200)
116
- Raykit::LogEvent.new(level,msg,{
117
- "SourceContext"=>"Raykit::Command",
118
- "Category"=>"Command",
119
- "Timeout"=>@timeout,
120
- "Directory"=>@directory,
121
- "Output"=>output,
122
- "Error"=>error,
123
- "ExitStatus"=>@exitstatus,
124
- "Elapsed"=>elapsed_str,
125
- "ElapsedSeconds"=>@elapsed
126
- })
127
- 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
128
47
 
129
- def log
130
- # --- Rolling File JSON -----
131
- log = Logger.new("#{Raykit::Environment::log_dir}/Raykit.Commands.txt",'daily')
132
- log.formatter = proc do |severity,datetime,progname,msg|
133
- "#{msg}\n"
134
- end
135
- secrets=Secrets.new()
136
- msg =secrets.hide(@command)#"?"# self.summary(false)
137
- level="Verbose"
138
- level="Warning" if @exitstatus != 0
139
- event = Raykit::LogEvent.new(level,msg,{
140
- "Timeout"=>@timeout,
141
- "Directory"=>@directory,
142
- "Output"=>@output,
143
- "Error"=>@error,
144
- "ExitStatus"=>@exitstatus,
145
- "Elapsed"=>elapsed_str})
146
- log.info event.to_json
147
- # ---------------------------
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)
148
63
  begin
149
- json=JSON.generate(to_hash)
150
- log_filename = Environment.get_dev_dir('log') + '/Raykit.Command/' + SecureRandom.uuid + '.json'
151
- log_dir = File.dirname(log_filename)
152
- FileUtils.mkdir_p(log_dir) if(!Dir.exist?(log_dir))
153
- File.open(log_filename, 'w') {|f| f.write(json) }
154
- if(@exitstatus == 0)
155
- LOG.log('Raykit.Command',Logger::Severity::INFO,json)
156
- else
157
- LOG.log('Raykit.Command',Logger::Severity::ERROR,json)
158
- end
159
- rescue JSON::GeneratorError => ge
160
- puts to_hash.to_s
161
- puts ge.to_s
162
- 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
163
70
  end
164
- end
165
-
166
- def elapsed_str()
167
- if(elapsed < 1.0)
168
- "%.0f" % (elapsed) + "s"
71
+ elapsed = Time.now - start
72
+ end
73
+ if thread.alive?
74
+ if Gem.win_platform?
75
+ `taskkill /f /pid #{pid}`
169
76
  else
170
- "%.0f" % (elapsed) + "s"
77
+ Process.kill("TERM", pid)
171
78
  end
79
+ @exitstatus = 5
80
+ @error = "timed out"
81
+ else
82
+ @exitstatus = thread.value.exitstatus
83
+ end
172
84
  end
173
-
174
- def summary(show_directory=false)
175
- checkmark="\u2713"
176
- #warning="\u26A0"
177
- error="\u0058"
178
- symbol=Rainbow(checkmark.encode('utf-8')).green
179
- symbol=Rainbow(error.encode('utf-8')).red if(@exitstatus!=0)
180
- if(show_directory)
181
- puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow + " (#{@directory})"
182
- else
183
- puts symbol + " " + elapsed_str.rjust(4) + " " + Rainbow(SECRETS.hide(@command)).yellow# + " (#{@directory})"
184
- end
185
- #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
186
101
  end
102
+ end
103
+ end
187
104
 
188
- def details
189
- #summary
190
- puts @output
191
- puts @error
192
- 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)
193
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
194
164
 
195
- def save()
196
- filename = Environment.get_dev_dir('log') + '/Commands/' + SecureRandom.uuid
197
- log_dir=File.dirname(filename)
198
- 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
199
170
 
200
- File.open(filename,'w'){|f|
201
- f.write(JSON.pretty_generate(self.to_hash))
202
- }
203
- self
204
- 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
205
184
 
206
- def to_hash()
207
- hash = Hash.new
208
- hash[:command] = @command
209
- hash[:directory] = @directory
210
- hash[:timeout] = @timeout
211
- hash[:start_time] = @start_time
212
- hash[:elapsed] = @elapsed
213
- hash[:output] = @output.force_encoding("ISO-8859-1").encode("UTF-8")
214
- hash[:error] = @error.force_encoding("ISO-8859-1").encode("UTF-8")
215
- hash[:exitstatus] = @exitstatus
216
- hash[:user] = @user
217
- hash[:machine] = @machine
218
- hash[:context] = "Raykit.Command"
219
- hash
220
- end
185
+ def details
186
+ # summary
187
+ puts @output
188
+ puts @error
189
+ puts
190
+ end
221
191
 
222
- def from_hash(hash)
223
- @command = hash["command"]
224
- @directory=hash["directory"]
225
- @timeout=hash["timeout"]
226
- @start_time=hash["start_time"]
227
- @elapsed=hash["elapsed"]
228
- @output=hash["output"]
229
- @error=hash["error"]
230
- @exitstatus=hash["exitstatus"]
231
- @user=hash["user"] if(hash.include?("user"))
232
- @machine=hash["machine"] if(hash.include?("machine"))
233
- 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)
234
196
 
235
- def self.parse(json)
236
- cmd=Command.new('')
237
- cmd.from_hash(JSON.parse(json))
238
- cmd
239
- end
197
+ File.open(filename, "w") do |f|
198
+ f.write(JSON.pretty_generate(to_hash))
199
+ end
200
+ self
201
+ end
240
202
 
241
- def self.parse_yaml_commands(yaml)
242
- #commands=Array.new()
243
- data = YAML.load(yaml)
244
- 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
245
249
  end
250
+ end
251
+ hash.each do |_k, v|
252
+ next unless v.is_a?(Hash)
246
253
 
247
- def self.get_script_commands(hash)
248
- commands=Array.new
249
- if(hash.key?('script'))
250
- hash['script'].each{|cmd|
251
- commands << cmd
252
- }
253
- end
254
- hash.each{|k,v|
255
- if(v.is_a?(Hash))
256
- subcommands=get_script_commands(v)
257
- subcommands.each{|c|
258
- commands << c
259
- }
260
- end
261
- }
262
- commands
254
+ subcommands = get_script_commands(v)
255
+ subcommands.each do |c|
256
+ commands << c
263
257
  end
258
+ end
259
+ commands
264
260
  end
265
- end
261
+ end
262
+ end