servicesnapshot 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8ff1d8a4e278cdeb225dde49082679043898486f
4
+ data.tar.gz: 731652a097ababe477e5d2462c00e56656813ed2
5
+ SHA512:
6
+ metadata.gz: d50205d5695f7a9245494378bbe44fbbb1151606cd848a1e360c9d3dadd6fac5adaeb70d1993e5ac4691094e7eaed6f07ce7f4df39f2000fbf5932a0184e6dc3
7
+ data.tar.gz: e3d1fddf8039c1c2e1a9843c19d714e100a7fdc63937425a5ac323df675d626b13fcc05bae44514fb0da58a5c4db4d4e0552fcaacbcbe5c751fcc9f2ee18ea8b
data/bin/servicesnapshot CHANGED
@@ -1,53 +1,54 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- #Add the currently running directory to the start of the load path
3
+ # Add the currently running directory to the start of the load path
4
4
  $:.unshift './'
5
5
 
6
- require "helper_functions"
7
- require "Snapshot/Ssh"
8
- require "Snapshot/Beanstalk"
9
- require "Snapshot/Shell"
10
- require "Snapshot/FluidDb"
6
+ require 'helper_functions'
7
+ require 'snapshot/Ssh'
8
+ require 'snapshot/Beanstalk'
9
+ require 'snapshot/Shell'
10
+ require 'snapshot/FluidDb'
11
11
 
12
- #Don't buffer stdout
12
+ # Don't buffer stdout
13
13
  $stdout.sync = true
14
14
 
15
- if ARGV.length != 1 then
16
- puts "Available dsl's"
17
- puts "==============="
18
- Dir.glob("./*.dsl").each do |dslFileName|
19
- puts File.basename(dslFileName)
20
- end
21
- Dir.glob("#{ENV['HOME']}/servicesnapshot/*.dsl").each do |dslFileName|
22
- puts File.basename(dslFileName)
23
- end
24
- puts "==============="
25
-
26
- abort( "Usage: servicesnapshot <path to dsl>" ) if ARGV.length != 1
15
+ if ARGV.length != 1
16
+ puts 'Available dsl''s'
17
+ puts '==============='
18
+ puts '=== ./'
19
+ Dir.glob('./*.dsl').each do |dsl_file_name|
20
+ puts File.basename(dsl_file_name)
21
+ end
22
+ puts "=== #{ENV['HOME']}/servicesnapshot/"
23
+ Dir.glob("#{ENV['HOME']}/servicesnapshot/*.dsl").each do |dsl_file_name|
24
+ puts File.basename(dsl_file_name)
25
+ end
26
+ puts '==============='
27
+
28
+ abort('Usage: servicesnapshot <path to dsl>') if ARGV.length != 1
27
29
  end
28
30
 
29
- dslName = getFileName(ARGV[0])
31
+ dsl_name = get_file_name(ARGV[0])
30
32
 
31
- #Need to remove file name extension
32
- ENV["APP_NAME"] = File.basename( dslName ) if ENV["APP_NAME"].nil?
33
+ # Need to remove file name extension
34
+ ENV['APP_NAME'] = File.basename(dsl_name) if ENV['APP_NAME'].nil?
33
35
 
34
- log "Loading dsl, #{dslName}", true
36
+ log "Loading dsl, #{dsl_name}", true
35
37
  begin
36
- load dslName
37
-
38
- rescue ArgumentError=>e
39
- puts "*** Your dsl is not formatted correctly"
40
- puts "*** Ensure each line has the format,"
41
- puts "*** <command>, [:arg=>value]"
42
- rescue ParameterMissingError=>e
43
- puts e.message
44
-
45
- rescue SystemExit=>e
46
- # rescue SIGTERM=>e
47
- rescue Exception=>e
48
- puts "What the ..."
49
- puts e.class.name
50
- puts e.message
51
- puts e.backtrace
38
+ load dsl_name
39
+ rescue ArgumentError
40
+ puts '*** Your dsl is not formatted correctly'
41
+ puts '*** Ensure each line has the format,'
42
+ puts '*** <command>, [:arg=>value]'
43
+ rescue ParameterMissingError => e
44
+ puts e.message
45
+
46
+ rescue SystemExit
47
+ puts 'SystemExit ...'
48
+
49
+ rescue StandardError => e
50
+ puts 'What the ...'
51
+ puts e.class.name
52
+ puts e.message
53
+ puts e.backtrace
52
54
  end
