Permalink
Browse files

Merge latest CRM bugfixes from master.

  • Loading branch information...
2 parents d557931 + 52b421e commit f950b9c312c302c2217deb018a7003989a017c81 @alexgandy alexgandy committed Mar 8, 2013
Showing with 197 additions and 33 deletions.
  1. +19 −19 models/machine.rb
  2. +178 −14 models/vmware_api_adaptor.rb
View
@@ -169,22 +169,25 @@ def readings(_interval = 300, _since = 10.minutes.ago.utc, _until = 5.minutes.ag
# machine = inode.vmware_api_adaptor.start(uuid)
# end
- # def stop(inode)
- # logger.info("machine.stop")
- # machine = inode.vmware_api_adaptor.stop(uuid)
- # end
-
- # def restart(inode)
- # logger.info("machine.restart")
- # machine = inode.vmware_api_adaptor.restart(uuid)
- # end
-
- # def force_stop(inode)
- # logger.info("machine.force_stop")
-
- # begin
- # vm.PowerOffVM_Task.wait_for_completion
- # @power_state = "stopping"
+ #def stop(inode)
+ # logger.info("machine.stop")
+ # machine = inode.vmware_api_adaptor.stop(uuid)
+ #end
+ #
+ #def restart(inode)
+ # logger.info("machine.restart")
+ # machine = inode.vmware_api_adaptor.restart(uuid)
+ #end
+ #
+ #def force_restart(inode)
+ # logger.info("machine.start")
+ # machine = inode.vmware_api_adaptor.force_restart(uuid)
+ #end
+ #
+ #def force_stop(inode)
+ # logger.info("machine.stop")
+ # machine = inode.vmware_api_adaptor.force_stop(uuid)
+ #end
# rescue RbVmomi::Fault => e
# logger.error(e.message)
@@ -259,9 +262,6 @@ def disks=(_disks)
private
-
-
-
# Helper Method for converting machine power states.
def self.convert_power_state(tools_status, power_status)
logger.info('machine.convert_power_state')
@@ -152,6 +152,56 @@ def hosts
return _hosts
end
+ def datastores
+ logger.info("vmware_api_adaptor.datastores")
+ datastores = []
+ self.hosts.each do |host|
+ hdsb = host[:host_mor].get_datastore_browser
+ hdsb.get_datastores.each do |ds|
+ # don't build a hash, or add it to the list of datastores if it's already there
+ if datastores.select {|d| d["moref_id"] == ds.get_mor.get_value }.empty?
+ ds_hash = {}
+ ds_hash["mor"] = ds
+ ds_hash["moref_id"] = ds.get_mor.get_value
+ ds_hash["name"] = ds.get_info.get_name if ds.get_info.get_name
+ ds_hash["max_file_size"] = ds.get_info.get_max_file_size if ds.get_info.get_max_file_size
+ ds_hash["free_space"] = ds.get_info.get_free_space if ds.get_info.get_free_space
+ ds_hash["url"] = ds.get_info.get_url if ds.get_info.get_url
+ ds_hash["media_files"] = virtual_disks(ds)
+ datastores << ds_hash
+ end
+ end
+ end
+
+ datastores
+ end
+
+ def virtual_disks(_datastore)
+ logger.info("vmware_api_adaptor.virtual_disks")
+ ds_browser = _datastore.get_browser
+ v_disk_filter = Vim::VmDiskFileQueryFilter.new();
+ v_disk_filter.set_controller_type(["VirtualIDEController"].to_java(:string))
+ search_spec = Vim::HostDatastoreBrowserSearchSpec.new()
+ search_spec.set_query([Vim::VmDiskFileQuery.new()])
+
+ media_files = []
+
+ temp_task = ds_browser.searchDatastoreSubFolders_Task("[#{_datastore.get_info.get_name}]", search_spec)
+ sleep(1) while ["queued", "running"].include?(temp_task.get_task_info.get_state.to_s)
+ results = temp_task.get_task_info.get_result.get_host_datastore_browser_search_results
+ results.each do |r|
+ if r.file.present?
+ r.file.each do |f|
+ media_files << {
+ "path" => f.get_path
+ }
+ end
+ end
+ end
+
+ return media_files
+ end
+
# --------------------------------------------------------
# Virtual Machines
# --------------------------------------------------------
@@ -344,25 +394,129 @@ def find_vm_by_uuid(_uuid)
end
def start(_uuid)
- logger.info("vmware_api_adaptor.start")
- machine = find_vm_by_uuid(_uuid)
- machine.power_on_vm_task(nil)
+ begin
+ logger.info("vmware_api_adaptor.start")
+ machine = find_vm_by_uuid(_uuid)
+ tasks = []
+ machine.map { |vm| tasks << vm["mor"].power_on_vm_task(nil) }
+ tasks.each do |t|
+ if t.present?
+ sleep(1) while ["queued", "running"].include?(t.get_task_info.get_state.to_s)
+ if t.get_task_info.get_state.to_s.include?("error")
+ raise Exceptions::Unrecoverable.new("Cannot Complete Requested Action: #{t.get_task_info.get_error.get_localized_message.to_s}")
+ end
+ end
+ end
+ rescue Java::RuntimeFault,
+ Java::RemoteException => e
+ logger.warn("Invalid #{e.get_localized_message.to_s}")
+ raise Exceptions::Unrecoverable.new("Cannot Complete Requested Action: #{e.class.to_s}")
+ rescue Vim::InvalidState,
+ Vim::TaskInProgress => e
+ logger.warn("Invalid #{e.class.to_s}")
+ raise Exceptions::MethodNotAllowed.new("Method Not Allowed: #{e.class.to_s}")
+ end
end
def stop(_uuid)
- logger.info("vmware_api_adaptor.stop")
- machine = find_vm_by_uuid(_uuid)
- machine.power_off_vm_task
+ begin
+ logger.info("vmware_api_adaptor.stop")
+ machine = find_vm_by_uuid(_uuid)
+ tasks = []
+ machine.map { |vm| tasks << vm["mor"].shutdown_guest }
+ tasks.each do |t|
+ if t.present?
+ sleep(1) while ["queued", "running"].include?(t.get_task_info.get_state.to_s)
+ if t.get_task_info.get_state.to_s.include?("error")
+ raise Exceptions::Unrecoverable.new("Cannot Complete Requested Action: #{t.get_task_info.get_error.get_localized_message.to_s}")
+ end
+ end
+ end
+ rescue Java::RuntimeFault,
+ Java::RemoteException => e
+ logger.warn("Invalid #{e.class.to_s}")
+ raise Exceptions::Unrecoverable.new("Cannot Complete Requested Action: #{e.class.to_s}")
+ rescue Vim::InvalidState,
+ Vim::TaskInProgress => e
+ logger.warn("Invalid #{e.class.to_s}")
+ raise Exceptions::MethodNotAllowed.new("Method Not Allowed: #{e.class.to_s}")
+ end
+ end
+
+ def force_stop(_uuid)
+ begin
+ logger.info("vmware_api_adaptor.stop")
+ machine = find_vm_by_uuid(_uuid)
+ tasks = []
+ machine.map { |vm| tasks << vm["mor"].power_off_vm_task }
+ tasks.each do |t|
+ if t.present?
+ sleep(1) while ["queued", "running"].include?(t.get_task_info.get_state.to_s)
+ if t.get_task_info.get_state.to_s.include?("error")
+ raise Exceptions::Unrecoverable.new("Cannot Complete Requested Action: #{t.get_task_info.get_error.get_localized_message.to_s}")
+ end
+ end
+ end
+ rescue Java::RuntimeFault,
+ Java::RemoteException => e
+ logger.warn("Invalid #{e.class.to_s}")
+ raise Exceptions::Unrecoverable.new("Cannot Complete Requested Action: #{e.class.to_s}")
+ rescue Vim::InvalidState,
+ Vim::TaskInProgress => e
+ logger.warn("Invalid #{e.class.to_s}")
+ raise Exceptions::MethodNotAllowed.new("Method Not Allowed: #{e.class.to_s}")
+ end
end
def restart(_uuid)
begin
logger.info("vmware_api_adaptor.restart")
machine = find_vm_by_uuid(_uuid)
- machine.reboot_guest
- rescue Java::ComVmwareVim25::ToolsUnavailable => e
- logger.warn("Invalid #{e.cause.shortDescription}")
- raise Exceptions::MethodNotAllowed.new("Cannot Complete Requested Action: #{e.cause.shortDescription}")
+ tasks = []
+ machine.map { |vm| tasks << vm["mor"].reboot_guest }
+ tasks.each do |t|
+ if t.present?
+ sleep(1) while ["queued", "running"].include?(t.get_task_info.get_state.to_s)
+ if t.get_task_info.get_state.to_s.include?("error")
+ raise Exceptions::Unrecoverable.new("Cannot Complete Requested Action: #{t.get_task_info.get_error.get_localized_message.to_s}")
+ end
+ end
+ end
+ rescue Java::RuntimeFault,
+ Java::RemoteException => e
+ logger.warn("Invalid #{e.class.to_s}")
+ raise Exceptions::Unrecoverable.new("Cannot Complete Requested Action: #{e.class.to_s}")
+ rescue Vim::InvalidState,
+ Vim::ToolsUnavailable,
+ Vim::TaskInProgress => e
+ logger.warn("Invalid #{e.class.to_s}")
+ raise Exceptions::MethodNotAllowed.new("Method Not Allowed: #{e.class.to_s}")
+ end
+ end
+
+ def force_restart(_uuid)
+ begin
+ logger.info("vmware_api_adaptor.restart")
+ machine = find_vm_by_uuid(_uuid)
+ tasks = []
+ machine.map { |vm| tasks << vm["mor"].reset_vm_task }
+ tasks.each do |t|
+ if t.present?
+ sleep(1) while ["queued", "running"].include?(t.get_task_info.get_state.to_s)
+ if t.get_task_info.get_state.to_s.include?("error")
+ raise Exceptions::Unrecoverable.new("Cannot Complete Requested Action: #{t.get_task_info.get_error.get_localized_message.to_s}")
+ end
+ end
+ end
+ rescue Java::RuntimeFault,
+ Java::RemoteException => e
+ logger.warn("Invalid #{e.class.to_s}")
+ raise Exceptions::Unrecoverable.new("Cannot Complete Requested Action: #{e.class.to_s}")
+ rescue Vim::InvalidState,
+ Vim::ToolsUnavailable,
+ Vim::TaskInProgress => e
+ logger.warn("Invalid #{e.class.to_s}")
+ raise Exceptions::MethodNotAllowed.new("Method Not Allowed: #{e.class.to_s}")
end
end
@@ -407,9 +561,20 @@ def readings(_vms, _start_time, _end_time)
temp_perf_query_spec.set_format("normal");
temp_perf_query_spec.set_interval_id(300);
temp_perf_query_spec.set_metric_id(perf_metric_ids)
- temp_perf_query_spec.set_start_time(_start_time)
- temp_perf_query_spec.set_end_time(_end_time)
+ temp_perf_query_spec.set_start_time(_start_time.utc)
+ temp_perf_query_spec.set_end_time(_end_time.utc)
query_spec_list << temp_perf_query_spec
+
+ # add empty readings here to avoid having to interate over the array again
+ vm["stats"] = {}
+ ((_start_time.utc + 5.minutes).._end_time.utc).step(5.minutes) do |ts|
+ vm["stats"][ts.strftime("%Y-%m-%dT%H:%M:%SZ")] = {
+ "virtualDisk.read.average.*" => 0,
+ "virtualDisk.write.average.*" => 0,
+ "net.received.average.*" => 0,
+ "net.transmitted.average.*" => 0
+ }
+ end
end
@@ -425,11 +590,10 @@ def readings(_vms, _start_time, _end_time)
values = pemb.get_value
entity = _vms.select { |e| e["external_vm_id"] == pemb.get_entity.get_value }.first
- entity["stats"] = {}
if infos.present?
infos.each_with_index do |info, info_index|
metric_hash = {}
- timestamp = info.get_timestamp.get_time.to_s.to_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")
+ timestamp = info.get_timestamp.get_time.to_s.to_datetime.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
metric_hash["timestamp"] = timestamp
if values.present?
values.each do |value|

0 comments on commit f950b9c

Please sign in to comment.