lib/ec2spec/client.rb in ec2spec-0.1.1 vs lib/ec2spec/client.rb in ec2spec-0.1.2
- old
+ new
@@ -1,54 +1,69 @@
-require 'logger'
+require 'ec2spec/json_formatter'
+require 'ec2spec/plain_text_formatter'
+require 'ec2spec/hash_formatter'
module Ec2spec
+ class UndefineFormatterError < StandardError; end
+
class Client
META_DATA_URL_BASE = 'http://169.254.169.254/latest/meta-data/'
META_DATA_INSTANCE_TYPE_PATH = '/instance-type'
META_DATA_INSTANCE_ID_PATH = '/instance-id'
+ DEFAULT_REGION = 'ap-northeast-1'
- TABLE_LABEL_WITH_METHODS = {
- 'instance_type' => :instance_type,
- 'instance_id' => :instance_id,
- 'memory' => :memory,
- 'price (USD/H)' => :price_per_unit,
- 'price (USD/M)' => :price_per_month,
+ OUTPUT_FORMATTERS = {
+ plain_text: PlainTextFormatter,
+ json: JsonFormatter,
+ hash: HashFormatter,
}
- def initialize(hosts, days)
- @log = Logger.new(STDOUT)
- @log.level = Logger::INFO
-
+ def initialize(hosts, days, format, region = DEFAULT_REGION)
@hosts = hosts
@days = days
+ @format = format
+ @region = region
+
+ extend_formatter
+ OfferFile.instance.prepare(@region)
end
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)
- output
+ results = threads.each(&:join).map(&:value)
+ output(results, @hosts)
end
private
+ 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
+
def exec_host_result(host, backend)
- @log.info("Started: #{host.host}")
+ Ec2spec.logger.info("Started: #{host.host}")
begin
host.instance_type = instance_type(backend)
host.instance_id = instance_id(backend)
- host.memory = memory(backend)
rescue Errno::ECONNREFUSED
host.na_values
end
- @log.info("Finished: #{host.host}")
+ Ec2spec.logger.info("Finished: #{host.host}")
host
end
def instance_type(backend)
cmd_result = backend.run_command(instance_type_cmd)
@@ -70,41 +85,17 @@
def instance_id_cmd
"curl -s #{metadata_url(META_DATA_INSTANCE_ID_PATH)}"
end
- def memory(backend)
- Specinfra::HostInventory::Memory.new(backend.host_inventory).get['total']
- end
-
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(host_name, @days)
+ host = Ec2spec::HostResult.new(@region, host_name, @days)
host.backend = backend
host
- end
-
- def output
- results = @results.map(&:value)
- table = Terminal::Table.new
- table.headings = table_header(results)
- table.rows = table_rows(results)
- column_count = @hosts.size + 1
- column_count.times { |i| table.align_column(i, :right) }
- puts table
- end
-
- def table_header(results)
- [''].concat(results.map(&:host))
- end
-
- def table_rows(results)
- TABLE_LABEL_WITH_METHODS.each_with_object([]) do |(k, v), row|
- row << [k].concat(results.map(&v))
- end
end
end
end