toolrack 0.3.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9695907a18e7af223dc46afdede9aadf6a63860cd85600eac575d1707f92122a
4
- data.tar.gz: 33b7a5e328a97b336f75d74e87adfd11c9d2e4e4db65096db9657cbc8d438a3b
3
+ metadata.gz: 1376b2757fe58eb3b84626106d08631b990f5fcc3955101ea68d652a620be53f
4
+ data.tar.gz: 78aacec33c7f4e9d654c7d73c3a0c08ff5f90d94c0cabcfbe356558627cbd80c
5
5
  SHA512:
6
- metadata.gz: ed2140ad2e0ddd44977b8375e02f4d81ce30e2c5c3c8ade312d0ebb24213373d38783a3f509994f23bb866977d74825e42f8ea585f78a1bd64afdb24d017294c
7
- data.tar.gz: ec78c82697275f8ad85900892a017dfe0be6edbc5a19f761d382d53dcf372c9f3c68f7d7bb4678b7af02d25a77346f2ba082c6d099d002ac563c9173fb40d8d3
6
+ metadata.gz: 2728bd75dfd2ef4a93d4059756e97409248d0c0f54bb30a1edfd90178899fdac604c31769830c524000638834bc7da01e7708cca3248db68e929cf58a7bb7ee5
7
+ data.tar.gz: d7b825fed51fefeec270b998c5f6c4f3250d3a1df4579d51c89cef150f18d3428d6ebd39e9e837e8b44498c6bce884e8b82b8e2df9bda7fabcf954917c3b2d39
data/.gitignore CHANGED
@@ -6,3 +6,9 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ *.gem
10
+ *.log
11
+ tags
12
+ Gemfile.lock
13
+ test.ssh
14
+ test.ssh.pub
data/README.md CHANGED
@@ -43,9 +43,10 @@ class EditController
43
43
  end
