Module: TestLab::Container::Configuration

Included in:
TestLab::Container
Defined in:
lib/testlab/container/configuration.rb

Instance Method Summary collapse

Instance Method Details

#build_lxc_config(lxc_config) ⇒ Boolean

LXC Container Configuration

Builds the LXC container configuration data.

Returns:

  • (Boolean)

    True if successful.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/testlab/container/configuration.rb', line 25

def build_lxc_config(lxc_config)
  lxc_config.clear

  lxc_config['lxc.arch']    = self.arch
  lxc_config['lxc.utsname'] = self.fqdn

  self.mounts.nil? or self.mounts.flatten.compact.each do |mount|
    lxc_config['lxc.mount.entry'] = mount
  end

  unless self.aa_profile.nil?
    lxc_config['lxc.aa_profile'] = self.aa_profile
  end

  unless self.cap_drop.nil?
    lxc_config['lxc.cap.drop'] = [self.cap_drop].flatten.compact.map(&:downcase).join(' ')
  end

  lxc_config.networks = build_lxc_network_conf(self.interfaces)

  lxc_config.save

  true
end

#build_lxc_network_conf(interfaces) ⇒ Array<Hash>

LXC Network Configuration

Builds an array of hashes containing the lxc configuration options for our network interfaces.

Returns:

  • (Array<Hash>)

    An array of hashes defining the containers interfaces for use in configuring LXC.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/testlab/container/configuration.rb', line 57

def build_lxc_network_conf(interfaces)
  networks = Array.new

  interfaces.each do |interface|
    networks << Hash[
      'lxc.network.type'   => :veth,
      'lxc.network.flags'  => :up,
      'lxc.network.link'   => interface.network.bridge,
      'lxc.network.name'   => interface.name,
      'lxc.network.hwaddr' => interface.mac,
      'lxc.network.ipv4'   => "#{interface.ip}/#{interface.cidr} #{interface.netmask}"
    ]
    if (self.primary_interface == interface)
      networks.last.merge!('lxc.network.ipv4.gateway' => :auto)
    end
  end

  networks
end

#configureBoolean

Configure the container

Configures the LXC subsystem for the container.

Returns:

  • (Boolean)

    True if successful.



11
12
13
14
15
16
17
18
# File 'lib/testlab/container/configuration.rb', line 11

def configure
  self.domain ||= self.node.domain
  self.arch   ||= detect_arch

  build_lxc_config(self.lxc.config)

  true
end