53
-
@@ -1,74 +1,70 @@
1
- require "net/ssh/gateway"
1
+ require 'net/ssh/gateway'
2
2
 
3
- class ParameterMissingError<StandardError
3
+ class ParameterMissingError < StandardError
4
4
  end
5
5
 
6
- def get_param( params, name, usage )
7
- return params[name] unless params[name].nil?
8
- return ENV[name.to_s] unless ENV[name.to_s].nil?
6
+ def get_param(params, name, usage)
7
+ return params[name] unless params[name].nil?
8
+ return ENV[name.to_s] unless ENV[name.to_s].nil?
9
9
 
10
+ msg = "*** Could not find parameter, #{name}, for command, " \
11
+ "#{caller[0][/`.*'/][1..-2]}\n" \
12
+ "*** #{usage}\n" \
13
+ "*** Try :#{name}=>'YourValue'"
10
14
 
11
- msg = %{*** Could not find parameter, #{name.to_s}, for command, #{caller[0][/`.*'/][1..-2]}
12
- *** #{usage}
13
- *** Try :#{name.to_s}=>'YourValue'
14
- }
15
-
16
- raise ParameterMissingError.new( msg )
15
+ fail ParameterMissingError, msg
17
16
  end
18
17
 
19
- def env( *args )
20
- raise "Must have an even number of argument to env" if args.length % 2 != 0
21
-
22
- (0..args.length-1).step(2) do |i|
23
- ENV[args[i]] = args[i+1]
24
- end
18
+ def env(*args)
19
+ fail 'Must have an even number of argument to env' if args.length.odd?
20
+
21
+ (0..args.length - 1).step(2) do |i|
22
+ ENV[args[i]] = args[i + 1]
23
+ end
25
24
  end
26
25
 
27
- def open_gateway( user, host )
28
- log "Opening SSH Gateway to, #{host}", true
29
- gateway = Net::SSH::Gateway.new(host, user)
30
- localPort = 29000
31
- opened = false
32
- while opened==false
33
- localPort = localPort + 1
34
- begin
35
- gateway.open('127.0.0.1', 11300, localPort)
36
- opened=true
37
- rescue Errno::EADDRINUSE
38
- end
26
+ def open_gateway(user, host)
27
+ log "Opening SSH Gateway to, #{host}", true
28
+ gateway = Net::SSH::Gateway.new(host, user)
29
+ local_port = 29_000
30
+ opened = false
31
+ while opened == false
32
+ local_port += 1
33
+ begin
34
+ gateway.open('127.0.0.1', 11_300, local_port)
35
+ opened = true
36
+ rescue Errno::EADDRINUSE
37
+ log "Errno::EADDRINUSE: #{local_port}, Trying next port up.", true
39
38
  end
40
- return [localPort,gateway]
39
+ end
40
+ [local_port, gateway]
41
41
  end
42
42
 
43
+ def log(string, verbose = false)
44
+ return if ENV['TESTING'] == 'true'
45
+ return unless !ENV['VERBOSE'].nil? || verbose == false
43
46
 
