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.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
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
forceparameter 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.
-
#processes ⇒ Array
Get container processes.
-
#restart ⇒ Hash
Restart container.
-
#running? ⇒ Boolean
Check if container is running.
-
#start ⇒ Hash
Start 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
10 11 12 |
# File 'lib/lxc/container.rb', line 10 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 |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
5 6 7 |
# File 'lib/lxc/container.rb', line 5 def pid @pid end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
4 5 6 |
# File 'lib/lxc/container.rb', line 4 def state @state end |
Instance Method Details
#clone_from(source) ⇒ Boolean
Create a new container from an existing container
187 188 189 190 191 192 193 194 195 |
# File 'lib/lxc/container.rb', line 187 def clone_from(source) raise ContainerError, "Container already exists." if exists? unless self.class.new(source).exists? raise ContainerError, "Source container does not exist." end LXC.run('clone', '-o', source, '-n', name) exists? end |
#clone_to(target) ⇒ LXC::Container
Clone to a new container from self
174 175 176 177 178 179 180 181 182 |
# File 'lib/lxc/container.rb', line 174 def clone_to(target) raise ContainerError, "Container does not exist." unless exists? if self.class.new(target).exists? raise ContainerError, "New container already exists." end LXC.run('clone', '-o', name, '-n', target) self.class.new target end |
#cpu_shares ⇒ Integer
Get container cpu shares
113 114 115 116 |
# File 'lib/lxc/container.rb', line 113 def cpu_shares result = run('cgroup', "cpu.shares").strip result.empty? ? nil : result.to_i end |
#cpu_usage ⇒ Float
Get container cpu usage in seconds
120 121 122 123 |
# File 'lib/lxc/container.rb', line 120 def cpu_usage result = run('cgroup', "cpuacct.usage").strip result.empty? ? nil : Float('%.4f' % (result.to_i / 1E9)) end |
#create(path) ⇒ Boolean
Create a new container
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/lxc/container.rb', line 138 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_path = "/usr/lib/lxc/templates/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', '-n', name, '-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.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/lxc/container.rb', line 204 def destroy(force=false) raise ContainerError, "Container does not exist." unless exists? if running? if force # This will force stop and destroy container automatically run('destroy', '-f') else raise ContainerError, "Container is running. Stop it first or use force=true" end else run('destroy') end !exists? end |
#exists? ⇒ Boolean
Check if container exists
32 33 34 |
# File 'lib/lxc/container.rb', line 32 def exists? LXC.run('ls').split("\n").uniq.include?(name) end |
#freeze ⇒ Hash
Freeze container
77 78 79 80 |
# File 'lib/lxc/container.rb', line 77 def freeze run('freeze') status end |
#frozen? ⇒ Boolean
Check if container is frozen
44 45 46 |
# File 'lib/lxc/container.rb', line 44 def frozen? status[:state] == 'FROZEN' end |
#memory_limit ⇒ Integer
Get container memory limit in bytes
107 108 109 |
# File 'lib/lxc/container.rb', line 107 def memory_limit run('cgroup', 'memory.limit_in_bytes').strip.to_i end |
#memory_usage ⇒ Integer
Get container memory usage in bytes
101 102 103 |
# File 'lib/lxc/container.rb', line 101 def memory_usage run('cgroup', 'memory.usage_in_bytes').strip.to_i end |
#processes ⇒ Array
Get container processes
127 128 129 130 131 132 133 |
# File 'lib/lxc/container.rb', line 127 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
70 71 72 73 |
# File 'lib/lxc/container.rb', line 70 def restart stop start end |
#running? ⇒ Boolean
Check if container is running
38 39 40 |
# File 'lib/lxc/container.rb', line 38 def running? status[:state] == 'RUNNING' end |
#start ⇒ Hash
Start container
56 57 58 59 |
# File 'lib/lxc/container.rb', line 56 def start run('start', '-d') status end |
#status ⇒ Hash
Get current status of container
23 24 25 26 27 28 |
# File 'lib/lxc/container.rb', line 23 def status str = LXC.run('info', '-n', name) @state = str.scan(/^state:\s+([\w]+)$/).flatten.first @pid = str.scan(/^pid:\s+(-?[\d]+)$/).flatten.first {:state => @state, :pid => @pid} end |
#stop ⇒ Hash
Stop container
63 64 65 66 |
# File 'lib/lxc/container.rb', line 63 def stop run('stop') status end |
#stopped? ⇒ Boolean
Check if container is stopped?
50 51 52 |
# File 'lib/lxc/container.rb', line 50 def stopped? exists? && status[:state] == 'STOPPED' end |
#to_hash ⇒ Hash
Get container attributes hash
16 17 18 19 |
# File 'lib/lxc/container.rb', line 16 def to_hash status {'name' => name, 'state' => state, 'pid' => pid} end |
#unfreeze ⇒ Hash
Unfreeze container
84 85 86 87 |
# File 'lib/lxc/container.rb', line 84 def unfreeze run('unfreeze') status end |
#wait(state) ⇒ Object
Wait for container to change status
91 92 93 94 95 96 97 |
# File 'lib/lxc/container.rb', line 91 def wait(state) if !LXC::Shell.valid_state?(state) raise ArgumentError, "Invalid container state: #{state}" end run('wait', '-s', state) end |