Class: ContainerManagerAdapter::Lxc

Inherits:
Object
  • Object
show all
Defined in:
lib/wf_node_api/container_manager_adapter/lxc.rb

Overview

Container adapter for LXC. Encapsulates all LXC specific command line wrapping

Instance Method Summary collapse

Instance Method Details

#container(name) ⇒ Hash

Returns information for a single container

Parameters:

  • The container name

Returns:

  • Hash with information about the container



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 128

def container(name)
  data = {}

  data[:name] = name
  data[:state] = translate_state(state(name))
  data[:ip_address] = ip_addr(name)
  data[:cpu_cores] = assigned_cpu_cores(name).count
  data[:memory_limit_bytes] = memory_limit(name).to_i
  data[:memory_usage_bytes] = memory_usage(name).to_i
  data[:disk_space_gb] = 0
  data[:disk_usage_gb] = 0
  data[:container_type] = 'lxc'

  data
end

#containersArray

Lists all available containers

Returns:



37
38
39
40
41
42
43
44
45
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 37

def containers
  container_list = []

  self.container_names.each do |item|
    container_list << container(item)
  end

  container_list
end

#create_container(name, ip_address, disk_size_gb, memory_limit_mb, cpu_core_count, template) ⇒ String

Creates a container with the given parameters

Parameters:

  • The container name

  • A valid IPv4 address

  • The disk size in GB

  • The memory limit in MB

  • Amount of Vcores to assign. 0 if no limit should be set

  • Name of the template to use

Returns:

  • CLI output

Raises:



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 156

def create_container(name, ip_address, disk_size_gb, memory_limit_mb, cpu_core_count, template)
  output = ''

  begin
    if cpu_core_count != 0
      cpuset = self.generate_cpu_set(cpu_core_count, ResourceManager.new('linux'))
    end

    if false == $lxc_cmd_create.has_key?(template)
      raise ArgumentError, "template does not exist"
    end

    create_result = Open3.capture3($lxc_cmd_create[template].gsub('[name]', name))

    output += create_result[0]
    output += create_result[1]

    config_path = $lxc_container_config_path.gsub('[name]', name)

    open(config_path, 'a') do |f|
      f << "lxc.network.ipv4=#{ip_address}/#{$lxc_ip_netmask}\n"
      f << "lxc.start.auto=1\n"
      f << "lxc.start.delay=5\n"

      if cpu_core_count.to_i != 0
        f << "lxc.cgroup.cpuset.cpus=#{cpuset}\n"
      end

      if memory_limit_mb.to_i != 0
        f << "lxc.cgroup.memory.limit_in_bytes=#{memory_limit_mb.to_s}M\n"
      end
    end

    $logger.info("creation of container " + name + " successful")
    return output.strip
  rescue => e
    $logger.warn("container " + name + " could not be created, rolling back...")

    # rollback
    delete(name) if exist?(name)

    output += e.message
    raise RuntimeError, output.strip
  end
end

#delete(name) ⇒ String

Deletes a container with the given name

Parameters:

  • The container name

Returns:

  • CLI output

Raises:



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 111

def delete(name)
  res = Open3.capture3($lxc_cmd_destroy.gsub('[name]', name))

  if res[1].empty? && res[2].exitstatus == 0
    $logger.info("container " + name + " successfully deleted")
    return res[0].strip
  end

  $logger.warn("container" + name + " could not be deleted")
  raise RuntimeError, res[1].strip
end

#exist?(name) ⇒ Boolean

Checks if a container with the given name exists

Parameters:

  • The container name

Returns:

  • Existing/not existing



207
208
209
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 207

def exist?(name)
  self.container_names().include?(name)
end

#free_cpu_core_count(resman) ⇒ Integer

Returns the amount of free cpu cores

Parameters:

  • A ResourceManager instance

Returns:

  • The amount of free cpu cores



216
217
218
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 216

def free_cpu_core_count(resman)
  self.free_cpu_cores(resman).count
end

#kill(name) ⇒ String

Kills a container with the given name

Parameters:

  • The container name

Returns:

  • CLI output

Raises:



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 92

def kill(name)
  res = Open3.capture3($lxc_cmd_kill.gsub('[name]', name))

  if res[1].empty? && res[2].exitstatus == 0
    $logger.info("container " + name + " successfully killed")
    return res[0].strip
  end

  $logger.warn("container " + name + " could not be killed")
  raise RuntimeError, res[1].strip
end

#start(name) ⇒ String

Starts a container with the given name

Parameters:

  • The container name

Returns:

  • CLI output

Raises:



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 54

def start(name)
  res = Open3.capture3($lxc_cmd_start.gsub('[name]', name))

  if res[1].empty? && res[2].exitstatus == 0
    $logger.info("container " + name + " successfully started")
    return res[0].strip
  end

  $logger.warn("container " + name + " could not be started")
  raise RuntimeError, res[1].strip
end

#stop(name) ⇒ String

Stops a container with the given name

Parameters:

  • The container name

Returns:

  • CLI output

Raises:



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 73

def stop(name)
  res = Open3.capture3($lxc_cmd_stop.gsub('[name]', name))

  if res[1].empty? && res[2].exitstatus == 0
    $logger.info("container " + name + " successfully stopped")
    return res[0].strip
  end

  $logger.warn("container " + name + " could not be stopped")
  raise RuntimeError, res[1].strip
end

#supported_templatesHash

Returns a list of supported templates

Returns:

  • List of supported templates



223
224
225
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 223

def supported_templates
  $lxc_cmd_create.keys
end