class Ec2spec::Client

Constants

CONNECTION_ERROR_WITH_MESSAGES
DEFAULT_REGION
META_DATA_INSTANCE_ID_PATH
META_DATA_INSTANCE_TYPE_PATH
META_DATA_URL_BASE
OUTPUT_FORMATTERS

Public Class Methods

new(hosts, days, format, region = DEFAULT_REGION) click to toggle source
# File lib/ec2spec/client.rb, line 27
def initialize(hosts, days, format, region = DEFAULT_REGION)
  @hosts = hosts
  @days = days
  @format = format
  @region = region

  Initializer.instance.do(region)

  extend_formatter
end

Public Instance Methods

prepare_price_calculator(unit, rate, calc_type, app_id = nil) click to toggle source
# File lib/ec2spec/client.rb, line 38
def prepare_price_calculator(unit, rate, calc_type, app_id = nil)
  PriceCalculator.instance.prepare(unit, rate, calc_type, app_id)
end
run() click to toggle source
# File lib/ec2spec/client.rb, line 42
def run
  hosts = @hosts.map { |host| target(host) }
  threads = hosts.map do |host|
    Thread.start(host.backend) do |backend|
      exec_host_result(host, backend)
    end
  end
  results = threads.each(&:join).map(&:value)
  output(results, @hosts)
end

Private Instance Methods

exec_host_result(host, backend) click to toggle source
# File lib/ec2spec/client.rb, line 76
def exec_host_result(host, backend)
  Ec2spec.logger.info("Started: #{host.host}")
  host_result(host, backend)
  host
end
extend_formatter() click to toggle source
# File lib/ec2spec/client.rb, line 55
def extend_formatter
  format_sym = begin
    @format.to_sym
  rescue NoMethodError
    raise UndefineFormatterError
  end

  raise UndefineFormatterError unless OUTPUT_FORMATTERS.key?(format_sym)
  extend OUTPUT_FORMATTERS[format_sym]
end
host_result(host, backend) click to toggle source
# File lib/ec2spec/client.rb, line 66
def host_result(host, backend)
  Ec2spec.logger.info("Finished: #{host.host}")
  host.instance_type = instance_type(backend)
  host.instance_id   = instance_id(backend)
rescue *CONNECTION_ERROR_WITH_MESSAGES.keys => e
  message = format(CONNECTION_ERROR_WITH_MESSAGES[e.class], host: host.host)
  Ec2spec.logger.error(message)
  host.na_values
end
instance_id(backend) click to toggle source
# File lib/ec2spec/client.rb, line 87
def instance_id(backend)
  cmd_result = backend.run_command(instance_id_cmd)
  cmd_result.stdout
end
instance_id_cmd() click to toggle source
# File lib/ec2spec/client.rb, line 100
def instance_id_cmd
  "curl -s #{metadata_url(META_DATA_INSTANCE_ID_PATH)}"
end
instance_type(backend) click to toggle source
# File lib/ec2spec/client.rb, line 82
def instance_type(backend)
  cmd_result = backend.run_command(instance_type_cmd)
  cmd_result.stdout
end
instance_type_cmd() click to toggle source
# File lib/ec2spec/client.rb, line 96
def instance_type_cmd
  "curl -s #{metadata_url(META_DATA_INSTANCE_TYPE_PATH)}"
end
metadata_url(path) click to toggle source
# File lib/ec2spec/client.rb, line 92
def metadata_url(path)
  "#{META_DATA_URL_BASE}#{path}"
end
target(host_name) click to toggle source
# File lib/ec2spec/client.rb, line 104
def target(host_name)
  ssh_options = Net::SSH::Config.for(host_name)
  backend = Specinfra::Backend::Ssh.new(
    host: ssh_options[:host_name],
    ssh_options: ssh_options,
  )
  host = Ec2spec::HostResult.new(@region, host_name, @days)
  host.backend = backend
  host
end