Class: LXC::Container
- Inherits:
-
Object
- Object
- LXC::Container
- Defined in:
- lib/lxc/container.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#clone_from(source) ⇒ Boolean
Create a new container from an existing container.
-
#clone_to(target) ⇒ LXC::Container
Clone to a new container from self.
-
#cpu_shares ⇒ Integer
Get container cpu shares.
-
#cpu_usage ⇒ Float
Get container cpu usage in seconds.
-
#create(path) ⇒ Boolean
Create a new container.
-
#destroy(force = false) ⇒ Boolean
Destroy the container If container is running and ‘force` parameter is true it will be stopped first.
-
#exists? ⇒ Boolean
Check if container exists.
-
#freeze ⇒ Hash
Freeze container.
-
#frozen? ⇒ Boolean
Check if container is frozen.
-
#initialize(name) ⇒ LXC::Container
constructor
Initialize a new LXC::Container instance.
-
#memory_limit ⇒ Integer
Get container memory limit in bytes.
-
#memory_usage ⇒ Integer
Get container memory usage in bytes.
-
#pid ⇒ Integer
Get PID of the container.
-
#processes ⇒ Array
Get container processes.
-
#restart ⇒ Hash
Restart container.
-
#running? ⇒ Boolean
Check if container is running.
-
#start ⇒ Hash
Start container.
-
#state ⇒ String
Get state of the container.
-
#status ⇒ Hash
Get current status of container.
-
#stop ⇒ Hash
Stop container.
-
#stopped? ⇒ Boolean
Check if container is stopped.
-
#to_hash ⇒ Hash
Get container attributes hash.
-
#unfreeze ⇒ Hash
Unfreeze container.
-
#wait(state) ⇒ Object
Wait for container to change status.
Constructor Details
#initialize(name) ⇒ LXC::Container
Initialize a new LXC::Container instance
8 9 10 |
# File 'lib/lxc/container.rb', line 8 def initialize(name) @name = name end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/lxc/container.rb', line 3 def name @name end |
Instance Method Details
#clone_from(source) ⇒ Boolean
Create a new container from an existing container
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/lxc/container.rb', line 198 def clone_from(source) raise ContainerError, "Container already exists." if exists? unless LXC.container(source).exists? raise ContainerError, "Source container does not exist." end run('clone', '-o', source) exists? end |
#clone_to(target) ⇒ LXC::Container
Clone to a new container from self
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/lxc/container.rb', line 184 def clone_to(target) raise ContainerError, "Container does not exist." unless exists? if LXC.container(target).exists? raise ContainerError, "New container already exists." end LXC.run('clone', '-o', name, '-n', target) LXC.container(target) end |
#cpu_shares ⇒ Integer
Get container cpu shares
122 123 124 125 |
# File 'lib/lxc/container.rb', line 122 def cpu_shares result = run('cgroup', "cpu.shares").to_s.strip result.empty? ? nil : result.to_i end |
#cpu_usage ⇒ Float
Get container cpu usage in seconds
129 130 131 132 |
# File 'lib/lxc/container.rb', line 129 def cpu_usage result = run('cgroup', "cpuacct.usage").to_s.strip result.empty? ? nil : Float('%.4f' % (result.to_i / 1E9)) end |
#create(path) ⇒ Boolean
Create a new container
147 148 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 |
# File 'lib/lxc/container.rb', line 147 def create(path) raise ContainerError, "Container already exists." if exists? if path.is_a?(Hash) args = "-n #{name}" if !!path[:config_file] unless File.exists?(path[:config_file]) raise ArgumentError, "File #{path[:config_file]} does not exist." end args += " -f #{path[:config_file]}" end if !!path[:template] template_dir = path[:template_dir] || '/usr/lib/lxc/templates' template_path = File.join(template_dir,"lxc-#{path[:template]}") unless File.exists?(template_path) raise ArgumentError, "Template #{path[:template]} does not exist." end args += " -t #{path[:template]} " end args += " -B #{path[:backingstore]}" if !!path[:backingstore] args += " -- #{path[:template_options].join(' ')}".strip if !!path[:template_options] LXC.run('create', args) exists? else raise ArgumentError, "File #{path} does not exist." unless File.exists?(path) LXC.run('create', '-f', path) exists? end end |
#destroy(force = false) ⇒ Boolean
Destroy the container If container is running and ‘force` parameter is true it will be stopped first. Otherwise it will raise exception.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/lxc/container.rb', line 216 def destroy(force=false) unless exists? raise ContainerError, "Container does not exist." end if running? if force == true stop else raise ContainerError, "Container is running. Stop it first or use force=true" end end run('destroy') !exists? end |
#exists? ⇒ Boolean
Check if container exists
41 42 43 |
# File 'lib/lxc/container.rb', line 41 def exists? LXC.run('ls').split("\n").uniq.include?(name) end |
#freeze ⇒ Hash
Freeze container
86 87 88 89 |
# File 'lib/lxc/container.rb', line 86 def freeze run('freeze') status end |
#frozen? ⇒ Boolean
Check if container is frozen
53 54 55 |
# File 'lib/lxc/container.rb', line 53 def frozen? status.state == 'frozen' end |
#memory_limit ⇒ Integer
Get container memory limit in bytes
116 117 118 |
# File 'lib/lxc/container.rb', line 116 def memory_limit run('cgroup', 'memory.limit_in_bytes').strip.to_i end |
#memory_usage ⇒ Integer
Get container memory usage in bytes
110 111 112 |
# File 'lib/lxc/container.rb', line 110 def memory_usage run('cgroup', 'memory.usage_in_bytes').strip.to_i end |
#pid ⇒ Integer
Get PID of the container
35 36 37 |
# File 'lib/lxc/container.rb', line 35 def pid status.pid end |
#processes ⇒ Array
Get container processes
136 137 138 139 140 141 142 |
# File 'lib/lxc/container.rb', line 136 def processes raise ContainerError, "Container is not running" if !running? str = run('ps', '--', '-eo pid,user,%cpu,%mem,args').strip lines = str.split("\n") ; lines.delete_at(0) lines.map { |l| parse_process_line(l) } end |
#restart ⇒ Hash
Restart container
79 80 81 82 |
# File 'lib/lxc/container.rb', line 79 def restart stop ; start status end |
#running? ⇒ Boolean
Check if container is running
47 48 49 |
# File 'lib/lxc/container.rb', line 47 def running? status.state == 'running' end |
#start ⇒ Hash
Start container
65 66 67 68 |
# File 'lib/lxc/container.rb', line 65 def start run('start', '-d') status end |
#state ⇒ String
Get state of the container
29 30 31 |
# File 'lib/lxc/container.rb', line 29 def state status.state end |
#status ⇒ Hash
Get current status of container
20 21 22 23 24 25 |
# File 'lib/lxc/container.rb', line 20 def status output = run('info') result = output.scan(/^state:\s+([\w]+)|pid:\s+(-?[\d]+)$/).flatten LXC::Status.new(result.first, result.last) end |
#stop ⇒ Hash
Stop container
72 73 74 75 |
# File 'lib/lxc/container.rb', line 72 def stop run('stop') status end |
#stopped? ⇒ Boolean
Check if container is stopped
59 60 61 |
# File 'lib/lxc/container.rb', line 59 def stopped? exists? && status.state == 'stopped' end |
#to_hash ⇒ Hash
Get container attributes hash
14 15 16 |
# File 'lib/lxc/container.rb', line 14 def to_hash status.to_hash.merge('name' => name) end |
#unfreeze ⇒ Hash
Unfreeze container
93 94 95 96 |
# File 'lib/lxc/container.rb', line 93 def unfreeze run('unfreeze') status end |
#wait(state) ⇒ Object
Wait for container to change status
100 101 102 103 104 105 106 |
# File 'lib/lxc/container.rb', line 100 def wait(state) if !LXC::Shell.valid_state?(status.state) raise ArgumentError, "Invalid container state: #{state}" end run('wait', '-s', state) end |