Class: LXC
- Inherits:
-
Object
- Object
- LXC
- 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
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)
-
- (LXC::Runner) runner
The runner we will use to execute all LXC commands.
Instance Method Summary (collapse)
-
- (Array<String>) checkconfig(*args)
Linux container configuration check.
-
- (LXC::Config) config
LXC configuration class.
-
- (LXC::Container) container(name)
Initialize container object.
-
- (Array<LXC::Container>) containers
Current containers.
-
- (Array<String>) exec(*args)
Linux container command execution wrapper.
-
- (Boolean) exists?(name)
Check if a container exists.
-
- (LXC) initialize(options = {})
constructor
A new instance of LXC.
-
- (String) inspect
Provides a concise string representation of the class.
-
- (Boolean) installed?
Linux containers installed?.
-
- (Array<String>) ls(*args)
List of containers.
-
- (Array<String>) ps(*args)
Linux container processes.
-
- (String) version(*args)
Linux container version.
Constructor Details
- (LXC) initialize(options = {})
Returns a new instance of LXC
38 39 40 41 |
# File 'lib/lxc.rb', line 38 def initialize(={}) @ui = ([:ui] || ZTK::UI.new) @runner = ([: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.
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.
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
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.
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.
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.
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.
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
162 163 164 165 166 167 168 169 |
# File 'lib/lxc.rb', line 162 def inspect = Array.new << "version=#{self.version.inspect}" << "runner=#{@runner.inspect}" if @runner = .join(' ') "#<LXC #{}>" end |
- (Boolean) installed?
Linux containers installed?
Checks the output of "lxc-checkconfig" to see if it returns a 'command not found' error.
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.
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.
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.
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 |