class CollinsShell::Cli

Public Instance Methods

console() click to toggle source
# File lib/collins_shell/cli.rb, line 144
def console
  ensure_password
  CollinsShell::Console.launch(options)
end
get_latest_version() click to toggle source
# File lib/collins_shell/cli.rb, line 159
def get_latest_version
  begin
    Gem.sources = ["http://repo.tumblr.net:9929"]
    gem = Gem::SpecFetcher.new
    shell = gem.fetch(Gem::Dependency.new('collins_shell')).flatten.first
  rescue Exception => e
    return "Could not retrieve latest gem info from #{Gem.sources.join(',')}"
  end
  if shell.nil? then
    "Could not retrieve latest gem info from repo.tumblr.net"
  else
    my_version = Gem::Version.new(get_version)
    if shell.version == my_version then
      "You are running the most recent version of collins-shell: #{my_version.to_s}"
    elsif shell.version > my_version then
      "Time to upgrade! You are running collins-shell #{my_version.to_s}, latest is #{shell.version.to_s}"
    else
      "You are probably a developer. You are running version #{my_version.to_s}, latest published is #{shell.version.to_s}"
    end
  end
end
get_version() click to toggle source
# File lib/collins_shell/cli.rb, line 150
def get_version
  version_file = File.absolute_path(File.join(File.dirname(__FILE__), '..', '..', 'VERSION'))
  if File.exists?(version_file) then
    File.read(version_file)
  else
    "0.0.0"
  end
end
latest() click to toggle source
# File lib/collins_shell/cli.rb, line 35
def latest
  puts(get_latest_version)
end
log(message) click to toggle source
# File lib/collins_shell/cli.rb, line 89
def log message
  batch_selector_operation Hash[
    :remote => options.remote,
    :operation => "log",
    :success_message => proc {|asset| "Logged to #{asset.tag}"},
    :error_message => proc{|asset| "Log to #{asset.tag}"},
    :confirmation_message => proc do |assets|
      "You are about to log '#{message}' on #{assets.length} hosts. ARE YOU SURE?"
    end
  ] do |client,asset|
    client.log!(asset, message, options.severity)
  end
end
logs(tag) click to toggle source
# File lib/collins_shell/cli.rb, line 107
def logs tag
  call_collins get_collins_client, "logs" do |client|
    severity = []
    if options.severity? then
      severity = options.severity
    end
    params = Hash[
      :filter => severity.join(';'),
      :size => options[:size].to_i,
      :sort => options[:sort]
    ]
    logs = client.logs tag, params.merge(:all_tag => 'all')
    logs.reverse! if options[:sort].to_s.downcase == 'desc'
    printer = CollinsShell::LogPrinter.new(tag, logs)
    puts printer.render
  end
end
power(action) click to toggle source
# File lib/collins_shell/cli.rb, line 72
def power action
  action = Collins::Power.normalize_action(action)
  call_collins get_collins_client, "power" do |client|
    client.log! options.tag, options.reason, 'ALERT'
    if client.power!(options.tag, action) then
      say_success "power #{action} on #{options.tag}"
    else
      say_error "power #{action} on #{options.tag}", :exit => true
    end
  end
end
power_status() click to toggle source
# File lib/collins_shell/cli.rb, line 48
def power_status
  batch_selector_operation Hash[
    :remote => options.remote,
    :operation => "power_status",
    :success_message => proc {|asset| "Got power status for #{asset.tag}"},
    :error_message => proc{|asset| "Error getting power status for #{asset.tag}"},
    :confirmation_message => proc do |assets|
      "You are about to check the power status of #{assets.length} hosts. ARE YOU SURE?"
    end
  ] do |client,asset|
    puts('*'*80)
    begin
      status = client.power_status(asset)
      say_success "Power status of #{asset.tag}: #{status}"
    rescue Exception => e
      print_error e, "Unable to check power status of #{asset.tag}", false
    end
  end
end
print_error(e, cmd = nil, separator = true) click to toggle source
search_logs(query) click to toggle source
# File lib/collins_shell/cli.rb, line 128
def search_logs query
  call_collins get_collins_client, "logs" do |client|
    params = Hash[
      :query => query,
      :size => options[:size].to_i,
      :sort => options[:sort],
      :sortField => 'date'
    ]
    logs = client.search_logs params
    printer = CollinsShell::LogPrinter.new("all assets", logs)
    puts printer.render
  end
end
version() click to toggle source
# File lib/collins_shell/cli.rb, line 40
def version
  puts("collins-shell #{get_version}")
end