Top Level Namespace
Defined Under Namespace
Classes: ParameterMissingError
Instance Method Summary collapse
- #beanstalk(params) ⇒ Object
- #beanstalk_queue(params) ⇒ Object
- #dsl_directory ⇒ Object
- #env(*args) ⇒ Object
- #fluiddb(params) ⇒ Object
- #format_output(title, content) ⇒ Object
- #get_file_name(path) ⇒ Object
- #get_param(params, name, usage) ⇒ Object
- #log(string, verbose = false) ⇒ Object
- #open_gateway(user, host) ⇒ Object
- #pad_string_with_white_space(string, length) ⇒ Object
- #shell(params) ⇒ Object
- #ssh(params) ⇒ Object
Instance Method Details
#beanstalk(params) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/snapshot/beanstalk.rb', line 37 def beanstalk(params) usage = 'beanstalk :user=><username>, :host=><hostname>, ' \ ':queues=><queue|[queue1,queue2,...]>' user = get_param(params, :user, usage) host = get_param(params, :host, usage) queues = nil begin queues = get_param(params, :queues, usage) rescue ParameterMissingError => e # log "beanstalk.ParameterMissingError. #{e.message}" end queues = [queues] if queues.class.name == 'String' local_port, _gateway = open_gateway(user, host) destination_url = "127.0.0.1:#{local_port}" log "Opened SSH Gateway to, #{host}, on, #{destination_url}", true list = [] log 'Connect to remote beanstalk', true beanstalk = Beanstalk::Pool.new([destination_url]) hash = {} beanstalk.list_tubes[destination_url].each do |name| tube_stats = beanstalk.stats_tube(name) hash[name] = tube_stats['current-jobs-ready'].to_s end if queues.nil? hash.each do |k, v| list << "#{k}(#{v})" end else queues.each do |k| list << "#{k}(#{hash[k]})" end end title = "beanstalk: #{user}@#{host} #{queues}" format_output(title, list.join("\n")) end |
#beanstalk_queue(params) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/snapshot/beanstalk.rb', line 3 def beanstalk_queue(params) usage = "beanstalk_queue :user=><username>, :host=><hostname>, :queue=><queue|[queue1,queue2,...]>" user = get_param(params, :user, usage) host = get_param(params, :host, usage) queue = get_param(params, :queue, usage) local_port, gateway = open_gateway( user, host ) destination_url = "127.0.0.1:#{local_port}" log "Opened SSH Gateway to, #{host}, on, #{destination_url}", true list = [] log 'Connect to remote beanstalk', true beanstalk = Beanstalk::Pool.new([destination_url]) beanstalk.watch(queue) tube_stats = beanstalk.stats_tube(queue) index = tube_stats['current-jobs-ready'].to_i log "Current number of msgs in tube, #{index}", true job_list = [] list = [] 1.upto(index) do job = beanstalk.reserve 1 job_list << job list << job.body end job_list.each do |job| job.release end title = "# beanstalk_queue: #{user}@#{host} #{queue}" format_output(title, "\n==> MSG <==\n" + list.join("\n==> MSG <==\n") + "\n\n") end |
#dsl_directory ⇒ Object
56 57 58 |
# File 'lib/helper_functions.rb', line 56 def dsl_directory "#{ENV['HOME']}/servicesnapshot" end |
#env(*args) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/helper_functions.rb', line 18 def env(*args) fail 'Must have an even number of argument to env' if args.length.odd? (0..args.length - 1).step(2) do |i| ENV[args[i]] = args[i + 1] end end |
#fluiddb(params) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/snapshot/fluiddb.rb', line 10 def fluiddb(params) usage = 'fluiddb :uri=><uri>, :sql=><sql>' uri_string = get_param(params, :uri, usage) sql_string = get_param(params, :sql, usage) db = FluidDb::Db(URI.parse(uri_string)) list = db.queryForResultset(sql_string, []) content = '' if list.count == 0 content = 'No rows returned' else max_width = Array.new(list[0].keys.length, 0) list[0].keys.each_with_index do |k, idx| max_width[idx] = k.length if max_width[idx] < k.length list.each do |row| max_width[idx] = row[k].to_s.length if max_width[idx] < row[k].to_s.length end end field_names = '' under_lines = '' list[0].keys.each_with_index do |field_name, idx| field_names += pad_string_with_white_space(field_name, max_width[idx]) under_lines += pad_string_with_white_space('=' * max_width[idx], max_width[idx]) end content = "#{field_names}\n#{under_lines}\n" list.each do |row| row.values.each_with_index do |v, idx| content += pad_string_with_white_space(v.to_s, max_width[idx]) end content += "\n" end end title = "fluiddb: #{uri_string} = #{sql_string}" format_output(title, content) end |
#format_output(title, content) ⇒ Object
52 53 54 |
# File 'lib/helper_functions.rb', line 52 def format_output(title, content) puts "\n===> #{title} <===\n#{content}" end |
#get_file_name(path) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/helper_functions.rb', line 60 def get_file_name(path) path = "#{path}.dsl" if File.extname(path) == '' p = path return p if File.exist?(p) p = "#{dsl_directory}/#{path}" return p if File.exist?(p) abort("Could not find the dsl you passed in, #{path}") end |
#get_param(params, name, usage) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/helper_functions.rb', line 6 def get_param(params, name, usage) return params[name] unless params[name].nil? return ENV[name.to_s] unless ENV[name.to_s].nil? msg = "*** Could not find parameter, #{name}, for command, " \ "#{caller[0][/`.*'/][1..-2]}\n" \ "*** #{usage}\n" \ "*** Try :#{name}=>'YourValue'" fail ParameterMissingError, msg end |
#log(string, verbose = false) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/helper_functions.rb', line 43 def log(string, verbose = false) return if ENV['TESTING'] == 'true' return unless !ENV['VERBOSE'].nil? || verbose == false type = verbose ? 'VERB' : 'INFO' = Time.new.strftime('%Y-%m-%d %H:%M:%S') puts "[#{type}] #{timestamp} :: #{string}" end |
#open_gateway(user, host) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/helper_functions.rb', line 26 def open_gateway(user, host) log "Opening SSH Gateway to, #{host}", true gateway = Net::SSH::Gateway.new(host, user) local_port = 29_000 opened = false while opened == false local_port += 1 begin gateway.open('127.0.0.1', 11_300, local_port) opened = true rescue Errno::EADDRINUSE log "Errno::EADDRINUSE: #{local_port}, Trying next port up.", true end end [local_port, gateway] end |
#pad_string_with_white_space(string, length) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/snapshot/fluiddb.rb', line 3 def pad_string_with_white_space(string, length) while string.length <= length + 1 string += ' ' end string end |
#shell(params) ⇒ Object
2 3 4 5 6 7 8 9 10 |
# File 'lib/snapshot/shell.rb', line 2 def shell(params) usage = 'shell :cmd=><cmd string>' cmd = get_param(params, :cmd, usage) content = `#{cmd}` title = "shell: #{cmd}" format_output(title, content) end |
#ssh(params) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/snapshot/ssh.rb', line 3 def ssh(params) usage = 'ssh :user=><username>, :host=><hostname>, :queues=><queue|[queue1,queue2,...]>' user = get_param(params, :user, usage) host = get_param(params, :host, usage) cmd = get_param(params, :cmd, usage) content = '' Net::SSH.start(host, user) do |ssh| content = ssh.exec!(cmd) end title = "ssh: #{user}@#{host} #{cmd}" format_output(title, content) end |