Class: LXC

Inherits:
Object
  • Object
show all
Defined in:
lib/lxc.rb,
lib/lxc/config.rb,
lib/lxc/runner.rb,
lib/lxc/version.rb,
lib/lxc/container.rb,
lib/lxc/runners/ssh.rb,
lib/lxc/runners/shell.rb

Overview

Top-Level LXC Class

Author:

Defined Under Namespace

Classes: Config, ConfigError, Container, ContainerError, LXCError, Runner, RunnerError

Constant Summary

REGEX_VERSION =

RegEx pattern for extracting the LXC Version from the "lxc-version" command output.

/^lxc version:\s+([\w\W]+)$/
VERSION =

LXC Gem Version

"0.6.0"

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (LXC) initialize(options = {})

Returns a new instance of LXC

Parameters:

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :use_sudo (Boolean) — default: false

    Whether or not to prefix all commands with ‘sudo’.

  • :use_ssh (Net::SSH, ZTK::SSH, nil) — default: nil

    Whether or not to execute all commands remotely via an SSH connection.



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

def initialize(options={})
  @ui       = (options[:ui] || ZTK::UI.new)
  @runner   = (options[:runner] || LXC::Runner::Shell.new(:ui => @ui))
end

Instance Attribute Details

- (LXC::Runner) runner=(value) - (LXC::Runner) runner

The runner we will use to execute all LXC commands.

Overloads:

Returns:

  • (LXC::Runner)

    Returns the instance of the runner we are using.



27
28
29
# File 'lib/lxc.rb', line 27

def runner
  @runner
end

Instance Method Details

- (Array<String>) checkconfig(*args)

Linux container configuration check

Runs the "lxc-checkconfig" command.

Parameters:

  • args (Array)

    Additional command-line arguments.

Returns:

  • (Array<String>)

    Output text of the “lxc-checkconfig” command.



126
127
128
# File 'lib/lxc.rb', line 126

def checkconfig(*args)
  ZTK::ANSI.uncolor(self.exec("lxc-checkconfig", *args)).split("\n")
end

- (LXC::Config) config

LXC configuration class

Gets the LXC configuration class object

Returns:

  • (LXC::Config)

    Returns the LXC configuration object.



48
49
50
# File 'lib/lxc.rb', line 48

def config
  @config ||= LXC::Config.new(self, "/etc/lxc/lxc.conf")
end

- (LXC::Container) container(name)

Initialize container object

Initalizes an LXC::Container class for the supplied container name.

Parameters:

  • name (String)

    The container name to initalize.

Returns:



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

def container(name)
  LXC::Container.new(:lxc => self, :name => name)
end

- (Array<LXC::Container>) containers

Current containers

Initalizes an LXC::Container object for all containers and returns them in an Array.

Returns:



68
69
70
71
72
73
# File 'lib/lxc.rb', line 68

def containers
  container_names = self.ls
  container_names.map do |container_name|
    LXC::Container.new(:lxc => self, :name => container_name)
  end
end

- (Array<String>) exec(*args)

Linux container command execution wrapper

Runs the supplied LXC command. The first element in the "args" splat is the command to be execute, the rest of the elements are treated as command line arguments.

If use_sudo is true then all commands will be prefix with "sudo". If use_ssh is non-nil then all commands will be execute via the assigned Net::SSH Session.

Parameters:

  • args (Array)

    Additional command-line arguments.

Returns:

  • (Array<String>)

    Stripped output text of the executed command.



156
157
158
# File 'lib/lxc.rb', line 156

def exec(*args)
  @runner.exec(*args)
end

- (Boolean) exists?(name)

Check if a container exists

Checks the container name list to see if the name supplied is an existing container.

Parameters:

  • name (String)

    The name of the container to check.

Returns:

  • (Boolean)

    Returns true of the container exists, false otherwise.



92
93
94
# File 'lib/lxc.rb', line 92

def exists?(name)
  self.ls(%(-1)).include?(name)
end

- (String) inspect

Provides a concise string representation of the class

Returns:

  • (String)


162
163
164
165
166
167
168
169
# File 'lib/lxc.rb', line 162

def inspect
  tags = Array.new
  tags << "version=#{self.version.inspect}"
  tags << "runner=#{@runner.inspect}" if @runner
  tags = tags.join(' ')

  "#<LXC #{tags}>"
end

- (Boolean) installed?

Linux containers installed?

Checks the output of "lxc-checkconfig" to see if it returns a 'command not found' error.

Returns:

  • (Boolean)

    True if LXC is installed; false otherwise.



136
137
138
139
140
141
142
# File 'lib/lxc.rb', line 136

def installed?
  if !!(self.checkconfig.join =~ /command not found/)
    false
  else
    true
  end
end

- (Array<String>) ls(*args)

List of containers

Runs the "lxc-ls" command.

Parameters:

  • args (Array)

    Additional command-line arguments.

Returns:

  • (Array<String>)

    A list of container names.



81
82
83
# File 'lib/lxc.rb', line 81

def ls(*args)
  self.exec("lxc-ls", *args).split("\n").join(' ').split.uniq
end

- (Array<String>) ps(*args)

Linux container processes

Runs the "lxc-ps" command.

Parameters:

  • args (Array)

    Additional command-line arguments.

Returns:

  • (Array<String>)

    Output text of the “lxc-ps” command.



102
103
104
# File 'lib/lxc.rb', line 102

def ps(*args)
  self.exec("lxc-ps", *args).split("\n")
end

- (String) version(*args)

Linux container version

Runs the "lxc-version" command.

Parameters:

  • args (Array)

    Additional command-line arguments.

Returns:

  • (String)

    The installed version of LXC. Returns nil if lxc-version is not found.



113
114
115
116
117
118
# File 'lib/lxc.rb', line 113

def version(*args)
  result = self.exec("lxc-version", *args).scan(REGEX_VERSION).flatten.compact
  result.first.strip
rescue
  nil
end