Class: LXC::Runner::Shell
- Inherits:
-
Object
- Object
- LXC::Runner::Shell
- Defined in:
- lib/lxc/runners/shell.rb
Instance Attribute Summary (collapse)
-
- (Boolean) use_sudo
Controls if sudo is prefixed on all executed commands.
Instance Method Summary (collapse)
-
- (Array<String>) exec(*args)
Linux container command execution wrapper.
-
- (Boolean) file(name, options = {}, &block)
File I/O Wrapper.
-
- (Shell) initialize(options = {})
constructor
A new instance of Shell.
-
- (String) inspect
Provides a concise string representation of the class.
Constructor Details
- (Shell) initialize(options = {})
Returns a new instance of Shell
25 26 27 28 29 30 |
# File 'lib/lxc/runners/shell.rb', line 25 def initialize(={}) @hostname = Socket.gethostname.split('.').first.strip @ui = ([:ui] || ZTK::UI.new) @use_sudo = ([:use_sudo] || true) end |
Instance Attribute Details
- (Boolean) use_sudo=(value) - (Boolean) use_sudo
Controls if sudo is prefixed on all executed commands.
20 21 22 |
# File 'lib/lxc/runners/shell.rb', line 20 def use_sudo @use_sudo end |
Instance Method Details
- (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.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/lxc/runners/shell.rb', line 44 def exec(*args) command = args.shift arguments = Array.new arguments << %(sudo) if (@use_sudo == true) arguments << command arguments << args arguments = arguments.flatten.compact.join(' ') output = Array.new begin ::ZTK::PTY.spawn(arguments) do |reader, writer, pid| while (buffer = reader.readpartial(1024)) output << buffer end end rescue EOFError # NOOP end output.join.strip end |
- (Boolean) file(name, options = {}, &block)
File I/O Wrapper
This method renders the supplied content to the file named name on the LXC host.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/lxc/runners/shell.rb', line 80 def file(name, ={}, &block) flags = ([:flags] || 'w') mode = ([:mode] || nil) target = [:target] chown = [:chown] chmod = [:chmod] target.nil? and raise SSHError, "You must supply a target file!" !block_given? and raise SSHError, "You must supply a block!" File.open(target, flags, mode) do |file| yield(file) file.respond_to?(:flush) and file.flush end chown.nil? or self.exec(%(chown -v #{chown} #{target})) chmod.nil? or self.exec(%(chmod -v #{chmod} #{target})) end |
- (String) inspect
Provides a concise string representation of the class
102 103 104 105 106 107 108 109 |
# File 'lib/lxc/runners/shell.rb', line 102 def inspect = Array.new << "host=#{@hostname.inspect}" << "use_sudo=#{@use_sudo.inspect}" = .join(' ') "#<#{self.class} #{}>" end |