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.



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

def initialize(*args)
  options = args.extract_options! # remove trailing hash (not used)
  @name = args.first
  @config = options[:config]
  @instance = options[:instance]
  @instance_cache_expires = Time.now
  @backups = Array(options[:backups])
end

Instance Attribute Details

#backupsObject

Returns the value of attribute backups.



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

def backups
  @backups
end

#configObject

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



46
47
48
# File 'lib/xen/slice.rb', line 46

def config
  @config
end

#imageObject

Returns the value of attribute image.



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

def image
  @image
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.all(options = {}) ⇒ Object



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

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

.find(*args) ⇒ Object



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

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

Instance Method Details

#config_newer_than_instance?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/xen/slice.rb', line 72

def config_newer_than_instance?
  instance && config.updated_at > instance.start_time 
end

#create_image(args) ⇒ Object



27
28
29
30
# File 'lib/xen/slice.rb', line 27

def create_image(args)
  args = hash.collect{|k,v| "#{k}=#{v}"}
  Xen::Command.create_image
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.



35
36
37
38
39
40
41
42
# File 'lib/xen/slice.rb', line 35

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

#running?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/xen/slice.rb', line 58

def running?
  self.instance ? true : false
end

#saveObject



76
77
78
# File 'lib/xen/slice.rb', line 76

def save
 @config.save
end

#startObject



62
63
64
65
# File 'lib/xen/slice.rb', line 62

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

#stateObject



54
55
56
# File 'lib/xen/slice.rb', line 54

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

#stopObject



67
68
69
70
# File 'lib/xen/slice.rb', line 67

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