tools 0.0.1 → 0.0.5
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 +5 -5
- data/.gitignore +3 -0
- data/.ruby-gemset +1 -1
- data/.ruby-version +1 -1
- data/Rakefile +45 -1
- data/TODO.txt +357 -0
- data/aux +111 -0
- data/bin/tools +66 -0
- data/lib/files/pt-BR.yml +207 -0
- data/lib/files/requireds.rb +28 -0
- data/lib/lib/cache.rb +48 -0
- data/lib/lib/config.rb +227 -0
- data/lib/lib/console.rb +147 -0
- data/lib/lib/display.rb +41 -0
- data/lib/lib/files.rb +51 -0
- data/lib/lib/log.rb +66 -0
- data/lib/lib/net.rb +141 -0
- data/lib/lib/utils.rb +364 -0
- data/lib/tools.rb +85 -1
- data/lib/tools/version.rb +2 -1
- data/test/minitest/minit-display.rb +29 -0
- data/test/minitest/minit-tools.rb +67 -0
- data/test/minitest/run +9 -0
- data/tools.gemspec +25 -3
- metadata +362 -10
data/lib/lib/console.rb
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
class ToolsConsole
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
|
6
|
+
def self.create_console
|
7
|
+
|
8
|
+
extend Prompt::DSL
|
9
|
+
|
10
|
+
group "Console commands"
|
11
|
+
|
12
|
+
desc "configure"
|
13
|
+
param :config_type , "Config type search for roots locations...", %(location show)
|
14
|
+
command "config :config_type" do |config_type|
|
15
|
+
|
16
|
+
Prompt.application.prompt = "#{Tools.configuration.console_prompt} console > ".light_blue
|
17
|
+
puts "Choise yuor config action .:".yellow
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
desc "history"
|
22
|
+
command "history" do
|
23
|
+
File.open(@history_file, 'r') do |f|
|
24
|
+
while line = f.gets
|
25
|
+
puts line
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "test"
|
31
|
+
command "test" do
|
32
|
+
puts 'Im a test.!'.yellow
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.my_method_missing
|
38
|
+
ap "my_method_missing"
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.run_console
|
42
|
+
Prompt.application.prompt = "#{Tools.configuration.console_prompt} console > ".light_green
|
43
|
+
@history_file = File.join(File.expand_path(File.dirname(__FILE__)).to_s, ".workin-history")
|
44
|
+
Prompt::Console.start @history_file
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
# class Console
|
57
|
+
|
58
|
+
# def initialize(options = {})
|
59
|
+
# commands
|
60
|
+
# end
|
61
|
+
|
62
|
+
# def commands
|
63
|
+
|
64
|
+
# extend Prompt::DSL
|
65
|
+
|
66
|
+
# group "Commands"
|
67
|
+
|
68
|
+
# desc "config"
|
69
|
+
# command "config" do ||
|
70
|
+
# Prompt.application.prompt = " tools > ".light_blue
|
71
|
+
# end
|
72
|
+
|
73
|
+
# desc "show"
|
74
|
+
# command "show :param" do |param|
|
75
|
+
# case param
|
76
|
+
# when 'config'
|
77
|
+
# puts "ROOT .: #{Tools.root}"
|
78
|
+
# puts "USER .: #{Tools.configuration.user}"
|
79
|
+
# puts "HOME .: #{Tools.configuration.home}"
|
80
|
+
# puts "PWD .: #{Tools.configuration.pwd}"
|
81
|
+
# puts "ldap_user .: #{Tools.configuration.ldap_user}"
|
82
|
+
# puts "ldap_pass .: #{Tools.configuration.ldap_pass}"
|
83
|
+
# Tools.configuration.info.each do |k,v|
|
84
|
+
# ap k
|
85
|
+
# ap v
|
86
|
+
# end
|
87
|
+
# else
|
88
|
+
# ap Tools.get_variable param
|
89
|
+
# end
|
90
|
+
# end
|
91
|
+
|
92
|
+
# desc "rsync"
|
93
|
+
# command "rsync :from :to" do |from, to|
|
94
|
+
|
95
|
+
# ap Tools.configuration.info[:directorys_to][from].nil?
|
96
|
+
|
97
|
+
# unless Tools.configuration.info[:directorys_to][from].nil?
|
98
|
+
# from = Tools.configuration.info[:directorys_to][from]
|
99
|
+
# from += ask("?? ")
|
100
|
+
# else
|
101
|
+
# sourcefiles = File.join("**", from)
|
102
|
+
# Dir.glob(sourcefiles).each do |source|
|
103
|
+
# ap source
|
104
|
+
# end
|
105
|
+
# end
|
106
|
+
|
107
|
+
# ap from
|
108
|
+
|
109
|
+
# # if File.file?(source)
|
110
|
+
# # result = Rsync.run(source, dest)
|
111
|
+
# # ap result
|
112
|
+
# # end
|
113
|
+
|
114
|
+
# end
|
115
|
+
|
116
|
+
# desc "connect"
|
117
|
+
# command "connect :host" do |host|
|
118
|
+
# if host.split('@').size == 2
|
119
|
+
# user = host.split('@')[0]
|
120
|
+
# host = host.split('@')[1]
|
121
|
+
# else
|
122
|
+
# unless Tools.configuration.info[:servers][host].nil?
|
123
|
+
# user = Tools.configuration.info[:servers][host].split('@')[0]
|
124
|
+
# host = Tools.configuration.info[:servers][host].split('@')[1]
|
125
|
+
# else
|
126
|
+
# ap "No hosts selected! Exiting..."
|
127
|
+
# end
|
128
|
+
# end
|
129
|
+
# ssh = Tools.ssh_connect_knowhost host, user
|
130
|
+
# Tools.set_variable 'ssh', ssh
|
131
|
+
# end
|
132
|
+
|
133
|
+
|
134
|
+
# desc "cmd"
|
135
|
+
# command "cmd :variable :command " do |variable, command|
|
136
|
+
# ssh = Tools.get_variable 'ssh'
|
137
|
+
# Tools.set_variable variable, (Tools.ssh_cmd ssh, command).split("\n")
|
138
|
+
# end
|
139
|
+
|
140
|
+
|
141
|
+
# Prompt.application.prompt = " tools > ".light_red
|
142
|
+
# history_file = ".tools-history"
|
143
|
+
# Prompt::Console.start history_file
|
144
|
+
|
145
|
+
# end
|
146
|
+
|
147
|
+
# end
|
data/lib/lib/display.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
class ToolsDisplay
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
# Tools to awesome prints
|
6
|
+
#
|
7
|
+
# ToolsDisplay.show "teste"
|
8
|
+
# ToolsDisplay.show "TEXT SAMELINE sem cor ", :sameline
|
9
|
+
# ToolsDisplay.show "TEXT GREEN", :green
|
10
|
+
# ToolsDisplay.show "TEXT YELLOW", :yellow
|
11
|
+
#
|
12
|
+
# @param arguments
|
13
|
+
# @return [String] printed
|
14
|
+
def self.show *arguments
|
15
|
+
post = arguments[0]
|
16
|
+
unless (post.class == String)
|
17
|
+
return post.class.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
color = arguments.extract_color
|
21
|
+
sameline = arguments.extract_symbol :sameline
|
22
|
+
nocolor = arguments.extract_symbol :nocolor
|
23
|
+
colorized = arguments.extract_symbol :colorized
|
24
|
+
|
25
|
+
unless sameline
|
26
|
+
post += "\n"
|
27
|
+
end
|
28
|
+
unless nocolor
|
29
|
+
printf "#{post}".colorize(color)
|
30
|
+
else
|
31
|
+
if colorized
|
32
|
+
ap post
|
33
|
+
else
|
34
|
+
printf "#{post}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
end
|
data/lib/lib/files.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
class ToolsFiles
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
# Create a directory in work area
|
10
|
+
#
|
11
|
+
# Sample
|
12
|
+
# ToolsFiles.create_dir Tools.home + '/2018/xykotools/tools/home', 'tools_home'
|
13
|
+
# home = (ToolsUtil.get_variable 'tools_home') => ~/2018/xykotools/tools/home
|
14
|
+
#
|
15
|
+
# @param directory
|
16
|
+
# @param directory_name
|
17
|
+
# @return
|
18
|
+
def self.create_dir directory, directory_name
|
19
|
+
unless directory.end_with? '/'
|
20
|
+
directory += '/'
|
21
|
+
end
|
22
|
+
complete_file = (directory + '/').gsub('//','/')
|
23
|
+
unless File.exists? complete_file
|
24
|
+
Dir.mkdir(complete_file)
|
25
|
+
end
|
26
|
+
ToolsUtil.set_variable directory_name, complete_file
|
27
|
+
end
|
28
|
+
|
29
|
+
# Create a file in work area
|
30
|
+
#
|
31
|
+
# Sample
|
32
|
+
#
|
33
|
+
# ToolsFiles.create_file home, 'xyko_file.txt', 'xyko_file'
|
34
|
+
# xyko = (ToolsUtil.get_variable 'xyko_file') => ~/2018/xykotools/tools/home/xyko_file.txt
|
35
|
+
#
|
36
|
+
# @param directory
|
37
|
+
# @param file_name
|
38
|
+
# @param file_name_set
|
39
|
+
# @return
|
40
|
+
def self.create_file directory, file_name, file_name_set
|
41
|
+
complete_file = (directory + '/' + file_name).gsub('//','/')
|
42
|
+
unless File.exists? complete_file
|
43
|
+
file = File.open( complete_file , 'w')
|
44
|
+
end
|
45
|
+
ToolsUtil.set_variable file_name_set, complete_file
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
end
|
data/lib/lib/log.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
class ToolsLog
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
# Create a Log file in work area
|
11
|
+
#
|
12
|
+
# Sample
|
13
|
+
#
|
14
|
+
# ToolsLog.create_log_file 'sample', '/myhome/tools/sample.log'
|
15
|
+
# log = ToolsUtil.get_variable "sample_logger" => Logger Object
|
16
|
+
#
|
17
|
+
# ToolsLog.create_log_file 'xyko', '/home/francisco/2018/xykotools/tools/xyko.log'
|
18
|
+
# ToolsLog.xyko_info 'teste do methodo1', :light_yellow
|
19
|
+
# ToolsLog.xyko_warn 'teste do methodo2'
|
20
|
+
# ToolsLog.xyko_error 'teste do methodo3'
|
21
|
+
# ToolsLog.xyko_debug 'teste do methodo4'
|
22
|
+
#
|
23
|
+
# @param logname
|
24
|
+
# @param logfile
|
25
|
+
# @return
|
26
|
+
def self.create_log_file log_name, log_file
|
27
|
+
if File.exists? log_file
|
28
|
+
unless File.ctime(log_file).to_date == Time.now.to_date
|
29
|
+
File.delete(log_file)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
ToolsUtil.set_variable "#{log_name}_log_file", log_file
|
33
|
+
ToolsUtil.set_variable "#{log_name}_logger", Logger.new(log_file, 'daily')
|
34
|
+
(ToolsUtil.get_variable "#{log_name}_logger").formatter = proc do |severity, datetime, progname, msg|
|
35
|
+
"#{severity.chars.first} #{datetime.strftime "%H:%M:%S"}: #{msg}\n"
|
36
|
+
end
|
37
|
+
#ToolsUtil.purge_files Tools.home+'/.tools/backup', '*', 14*24*60*60
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.method_missing(method, *args, &block)
|
41
|
+
#expected call format => STRING_LOGGER_TYPE + '_' + LOGGER_TYPE
|
42
|
+
# Ex.: tools_info
|
43
|
+
logger_name = method.to_s.split('_').first
|
44
|
+
logger_method = method.to_s.split('_').last
|
45
|
+
logger = ToolsUtil.get_variable "#{logger_name}_logger"
|
46
|
+
color = args.extract_color
|
47
|
+
if color.eql? :default
|
48
|
+
case logger_method
|
49
|
+
when 'info'
|
50
|
+
color = :cyan
|
51
|
+
when 'warn'
|
52
|
+
color = :yellow
|
53
|
+
when 'debug'
|
54
|
+
color = :green
|
55
|
+
when 'error'
|
56
|
+
color = :light_red
|
57
|
+
when 'exit'
|
58
|
+
log_file = ToolsUtil.get_variable "#{logger_name}_log_file"
|
59
|
+
ToolsDisplay.show "\tError in ToolsUtil. See details in '#{log_file}'", :light_yellow
|
60
|
+
exit
|
61
|
+
end
|
62
|
+
end
|
63
|
+
logger.method(logger_method).call args.first.colorize(color)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/lib/lib/net.rb
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
class ToolsNet
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
|
6
|
+
# Resolv a ip to a dns.
|
7
|
+
#
|
8
|
+
# @param ip variable ip to resolv
|
9
|
+
# @return [String] Dns name resolved
|
10
|
+
def self.resolv_ip_name ip
|
11
|
+
s = ''
|
12
|
+
begin
|
13
|
+
ret = Resolv.new.getname(ip)
|
14
|
+
return ret.instance_variable_get('@labels').join('.')
|
15
|
+
rescue Exception => e
|
16
|
+
s = 'edi error: ' + e.message
|
17
|
+
end
|
18
|
+
return s.strip
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
# Resolv a dns to a ip.
|
23
|
+
#
|
24
|
+
# @param domain variable ip to resolv
|
25
|
+
# @return [String] Dns address resolved
|
26
|
+
def self.resolv_dns domain
|
27
|
+
begin
|
28
|
+
dns = Dnsruby::DNS.new()
|
29
|
+
ret = dns.getaddress(domain).to_s
|
30
|
+
rescue Exception => e
|
31
|
+
case e.message
|
32
|
+
when "Dnsruby::NXDomain"
|
33
|
+
ret = nil
|
34
|
+
else
|
35
|
+
ret = e.message
|
36
|
+
end
|
37
|
+
end
|
38
|
+
return ret
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
# Do the request, validate and decode response and do the retries if needed
|
43
|
+
#
|
44
|
+
# - restclient_obj: initialized RestClient object
|
45
|
+
# - path: URL path to request
|
46
|
+
# - method: symbol for HTTP Method to request
|
47
|
+
# - method_opts: options as passed to a RestClient call
|
48
|
+
# - validate_opts: options for response validation as used with
|
49
|
+
# validate_decode_response()
|
50
|
+
# - retry_opts: when_res: [nil, [], {}, 'str'], when_code: [404, 500],
|
51
|
+
# conditionals to retry even if no exceptions were catched
|
52
|
+
# @param restclient_obj Rest Object
|
53
|
+
# @param path path
|
54
|
+
# @param method method
|
55
|
+
# @param caller caller
|
56
|
+
# @param method_opts method opts
|
57
|
+
# @param validate_opts validate opts
|
58
|
+
# @param retry_opts retry opts
|
59
|
+
# @param show_progress default false
|
60
|
+
def self.doreq( restclient_obj, path, method, caller, method_opts: {},validate_opts: {}, retry_opts: {}, show_progress: false)
|
61
|
+
res = nil
|
62
|
+
code = nil
|
63
|
+
data = method_opts.fetch(:data, nil)
|
64
|
+
method_opts.delete(:data)
|
65
|
+
|
66
|
+
# Retry loop due to:
|
67
|
+
# - 404 from Router API right after applying patch over a target
|
68
|
+
# - Intermittent connection reset from Manager API (HTTPS)
|
69
|
+
retries = retry_opts.fetch(:attempts, 10)
|
70
|
+
1.upto(retries) do |try|
|
71
|
+
flag_error = false
|
72
|
+
# The request
|
73
|
+
begin
|
74
|
+
restclient_obj[path].send(method, *data, **method_opts) do |response, request, result|
|
75
|
+
res = validate_decode_response(response, request, result, validate_opts)
|
76
|
+
code = result.code.to_i
|
77
|
+
end
|
78
|
+
rescue Exception => error
|
79
|
+
ap error
|
80
|
+
flag_error = true
|
81
|
+
end
|
82
|
+
# Other conditionals to retry
|
83
|
+
unless retry_opts.empty?
|
84
|
+
if retry_opts.has_key?(:when_res)
|
85
|
+
flag_error = true if retry_opts[:when_res].include?(res)
|
86
|
+
end
|
87
|
+
if retry_opts.has_key?(:when_code)
|
88
|
+
flag_error = true if retry_opts[:when_code].include?(code)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
return res unless flag_error # got response, break the loop
|
92
|
+
doReqMSG = sprintf "%d/%d", try, retries
|
93
|
+
if show_progress
|
94
|
+
ap doReqMSG
|
95
|
+
end
|
96
|
+
if try >= retries
|
97
|
+
ap doreq_code
|
98
|
+
return nil
|
99
|
+
else
|
100
|
+
sleep 1 # wait before next
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
# Return a valid decode response.
|
107
|
+
#
|
108
|
+
# @param response
|
109
|
+
# @param request
|
110
|
+
# @param result
|
111
|
+
# @param validate_opts
|
112
|
+
# @return [Object type] Array or Proc or String or Error
|
113
|
+
def self.validate_decode_response response, request, result, validate_opts
|
114
|
+
if validate_opts.has_key? :expected
|
115
|
+
expected = validate_opts[:expected]
|
116
|
+
case expected.class.name
|
117
|
+
when 'Array'
|
118
|
+
if validate_opts[:expected].include? response
|
119
|
+
return true
|
120
|
+
else
|
121
|
+
return false
|
122
|
+
end
|
123
|
+
when 'Proc'
|
124
|
+
response = expected.call(response, request, result)
|
125
|
+
return response
|
126
|
+
when 'String'
|
127
|
+
return result if validate_opts[:expected].eql? 'result'
|
128
|
+
return request if validate_opts[:expected].eql? 'request'
|
129
|
+
return response if validate_opts[:expected].eql? 'response'
|
130
|
+
else
|
131
|
+
ap expected.class.name
|
132
|
+
return response
|
133
|
+
end
|
134
|
+
else
|
135
|
+
return response
|
136
|
+
end
|
137
|
+
return true
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
end
|