44
44
  ```
45
45
 
46
- Currently it has only 2 modules:
46
+ Currently it has 3 modules:
47
47
  * Condition Utilities
48
48
  * is_empty?(obj) - I find rather effort intensive to call if not (obj.nil? or obj.empty?) for items that I want to check for validity before process. Hence this method shall test for .nil?. If the object also respond to :empty? it shall be called again. For example integer type does not support .empty?
49
+ * not_empty?(obj) - Reverse of the is\_empty?(obj) above
49
50
 
50
51
  * Exception Utilities
51
52
  * raise_if_empty(obj, message, error) - Extension from the is_empty?() above, usually if it is empty, an exception shall be raised. It is just combined the conditions with raise of exception.
@@ -55,6 +56,12 @@ Currently it has only 2 modules:
55
56
  * raise_if_false(obj, message, error) - As the name implied
56
57
  * raise_if_true(obj, message, error) - As the name implied
57
58
 
59
+ * Process Utilities
60
+ * exec(command, options = { ), &block) - Passing in the command into the method and result is returned to caller. If options is not given shall go to basic execution which is the backtick (\`). If block empty, result of the command is returned to caller. If block is given, result (true or false of the execution of command and any result from STDOUT is call to block provider(e.g. block.call($?, result)
61
+
62
+ # Note on Process Utilities
63
+
64
+ The command is passed as it is to the operating system which includes fatal operation such as "rm -rf". Library at current stage made no filtering on system harmful command hence the caller of the library has to take extra precaution on what command is being passed into the library
58
65
 
59
66
 
60
67
 
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
3
 
4
+ require 'devops_helper'
5
+
4
6
  Rake::TestTask.new(:test) do |t|
5
7
  t.libs << "test"
6
8
  t.libs << "lib"
data/lib/toolrack.rb CHANGED
@@ -3,9 +3,12 @@ require "toolrack/version"
3
3
  require 'tlogger'
4
4
  require 'singleton'
5
5
 
6
+ require 'fileutils'
7
+
8
+ require_relative 'toolrack/global'
6
9
  require_relative 'toolrack/exception_utils'
7
10
  require_relative 'toolrack/condition_utils'
8
- #require_relative 'toolrack/process_utils'
11
+ require_relative 'toolrack/process_utils'
9
12
  require_relative 'toolrack/runtime_utils'
10
13
 
11
14
  module Antrapol
@@ -13,32 +16,9 @@ module Antrapol
13
16
  class Error < StandardError; end
14
17
  # Your code goes here...
15
18
 
16
- class GlobalConf
17
- include Singleton
19
+ end
20
+ end
18
21
 
19
- end
22
+ ToolRack = Antrapol::ToolRack
20
23
 
21
- class Logger
22
- include Singleton
23
- include Antrapol::ToolRack::ConditionUtils
24
24
 
25
- attr_reader :glogger
26
- def initialize
27
- # boolean
28
- loggerDebug = ENV['TOOLRACK_DEBUG']
29
- logFile = ENV['TOOLRACK_LOGFILE']
30
- maxLogNo = ENV['TOOLRACK_MAX_LOGFILE'] || 10
31
- logFileSize = ENV['TOOLRACK_MAX_LOGFILE_SIZE'] || 10*1024*1024
32
-
33
- if not is_empty?(loggerDebug) and loggerDebug
34
- @glogger = Tlogger.new(STDOUT)
35
- elsif not is_empty?(logFile)
36
- @glogger = Tlogger.new(logFile,maxLogNo,logFileSize)
37
- else
38
- @glogger = Tlogger.new('toolrack.log',maxLogNo,logFileSize)
39
- end
40
- end
41
- end
42
-
43
- end
44
- end
@@ -5,10 +5,16 @@ module Antrapol
5
5
  module ConditionUtils
6
6
 
7
7
  def is_empty?(obj)
8
- if obj.nil?
8
+ if not defined?(obj)
9
+ true
10
+ elsif obj.nil?
9
11
  true
10
12
  elsif obj.respond_to?(:empty?)
11
- obj.empty?
13
+ if obj.respond_to?(:strip)
14
+ obj.strip.empty?
15
+ else
16
+ obj.empty?
17
+ end
12
18
  else
13
19
  false
14
20
  end
@@ -0,0 +1,35 @@
1
+
2
+ require 'singleton'
3
+ require_relative 'condition_utils'
4
+
5
+ module Antrapol
6
+ module ToolRack
7
+
8
+ class Logger
9
+ include Singleton
10
+ include Antrapol::ToolRack::ConditionUtils
11
+
12
+ attr_reader :glogger
13
+ def initialize
14
+ # boolean
15
+ loggerDebug = ENV['TOOLRACK_DEBUG']
16
+ logFile = ENV['TOOLRACK_LOGFILE'] || File.join(Dir.home, 'antrapol_logs','toolrack.log')
17
+ maxLogNo = ENV['TOOLRACK_MAX_LOGFILE'] || 10
18
+ logFileSize = ENV['TOOLRACK_MAX_LOGFILE_SIZE'] || 10*1024*1024
19
+
20
+ logFileDir = File.dirname(logFile)
21
+ if not File.exist?(logFileDir)
22
+ FileUtils.mkdir_p(logFileDir)
23
+ end
24
+
25
+ if not_empty?(loggerDebug) and (loggerDebug.downcase == 'true')
26
+ @glogger = Tlogger.new(STDOUT)
27
+ else
28
+ @glogger = Tlogger.new(logFile,maxLogNo,logFileSize)
29
+ end
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+
@@ -1,21 +1,64 @@
1
1
 
2
- require 'pty'
2
+ require_relative 'global'
3
+ require_relative 'runtime_utils'
4
+
5
+ if Antrapol::ToolRack::RuntimeUtils.on_windows?
6
+ # pty causing "function 'openpty' not found in msvcrt.dll" in some
7
+ # windows platform. Due to patches?
8
+ Antrapol::ToolRack::Logger.instance.glogger.debug "On Windows. Not going to load gem 'pty'"
9
+ else
10
+ require 'pty'
11
+ end
3
12
  require 'expect'
4
13
  require 'io/console'
5
14
  require 'tlogger'
6
15
  require 'open3'
7
16
 
8
17
 
18
+
9
19
  module Antrapol
10
20
  module ToolRack
11
21
  module ProcessUtils
12
22
 
23
+ def exec(cmd, opts = { }, &block)
24
+ type = opts[:exec_type]
25
+ if not type.nil?
26
+ instance(type, cmd, opts, &block)
27
+ else
28
+ instance(:basic, cmd, opts, &block)
29
+ end
30
+ end
31
+
32
+ def instance(type, cmd, opts = { }, &block)
33
+ case type
34
+ when :basic
35
+ Antrapol::ToolRack::Logger.instance.glogger.debug "Basic execution"
36
+ basic_exec(cmd, &block)
37
+ when :system
38
+ Antrapol::ToolRack::Logger.instance.glogger.debug "System execution"
39
+ system_exec(cmd, opts, &block)
40
+ when :popen3
41
+ Antrapol::ToolRack::Logger.instance.glogger.debug "Popen3 execution"
42
+ popen3_exec(cmd, opts, &block)
43
+ else
44
+ Antrapol::ToolRack::Logger.instance.glogger.debug "Basic (fallback) execution"
45
+ # basic
46
+ basic_exec(cmd, &block)
47
+ end
48
+ end
49
+
50
+ private
13
51
  # backtick
14
52
  # backtick will only return at the end of the process.
15
53
  # If in the event there is a prompt to user, this should nto be used.
16
- def basic_exec(cmd)
54
+ def basic_exec(cmd, &block)
17
55
  Antrapol::ToolRack::Logger.instance.glogger.debug "Basic shell exec command : #{cmd}"
18
- `#{cmd}`
56
+ res = `#{cmd}`
57
+ if block
58
+ block.call($?, res)
59
+ else
60
+ res
61
+ end
19
62
  end # basic_shell_exec
