From 305254ee2500e899bf6410ef9b76f3a496dbfdaa Mon Sep 17 00:00:00 2001 From: Matt Hicks Date: Wed, 11 Jun 2008 08:26:17 -0400 Subject: Adding support for returning a specific vm --- everest-lib/lib/everest-lib.rb | 24 +++++++++++++++------ everest-lib/pkg/everest-lib-0.0.9.gem | Bin 28672 -> 28672 bytes everest-lib/pkg/everest-lib-0.0.9.tgz | Bin 25969 -> 26071 bytes .../pkg/everest-lib-0.0.9/lib/everest-lib.rb | 24 +++++++++++++++------ 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/everest-lib/lib/everest-lib.rb b/everest-lib/lib/everest-lib.rb index 0883b7b..ce494e6 100644 --- a/everest-lib/lib/everest-lib.rb +++ b/everest-lib/lib/everest-lib.rb @@ -102,12 +102,12 @@ module EverestLib end def vm(name) - vms {|vm| vm if vm[:name] == name}.flatten + parse_rows("virsh dominfo #{name}", [:id, :name, :uuid, :os_type, :state, :cpus, :cpu_time, :max_mem, :used_mem]) end def vms(&block) block ||= lambda {|val| val} - _exec('virsh list | tail -n +3', [:id, :name, :state], &block) + parse_columns('virsh list | tail -n +3', [:id, :name, :state], &block) end # Collect the list of available volume groups @@ -155,7 +155,20 @@ module EverestLib lvs_name.split("-disk0")[0] end - def _exec(command, columns, method = 'collect') + def parse_columns(command, fields, method = 'collect') + exec_retry(command).split("\n").send(method, &lambda { |row| + # Combine the columns and the values into a hash + yield Hash[*fields.zip(row.split(" ")).flatten] + }) + end + + def parse_rows(command, fields, delimiter = ':') + row_values = exec_retry(command).split("\n").collect {|row| row.split(delimiter)[1].strip} + # Combine the columns and the values into a hash + Hash[*fields.zip(row_values).flatten] + end + + def exec_retry(command) output = `#{command}` while not $?.success? do @@ -163,9 +176,6 @@ module EverestLib output = `#{command}` end - output.split("\n").send(method, &lambda { |row| - # Combine the columns and the values into a hash - yield Hash[*columns.zip(row.split(" ")).flatten] - }) + return output end end diff --git a/everest-lib/pkg/everest-lib-0.0.9.gem b/everest-lib/pkg/everest-lib-0.0.9.gem index 80d35be..5380b41 100644 Binary files a/everest-lib/pkg/everest-lib-0.0.9.gem and b/everest-lib/pkg/everest-lib-0.0.9.gem differ diff --git a/everest-lib/pkg/everest-lib-0.0.9.tgz b/everest-lib/pkg/everest-lib-0.0.9.tgz index 16125f6..7cd7bed 100644 Binary files a/everest-lib/pkg/everest-lib-0.0.9.tgz and b/everest-lib/pkg/everest-lib-0.0.9.tgz differ diff --git a/everest-lib/pkg/everest-lib-0.0.9/lib/everest-lib.rb b/everest-lib/pkg/everest-lib-0.0.9/lib/everest-lib.rb index 0883b7b..ce494e6 100644 --- a/everest-lib/pkg/everest-lib-0.0.9/lib/everest-lib.rb +++ b/everest-lib/pkg/everest-lib-0.0.9/lib/everest-lib.rb @@ -102,12 +102,12 @@ module EverestLib end def vm(name) - vms {|vm| vm if vm[:name] == name}.flatten + parse_rows("virsh dominfo #{name}", [:id, :name, :uuid, :os_type, :state, :cpus, :cpu_time, :max_mem, :used_mem]) end def vms(&block) block ||= lambda {|val| val} - _exec('virsh list | tail -n +3', [:id, :name, :state], &block) + parse_columns('virsh list | tail -n +3', [:id, :name, :state], &block) end # Collect the list of available volume groups @@ -155,7 +155,20 @@ module EverestLib lvs_name.split("-disk0")[0] end - def _exec(command, columns, method = 'collect') + def parse_columns(command, fields, method = 'collect') + exec_retry(command).split("\n").send(method, &lambda { |row| + # Combine the columns and the values into a hash + yield Hash[*fields.zip(row.split(" ")).flatten] + }) + end + + def parse_rows(command, fields, delimiter = ':') + row_values = exec_retry(command).split("\n").collect {|row| row.split(delimiter)[1].strip} + # Combine the columns and the values into a hash + Hash[*fields.zip(row_values).flatten] + end + + def exec_retry(command) output = `#{command}` while not $?.success? do @@ -163,9 +176,6 @@ module EverestLib output = `#{command}` end - output.split("\n").send(method, &lambda { |row| - # Combine the columns and the values into a hash - yield Hash[*columns.zip(row.split(" ")).flatten] - }) + return output end end -- cgit