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



149
150
151
152
153
154
155
156
157
# File 'lib/wf_node_api/container_manager.rb', line 149

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)


31
32
33
34
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
# File 'lib/wf_node_api/container_manager.rb', line 31

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:



173
174
175
176
177
178
179
# File 'lib/wf_node_api/container_manager.rb', line 173

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

  @adapter.container(name)
end

#containersArray

Lists all available containers

Returns:

  • (Array)


162
163
164
# File 'lib/wf_node_api/container_manager.rb', line 162

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

Returns:

  • (String)

    CLI output

Raises:



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/wf_node_api/container_manager.rb', line 267

def create_container(name, ip_address, disk_size_gb, memory_limit_mb, cpu_core_count)
  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)
end

#delete(name) ⇒ String

Deletes a container with the given name

Parameters:

  • name (String)

    The container name

Returns:

  • (String)

    CLI output

Raises:



246
247
248
249
250
251
252
# File 'lib/wf_node_api/container_manager.rb', line 246

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



186
187
188
# File 'lib/wf_node_api/container_manager.rb', line 186

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:



230
231
232
233
234
235
236
# File 'lib/wf_node_api/container_manager.rb', line 230

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:



198
199
200
201
202
203
204
# File 'lib/wf_node_api/container_manager.rb', line 198

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:



214
215
216
217
218
219
220
# File 'lib/wf_node_api/container_manager.rb', line 214

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

  @adapter.stop(name)
end