20
63
 
21
64
  # system
@@ -46,6 +89,11 @@ module Antrapol
46
89
 
47
90
  Antrapol::ToolRack::Logger.instance.glogger.debug "PTY exec command : #{cmd}"
48
91
 
92
+ # pty seems error running on windows + jruby
93
+ if Antrapol::ToolRack::RuntimeUtils.on_windows?
94
+ raise Exception, "You're running on Windows. There have been report that error \"function 'openpty' not found in msvcrt.dll\". Probably due to patches. For now pty_exec() shall be halted"
95
+ end
96
+
49
97
  logger = opts[:logger] || Tlogger.new(STDOUT)
50
98
  expect = opts[:expect] || { }
51
99
  bufSize = opts[:intBufSize] || 1024000
@@ -4,21 +4,26 @@ module Antrapol
4
4
  module ToolRack
5
5
  module RuntimeUtils
6
6
 
7
- def detect_os
8
- case RbConfig::CONFIG['host_os']
9
- when /cygwin|mswin|mingw|bccwin|wince|emx/
10
- :win
11
- when /darwin|mac/
12
- :mac
13
- else
14
- :linux
15
- end
7
+ def RuntimeUtils.on_windows?
8
+ (RbConfig::CONFIG['host_os'] =~ /cygwin|mswin|mingw|bccwin|wince|emx/) != nil
16
9
  end
17
10
 
18
- def detect_ruby
19
-
11
+ def RuntimeUtils.on_mac?
12
+ (RbConfig::CONFIG['host_os'] =~ /darwin|mac/) != nil
20
13
  end
21
14
 
22
- end
23
- end
24
- end
15
+ def RuntimeUtils.on_linux?
16
+ (RbConfig::CONFIG['host_os'] =~ /linux/) != nil
17
+ end
18
+
19
+ def RuntimeUtils.on_ruby?
20
+ not on_jruby?
21
+ end
22
+
23
+ def RuntimeUtils.on_jruby?
24
+ (RUBY_PLATFORM =~ /java/) != nil
25
+ end
26
+
27
+ end # RuntimeUtils
28
+ end # ToolRack
29
+ end #Antrapol
@@ -0,0 +1,23 @@
1
+
2
+
3
+
4
+ module Antrapol
5
+ module Utils
6
+
7
+ def set_session_exception(exp)
8
+ if not exp.nil?
9
+ @sExp = exp
10
+ end
11
+ end
12
+
13
+ def session_exception
14
+ if not @sExp.nil?
15
+ @sExcp
16
+ else
17
+ Antrapol::ToolRack::Error
18
+ end
19
+ end
20
+ alias_method :session_exception, :s_exp
21
+
22
+ end
23
+ end
@@ -1,6 +1,6 @@
1
1
  module Antrapol
2
2
  module ToolRack
3
- VERSION = "0.3.0"
3
+ VERSION = "0.6.0"
4
4
  end
5
5
  end
6
6
 
