class Awslist::AwsToolCli
Attributes
hash[RW]
output[RW]
parser[RW]
Public Class Methods
new()
click to toggle source
# File lib/awslist.rb, line 11 def initialize @hash = {} @set = 'n' @instance_list = {} @options = {:os => 'linux' , :region => 'us-east-1' , :cpu_req => 0 , :mem_req => 0 , :reload => 'n' , :instance => 'nil'} @os_list = { 'linux' => 0, 'rhel' => 0, 'sles' => 0, 'mswin' => 0, 'mswinSQL' => 0, 'mswinSQLWeb' => 0, 'mswinSQLEnterprise' => 0 } @region_list = { 'us-east-1' => 0, 'us-east-2' => 0, 'us-west-2' => 0, 'us-west-1' => 0, 'ca-central-1' => 0, 'eu-west-1' => 0, 'eu-west-2' => 0, 'eu-central-1' => 0, 'ap-southeast-1' => 0, 'ap-northeast-1' => 0, 'ap-southeast-2' => 0, 'ap-northeast-2' => 0, 'ap-south-1' => 0, 'sa-east-1' => 0, 'us-gov-west-1' => 0 } @parser = OptionParser.new do |opts| opts.banner = 'Usage: awslist [options]' opts.on('-t' , '--type INSTANCE_TYPE') do |instance| @options[:instance] = instance.strip end opts.on('-os OPERATING_SYSTEM', 'OS list : linux , rhel , sles , mswin , mswinSQL , mswinSQLWeb , mswinSQLEnterprise' ) do |os| @options[:os] = os.strip.downcase; end opts.on('-c', '--cpu CPU_REQUIREMENT', 'Basic value of cpu required in integer') do |cpu| @options[:cpu_req] = cpu.strip; end opts.on('-m', '--mem MEMORY_REQUIREMENT', 'Basic value of memory required in integer') do |memory| @options[:mem_req] = memory.strip; end opts.on('-r', '--region REGION', 'Required region , list : us-east-1 , us-east-2 , us-west-2 , us-west-1 , ca-central-1 , eu-west-1 , eu-west-2 , eu-central-1 , ap-southeast-1 , ap-northeast-1 , ap-southeast-2 , ap-northeast-2 , ap-south-1 , sa-east-1 , us-gov-west-1') do |reg| @options[:region] = reg.strip.downcase; end opts.on('--reload y/n' , 'Fetch fresh data from website') do |get| @options[:reload] = get.strip.downcase; @set = 'y' end opts.on('-h', '--help', 'Displays Help') do puts opts exit end end end
Public Instance Methods
prints()
click to toggle source
# File lib/awslist.rb, line 148 def prints yo = [] @output.each do |hashes_everywhere| value = hashes_everywhere['price'].to_f yo << [hashes_everywhere['size'],hashes_everywhere['vCPU'],hashes_everywhere['memoryGiB'],value.round(3),(value*24*30).round(3)] end table = Terminal::Table.new :headings => ['Instance Type' , 'vCPU' , 'memoryGiB' , 'Price per hr(in USD $)' , 'Price per month(in USD $)'], :rows => yo puts table end
refine_output()
click to toggle source
# File lib/awslist.rb, line 123 def refine_output a = [] b = [] @hash[@options[:os]][@options[:region]]['vCPU'].each do |k,v| if k.to_f >= @options[:cpu_req].to_s.strip.to_f v.each do |c| if (@options[:instance] != 'nil' && c['size'] == @options[:instance]) || @options[:instance] == 'nil' a << c end end end end @hash[@options[:os]][@options[:region]]['memoryGiB'].each do |k,v| if k.to_f >= @options[:mem_req].to_s.strip.to_f v.each do |c| if (@options[:instance] != 'nil' && c['size'] == @options[:instance]) || @options[:instance] == 'nil' b << c end end end end @output = a & b end
reload_files()
click to toggle source
# File lib/awslist.rb, line 70 def reload_files ['linux','rhel','sles','mswin','mswinSQL','mswinSQLWeb','mswinSQLEnterprise'].each do |x| s = RestClient.get "https://a0.awsstatic.com/pricing/1/ec2/#{x}-od.min.js" s.gsub!(/^.*callback\(/, '') # removes the comment and callback function from the start of the string s.gsub!(/\);$/, '') # removes the end of the callback function s.gsub!(/(\w+):/, '"\1":') hash = JSON.parse(s) File.open("#{x}","w") { |file| file.write(hash.to_json) } end end
reload_hash()
click to toggle source
# File lib/awslist.rb, line 81 def reload_hash ['linux','rhel','sles','mswin','mswinSQL','mswinSQLWeb','mswinSQLEnterprise'].each do |x| s = JSON.parse(File.read(x)) @hash[x] = @hash.fetch(x,{}) s['config']['regions'].each do |region_spec| str = region_spec['region'] @hash[x][str] = @hash[x].fetch(str,{}) @hash[x][str]['vCPU'] = @hash[x][str].fetch('vCPU',{}) @hash[x][str]['memoryGiB'] = @hash[x][str].fetch('memoryGiB',{}) region_spec['instanceTypes'].each do |type| type['sizes'].each do |instance_spec| instance_hash = {} instance_hash['size'] = instance_spec['size'] instance_hash['vCPU'] = instance_spec['vCPU'] instance_hash['ECU'] = instance_spec['ECU'] instance_hash['memoryGiB'] = instance_spec['memoryGiB'] instance_hash['storageGB'] = instance_spec['storageGB'] instance_hash['price'] = instance_spec['valueColumns'][0]['prices']['USD'] cpu = instance_hash['vCPU'] mem = instance_hash['memoryGiB'] @hash[x][str]['vCPU'][cpu] = @hash[x][str]['vCPU'].fetch(cpu,[]) @hash[x][str]['vCPU'][cpu] << instance_hash @hash[x][str]['memoryGiB'][mem] = @hash[x][str]['memoryGiB'].fetch(mem,[]) @hash[x][str]['memoryGiB'][mem] << instance_hash @instance_list[instance_hash['size']] = 1 end end end end File.open('Complete_Data','w'){ |file| file.write(@hash.to_json) } File.open('Instance_list','w'){ |file| file.write(@instance_list.to_json)} end
reloaded_hash()
click to toggle source
# File lib/awslist.rb, line 118 def reloaded_hash @hash = JSON.parse(File.read('Complete_Data')) @instance_list = JSON.parse(File.read('Instance_list')) end
what_to_call()
click to toggle source
# File lib/awslist.rb, line 158 def what_to_call if @options[:reload] == 'y' || !File.exist?('Complete_Data') || !File.exist?('Instance_list') reload_files reload_hash elsif @set == 'y' && @options[:reload] != 'n' puts @parser exit else reloaded_hash end begin raise if !Integer(@options[:cpu_req]) raise if !Integer(@options[:mem_req]) raise if !@os_list.key?(@options[:os]) raise if !@region_list.key?(@options[:region]) raise AwsToolCli if @options[:instance] != 'nil' && !@instance_list.key?(@options[:instance]) rescue AwsToolCli puts sprintf('Wrong instance name. Please try :') yo = [] @instance_list.each do |k,v| yo << [k] end table = Terminal::Table.new :headings => ['Instance Type'], :rows => yo puts table exit rescue puts @parser exit end end