44
- def log( string, verbose=false )
45
- return if ENV["TESTING"]=="true"
46
-
47
- type = verbose ? "VERB" : "INFO"
48
- if !ENV["VERBOSE"].nil? || verbose==false then
49
- timestamp = Time.new.strftime( "%Y-%m-%d %H:%M:%S" )
50
- puts "[#{type}] #{timestamp} :: #{string}"
51
- end
47
+ type = verbose ? 'VERB' : 'INFO'
48
+ timestamp = Time.new.strftime('%Y-%m-%d %H:%M:%S')
49
+ puts "[#{type}] #{timestamp} :: #{string}"
52
50
  end
53
51
 
54
- def formatOutput( title, content )
55
- puts "\n===> #{title} <===\n#{content}"
52
+ def format_output(title, content)
53
+ puts "\n===> #{title} <===\n#{content}"
56
54
  end
57
55
 
58
- def getDslDirectory
59
- return "#{ENV['HOME']}/servicesnapshot"
56
+ def dsl_directory
57
+ "#{ENV['HOME']}/servicesnapshot"
60
58
  end
61
59
 
60
+ def get_file_name(path)
61
+ path = "#{path}.dsl" if File.extname(path) == ''
62
62
 
63
- def getFileName( path )
64
- path = "#{path}.dsl" if File.extname(path) == ""
65
-
66
- p = path
67
- return p if File.exists?( p )
63
+ p = path
64
+ return p if File.exist?(p)
68
65
 
69
- p = "#{getDslDirectory}/#{path}"
70
- return p if File.exists?( p )
66
+ p = "#{dsl_directory}/#{path}"
67
+ return p if File.exist?(p)
71
68
 
72
- abort( "Could not find the dsl you passed in, #{path}" )
69
+ abort("Could not find the dsl you passed in, #{path}")
73
70
  end
