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



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

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)


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

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, template)

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

  • template (String)

    Name of the template to use

Returns:

  • (String)

    CLI output

Raises:

  • (RuntimeError)


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
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 154

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

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

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

    new_context = highest_context() + 1
    cmd = $vserver_cmd_create[template].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
    if memory_limit_mb.to_i != 0
        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)
    end

    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)


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

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



210
211
212
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 210

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



219
220
221
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 219

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)


97
98
99
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 97

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)


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

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)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 73

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

- (Hash) supported_templates

Returns a list of supported templates

Returns:

  • (Hash)

    List of supported templates



226
227
228
# File 'lib/wf_node_api/container_manager_adapter/vserver.rb', line 226

def supported_templates
  $vserver_cmd_create.keys
end