@@ -0,0 +1,74 @@
1
+
2
+
3
+
4
+ require 'pty'
5
+ require 'expect'
6
+
7
+ #m, s = PTY.open
8
+ #r, w = IO.pipe
9
+ #
10
+ #pid = spawn("/usr/bin/openvpn --config /home/chris/.openvpn_cli/sg2-ovpn-tcp.ovpn", in: r, out: s)
11
+ #r.close
12
+ #s.close
13
+ #
14
+ #
15
+ #ret = begin
16
+ # p m.gets
17
+ # rescue Errno::EIO
18
+ # nil
19
+ # end
20
+ #
21
+
22
+
23
+ ##PTY.spawn("/usr/bin/sudo /usr/bin/openvpn --config /home/chris/.openvpn_cli/sg2-ovpn-tcp.ovpn") do |read, write, pid|
24
+ #PTY.spawn("/usr/bin/openvpn --config /home/chris/.openvpn_cli/sg2-ovpn-tcp.ovpn") do |read, write, pid|
25
+ # #read.expect(/\[sudo\] password for chris:/) { |m|
26
+ # # p m
27
+ # # puts "expect"
28
+ # # write.printf("chr1st0pher1120\n")
29
+ # #}
30
+ # #read.expect(/Username:/) { |msg|
31
+ # # puts "-- #{msg}"
32
+ # # write.printf("purevpn0s2643230\r\n")
33
+ # #}
34
+ #
35
+ # #read.expect(/no echo\)/) { |msg|
36
+ # # puts "++ #{msg}"
37
+ # # write.printf("\t@ntr@p0l.c0m\r\n")
38
+ # #}
39
+ #
40
+ # loop do
41
+ # read.expect(/\n/) { |l|
42
+ # p l
43
+ # @ln = l
44
+ # }
45
+ # break if @ln.nil?
46
+ # end
47
+ #end
48
+
49
+ read, write, pid = PTY.spawn("/usr/bin/openvpn --config /home/chris/.openvpn_cli/sg2-ovpn-tcp.ovpn")
50
+
51
+ puts "PID : #{pid}"
52
+
53
+ trap "SIGINT" do
54
+ read.close
55
+ write.close
56
+ begin
57
+ puts "Killing process"
58
+ Process.kill("HUP",pid)
59
+ puts "Kill done. Start waiting"
60
+ Process.wait(pid)
61
+ STDERR.puts "Cleanup Done!"
62
+ rescue PTY::ChildExited
63
+ end
64
+ end
65
+
66
+ loop do
67
+ read.expect(/\n/) do |l|
68
+ puts l
69
+ @ln = l
70
+ end
71
+ break if @ln.nil?
72
+ end
73
+
74
+
data/toolrack.gemspec CHANGED
@@ -27,4 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.require_paths = ["lib"]
28
28
 
29
29
  spec.add_dependency "tlogger", "~> 0.21"
30
+
31
+ spec.add_development_dependency "devops_helper", "~> 0.1.0"
30
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toolrack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-13 00:00:00.000000000 Z
11
+ date: 2021-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tlogger
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.21'
27
+ - !ruby/object:Gem::Dependency
28
+ name: devops_helper
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
27
41
  description: Just collections of utilities
28
42
  email:
29
43
  - chrisliaw@antrapol.com
@@ -42,8 +56,10 @@ files:
42
56
  - lib/toolrack.rb
43
57
  - lib/toolrack/condition_utils.rb
44
58
  - lib/toolrack/exception_utils.rb
59
+ - lib/toolrack/global.rb
45
60
  - lib/toolrack/process_utils.rb
46
61
  - lib/toolrack/runtime_utils.rb
62
+ - lib/toolrack/utils.rb
47
63
  - lib/toolrack/version.rb
48
64
  - process_test/backtick_test.rb
49
65
  - process_test/pty_test.rb
@@ -55,7 +71,7 @@ licenses: []
55
71
  metadata:
56
72
  homepage_uri: https://github.com/chrisliaw/toolrack
57
73
  source_code_uri: https://github.com/chrisliaw/toolrack
58
- post_install_message:
74
+ post_install_message:
59
75
  rdoc_options: []
60
76
  require_paths:
61
77
  - lib
@@ -70,8 +86,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
86
  - !ruby/object:Gem::Version
71
87
  version: '0'
72
88
  requirements: []
73
- rubygems_version: 3.1.4
74
- signing_key:
89
+ rubygems_version: 3.1.2
90
+ signing_key:
75
91
  specification_version: 4
76
92
  summary: Collection of simple utilities but I find it increase clarity
77
93
  test_files: []