Method: ContainerManagerAdapter::Vserver#create_container

Defined in:
lib/wf_node_api/container_manager_adapter/vserver.rb

#create_container(name, ip_address, disk_size_gb, memory_limit_mb, cpu_core_count, template) ⇒ 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 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