74
-
@@ -0,0 +1,77 @@
1
+ require 'beanstalk-client'
2
+
3
+ def beanstalk_queue(params)
4
+ usage = "beanstalk_queue :user=><username>, :host=><hostname>, :queue=><queue|[queue1,queue2,...]>"
5
+ user = get_param(params, :user, usage)
6
+ host = get_param(params, :host, usage)
7
+ queue = get_param(params, :queue, usage)
8
+
9
+ local_port, gateway = open_gateway( user, host )
10
+
11
+ destination_url = "127.0.0.1:#{local_port}"
12
+ log "Opened SSH Gateway to, #{host}, on, #{destination_url}", true
13
+ list = []
14
+ log 'Connect to remote beanstalk', true
15
+ beanstalk = Beanstalk::Pool.new([destination_url])
16
+ beanstalk.watch(queue)
17
+ tube_stats = beanstalk.stats_tube(queue)
18
+ index = tube_stats['current-jobs-ready'].to_i
19
+ log "Current number of msgs in tube, #{index}", true
20
+ job_list = []
21
+ list = []
22
+ 1.upto(index) do
23
+ job = beanstalk.reserve 1
24
+ job_list << job
25
+ list << job.body
26
+ end
27
+
28
+ job_list.each do |job|
29
+ job.release
30
+ end
31
+
32
+ title = "# beanstalk_queue: #{user}@#{host} #{queue}"
33
+ format_output(title, "\n==> MSG <==\n" + list.join("\n==> MSG <==\n") +
34
+ "\n\n")
35
+ end
36
+
37
+ def beanstalk(params)
38
+ usage = 'beanstalk :user=><username>, :host=><hostname>, ' \
39
+ ':queues=><queue|[queue1,queue2,...]>'
40
+ user = get_param(params, :user, usage)
41
+ host = get_param(params, :host, usage)
42
+
43
+ queues = nil
44
+ begin
45
+ queues = get_param(params, :queues, usage)
46
+ rescue ParameterMissingError => e
47
+ # log "beanstalk.ParameterMissingError. #{e.message}"
48
+ end
49
+ queues = [queues] if queues.class.name == 'String'
50
+
51
+ local_port, _gateway = open_gateway(user, host)
52
+
53
+ destination_url = "127.0.0.1:#{local_port}"
54
+ log "Opened SSH Gateway to, #{host}, on, #{destination_url}", true
55
+ list = []
56
+ log 'Connect to remote beanstalk', true
57
+ beanstalk = Beanstalk::Pool.new([destination_url])
58
+
59
+ hash = {}
60
+ beanstalk.list_tubes[destination_url].each do |name|
61
+ tube_stats = beanstalk.stats_tube(name)
62
+ hash[name] = tube_stats['current-jobs-ready'].to_s
63
+ end
64
+
65
+ if queues.nil?
66
+ hash.each do |k, v|
67
+ list << "#{k}(#{v})"
68
+ end
69
+ else
70
+ queues.each do |k|
71
+ list << "#{k}(#{hash[k]})"
72
+ end
73
+ end
74
+
75
+ title = "beanstalk: #{user}@#{host} #{queues}"
76
+ format_output(title, list.join("\n"))
77
+ end
@@ -0,0 +1,49 @@
1
+ require 'FluidDb'
2
+
3
+ def pad_string_with_white_space(string, length)
4
+ while string.length <= length + 1
5
+ string += ' '
6
+ end
7
+ string
8
+ end
9
+
10
+ def fluiddb(params)
11
+ usage = 'fluiddb :uri=><uri>, :sql=><sql>'
12
+ uri_string = get_param(params, :uri, usage)
13
+ sql_string = get_param(params, :sql, usage)
14
+
15
+ db = FluidDb::Db(URI.parse(uri_string))
16
+ list = db.queryForResultset(sql_string, [])
17
+ content = ''
18
+ if list.count == 0
19
+ content = 'No rows returned'
20
+ else
21
+
22
+ max_width = Array.new(list[0].keys.length, 0)
23
+ list[0].keys.each_with_index do |k, idx|
24
+ max_width[idx] = k.length if max_width[idx] < k.length
25
+ list.each do |row|
26
+ max_width[idx] = row[k].to_s.length if max_width[idx] < row[k].to_s.length
27
+ end
28
+ end
29
+
30
+ field_names = ''
31
+ under_lines = ''
32
+ list[0].keys.each_with_index do |field_name, idx|
33
+ field_names += pad_string_with_white_space(field_name, max_width[idx])
34
+ under_lines += pad_string_with_white_space('=' * max_width[idx], max_width[idx])
35
+ end
36
+
37
+ content = "#{field_names}\n#{under_lines}\n"
38
+
39
+ list.each do |row|
40
+ row.values.each_with_index do |v, idx|
41
+ content += pad_string_with_white_space(v.to_s, max_width[idx])
42
+ end
43
+ content += "\n"
44
+ end
45
+ end
46
+
47
+ title = "fluiddb: #{uri_string} = #{sql_string}"
48
+ format_output(title, content)
49
+ end
@@ -0,0 +1,10 @@
1
+
2
+ def shell(params)
3
+ usage = 'shell :cmd=><cmd string>'
4
+ cmd = get_param(params, :cmd, usage)
5
+
6
+ content = `#{cmd}`
7
+
8
+ title = "shell: #{cmd}"
9
+ format_output(title, content)
10
+ end
@@ -0,0 +1,16 @@
1
+ require 'net/ssh'
2
+
3
+ def ssh(params)
4
+ usage = 'ssh :user=><username>, :host=><hostname>, :queues=><queue|[queue1,queue2,...]>'
5
+ user = get_param(params, :user, usage)
6
+ host = get_param(params, :host, usage)
7
+ cmd = get_param(params, :cmd, usage)
8
+
9
+ content = ''
10
+ Net::SSH.start(host, user) do |ssh|
11
+ content = ssh.exec!(cmd)
12
+ end
13
+
14
+ title = "ssh: #{user}@#{host} #{cmd}"
15
+ format_output(title, content)
16
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: servicesnapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Guy Irvine
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-01-09 00:00:00.000000000 Z
11
+ date: 2016-08-16 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: The fastest way to get a snapshot of your system.
15
14
  email: guy@guyirvine.com
