Class: Xen::Slice
- Inherits:
-
Object
- Object
- Xen::Slice
- Defined in:
- lib/xen/slice.rb
Instance Attribute Summary collapse
-
#backups ⇒ Object
Returns the value of attribute backups.
-
#config_file ⇒ Object
XXX We’re assuming other processes aren’t going to edit config_files This is reasonable in simple cases.
-
#image ⇒ Object
Returns the value of attribute image.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #config_file_newer_than_instance? ⇒ Boolean
- #create_backup(options = {}) ⇒ Object
- #create_image(*args) ⇒ Object
- #first_ip ⇒ Object
-
#initialize(*args) ⇒ Slice
constructor
A new instance of Slice.
-
#instance ⇒ Object
Cache Xen instance info to reduce system calls to xm command.
-
#ip ⇒ Object
Primary IP.
- #root_disk ⇒ Object
- #running? ⇒ Boolean
- #save ⇒ Object
- #start ⇒ Object
- #state ⇒ Object
- #stop(options = {}) ⇒ Object
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) = args. @name = [:name] @config_file = [:config_file] @instance = [:instance] @instance_cache_expires = Time.now @backups = Array([:backups]) end |
Instance Attribute Details
#backups ⇒ Object
Returns the value of attribute backups.
3 4 5 |
# File 'lib/xen/slice.rb', line 3 def backups @backups end |
#config_file ⇒ Object
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 |
#image ⇒ Object
Returns the value of attribute image.
3 4 5 |
# File 'lib/xen/slice.rb', line 3 def image @image end |
#name ⇒ Object
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(={}) self.find(:all, ) end |
.find(*args) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/xen/slice.rb', line 5 def self.find(*args) = args. case args.first when :all then Xen::ConfigFile.find(:all, ).collect { |config_file| new :name => config_file.name } when :running then Xen::Instance.find(:all, ).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
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( = {}) 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) = args..stringify_keys # Load default values for options that have not been set .reverse_merge! Xen::XenToolsConf.find.to_hash # Set some derived options .reverse_merge! 'hostname' => name # Name host after this slice ['dhcp'] = true unless ['ip'] ['accounts'].to_i == 1 ? ['accounts'] = true : .delete('accounts') ['swap'] ||= ['memory'].to_i * 2 if ['root_pass'] ['role'] = 'passwd' ['role-args'] = ['root_pass'] end unless [nil,''].include?(['tarball']) ['install-method'] = 'tar' ['install-source'] = ['tarball'] .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 .keys.each { |key| .delete(key) unless args.include?(key) } Xen::Command.create_image() end |
#first_ip ⇒ Object
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 |
#instance ⇒ Object
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 |
#ip ⇒ Object
Primary IP
126 127 128 |
# File 'lib/xen/slice.rb', line 126 def ip config_file.vifs[0].ip end |
#root_disk ⇒ Object
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
99 100 101 |
# File 'lib/xen/slice.rb', line 99 def running? self.instance ? true : false end |
#save ⇒ Object
117 118 119 |
# File 'lib/xen/slice.rb', line 117 def save @config_file.save end |
#start ⇒ Object
103 104 105 106 |
# File 'lib/xen/slice.rb', line 103 def start Xen::Instance.create(@name) @instance = Xen::Instance.find(@name) end |
#state ⇒ Object
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(={}) Xen::Instance.shutdown(@name, ) @instance = Xen::Instance.find(@name) end |