Class: LXC::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/lxc/container.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ LXC::Container

Initialize a new LXC::Container instance

Parameters:

  • name (String)

    container name



10
11
12
# File 'lib/lxc/container.rb', line 10

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/lxc/container.rb', line 3

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



5
6
7
# File 'lib/lxc/container.rb', line 5

def pid
  @pid
end

#stateObject (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

Parameters:

  • source (String)

    name of existing container

Returns:

  • (Boolean)

Raises:



164
165
166
167
168
169
170
171
172
# File 'lib/lxc/container.rb', line 164

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

Parameters:

  • target (String)

    name of new container

Returns:

Raises:



151
152
153
154
155
156
157
158
159
# File 'lib/lxc/container.rb', line 151

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

#create(path) ⇒ Boolean

Create a new container

Parameters:

  • path (String)

    path to container config file or [Hash] options

Returns:

  • (Boolean)

Raises:



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/lxc/container.rb', line 116

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.

Parameters:

  • force (Boolean) (defaults to: false)

    force destruction

Returns:

  • (Boolean)

    true if container was destroyed

Raises:



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/lxc/container.rb', line 181

def destroy(force=false)
  raise ContainerError, "Container does not exist." unless exists?
  if running?
    if force
      # This will force stop and destroy container automatically
      LXC.run('destroy', '-n', '-f', name)
    else
      raise ContainerError, "Container is running. Stop it first or use force=true"
    end
  else
    LXC.run('destroy', '-n', name)
  end  
  !exists?
end

#exists?Boolean

Check if container exists

Returns:

  • (Boolean)


32
33
34
# File 'lib/lxc/container.rb', line 32

def exists?
  LXC.run('ls').split("\n").uniq.include?(name)
end

#freezeHash

Freeze container

Returns:

  • (Hash)

    container status hash



71
72
73
74
# File 'lib/lxc/container.rb', line 71

def freeze
  LXC.run('freeze', '-n', name)
  status
end

#frozen?Boolean

Check if container is frozen

Returns:

  • (Boolean)


44
45
46
# File 'lib/lxc/container.rb', line 44

def frozen?
  status[:state] == 'FROZEN'
end

#memory_limitInteger

Get container memory limit in bytes

Returns:

  • (Integer)


100
101
102
# File 'lib/lxc/container.rb', line 100

def memory_limit
  LXC.run('cgroup', '-n', name, 'memory.limit_in_bytes').strip.to_i
end

#memory_usageInteger

Get container memory usage in bytes

Returns:

  • (Integer)


94
95
96
# File 'lib/lxc/container.rb', line 94

def memory_usage
  LXC.run('cgroup', '-n', name, 'memory.usage_in_bytes').strip.to_i
end

#processesArray

Get container processes

Returns:

  • (Array)

    list of all processes

Raises:



106
107
108
109
110
111
# File 'lib/lxc/container.rb', line 106

def processes
  raise ContainerError, "Container is not running" if !running?
  str = LXC.run('ps', '-n', name, '--', '-eo pid,user,%cpu,%mem,args').strip
  lines = str.split("\n") ; lines.delete_at(0)
  lines.map { |l| parse_process_line(l) }
end

#restartHash

Restart container

Returns:

  • (Hash)

    container status hash



64
65
66
67
# File 'lib/lxc/container.rb', line 64

def restart
  stop
  start
end

#running?Boolean

Check if container is running

Returns:

  • (Boolean)


38
39
40
# File 'lib/lxc/container.rb', line 38

def running?
  status[:state] == 'RUNNING'
end

#startHash

Start container

Returns:

  • (Hash)

    container status hash



50
51
52
53
# File 'lib/lxc/container.rb', line 50

def start
  LXC.run('start', '-d', '-n', name)
  status
end

#statusHash

Get current status of container

Returns:

  • (Hash)

    hash with :state and :pid attributes



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

#stopHash

Stop container

Returns:

  • (Hash)

    container status hash



57
58
59
60
# File 'lib/lxc/container.rb', line 57

def stop
  LXC.run('stop', '-n', name)
  status
end

#to_hashHash

Get container attributes hash

Returns:

  • (Hash)


16
17
18
19
# File 'lib/lxc/container.rb', line 16

def to_hash
  status
  {'name' => name, 'state' => state, 'pid' => pid}
end

#unfreezeHash

Unfreeze container

Returns:

  • (Hash)

    container status hash



78
79
80
81
# File 'lib/lxc/container.rb', line 78

def unfreeze
  LXC.run('unfreeze', '-n', name)
  status
end

#wait(state) ⇒ Object

Wait for container to change status

Parameters:

  • state (String)

    state name



85
86
87
88
89
90
# File 'lib/lxc/container.rb', line 85

def wait(state)
  if !LXC::Shell.valid_state?(state)
    raise ArgumentError, "Invalid container state: #{state}"
  end
  LXC.run('wait', '-n', name, '-s', state)
end