Class: Xen::Slice

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Slice

Returns a new instance of Slice.



19
20
21
22
23
24
25
26
# File 'lib/xen/slice.rb', line 19

def initialize(*args)
  options = args.extract_options! 
  @name = options[:name]
  @config_file = options[:config_file]
  @instance = options[:instance]
  @instance_cache_expires = Time.now
  @backups = Array(options[:backups])
end

Instance Attribute Details

#backupsObject

Returns the value of attribute backups.



3
4
5
# File 'lib/xen/slice.rb', line 3

def backups
  @backups
end

#config_fileObject

XXX We’re assuming other processes aren’t going to edit config_files This is reasonable in simple cases.



79
80
81
# File 'lib/xen/slice.rb', line 79

def config_file
  @config_file
end

#imageObject

Returns the value of attribute image.



3
4
5
# File 'lib/xen/slice.rb', line 3

def image
  @image
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/xen/slice.rb', line 3

def name
  @name
end

Class Method Details

.all(options = {}) ⇒ Object



15
16
17
# File 'lib/xen/slice.rb', line 15

def self.all(options={})
  self.find(:all, options)
end

.find(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/xen/slice.rb', line 5

def self.find(*args)
  options = args.extract_options!
  case args.first
    when :all       then Xen::ConfigFile.find(:all, options).collect { |config_file| new :name => config_file.name }
    when :running   then Xen::Instance.find(:all, options).collect { |instance| new :name => instance.name }
    # Retrieve a Slice by name
    else            Xen::ConfigFile.find_by_name(args.first) && new(:name => args.first)
  end
end

Instance Method Details

#config_file_newer_than_instance?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/xen/slice.rb', line 113

def config_file_newer_than_instance?
  instance && config_file.updated_at > instance.start_time 
end

#create_backup(options = {}) ⇒ Object



87
88
89
# File 'lib/xen/slice.rb', line 87

def create_backup(options = {})
  Xen::Backup.create(name, :options)
end

#create_image(*args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xen/slice.rb', line 28

def create_image(*args)
  options = args.extract_options!.stringify_keys
  
  # Load default values for options that have not been set
  options.reverse_merge! Xen::XenToolsConf.find.to_hash 
  
  # Set some derived options
  options.reverse_merge! 'hostname' => name # Name host after this slice
  options['dhcp'] = true unless options['ip']
  options['accounts'].to_i == 1 ? options['accounts'] = true : options.delete('accounts')
  options['swap'] ||= options['memory'].to_i * 2
  if options['root_pass']
    options['role'] = 'passwd'
    options['role-args'] = options['root_pass']
  end
  unless [nil,''].include?(options['tarball'])
    options['install-method'] = 'tar'
    options['install-source'] = options['tarball']
    options.delete('dist')
  end
  
  args = %w(hostname dist memory size
            force boot
            role role-args roledir
            dir lvm mirror 
            ip mac netmask broadcast gateway dhcp
            swap
            accounts admins cache config fs image image-dev initrd 
            keep kernel modules output install hooks partitions 
            passwd tar-cmd extension swap-dev noswap ide arch 
            install-method install-source template evms)
  # Remove options that are not in allowed argument list
  options.keys.each { |key| options.delete(key) unless args.include?(key) }
  
  Xen::Command.create_image(options)
end

#first_ipObject



95
96
97
# File 'lib/xen/slice.rb', line 95

def first_ip
  config_file.vifs.first.ip if config_file and config_file.vifs
end

#instanceObject

Cache Xen instance info to reduce system calls to xm command. It still needs to be checked regularly as operations like shutdown and create can take a while.



68
69
70
71
72
73
74
75
# File 'lib/xen/slice.rb', line 68

def instance
  if @instance_cache_expires > Time.now
    @instance
  else
    @instance_cache_expires = Time.now + Xen::INSTANCE_OBJECT_LIFETIME
    @instance = Xen::Instance.find(@name) if @name
  end
end

#ipObject

Primary IP



126
127
128
# File 'lib/xen/slice.rb', line 126

def ip
  config_file.vifs[0].ip
end

#root_diskObject



121
122
123
# File 'lib/xen/slice.rb', line 121

def root_disk
  config_file.vbds.detect { |vbd| vbd.name == "#{name}-disk" } unless config_file.nil?
end

#running?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/xen/slice.rb', line 99

def running?
  self.instance ? true : false
end

#saveObject



117
118
119
# File 'lib/xen/slice.rb', line 117

def save
 @config_file.save
end

#startObject



103
104
105
106
# File 'lib/xen/slice.rb', line 103

def start
  Xen::Instance.create(@name)
  @instance = Xen::Instance.find(@name)
end

#stateObject



91
92
93
# File 'lib/xen/slice.rb', line 91

def state
  self.instance ? :running : :stopped
end

#stop(options = {}) ⇒ Object



108
109
110
111
# File 'lib/xen/slice.rb', line 108

def stop(options={})
  Xen::Instance.shutdown(@name, options)
  @instance = Xen::Instance.find(@name)
end