Skip to content

Instantly share code, notes, and snippets.

@jiphex
Created September 26, 2013 11:30
Show Gist options
  • Save jiphex/6712895 to your computer and use it in GitHub Desktop.
Save jiphex/6712895 to your computer and use it in GitHub Desktop.
CLI BigV price calculator. May not be accurate.
#!/usr/bin/env ruby1.9.1
# encoding:utf-8
#
require 'ostruct'
require 'optparse'
vm = OpenStruct.new {}
vm.memory = 1
vm.cpu = 1
vm.disc = {:sata=>20} # doesn't change
vm.extradisc = {}
vm.transfer = 200
vm.windows = false
vm.warnings = []
vm.vatrate = 100
quiet = false
verbose = false
OptionParser.new do |opts|
opts.banner = "Usage: bigvprice.rb [options]"
opts.on("-m","--memory=GB","Add memory in GB") do |m|
vm.memory += m.to_i
end
opts.on("-M","--set-memory=GB","Set memory in GB") do |m|
vm.memory = m.to_i
end
opts.on("-c","--cpu=CPU","Add N CPUs") do |c|
vm.cpu += c.to_i
end
opts.on("-C","--set-cpu=CPU","Set N CPUs") do |c|
vm.cpu = c.to_i
end
opts.on("-x","--sata=GB","Add N GB of SATA disk") do |x|
vm.extradisc[:sata] ||= 0
vm.extradisc[:sata]+=x.to_i
end
opts.on("-X","--set-sata=GB","Set N GB of SATA disk") do |xx|
if xx.to_i < 20
vm.warnings << "20GB of disk space is included."
xx = 20
end
xx = (xx.to_i-20) # have to take off the free disk space
vm.extradisc[:sata] ||= 0
vm.extradisc[:sata] = xx.to_i
end
opts.on("-y","--sas=GB","Add N GB of SAS disk") do |y|
vm.extradisc[:sas] ||= 0
vm.extradisc[:sas]+=y.to_i
end
opts.on("-Y","--set-sas=GB","Set N GB of SAS disk") do |y|
vm.extradisc[:sas] ||= 0
vm.extradisc[:sas]=y.to_i
end
opts.on("-z","--ssd=GB","Add N GB of SSD disk") do |z|
vm.extradisc[:ssd] ||= 0
vm.extradisc[:ssd]+=z.to_f
end
opts.on("-Z","--set-ssd=GB","Set N GB of SSD disk") do |z|
vm.extradisc[:ssd] ||= 0
vm.extradisc[:ssd]=z.to_f
end
opts.on("-a","--archive=GB","Add N GB of Archive disk") do |a|
vm.extradisc[:archive] ||= 0
vm.extradisc[:archive]+=a.to_i
end
opts.on("-w","--windows","Add a Windows Server license") do |w|
vm.windows = true
end
opts.on("-t","--transfer N","Add N GB of data transfer per month") do |d|
if d.to_i < 50
d = 50
end
vm.data+=(d.to_i/50).ceil
end
opts.on("-T","--set-transfer N","Set N GB of data transfer per month") do |d|
vm.data=(d.to_i/50).ceil
end
opts.on("-v", "--vat", "Add VAT") do |v|
vm.vatrate = 120
end
opts.on("-q", "--quiet", "Ignore VM warnings") do
verbose = false
quiet = true
end
opts.on("-V", "--verbose", "Display VM info") do
quiet = false
verbose = true
end
end.parse!
price = {:base=>10}
# first, the CPU juggling
if 4*(vm.cpu-1) > (vm.memory)
vm.memory = (vm.cpu-1)*4
vm.warnings << "Note: upgrade VM (free) to #{vm.memory}GB of memory."
end
if vm.cpu < (vm.memory/4)+1
vm.cpu = (vm.memory/4)+1
vm.warnings << "Note: upgrade VM (free) to #{vm.cpu} CPUs."
end
price[:memory] = (vm.memory-1)*10 # memory = £10 per GB with 1 free
price[:windows] = (vm.memory) if vm.windows # Windows is £1 per month per GB
# do the disc calculation
discprice = {:sata=>10,
:sas=>5,
:ssd=>0.5,
:archive=>50}.each do |grade,size|
if vm.extradisc[grade].nil?
0
else
price[:grade] = (vm.extradisc[grade]/size)*2
end
end
# VAT
#price = (price/100)*vm.vatrate
totprice = price.map do |a,b|
b
end.inject(:+)
if vm.vatrate > 100
totprice = (totprice.to_f/100)*vm.vatrate
end
vm.disc.merge!(vm.extradisc) do |k,a,b|
a+b
end
if verbose
puts "VM Summary:"
puts " %s GB memory" % vm.memory.to_s.ljust(5)
puts " %s CPUs" % vm.cpu.to_s.ljust(5)
vm.disc.each do |grade,amt|
next if amt.nil?
puts " %2.1f GB of %s storage" % [amt,grade]
end
puts " plus Windows license" if vm.windows
puts (" plus VAT at %2d%%" % vm.vatrate) if vm.vatrate > 100
end
vatstr = " including VAT at %2d%%" % (vm.vatrate-100)
puts "VM Total price: £%2.02f%s" % [totprice,vm.vatrate != 100 ? vatstr : ""]
unless quiet
vm.warnings.each do |w|
puts " * #{w}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment