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.



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

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)


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

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

#create_backup(options = {}) ⇒ Object



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

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
# 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['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



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

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.



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

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



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

def ip
  config_file.vifs[0].ip
end

#root_diskObject



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

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

#running?Boolean

Returns:

  • (Boolean)


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

def running?
  self.instance ? true : false
end

#saveObject



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

def save
 @config_file.save
end

#startObject



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

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

#stateObject



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

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

#stopObject



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

def stop
  Xen::Instance.shutdown(@name)
  @instance = Xen::Instance.find(@name)
end