Permalink
Browse files

Merge pull request #10 from lflux/datastore-list

Add support for showing datastores with their available size and capacity
  • Loading branch information...
2 parents 717f28e + 7adba58 commit 5efd21582e38ceec4286683be930e145ffb807f4 @ezrapagel ezrapagel committed Mar 6, 2012
Showing with 58 additions and 0 deletions.
  1. +58 −0 lib/chef/knife/vsphere_datastore_list.rb
@@ -0,0 +1,58 @@
+# Copyright (C) 2012, SCM Ventures AB
+# Author: Ian Delahorne <ian@scmventures.se>
+#
+# Permission to use, copy, modify, and/or distribute this software for
+# any purpose with or without fee is hereby granted, provided that the
+# above copyright notice and this permission notice appear in all
+# copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
+# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
+# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE
+
+require 'chef/knife'
+require 'chef/knife/BaseVsphereCommand'
+
+def number_to_human_size(number)
+ number = number.to_f
+ storage_units_fmt = ["byte", "kB", "MB", "GB", "TB"]
+ base = 1024
+ if number.to_i < base
+ unit = storage_units_fmt[0]
+ else
+ max_exp = storage_units_fmt.size - 1
+ exponent = (Math.log(number) / Math.log(base)).to_i # Convert to base
+ exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
+ number /= base ** exponent
+ unit = storage_units_fmt[exponent]
+ end
+
+ return sprintf("%0.2f %s", number, unit)
+end
+
+
+# Lists all known data stores in datacenter with sizes
+class Chef::Knife::VsphereDatastoreList < Chef::Knife::BaseVsphereCommand
+
+ banner "knife vsphere datastore list"
+
+ get_common_options
+ def run
+ $stdout.sync = true
+
+ vim = get_vim_connection
+ dcname = config[:vsphere_dc] || Chef::Config[:knife][:vsphere_dc]
+ dc = config[:vim].serviceInstance.find_datacenter(dcname) or abort "datacenter not found"
+ dc.datastore.each do |store|
+ avail = number_to_human_size(store.summary[:freeSpace])
+ cap = number_to_human_size(store.summary[:capacity])
+ puts "#{ui.color("Datastore", :cyan)}: #{store.name} (#{avail} / #{cap})"
+ end
+ end
+end
+

0 comments on commit 5efd215

Please sign in to comment.