@@ -19,36 +18,35 @@ extensions: []
19
18
  extra_rdoc_files: []
20
19
  files:
21
20
  - lib/helper_functions.rb
22
- - lib/Snapshot/Beanstalk.rb
23
- - lib/Snapshot/FluidDb.rb
24
- - lib/Snapshot/Shell.rb
25
- - lib/Snapshot/Ssh.rb
21
+ - lib/snapshot/beanstalk.rb
22
+ - lib/snapshot/fluiddb.rb
23
+ - lib/snapshot/shell.rb
24
+ - lib/snapshot/ssh.rb
26
25
  - bin/servicesnapshot
27
26
  - LICENSE
28
27
  - README.md
29
28
  homepage: http://rubygems.org/gems/servicesnapshot
30
- licenses: []
29
+ licenses:
30
+ - LGPL-3.0
31
+ metadata: {}
31
32
  post_install_message:
32
33
  rdoc_options: []
33
34
  require_paths:
34
35
  - lib
35
36
  required_ruby_version: !ruby/object:Gem::Requirement
36
- none: false
37
37
  requirements:
38
- - - ! '>='
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  required_rubygems_version: !ruby/object:Gem::Requirement
42
- none: false
43
42
  requirements:
44
- - - ! '>='
43
+ - - '>='
45
44
  - !ruby/object:Gem::Version
46
45
  version: '0'
47
46
  requirements: []
48
47
  rubyforge_project:
49
- rubygems_version: 1.8.11
48
+ rubygems_version: 2.0.14.1
50
49
  signing_key:
51
- specification_version: 3
50
+ specification_version: 4
52
51
  summary: ServiceSnapshot
53
52
  test_files: []
