class Object

Public Instance Methods

beanstalk(params) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/helper_functions.rb, line 56
def dsl_directory
  "#{ENV['HOME']}/servicesnapshot"
end
env(*args) click to toggle source
# 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) click to toggle source
# 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 = FluidDb2.db(URI.parse(uri_string))
  list = db.query_for_resultset(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) click to toggle source
# File lib/helper_functions.rb, line 52
def format_output(title, content)
  puts "\n===> #{title} <===\n#{content}"
end
get_file_name(path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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'
  timestamp = Time.new.strftime('%Y-%m-%d %H:%M:%S')
  puts "[#{type}] #{timestamp} :: #{string}"
end
open_gateway(user, host) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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