lib/testlab/container.rb in testlab-0.0.3 vs lib/testlab/container.rb in testlab-0.0.4
- old
+ new
@@ -12,139 +12,104 @@
belongs_to :node, :class_name => 'TestLab::Node'
attribute :provisioner
attribute :config
+ attribute :domain
+
+ attribute :user
+ attribute :keys
+
attribute :interfaces
attribute :distro
attribute :release
attribute :arch
attribute :persist
+
+ autoload :Actions, 'testlab/container/actions'
+ autoload :Generators, 'testlab/container/generators'
+ autoload :Lifecycle, 'testlab/container/lifecycle'
+ autoload :LXC, 'testlab/container/lxc'
+ autoload :Network, 'testlab/container/network'
+ autoload :Status, 'testlab/container/status'
+
+ include TestLab::Container::Actions
+ include TestLab::Container::Generators
+ include TestLab::Container::Lifecycle
+ include TestLab::Container::LXC
+ include TestLab::Container::Network
+ include TestLab::Container::Status
+
+
def initialize(*args)
super(*args)
@ui = TestLab.ui
@provisioner = self.provisioner.new(self.config) if !self.provisioner.nil?
end
- def status
- interfaces = self.interfaces.collect{ |key, value| "#{key}:#{value[:name]}:#{value[:ip]}" }.join(', ')
+################################################################################
- {
- :id => self.id,
- :state => self.state,
- :distro => self.distro,
- :release => self.release,
- :interfaces => interfaces,
- :provisioner => self.provisioner,
- :node_id => self.node.id
- }
- end
+ # Does the container exist?
+ def exists?
+ @ui.logger.debug { "Container Exists?: #{self.id} " }
- # Our LXC Container class
- def lxc
- @lxc ||= self.node.lxc.container(self.id)
+ self.lxc.exists?
end
- # Create the container
- def create
- @ui.logger.debug { "Container Create: #{self.id} " }
+################################################################################
- self.arch ||= detect_arch
-
- self.lxc.config.clear
-
- self.lxc.config['lxc.utsname'] = self.id
-
- self.interfaces.each do |network, network_config|
- n = Hash.new
- n['lxc.network.type'] = :veth
- n['lxc.network.flags'] = :up
- n['lxc.network.link'] = TestLab::Network.first(network).bridge
- n['lxc.network.name'] = (network_config[:name] || "eth0")
- n['lxc.network.hwaddr'] = (network_config[:mac] || generate_mac)
- n['lxc.network.ipv4'] = (network_config[:ip] || generate_ip)
- self.lxc.config.networks << n
- end
-
- self.lxc.config.save
-
- self.lxc.create(*create_args)
+ # SSH to the container
+ def ssh(options={})
+ self.node.container_ssh(self, options)
end
- # Destroy the container
- def destroy
- @ui.logger.debug { "Container Destroy: #{self.id} " }
-
- self.lxc.destroy
+ def ip
+ self.primary_interface.last[:ip].split('/').first
end
- # Start the container
- def up
- @ui.logger.debug { "Container Up: #{self.id} " }
-
- self.lxc.start
+ # Returns the CIDR of the container
+ def cidr
+ self.primary_interface.last[:ip].split('/').last.to_i
end
- # Stop the container
- def down
- @ui.logger.debug { "Container Down: #{self.id} " }
+ def ptr
+ octets = self.ip.split('.')
- self.lxc.stop
- end
+ result = case self.cidr
+ when 0..7 then
+ octets[-4,4]
+ when 8..15 then
+ octets[-3,3]
+ when 16..23 then
+ octets[-2,2]
+ when 24..31 then
+ octets[-1,1]
+ end
- # Reload the container
- def reload
- @ui.logger.debug { "Container Reload: #{self.id} " }
-
- self.down
- self.up
+ result.reverse.join('.')
end
- # Does the container exist?
- def exists?
- @ui.logger.debug { "Container Exists?: #{self.id} " }
-
- self.lxc.exists?
+ def primary_interface
+ if self.interfaces.any?{ |i,c| c[:primary] == true }
+ self.interfaces.find{ |i,c| c[:primary] == true }
+ else
+ self.interfaces.first
+ end
end
- # State of the container
- def state
- self.lxc.state
- end
+ class << self
-################################################################################
+ def domains
+ self.all.map(&:domain).compact
+ end
- # Container Callback: after_create
- def after_create
- @ui.logger.debug { "Container Callback: After Create: #{self.id} " }
end
- # Container Callback: after_up
- def after_up
- @ui.logger.debug { "Container Callback: After Up: #{self.id} " }
-
- self.create
- self.up
- end
-
- # Container Callback: before_down
- def before_down
- @ui.logger.debug { "Container Callback: Before Down: #{self.id} " }
-
- self.down
- self.destroy
- end
-
- # Container Callback: before_destroy
- def before_destroy
- @ui.logger.debug { "Container Callback: Before Destroy: #{self.id} " }
- end
-
################################################################################
# Method missing handler
def method_missing(method_name, *method_args)
@ui.logger.debug { "CONTAINER METHOD MISSING: #{method_name.inspect}(#{method_args.inspect})" }
@@ -152,66 +117,9 @@
if (defined?(@provisioner) && @provisioner.respond_to?(method_name))
@provisioner.send(method_name, [self, *method_args].flatten)
else
super(method_name, *method_args)
end
- end
-
-################################################################################
- private
-################################################################################
-
- # Returns arguments for lxc-create based on our distro
- def create_args
- case self.distro.downcase
- when "ubuntu" then
- %W(-f /etc/lxc/#{self.id} -t #{self.distro} -- --release #{self.release} --arch #{arch})
- when "fedora" then
- %W(-f /etc/lxc/#{self.id} -t #{self.distro} -- --release #{self.release})
- end
- end
-
- # Attempt to detect the architecture of the node our container is running on
- def detect_arch
- case self.distro.downcase
- when "ubuntu" then
- ((self.node.arch =~ /x86_64/) ? "amd64" : "i386")
- when "fedora" then
- ((self.node.arch =~ /x86_64/) ? "amd64" : "i686")
- end
- end
-
- def generate_ip
- octets = [ 192..192,
- 168..168,
- 0..254,
- 1..254 ]
- ip = Array.new
- for x in 1..4 do
- ip << octets[x-1].to_a[rand(octets[x-1].count)].to_s
- end
- ip.join(".")
- end
-
- def generate_mac
- digits = [ %w(0),
- %w(0),
- %w(0),
- %w(0),
- %w(5),
- %w(e),
- %w(0 1 2 3 4 5 6 7 8 9 a b c d e f),
- %w(0 1 2 3 4 5 6 7 8 9 a b c d e f),
- %w(5 6 7 8 9 a b c d e f),
- %w(3 4 5 6 7 8 9 a b c d e f),
- %w(0 1 2 3 4 5 6 7 8 9 a b c d e f),
- %w(0 1 2 3 4 5 6 7 8 9 a b c d e f) ]
- mac = ""
- for x in 1..12 do
- mac += digits[x-1][rand(digits[x-1].count)]
- mac += ":" if (x.modulo(2) == 0) && (x != 12)
- end
- mac
end
end
end