tools 0.0.7 → 0.3.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.ruby-version +1 -1
- data/Gemfile +2 -1
- data/Rakefile +8 -2
- data/aux +481 -16
- data/aux2 +15 -2
- data/lib/files/requireds.rb +12 -2
- data/lib/lib/cache.rb +115 -26
- data/lib/lib/config.rb +48 -58
- data/lib/lib/display.rb +7 -5
- data/lib/lib/files.rb +15 -7
- data/lib/lib/log.rb +11 -3
- data/lib/lib/net.rb +123 -7
- data/lib/lib/prompt.rb +54 -0
- data/lib/lib/utils.rb +143 -18
- data/lib/tools.rb +1 -0
- data/lib/tools/version.rb +1 -1
- data/tools.gemspec +31 -18
- metadata +191 -103
data/lib/lib/net.rb
CHANGED
@@ -2,6 +2,21 @@ require 'singleton'
|
|
2
2
|
class ToolsNet
|
3
3
|
include Singleton
|
4
4
|
|
5
|
+
def self.ping? host
|
6
|
+
return Net::Ping::External.new(host || '0.0.0.1000', timeout=1).ping?
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.get_current_ip
|
10
|
+
# begin
|
11
|
+
# ip = Socket::getaddrinfo(Socket.gethostname, "echo", Socket::AF_INET)[0][3]
|
12
|
+
# return ip if IPAddress.valid?(ip)
|
13
|
+
# rescue Exception => e
|
14
|
+
# end
|
15
|
+
ip = `ifconfig | grep -E '10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk '{print $2}'`
|
16
|
+
ip = ip.split("\n").first if ip.include? "\n"
|
17
|
+
return ip if IPAddress.valid?(ip)
|
18
|
+
end
|
19
|
+
|
5
20
|
|
6
21
|
# Resolv a ip to a dns.
|
7
22
|
#
|
@@ -13,7 +28,12 @@ class ToolsNet
|
|
13
28
|
ret = Resolv.new.getname(ip)
|
14
29
|
return ret.instance_variable_get('@labels').join('.')
|
15
30
|
rescue Exception => e
|
16
|
-
|
31
|
+
case e.message
|
32
|
+
when "Dnsruby::NXDomain"
|
33
|
+
return nil
|
34
|
+
else
|
35
|
+
return e.message
|
36
|
+
end
|
17
37
|
end
|
18
38
|
return s.strip
|
19
39
|
end
|
@@ -30,9 +50,9 @@ class ToolsNet
|
|
30
50
|
rescue Exception => e
|
31
51
|
case e.message
|
32
52
|
when "Dnsruby::NXDomain"
|
33
|
-
|
53
|
+
return nil
|
34
54
|
else
|
35
|
-
|
55
|
+
return e.message
|
36
56
|
end
|
37
57
|
end
|
38
58
|
return ret
|
@@ -52,12 +72,11 @@ class ToolsNet
|
|
52
72
|
# @param restclient_obj Rest Object
|
53
73
|
# @param path path
|
54
74
|
# @param method method
|
55
|
-
# @param caller caller
|
56
75
|
# @param method_opts method opts
|
57
76
|
# @param validate_opts validate opts
|
58
77
|
# @param retry_opts retry opts
|
59
78
|
# @param show_progress default false
|
60
|
-
def self.doreq( restclient_obj, path, method,
|
79
|
+
def self.doreq( restclient_obj, path, method, method_opts: {},validate_opts: {}, retry_opts: {}, show_progress: false)
|
61
80
|
res = nil
|
62
81
|
code = nil
|
63
82
|
data = method_opts.fetch(:data, nil)
|
@@ -76,7 +95,6 @@ class ToolsNet
|
|
76
95
|
code = result.code.to_i
|
77
96
|
end
|
78
97
|
rescue Exception => error
|
79
|
-
ap error
|
80
98
|
flag_error = true
|
81
99
|
end
|
82
100
|
# Other conditionals to retry
|
@@ -102,7 +120,6 @@ class ToolsNet
|
|
102
120
|
end
|
103
121
|
end
|
104
122
|
|
105
|
-
|
106
123
|
# Return a valid decode response.
|
107
124
|
#
|
108
125
|
# @param response
|
@@ -138,4 +155,103 @@ class ToolsNet
|
|
138
155
|
end
|
139
156
|
|
140
157
|
|
158
|
+
# Validate.: port number between valid range?.
|
159
|
+
#
|
160
|
+
# @param port number to be validate.
|
161
|
+
# @return [Boolean]
|
162
|
+
def self.valid_port? port
|
163
|
+
if port.to_s.strip.match(/^\d{1,5}(?!\d)$/).nil?
|
164
|
+
return false
|
165
|
+
end
|
166
|
+
unless port.to_i.between?(1, 65535)
|
167
|
+
return false
|
168
|
+
end
|
169
|
+
return true
|
170
|
+
end
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
# Validate.: ip number or mask are valids?.
|
175
|
+
#
|
176
|
+
# @param that number or mask to be validate.
|
177
|
+
# @return [Hash] result info ip or ask or error
|
178
|
+
def self.validate_ipaddress that
|
179
|
+
result = {}
|
180
|
+
result[:status] = false
|
181
|
+
result[:type] = ''
|
182
|
+
result[:msg] = ''
|
183
|
+
result[:address] = that
|
184
|
+
result[:resolv] = ''
|
185
|
+
|
186
|
+
if IPAddress::valid_ipv4? that
|
187
|
+
begin
|
188
|
+
ip = IPAddress::valid_ipv4? that
|
189
|
+
result[:status] = true
|
190
|
+
result[:type] = 'ip'
|
191
|
+
result[:oct1] = that.split('.')[0]
|
192
|
+
result[:oct2] = that.split('.')[1]
|
193
|
+
result[:oct3] = that.split('.')[2]
|
194
|
+
result[:oct4] = that.split('.')[3]
|
195
|
+
rescue Exception => e
|
196
|
+
result[:msg] = e.to_s
|
197
|
+
return
|
198
|
+
end
|
199
|
+
else
|
200
|
+
begin
|
201
|
+
net = NetAddr::CIDR.create that
|
202
|
+
result[:type] = 'mask'
|
203
|
+
result[:status] = true
|
204
|
+
rescue Exception => e
|
205
|
+
ip = resolv_dns that
|
206
|
+
if IPAddress::valid_ipv4? ip
|
207
|
+
result[:status] = true
|
208
|
+
result[:type] = 'ip'
|
209
|
+
result[:oct1] = ip.split('.')[0]
|
210
|
+
result[:oct2] = ip.split('.')[1]
|
211
|
+
result[:oct3] = ip.split('.')[2]
|
212
|
+
result[:oct4] = ip.split('.')[3]
|
213
|
+
else
|
214
|
+
result[:msg] = e.to_s
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
return result
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
# Validate.: ip number is a backend?.
|
223
|
+
#
|
224
|
+
# @param original_addr number to be validate.
|
225
|
+
# @return [Boolean]
|
226
|
+
def self.is_backend? original_addr
|
227
|
+
glbbackend = NetAddr::CIDR.create('10.0.0.0/8')
|
228
|
+
return glbbackend.contains? original_addr
|
229
|
+
end
|
230
|
+
|
231
|
+
def self.valid_ip? original_addr
|
232
|
+
status = false
|
233
|
+
begin
|
234
|
+
status = IPAddress.valid?(original_addr)
|
235
|
+
rescue
|
236
|
+
end
|
237
|
+
return status
|
238
|
+
end
|
239
|
+
|
240
|
+
# Validate.: ip number is avalid network.
|
241
|
+
#
|
242
|
+
# @param original_addr network number to be validate.
|
243
|
+
# @return [Boolean]
|
244
|
+
def self.valid_network? original_addr
|
245
|
+
status = false
|
246
|
+
begin
|
247
|
+
ip = IPAddress original_addr
|
248
|
+
status = true if ip.network?
|
249
|
+
rescue
|
250
|
+
end
|
251
|
+
return status
|
252
|
+
end
|
253
|
+
|
254
|
+
|
255
|
+
|
256
|
+
|
141
257
|
end
|
data/lib/lib/prompt.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
class ToolsPrompt
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.yes? *args
|
9
|
+
prompt = TTY::Prompt.new
|
10
|
+
prompt.yes? *args
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.no? *args
|
14
|
+
prompt = TTY::Prompt.new
|
15
|
+
prompt.no? *args
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.ask *args
|
19
|
+
prompt = TTY::Prompt.new
|
20
|
+
result = prompt.ask *args
|
21
|
+
return result
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.mask *args
|
25
|
+
prompt = TTY::Prompt.new
|
26
|
+
result = prompt.mask *args
|
27
|
+
return result
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.select *args
|
31
|
+
prompt = TTY::Prompt.new
|
32
|
+
result = prompt.select *args
|
33
|
+
return result
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.multi_select *args
|
37
|
+
prompt = TTY::Prompt.new
|
38
|
+
result = prompt.multi_select *args
|
39
|
+
return result
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.enum_select *args
|
43
|
+
prompt = TTY::Prompt.new
|
44
|
+
result = prompt.multi_select *args
|
45
|
+
return result
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.expand *args
|
49
|
+
prompt = TTY::Prompt.new
|
50
|
+
result = prompt.expand *args
|
51
|
+
return result
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/lib/lib/utils.rb
CHANGED
@@ -9,13 +9,50 @@ class ToolsUtil
|
|
9
9
|
ToolsFiles.instance
|
10
10
|
ToolsConfig.instance
|
11
11
|
ToolsLog.instance
|
12
|
+
ToolsPrompt.instance
|
13
|
+
unless File.exists? Tools.home+'/.tools'
|
14
|
+
FileUtils.mkdir_p(Tools.home+'/.tools')
|
15
|
+
end
|
12
16
|
tools_logfile = Tools.home+'/.tools/tools.log'
|
13
17
|
ToolsLog.create_log_file 'tools', tools_logfile
|
14
18
|
end
|
15
19
|
|
20
|
+
# Synbolize all keys in hash.
|
21
|
+
#
|
22
|
+
# @param hash
|
23
|
+
# return hash symbolized
|
24
|
+
def self.symbolize_keys(hash)
|
25
|
+
hash.inject({}){|result, (key, value)|
|
26
|
+
new_key = case key
|
27
|
+
when String then key.to_sym
|
28
|
+
else key
|
29
|
+
end
|
30
|
+
new_value = case value
|
31
|
+
when Hash then symbolize_keys(value)
|
32
|
+
else value
|
33
|
+
end
|
34
|
+
result[new_key] = new_value
|
35
|
+
result
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
# Capture string from a prompt.
|
41
|
+
#
|
42
|
+
# @param prompt
|
43
|
+
# @param hidden option
|
44
|
+
# @return String
|
45
|
+
def self.ask_prompt(prompt = '', hidden = true)
|
46
|
+
if hidden
|
47
|
+
a = ask("#{prompt}") { |q| q.echo = "*"; } #q.case = :downcase }
|
48
|
+
else
|
49
|
+
a = ask("#{prompt}") #{ |q| q.case = :downcase }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
16
53
|
# Test a valid json string.
|
17
54
|
#
|
18
|
-
# @param Json string to be tested
|
55
|
+
# @param source Json string to be tested
|
19
56
|
# @return boolean
|
20
57
|
def self.valid_json? source
|
21
58
|
begin
|
@@ -27,7 +64,7 @@ class ToolsUtil
|
|
27
64
|
|
28
65
|
# Test a valid yaml string.
|
29
66
|
#
|
30
|
-
# @param Yaml string to be tested
|
67
|
+
# @param source Yaml string to be tested
|
31
68
|
# @return boolean
|
32
69
|
def self.valid_yaml? source
|
33
70
|
begin
|
@@ -93,12 +130,17 @@ class ToolsUtil
|
|
93
130
|
# @param variable variable name to retrive
|
94
131
|
# @return boolean
|
95
132
|
def self.instance_variable? variable
|
96
|
-
ap self.instance_variables
|
97
|
-
exit
|
98
|
-
return self.instance_variable_get("
|
133
|
+
# ap self.instance_variables.include? variable.to_sym
|
134
|
+
# exit
|
135
|
+
return self.instance_variable_get(":@#{variable}")
|
99
136
|
end
|
100
137
|
|
101
|
-
|
138
|
+
# Get all existent class variablea.
|
139
|
+
#
|
140
|
+
# @return variables
|
141
|
+
def self.get_variables
|
142
|
+
return self.instance_variables
|
143
|
+
end
|
102
144
|
|
103
145
|
# Return a plain text for content of String or Hash or Array.
|
104
146
|
#
|
@@ -199,6 +241,36 @@ class Hash
|
|
199
241
|
end
|
200
242
|
|
201
243
|
|
244
|
+
class Hash
|
245
|
+
|
246
|
+
# ensures nested hash from keys, and sets final key to value
|
247
|
+
# keys: Array of Symbol|String
|
248
|
+
# value: any
|
249
|
+
def nested_set(keys, value)
|
250
|
+
raise "DEBUG: nested_set keys must be an Array" unless keys.is_a?(Array)
|
251
|
+
|
252
|
+
final_key = keys.pop
|
253
|
+
return unless valid_key?(final_key)
|
254
|
+
position = self
|
255
|
+
for key in keys
|
256
|
+
return unless valid_key?(key)
|
257
|
+
position[key] = {} unless position[key].is_a?(Hash)
|
258
|
+
position = position[key]
|
259
|
+
end
|
260
|
+
position[final_key] = value
|
261
|
+
end
|
262
|
+
|
263
|
+
private
|
264
|
+
|
265
|
+
# returns true if key is valid
|
266
|
+
def valid_key?(key)
|
267
|
+
return true if key.is_a?(Symbol) || key.is_a?(String)
|
268
|
+
raise "DEBUG: nested_set invalid key: #{key} (#{key.class})"
|
269
|
+
end
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
|
202
274
|
class String
|
203
275
|
|
204
276
|
# Self test nil String class.
|
@@ -250,6 +322,20 @@ class String
|
|
250
322
|
Encrypt.load self, key
|
251
323
|
end
|
252
324
|
|
325
|
+
# Self test numeric String class.
|
326
|
+
#
|
327
|
+
# @return boolean
|
328
|
+
def numeric?
|
329
|
+
Float(aux) != nil rescue false
|
330
|
+
end
|
331
|
+
|
332
|
+
# Self test digits String class.
|
333
|
+
#
|
334
|
+
# @return boolean
|
335
|
+
def num?
|
336
|
+
!!match(/^[[:digit:]]+$/)
|
337
|
+
end
|
338
|
+
|
253
339
|
# Self test alphanum String class.
|
254
340
|
#
|
255
341
|
# @return boolean
|
@@ -264,6 +350,17 @@ class String
|
|
264
350
|
!!match(/^[[:alpha:]]+$/)
|
265
351
|
end
|
266
352
|
|
353
|
+
def help?
|
354
|
+
if self.eql? '?' or
|
355
|
+
self.eql? '-h' or
|
356
|
+
self.eql? '--help' or
|
357
|
+
self.eql? 'help'
|
358
|
+
return true
|
359
|
+
else
|
360
|
+
return false
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
267
364
|
end
|
268
365
|
|
269
366
|
|
@@ -300,6 +397,14 @@ class Object
|
|
300
397
|
def string?
|
301
398
|
self.is_a?(String)
|
302
399
|
end
|
400
|
+
|
401
|
+
# Self test nil Object class.
|
402
|
+
#
|
403
|
+
# @return boolean
|
404
|
+
# def nil?
|
405
|
+
# return '' if self == nil
|
406
|
+
# end
|
407
|
+
|
303
408
|
end
|
304
409
|
|
305
410
|
class Array
|
@@ -396,20 +501,40 @@ class Array
|
|
396
501
|
return result
|
397
502
|
end
|
398
503
|
|
399
|
-
def extract_option_value option
|
400
|
-
result = false
|
401
|
-
value = nil
|
402
|
-
if self.include? option
|
403
|
-
index = self.index(option)
|
404
|
-
self.delete_at(index)
|
405
|
-
result = true
|
406
|
-
value = self.at(index)
|
407
|
-
self.delete_at(index)
|
408
|
-
end
|
409
|
-
return [result, value]
|
410
|
-
end
|
504
|
+
def extract_option_value option, args={}
|
411
505
|
|
506
|
+
if args[:multiple].nil?
|
507
|
+
args[:multiple] = false
|
508
|
+
end
|
412
509
|
|
510
|
+
if args[:separator].nil?
|
511
|
+
args[:separator] = '='
|
512
|
+
end
|
413
513
|
|
514
|
+
result = false
|
515
|
+
if args[:multiple]
|
516
|
+
multiple_value = []
|
517
|
+
while self.include? option
|
518
|
+
index = self.index(option)
|
519
|
+
self.delete_at(index)
|
520
|
+
result = true
|
521
|
+
value = self.at(index)
|
522
|
+
multiple_value << self.at(index).split(args[:separator]).first
|
523
|
+
multiple_value << self.at(index).split(args[:separator]).last
|
524
|
+
self.delete_at(index)
|
525
|
+
end
|
526
|
+
return [result, multiple_value]
|
527
|
+
else
|
528
|
+
value = nil
|
529
|
+
while self.include? option
|
530
|
+
index = self.index(option)
|
531
|
+
self.delete_at(index)
|
532
|
+
result = true
|
533
|
+
value = self.at(index)
|
534
|
+
self.delete_at(index)
|
535
|
+
end
|
536
|
+
return [result, value]
|
537
|
+
end
|
538
|
+
end
|
414
539
|
|
415
540
|
end
|