Class: ContainerManager

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

Overview

Manager for containers, meta class

Instance Method Summary collapse

Constructor Details

#initialize(container_type) ⇒ ContainerManager

Initializes the manager with the matching adapter

Parameters:

  • container_type (String)

    Container adapter

Raises:

  • (ArgumentError)

    if the container type is invalid



153
154
155
156
157
158
159
160
161
# File 'lib/wf_node_api/container_manager.rb', line 153

def initialize(container_type)
  if container_type == 'lxc'
    @adapter = ContainerManagerAdapter::Lxc.new
  elsif container_type == 'vserver'
    @adapter = ContainerManagerAdapter::Vserver.new
  else
    raise ArgumentError, 'invalid container type'
  end
end

Instance Method Details

#checkObject

Checks the installation with a simple test procedure

Raises:

  • (StandardError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/wf_node_api/container_manager.rb', line 35

def check
  name = (0...8).map { (65 + rand(26)).chr }.join
  puts "==> Random name for the container: #{name}"

  # containers should return an array
  puts "==> Listing containers"
  res = containers()
  #puts res.inspect

  raise 'containers() failed' unless res.is_a?(Array)
  puts "==> PASS"

  # container should not work
  puts "==> Getting container info on non-existant container (should not work)"

  begin
    res = container(name)
    raise 'container() failed'
  rescue => e
    puts "==> PASS"
  end

  # delete should not work
  puts "==> Delete non-existing container (should not work)"

  begin
    res = delete(name)
    raise 'delete() failed'
  rescue => e
    puts "==> PASS"
  end

  # start should not work
  puts "==> Starting non-existing container (should not work)"

  begin
    res = start(name)
    raise 'start() failed'
  rescue => e
    puts "==> PASS"
  end

  # stop should not work
  puts "==> Stopping non-existing container (should not work)"

  begin
    res = stop(name)
    raise 'stop() failed'
  rescue => e
    puts "==> PASS"
  end

  # kill should not work
  puts "==> Killing non-existing container (should not work)"

  begin
    res = kill(name)
    raise 'kill() failed'
  rescue => e
    puts "==> PASS"
  end

  # create should work
  puts "==> Create container"
  create_container(name, '1.2.3.4', 1, 64, 1)
  puts "==> PASS"

  # start should work
  puts "==> Starting container"
  start(name)
  puts "==> PASS"

  # stop should work
  puts "==> Stopping container"
  stop(name)
  puts "==> PASS"

  # kill should not work
  puts "==> Killing stopped container (should not work)"

  begin
    res = kill(name)
    raise 'kill() failed'
  rescue => e
    puts "==> PASS"
  end

  # stop should not work
  puts "==> Stopping stopped container (should not work)"

  begin
    res = stop(name)
    raise 'stop() failed'
  rescue => e
    puts "==> PASS"
  end

  # start should work
  puts "==> Starting container"
  start(name)
  puts "==> PASS"

  # kill should work
  puts "==> Killing container"
  kill(name)
  puts "==> PASS"

  # delete should work
  puts "==> Deleting container"
  delete(name)
  puts "==> PASS"
end

#container(name) ⇒ Hash

Returns information for a single container

Parameters:

  • name (String)

    The container name

Returns:

  • (Hash)

    Hash with information about the container

Raises:



177
178
179
180
181
182
183
# File 'lib/wf_node_api/container_manager.rb', line 177

def container(name)
  if !@adapter.exist?(name)
    raise ::NotFoundError
  end

  @adapter.container(name)
end

#containersArray

Lists all available containers

Returns:

  • (Array)


166
167
168
# File 'lib/wf_node_api/container_manager.rb', line 166

def containers
  @adapter.containers
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:

  • 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

  • template (String)

    Name of the template to use

Returns:

  • (String)

    CLI output

Raises:



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/wf_node_api/container_manager.rb', line 279

def create_container(name, ip_address, disk_size_gb, memory_limit_mb, cpu_core_count, template)
  disk_size_gb = disk_size_gb.to_i
  memory_limit_mb = memory_limit_mb.to_i
  cpu_core_count = cpu_core_count.to_i

  if name.nil? || name.empty? || (/^([A-Za-z0-9_]+)$/ =~ name).nil?
    raise ArgumentError, 'container name is invalid'
  end

  if ip_address.nil? || ip_address.empty? || (/^#{Resolv::IPv4::Regex}$/ =~ ip_address).nil?
    raise ArgumentError, 'the submitted ip address is not a valid ipv4 address'
  end

  if @adapter.exist?(name)
    raise ::NotFoundError
  end

  @adapter.create_container(name, ip_address, disk_size_gb, memory_limit_mb, cpu_core_count, template)
end

#delete(name) ⇒ String

Deletes a container with the given name

Parameters:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:



257
258
259
260
261
262
263
# File 'lib/wf_node_api/container_manager.rb', line 257

def delete(name)
  if !@adapter.exist?(name)
    raise ::NotFoundError
  end

  @adapter.delete(name)
end

#free_cpu_core_count(resman) ⇒ Integer

Returns the amount of free cpu cores

Parameters:

Returns:

  • (Integer)

    The amount of free cpu cores



197
198
199
# File 'lib/wf_node_api/container_manager.rb', line 197

def free_cpu_core_count(resman)
  @adapter.free_cpu_core_count(resman)
end

#kill(name) ⇒ String

Kills a container with the given name

Parameters:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:



241
242
243
244
245
246
247
# File 'lib/wf_node_api/container_manager.rb', line 241

def kill(name)
  if !@adapter.exist?(name)
    raise ::NotFoundError
  end

  @adapter.kill(name)
end

#start(name) ⇒ String

Starts a container with the given name

Parameters:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:



209
210
211
212
213
214
215
# File 'lib/wf_node_api/container_manager.rb', line 209

def start(name)
  if !@adapter.exist?(name)
    raise ::NotFoundError
  end

  @adapter.start(name)
end

#stop(name) ⇒ String

Stops a container with the given name

Parameters:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:



225
226
227
228
229
230
231
# File 'lib/wf_node_api/container_manager.rb', line 225

def stop(name)
  if !@adapter.exist?(name)
    raise ::NotFoundError
  end

  @adapter.stop(name)
end

#supported_templatesHash

Returns a list of supported templates

Returns:

  • (Hash)

    List of supported templates



188
189
190
# File 'lib/wf_node_api/container_manager.rb', line 188

def supported_templates
  @adapter.supported_templates
end