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:

  • name (String)

    The container name

Returns:

  • (Hash)

    Hash with information about the container



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 124

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:

  • (Array)


33
34
35
36
37
38
39
40
41
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 33

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) ⇒ String

Creates a container with the given parameters

Parameters:

  • name (String)

    The container name

  • ip_address (String)

    A valid IPv4 address

  • disk_size_gb (Integer)

    The disk size in GB

  • memory_limit_mb (Integer)

    The memory limit in MB

  • cpu_core_count (Integer)

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

Returns:

  • (String)

    CLI output

Raises:

  • (RuntimeError)


151
152
153
154
155
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
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 151

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

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

    create_result = Open3.capture3($lxc_cmd_create.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 != 0
        f << "lxc.cgroup.cpuset.cpus=#{cpuset}\n"
      end

      f << "lxc.cgroup.memory.limit_in_bytes=#{memory_limit_mb.to_s}M\n"
    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:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:

  • (RuntimeError)


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 107

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:

  • name (String)

    The container name

Returns:

  • (Boolean)

    Existing/not existing



196
197
198
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 196

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

#free_cpu_core_count(resman) ⇒ Integer

Returns the amount of free cpu cores

Parameters:

Returns:

  • (Integer)

    The amount of free cpu cores



205
206
207
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 205

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

#kill(name) ⇒ String

Kills a container with the given name

Parameters:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:

  • (RuntimeError)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 88

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:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:

  • (RuntimeError)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 50

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:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:

  • (RuntimeError)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/wf_node_api/container_manager_adapter/lxc.rb', line 69

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