Class: ContainerManagerAdapter::Vserver

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

Overview

Container adapter for linux-vserver. Encapsulates all vserver specific command line wrapping

Instance Method Summary (collapse)

Instance Method Details

- (Hash) container(name)

Returns information for a single container

Parameters:

  • name (String)

    The container name

Returns:

  • (Hash)

    Hash with information about the container



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 121

def container(name)
  data = {}

  data[:name] = name
  data[:state] = translate_state(state(name))
  data[:ip_address] = ip_addr(name)
  data[:cpu_usage] = 0
  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] = 'vserver'

  data
end

- (Array) containers

Lists all available containers

Returns:

  • (Array)


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

def containers
  container_list = []

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

  container_list
end

- (String) create_container(name, ip_address, disk_size_gb, memory_limit_mb, cpu_core_count)

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 for no limit

Returns:

  • (String)

    CLI output

Raises:

  • (RuntimeError)


149
150
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
190
191
192
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 149

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

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

    new_context = highest_context() + 1
    cmd = $vserver_cmd_create.gsub('[name]', name).gsub('[ip_address]', ip_address).gsub('[context]', new_context.to_s)

    create_result = Open3.capture3(cmd)

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

    if create_result[2].exitstatus != 0
      raise RuntimeError, 'command did not exit with status 0'
    end

    # memory limit
    memory_limit_bytes = memory_limit_mb.to_i * 1024 * 1024
    page_size = `#{$page_size_cmd}`.to_i
    memory_limit_pages = memory_limit_bytes / page_size

    write_config_file(name, '/rlimits/rss.hard', memory_limit_pages.to_s)

    if cpu_core_count != 0
      # cpu core limit
      write_config_file(name, '/cgroup/cpuset.cpus', cpuset)
    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

- (String) delete(name)

Deletes a container with the given name

Parameters:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:

  • (RuntimeError)


104
105
106
107
108
109
110
111
112
113
114
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 104

def delete(name)
  res = Open3.capture3($vserver_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

- (Boolean) exist?(name)

Checks if a container with the given name exists

Parameters:

  • name (String)

    The container name

Returns:

  • (Boolean)

    Existing/not existing



199
200
201
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 199

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

- (Integer) free_cpu_core_count(resman)

Returns the amount of free cpu cores

Parameters:

Returns:

  • (Integer)

    The amount of free cpu cores



208
209
210
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 208

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

- (String) kill(name)

Kills a container with the given name

Parameters:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:

  • (RuntimeError)


93
94
95
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 93

def kill(name)
  return stop(name)
end

- (String) start(name)

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/vserver.rb', line 50

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

  if res[2].exitstatus == 0 && state(name) == 'RUNNING'
    $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

- (String) stop(name)

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
80
81
82
83
84
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 69

def stop(name)
  if state(name) == 'STOPPED'
    $logger.warn("container " + name + " could not be stopped, because it is not running")
    raise RuntimeError, 'container is not running'
  end

  res = Open3.capture3($vserver_cmd_stop.gsub('[name]', name))

  if res[2].exitstatus == 0 && state(name) == 'STOPPED'
    $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