Class: Xen::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/xen/command.rb

Defined Under Namespace

Classes: ExternalFailure

Class Method Summary collapse

Class Method Details

.create_backup(*args) ⇒ Object



96
97
98
99
100
# File 'lib/xen/command.rb', line 96

def self.create_backup(*args)
  name = args.shift
  slice = Xen::Slice.find(name)
  slice.create_backup
end

.create_image(*args) ⇒ Object

Xen::Command.create_image(‘memory=512’, :size => ‘10Gb’)

> “xm-create-image memory=512 size=10Gb”

XXX call with a hash by default



86
87
88
89
90
91
92
93
94
# File 'lib/xen/command.rb', line 86

def self.create_image(*args)
  options = args.extract_options!
  cmd = "xen-create-image #{args.concat(options.to_args).join(' ')}"
  puts 
  puts "Running the command:"
  puts cmd
  puts
  system(cmd)
end

.detailed_instance_list(name = '') ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/xen/command.rb', line 64

def self.detailed_instance_list(name='')
  cmd = "xm list --long #{name}"
  raw_entries = `#{cmd}`.split(/\n\(domain/)
  raw_entries.collect do |entry|
    attributes = entry.scan(/\((name|domid|memory|vcpus|state|cpu_time|start_time) (.*)\)/)
    attributes.inject({}) { |m, (key, val)| m[key.to_sym] = val; m } 
  end
end

.lv_list(vg_name) ⇒ Object

Return list of logical volumes



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/xen/command.rb', line 38

def self.lv_list(vg_name)
  cmd = "lvs --noheadings --nosuffix --options vg_name,lv_name,lv_size --units g #{vg_name}"
  raw = `#{cmd}`
  raw.scan(/(\S+)\s+(\S+)\s+(\S+)/).collect{ |vg_name, lv_name, size| 
    {
    :vg => vg_name,
    :name => lv_name,
    :size => size
    }
  }
end

.lv_size(vg_name, lv_name) ⇒ Object

Return the size of a logical volume in gigabytes



32
33
34
35
# File 'lib/xen/command.rb', line 32

def self.lv_size(vg_name, lv_name)
  cmd = "lvs --noheadings --nosuffix --options lv_size --units g #{vg_name}/#{lv_name}"
  `#{cmd}`.strip
end

.run(cmd) ⇒ Object

def self.xm_list

raw = `xm list`.scan(/(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/)
headers = raw.delete_at(0)
raw.map do |row|
  headers.enum_with_index.inject({}) { |m, (head, i)| m[head] = row[i]; m  }
end

end



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xen/command.rb', line 14

def self.run(cmd)
  output = []
  error = nil
  stat = Open4.popen4(cmd) do |pid, stdin, stdout, stderr|
    while line = stdout.gets
      output << line.strip
    end
    error = stderr.read.strip
  end
  # if stat.exited? # Is this needed?
    if stat.exitstatus > 0
      raise ExternalFailure, "Fatal error, `#{cmd}` returned #{stat.exitstatus} with '#{error}'"
    end
  # end
  return output
end

.shutdown_instance(name, options = {}) ⇒ Object



77
78
79
# File 'lib/xen/command.rb', line 77

def self.shutdown_instance(name, options={})
  `xm shutdown #{'-w' if options[:blocking]} #{name}`
end

.start_instance(config_file) ⇒ Object



73
74
75
# File 'lib/xen/command.rb', line 73

def self.start_instance(config_file)
  `xm create #{config_file}`
end

.vg_listObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/xen/command.rb', line 50

def self.vg_list
  cmd = "vgs --noheadings --units g --nosuffix --options vg_name,vg_size,vg_free,lv_count,max_lv"
  raw = `#{cmd}`
  raw.scan(/(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/).collect{ |vg_name, vg_size, vg_free, lv_count, max_lv|
    {
    :name => vg_name,
    :size => vg_size,
    :free => vg_free,
    :lv_count => lv_count,
    :max_lv => max_lv 
    }
  }
end

.xm_infoObject



102
103
104
105
# File 'lib/xen/command.rb', line 102

def self.xm_info
  result = `/usr/sbin/xm info`
  result.scan(/(\S+)\s*:\s*([^\n]+)/).inject({}){ |m, (i,j)| m[i.to_sym] = j; m }
end