54
- has_rdoc:
@@ -1,78 +0,0 @@
1
- require "beanstalk-client"
2
-
3
- def beanstalk_queue( params )
4
-
5
- usage = "beanstalk_queue :user=><username>, :host=><hostname>, :queue=><queue|[queue1,queue2,...]>"
6
- user = get_param( params, :user, usage )
7
- host = get_param( params, :host, usage )
8
- queue = get_param( params, :queue, usage )
9
-
10
-
11
- localPort, gateway = open_gateway( user, host )
12
-
13
- destinationUrl = "127.0.0.1:#{localPort}"
14
- log "Opened SSH Gateway to, #{host}, on, #{destinationUrl}", true
15
- list = Array.new
16
- log "Connect to remote beanstalk", true
17
- beanstalk = Beanstalk::Pool.new([destinationUrl])
18
- beanstalk.watch( queue )
19
- tubeStats = beanstalk.stats_tube( queue )
20
- index = tubeStats["current-jobs-ready"].to_i
21
- log "Current number of msgs in tube, #{index}", true
22
- jobList = Array.new
23
- list = Array.new
24
- 1.upto(index) do
25
- job = beanstalk.reserve 1
26
- jobList << job
27
- list << job.body
28
- end
29
-
30
- jobList.each do |job|
31
- job.release
32
- end
33
-
34
- title = "# beanstalk_queue: #{user}@#{host} #{queue}"
35
- formatOutput( title, "\n==> MSG <==\n\n" + list.join( "\n==> MSG <==\n\n" ) + "\n\n" )
36
- end
37
-
38
-
39
- def beanstalk( params )
40
- usage = "beanstalk :user=><username>, :host=><hostname>, :queues=><queue|[queue1,queue2,...]>"
41
- user = get_param( params, :user, usage )
42
- host = get_param( params, :host, usage )
43
-
44
- queues = nil
45
- begin
46
- queues = get_param( params, :queues, usage )
47
- rescue ParameterMissingError=>e
48
- end
49
- queues = [queues] if queues.class.name == "String"
50
-
51
- localPort, gateway = open_gateway( user, host )
52
-
53
- destinationUrl = "127.0.0.1:#{localPort}"
54
- log "Opened SSH Gateway to, #{host}, on, #{destinationUrl}", true
55
- list = Array.new
56
- log "Connect to remote beanstalk", true
57
- beanstalk = Beanstalk::Pool.new([destinationUrl])
58
-
59
- hash = Hash.new
60
- beanstalk.list_tubes[destinationUrl].each do |name|
61
- tubeStats = beanstalk.stats_tube(name)
62
- hash[name] = tubeStats["current-jobs-ready"].to_s
63
- end
64
-
65
- if queues.nil? then
66
- hash.each do |k,v|
67
- list << "#{k}(#{v})"
68
- end
69
- else
70
- queues.each do |k|
71
- list << "#{k}(#{hash[k]})"
72
- end
73
- end
74
-
75
- title = "beanstalk: #{user}@#{host} #{queues}"
76
- formatOutput( title, list.join( "\n" ) )
77
- end
78
-
@@ -1,49 +0,0 @@
1
- require "FluidDb"
2
-
3
- def padStringWithWhiteSpace( string, length )
4
- while string.length <= length+1
5
- string = string + " "
6
- end
7
- return string
8
- end
9
-
10
- def fluiddb( params )
11
- usage = "fluiddb :uri=><uri>, :sql=><sql>"
12
- uri_string = get_param( params, :uri, usage )
13
- sql_string = get_param( params, :sql, usage )
14
-
15
- db = FluidDb::Db( URI.parse( uri_string ) )
16
- list = db.queryForResultset( sql_string, [] )
17
- content = ""
18
- if list.count == 0 then
19
- content = "No rows returned"
20
- else
21
-
22
- max_width = Array.new( list[0].keys.length, 0 )
23
- list[0].keys.each_with_index do |k,idx|
24
- max_width[idx] = k.length if max_width[idx] < k.length
25
- list.each do |row|
26
- max_width[idx] = row[k].to_s.length if max_width[idx] < row[k].to_s.length
27
- end
28
- end
29
-
30
- fieldNames = ""
31
- underLines = ""
32
- list[0].keys.each_with_index do |fieldName, idx|
33
- fieldNames = fieldNames + padStringWithWhiteSpace( fieldName, max_width[idx])
34
- underLines = underLines + padStringWithWhiteSpace( "=" * max_width[idx], max_width[idx])
35
- end
36
-
37
- content = "#{fieldNames}\n#{underLines}\n"
38
-
39
- list.each do |row|
40
- row.values.each_with_index do |v,idx|
41
- content = content + padStringWithWhiteSpace( v.to_s, max_width[idx])
42
- end
43
- content = content + "\n"
44
- end
45
- end
46
-
47
- title = "fluiddb: #{uri_string} = #{sql_string}"
48
- formatOutput( title, content )
49
- end
@@ -1,12 +0,0 @@
1
- require "net/ssh"
2
-
3
- def shell( params )
4
- usage = "shell :cmd=><cmd string>"
5
- cmd = get_param( params, :cmd, usage )
6
-
7
- # content = system cmd
8
- content = `#{cmd}`
9
-
10
- title = "shell: #{cmd}"
11
- formatOutput( title, content )
12
- end
data/lib/Snapshot/Ssh.rb DELETED
@@ -1,16 +0,0 @@
1
- require "net/ssh"
2
-
3
- def ssh( params )
4
- usage = "ssh :user=><username>, :host=><hostname>, :queues=><queue|[queue1,queue2,...]>"
5
- user = get_param( params, :user, usage )
6
- host = get_param( params, :host, usage )
7
- cmd = get_param( params, :cmd, usage )
8
-
9
- content = ""
10
- Net::SSH.start(host, user) do |ssh|
11
- content = ssh.exec!(cmd)
12
- end
13
-
14
- title = "ssh: #{user}@#{host} #{cmd}"
15
- formatOutput( title, content )
16
- end