module Scli
Public Class Methods
attvol()
click to toggle source
# File lib/plugins/attach-volume.rb, line 2 def self.attvol cli = MyCLI.new cli.parse_options volume_id = cli.config[:volume_id] instance_id = cli.config[:instance_id] if volume_id.nil? || instance_id.nil? puts "Instance and volume id's are required, please retry." else server = Scli::Compute.new.servers.get(instance_id) volume = Scli::Storage.new.volumes.get(volume_id) if server.nil? puts "Could not find server: #{instance_id}" elsif volume.nil? puts "Could not find volume: #{volume_id}" else if server.attach(volume_id.to_i) # Note: attach() takes a string, not an object print_volumes(volume) puts "Is being attached to instance:".red print_servers(server) else puts "Volume could not be attached for some reason..." end end end end
cadd()
click to toggle source
# File lib/plugins/create-address.rb, line 2 def self.cadd cli = MyCLI.new cli.parse_options offering_id = cli.config[:offering_id] location_id = cli.config[:location_id] vlan_id = cli.config[:vlan_id] if offering_id.nil? || location_id.nil? puts "No offering and or location id was found, please retry" else options = vlan_id.nil? ? {} : {:vlan_id => vlan_id} response = Scli::Compute.new.create_address(location_id, offering_id, options) if response.status == 200 puts "IP Created successfully: #{response.body.inspect}" else puts "Failed with #{response.status} error of: #{response.body.inspect}" end end end
cin()
click to toggle source
# File lib/plugins/create-instance.rb, line 2 def self.cin cli = MyCLI.new cli.parse_options name = cli.config[:name] image_id = cli.config[:image_id] instance_type = cli.config[:instance_type] location_id = cli.config[:location_id] #Optional args opts = {} opt_merge(opts, :key_name, cli.config[:key_id]) opt_merge(opts, :ip, cli.config[:address_id]) opt_merge(opts, :volume_id, cli.config[:volume_id]) opt_merge(opts, :vlan_id, cli.config[:vlan_id]) opt_merge(opts, :secondary_ip, cli.config[:secondary_address_id]) opt_merge(opts, :is_mini_ephemeral, cli.config[:mini_ephemeral]) opt_merge(opts, :configuration_data, cli.config[:configuration_data]) opt_merge(opts, :anti_collocation_instance, cli.config[:anti_colo]) if name.nil? || location_id.nil? || image_id.nil? || instance_type.nil? puts "Missing one of the following (name, location_id, image_id OR instance_type)." else response = Scli::Compute.new.create_instance(name, image_id, instance_type, location_id, opts) if response.status == 200 puts "Instance Created successfully: #{response.body.inspect}" else puts "Failed with #{response.status} error of: #{response.body.inspect}" end end end
ckey()
click to toggle source
# File lib/plugins/create-key.rb, line 2 def self.ckey cli = MyCLI.new cli.parse_options name = cli.config[:name] if name.nil? puts "You must provide a name for the key." else response = Scli::Compute.new.create_key(name) #Note: This method can take a pub key too, not supported yet. if response.status == 200 puts "Key Created successfully... Copy private key to a safe location." puts "Key name: #{response.body['keyName']}" puts "Key Contents:\n #{response.body['keyMaterial']}" else puts "Failed with #{response.status} error of: #{response.body.inspect}" end end end
config_file_exists?(config_file_path)
click to toggle source
# File lib/generic.rb, line 30 def self.config_file_exists?(config_file_path) File.exists?(File.expand_path(config_file_path)) end
cvol()
click to toggle source
# File lib/plugins/create-volume.rb, line 2 def self.cvol cli = MyCLI.new cli.parse_options offering_id = cli.config[:offering_id] location_id = cli.config[:location_id] format = cli.config[:format] size = cli.config[:size] name = cli.config[:name] if offering_id.nil? puts "You did not provide an offering id, using a generic id of 20035200." offering_id = 20035200 end if format.nil? || name.nil? || size.nil? || location_id.nil? || offering_id.nil? puts "Missing offering_id or location_id or format or size or name, please retry." else if volume_format_valid?(format) && volume_size_valid?(size) && volume_offering_valid?(offering_id) response = Scli::Storage.new.create_volume(name, offering_id, format, location_id, size) if response.status == 200 puts "Volume Created successfully: #{response.body.inspect}" else puts "Failed with #{response.status} error of: #{response.body.inspect}" end end end end
dadd()
click to toggle source
# File lib/plugins/describe-address.rb, line 2 def self.dadd cli = MyCLI.new cli.parse_options address_id = cli.config[:address_id] || ARGV[1] if address_id.nil? || is_address_id?(address_id) addresses = (address_id.nil?) ? Scli::Compute.new.addresses : Scli::Compute.new.addresses.get(address_id) print_object("Addresses", addresses, [:id, :location, :ip, :state, :instance_id, :hostname, :mode, :owner]) else puts "Got an invalid address id, please retry." end end
daddoff()
click to toggle source
# File lib/plugins/describe-address-offering.rb, line 2 def self.daddoff cli = MyCLI.new cli.parse_options addresses = Scli::Compute.new.list_address_offerings.body['addresses'] table_to_print = addresses.collect do |offering| [offering['id'], offering['ipType'], offering['location'], offering['price']['rate'], offering['price']['countryCode'], offering['price']['currencyCode']] end table = Terminal::Table.new :title => "IP Address Offerings (#{addresses.count})".cyan, :headings => ["id".green, "IpType".green, "location".green, "price rate".green, "price country".green, "price currency".green], :rows => table_to_print puts table end
delvol()
click to toggle source
# File lib/plugins/terminate-volume.rb, line 2 def self.delvol cli = MyCLI.new cli.parse_options volume_id = cli.config[:volume_id] || ARGV[1] if volume_id.nil? || !is_volume_id?(volume_id) puts "Volume ID is either missing or invalid, please retry." else volume = Scli::Storage.new.volumes.get(volume_id) if volume.nil? puts "Could not find volume: #{volume_id}" else if volume.destroy print_volumes(volume) puts "Is being destroyed...".red else puts "Volume could not be destroyed for some reason..." end end end end
detvol()
click to toggle source
# File lib/plugins/detach-volume.rb, line 2 def self.detvol cli = MyCLI.new cli.parse_options volume_id = cli.config[:volume_id] || ARGV[1] instance_id = cli.config[:instance_id] if volume_id.nil? puts "Volume ID is required, please retry." else volume = Scli::Storage.new.volumes.get(volume_id) instance_id = volume.instance_id if instance_id.nil? server = Scli::Compute.new.servers.get(instance_id) if server.nil? puts "Could not find server: #{instance_id}" elsif volume.nil? puts "Could not find volume: #{volume_id}" else if server.detach(volume_id) # Note: detach() takes a string, not an object print_volumes(volume) puts "Is being detached from instance:".red print_servers(server) else puts "Volume could not be detached for some reason..." end end end end
dim()
click to toggle source
# File lib/plugins/describe-image.rb, line 2 def self.dim cli = MyCLI.new cli.parse_options image_id = cli.config[:image_id] || yield_regular_input(ARGV[1]) private_imgs = cli.config[:private] if image_id.nil? images = private_imgs ? Scli::Compute.new.images.reject{|img| img.visibility != "PRIVATE"} : Scli::Compute.new.images print_object("Images", images, [:id, :name, :architecture, :platform, :visibility, :supported_instance_types, :description, :location, :owner, :created_at, :state]) else if is_image_id?(image_id) print_object("Image", Scli::Compute.new.images.get(image_id), [:id, :name, :architecture, :platform, :visibility, :supported_instance_types, :description, :location, :owner, :created_at, :state]) else puts "Image id provided is invalid" end end end
din()
click to toggle source
# File lib/plugins/describe-instance.rb, line 2 def self.din cli = MyCLI.new cli.parse_options instance_id = cli.config[:instance_id] || ARGV[1] if instance_id.nil? print_servers(Scli::Compute.new.servers) else if is_instance_id?(instance_id) print_servers(Scli::Compute.new.servers.get(instance_id)) else puts "Instance id provided is invalid" end end end
dint()
click to toggle source
# File lib/plugins/describe-instance-types.rb, line 2 def self.dint instance_types = [] instance_types << "COP32.1/2048/60" instance_types << "BRZ32.1/2048/60*175" instance_types << "SLV32.2/4096/60*350" instance_types << "GLD32.4/4096/60*350" instance_types << "COP64.2/4096/60" instance_types << "BRZ64.2/4096/60*500*350" instance_types << "SLV64.4/8192/60*500*500" instance_types << "GLD64.8/16384/60*500*500" instance_types << "PLT64.16/16384/60*500*500*500*500" table_to_print = [] instance_types.each do |inst_type| name, ram, disks = inst_type.split("/") color = name.slice!(0..2) bits, cpus = name.split(".") disk = disks.split("*") total_ephemeral_size = disk.inject{|sum,x| sum.to_i + x.to_i } total_ephemeral_non_root = (disk.count > 1) ? (disk.drop(1).inject{|sum,x| sum.to_i + x.to_i }) : 0 total_ephemeral_devices = (disk.count > 1) ? (disk.drop(1).count) : 0 table_to_print << [inst_type, color, bits, cpus, ram, total_ephemeral_size, total_ephemeral_non_root, total_ephemeral_devices] end table = Terminal::Table.new :title => "Instance Types".cyan, :headings => ["id".green, "color".green, "bits".green, "CPU(s)".green, "RAM".green, "Total Ephemeral".green, "Total Ephemeral (Non-Root)".green, "Ephemeral devices (Non-Root)".green], :rows => table_to_print puts table puts "Note: This table is not from an API, its simply a helper for the non-rememberable naming IBM gives their instances - List may update/change." end
dkey()
click to toggle source
# File lib/plugins/describe-key.rb, line 2 def self.dkey cli = MyCLI.new cli.parse_options key_id = cli.config[:key_id] || ARGV[1] keys = key_id.nil? ? Scli::Compute.new.keys : Scli::Compute.new.keys.get(key_id) print_object("Keys", keys, [:name, :default, :public_key, :instance_ids, :modified_at]) end
dloc()
click to toggle source
# File lib/plugins/describe-location.rb, line 2 def self.dloc cli = MyCLI.new cli.parse_options location_id = cli.config[:location_id] || ARGV[1] locations = location_id.nil? ? Scli::Compute.new.locations : Scli::Compute.new.locations.get(location_id) print_object("Locations", locations, [:id, :name, :location, :capabilities, :description]) end
dvlan()
click to toggle source
# File lib/plugins/describe-vlan.rb, line 2 def self.dvlan cli = MyCLI.new cli.parse_options vlan_id = cli.config[:vlan_id] || ARGV[1] if vlan_id.nil? print_object("VLANs", Scli::Compute.new.vlans, [:id, :name, :location]) else if is_vlan_id?(vlan_id) print_object("VLANs", Scli::Compute.new.vlans.get(vlan_id), [:id, :name, :location]) else puts "Got an invalid VLAN id, please retry." end end end
dvol()
click to toggle source
# File lib/plugins/describe-volume.rb, line 2 def self.dvol cli = MyCLI.new cli.parse_options volume_id = cli.config[:volume_id] || ARGV[1] if volume_id.nil? print_volumes(Scli::Storage.new.volumes) else if is_volume_id?(volume_id) print_volumes(Scli::Storage.new.volumes.get(volume_id)) else puts "Volume id provided is invalid" end end end
dvoloff()
click to toggle source
# File lib/plugins/describe-volume-offering.rb, line 2 def self.dvoloff cli = MyCLI.new cli.parse_options offerings = Scli::Storage.new.list_offerings.body['volumes'] table_to_print = offerings.collect do |offering| [offering['id'], offering['name'], format_location(offering['location']), offering['price']['rate'], offering['price']['currencyCode'], offering['price']['unitOfMeasure'], offering['formats'].collect{|format| format['id']}.join(","), offering['capacity']] end table = Terminal::Table.new :title => "Volume Offerings (#{offerings.count})".cyan, :headings => ["id".green, "Name".green, "location".green, "price rate".green, "price currency".green, "Price Measure".green, "Formats".green, "Capacity".green], :rows => table_to_print puts table end
env_populated?()
click to toggle source
# File lib/generic.rb, line 83 def self.env_populated? (ENV['IBM_SC_USERNAME'].nil? || ENV['IBM_SC_PASSWORD'].nil?) ? false : (!ENV['IBM_SC_USERNAME'].empty? && !ENV['IBM_SC_PASSWORD'].empty?) end
format_capabilities(capable, full = false)
click to toggle source
# File lib/formatters.rb, line 45 def self.format_capabilities(capable, full = false) capabilities = capable.collect do |cap| "#{cap['id']} => #{cap['entries']}" unless cap['entries'].size == 0 || cap.nil? end cap_print = capabilities.reject{|cap| cap.nil?}.join(",") full ? cap_print : "#{cap_print[0..12]}.." end
format_description(description)
click to toggle source
# File lib/formatters.rb, line 41 def self.format_description(description) (description.size > 14) ? "#{description[0..12]}.." : description end
format_image_instance_types(instance_types, single = false)
click to toggle source
# File lib/formatters.rb, line 73 def self.format_image_instance_types(instance_types, single = false) supported_types = [] instance_types.each do |it| supported_types << ((single) ? it.id : format_type(it.id)) end (single ? supported_types.join("\n") : supported_types.join(",")) end
format_instance_id(instance_id)
click to toggle source
# File lib/formatters.rb, line 59 def self.format_instance_id(instance_id) if instance_id.class == String (instance_id.nil? || instance_id.to_s == "0") ? "Detached".red : instance_id.green elsif instance_id.nil? "NA".red else instance_id.join(",") end end
format_ip(ip)
click to toggle source
# File lib/formatters.rb, line 53 def self.format_ip(ip) return "NA".red if ip.nil? || ip == "" first_octet = ip.split(".").first first_octet == "10" ? ip.cyan : ip.magenta end
format_is_default?(default)
click to toggle source
# File lib/formatters.rb, line 29 def self.format_is_default?(default) default ? "True".green : "False".red end
format_location(location)
click to toggle source
# File lib/formatters.rb, line 89 def self.format_location(location) case location.to_i when 41 "Raleigh" when 61 "Ehningen" when 82 "Boulder" when 101 "Markham" when 121 "Makuhari" when 141 "Singapore" else location end end
format_name(name)
click to toggle source
# File lib/formatters.rb, line 37 def self.format_name(name) (name.size > 34) ? "#{name[0..32]}.." : name end
format_owner(owner)
click to toggle source
# File lib/formatters.rb, line 33 def self.format_owner(owner) (owner.size > 8) ? "#{owner[0..6]}.." : owner end
format_size(vol_size)
click to toggle source
# File lib/formatters.rb, line 81 def self.format_size(vol_size) if vol_size.to_i > 1024 "#{vol_size.to_i / 1024}TB" else "#{vol_size}GB" end end
format_state(state)
click to toggle source
# File lib/formatters.rb, line 14 def self.format_state(state) case state when "Active", "Attached", "Available" state.green when "Requesting", "Provisioning", "New" state.yellow else if state.nil? "Nil".red else state.red end end end
format_table_titles(titles)
click to toggle source
# File lib/formatters.rb, line 8 def self.format_table_titles(titles) titles.collect do |title| word_for_title(title.to_s).green end end
format_type(instance_type)
click to toggle source
# File lib/formatters.rb, line 69 def self.format_type(instance_type) instance_type.to_s.split(".").first end
gco()
click to toggle source
# File lib/plugins/get-console-output.rb, line 2 def self.gco cli = MyCLI.new cli.parse_options instance_id = cli.config[:instance_id] || yield_regular_input(ARGV[1]) if instance_id.nil? || !is_instance_id?(instance_id) puts "An invalid instance id was provided." else response = Scli::Compute.new.get_instance_logs(instance_id) if response.status == 200 puts "Console log output:" puts "#{response.body['logs'].join("\n")}" else puts "Failed with #{response.status} error of: #{response.body.inspect}" end end end
generate_config_file(config_file_path)
click to toggle source
# File lib/generic.rb, line 34 def self.generate_config_file(config_file_path) puts "Config file #{config_file_path} does not exist, let's create it." puts "What is your IBM SC Username?" ibm_username = $stdin.gets puts "What is your IBM SC Password?" ibm_password = $stdin.gets Dir.mkdir(File.expand_path(File.dirname(config_file_path))) unless File.exists?(File.expand_path(File.dirname(config_file_path))) ibm_config = File.open(File.expand_path(config_file_path), "w") ibm_config.puts "ibm_username #{ibm_username}" ibm_config.puts "ibm_password #{ibm_password}" ibm_config.close puts "Config file written." end
is_address_id?(address_id)
click to toggle source
# File lib/validators.rb, line 7 def self.is_address_id?(address_id) address_id.to_i.to_s.size >= 6 end
is_image_id?(image_id)
click to toggle source
# File lib/validators.rb, line 19 def self.is_image_id?(image_id) image_id.to_i.to_s.size >= 8 end
is_instance_id?(instance_id)
click to toggle source
# File lib/validators.rb, line 23 def self.is_instance_id?(instance_id) instance_id.to_i.to_s.size >= 6 end
is_vlan_id?(vlan_id)
click to toggle source
# File lib/validators.rb, line 15 def self.is_vlan_id?(vlan_id) vlan_id.to_i.to_s.size == 3 end
is_volume_id?(volume_id)
click to toggle source
# File lib/validators.rb, line 11 def self.is_volume_id?(volume_id) volume_id.to_i.to_s.size >= 5 end
opt_merge(options, name, data)
click to toggle source
# File lib/generic.rb, line 79 def self.opt_merge(options, name, data) options.merge!({name => data}) unless data.nil? end
options()
click to toggle source
# File lib/scli.rb, line 117 def self.options cli = MyCLI.new cli.parse_options cli.config.merge!(read_config(File.expand_path(cli.config[:config_file])) || {}) end
print_object(title, objects, data_to_print, options = {})
click to toggle source
# File lib/generic.rb, line 48 def self.print_object(title, objects, data_to_print, options = {}) table_to_print = [] if objects.respond_to?('each') # Its not a single object title = title + "(#{objects.count})" objects.each do |object| table_to_print << process_data_to_format(object, data_to_print) table_to_print << :separator if options[:separator] end else table_to_print << process_data_to_format(objects, data_to_print, true) end table = Terminal::Table.new :title => title.cyan, :headings => format_table_titles(data_to_print), :rows => table_to_print puts table end
print_servers(servers)
click to toggle source
# File lib/generic.rb, line 75 def self.print_servers(servers) print_object("Servers", servers, [:id, :name, :ip, :owner, :vlan_id, :volume_ids, :instance_type, :launched_at, :location_id, :state]) end
print_volumes(volumes)
click to toggle source
# File lib/generic.rb, line 63 def self.print_volumes(volumes) print_object("Volumes", volumes, [:id, :name, :size, :instance_id, :owner, :format, :location_id, :offering_id, :created_at, :state]) if volumes.class.to_s == "Fog::Storage::IBM::Volumes" total_vol_size = 0 volumes.each do |vol| next if vol.size.nil? total_vol_size += vol.size.to_i end puts "Total #{(total_vol_size.to_i / 1024.0).round / 1.0}Tb of storage" end end
process_data_to_format(object, data_to_print, single = false)
click to toggle source
# File lib/formatters.rb, line 108 def self.process_data_to_format(object, data_to_print, single = false) print_array = [] data_to_print.each do |data| print_array << case data.to_s when /state/ format_state(object.send(data)) when /supported_instance_types/ single ? format_image_instance_types(object.send(data), true) : format_image_instance_types(object.send(data)) when /capabilities/ single ? format_capabilities(object.send(data), true) : format_capabilities(object.send(data)) when /default/ format_is_default?(object.send(data)) when /owner/ single ? object.send(data) : format_owner(object.send(data)) when /name/ format_name(object.send(data)) when /description/ single ? object.send(data) : format_description(object.send(data)) when "ip" format_ip(object.send(data)) when /volume_ids/ object.send(data).join(",") when /instance_id/ format_instance_id(object.send(data)) when /public_key/ format_description(object.send(data)) when /type/ if object.send(data).class == String format_type(object.send(data)) else object.send(data) end when /size/ format_size(object.send(data)) when /location/ format_location(object.send(data)) else object.send(data) end end print_array end
read_config(config_file_path)
click to toggle source
# File lib/generic.rb, line 87 def self.read_config(config_file_path) options = {} if File.exists?(config_file_path) config_file = File.open(config_file_path,'r') config_file.each_line do |row| option, data = row.split options[option.to_sym] = data end end options end
reboot()
click to toggle source
# File lib/plugins/reboot.rb, line 2 def self.reboot cli = MyCLI.new cli.parse_options instance_id = cli.config[:instance_id] || ARGV[1] if instance_id.nil? || !is_instance_id?(instance_id) puts "Instance id provided is invalid" else server = Scli::Compute.new.servers.get(instance_id) if server.nil? puts "Server could not be found, check instance_id and state to ensure it can be rebooted..." else if server.reboot puts "Reboot successful..." else puts "Reboot failed, check instance_id and state to ensure it can be rebooted..." end end end end
tad()
click to toggle source
# File lib/plugins/terminate-address.rb, line 2 def self.tad cli = MyCLI.new cli.parse_options address_id = cli.config[:address_id] || ARGV[1] if address_id.nil? || !is_address_id?(address_id) puts "Address id provided is invalid" else response = Scli::Compute.new.delete_address(address_id) if response.status == 200 puts "Address successfully terminated: #{response.body.inspect}" else puts "Address terminated failed #{response.status} with #{response.body.inspect}" end end end
tim()
click to toggle source
# File lib/plugins/terminate-image.rb, line 2 def self.tim cli = MyCLI.new cli.parse_options image_id = cli.config[:image_id] || ARGV[1] if image_id.nil? || !is_image_id?(image_id) puts "Image id provided is invalid" else response = Scli::Compute.new.delete_image(image_id) if response.status == 200 puts "Image successfully terminated: #{response.body.inspect}" else puts "Image terminated failed #{response.status} with #{response.body.inspect}" end end end
tin()
click to toggle source
# File lib/plugins/terminate-instance.rb, line 2 def self.tin cli = MyCLI.new cli.parse_options instance_id = cli.config[:instance_id] || ARGV[1] if instance_id.nil? || !is_instance_id?(instance_id) puts "Instance id provided is invalid" else server = Scli::Compute.new.servers.get(instance_id) if server.nil? puts "Could not find server #{instance_id}, please check instance_id and state and retry." else if server.destroy print_servers(server) puts "Is being destroyed...".red else puts "Could not destroy server #{instance_id}, please check instance_id and state and retry." end end end end
tkey()
click to toggle source
# File lib/plugins/terminate-key.rb, line 2 def self.tkey cli = MyCLI.new cli.parse_options name = cli.config[:name] || yield_regular_input(ARGV[1]) if name.nil? puts "Key name not provided" else response = Scli::Compute.new.delete_key(name) if response.status == 200 puts "Key successfully terminated: #{response.body.inspect}" else puts "Key termination failed #{response.status} with #{response.body.inspect}" end end end
volume_format_valid?(format)
click to toggle source
# File lib/validators.rb, line 27 def self.volume_format_valid?(format) valid_formats = ["EXT3", "RAW"] if valid_formats.include?(format) true else puts "The format you provided is invalid, it must be one of #{valid_formats.join(",")}" false end end
volume_offering_valid?(offering_id)
click to toggle source
# File lib/validators.rb, line 47 def self.volume_offering_valid?(offering_id) unless offering_id.to_s == "20035200" puts "=" * 60 puts "WARN: IBM has many offering ID's for volumes, however most do not support large blocks and they seem to be migrating away from using them." puts "WARN: Every volume created in the WebUI uses a offering id of 20035200, so you probably want to use that." puts "=" * 60 end true end
volume_size_valid?(size)
click to toggle source
# File lib/validators.rb, line 37 def self.volume_size_valid?(size) valid_sizes = [60, 256, 512, 1024, 2048, 4112, 8224, 10240] if valid_sizes.include?(size.to_i) true else puts "The size you provided is invalid, it must be one of #{valid_sizes.join(",")}" false end end
word_for_title(string)
click to toggle source
# File lib/formatters.rb, line 2 def self.word_for_title(string) string.gsub("_"," ").gsub(/\w+/) do |word| word.capitalize end end
yield_regular_input(argument)
click to toggle source
# File lib/validators.rb, line 2 def self.yield_regular_input(argument) return nil if argument.nil? (argument[0] == "-") ? nil : argument end