Class: LXC::Runner::SSH
- Inherits:
-
Object
- Object
- LXC::Runner::SSH
- Defined in:
- lib/lxc/runners/ssh.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(options = {}, &block)
File I/O Wrapper.
-
- (SSH) initialize(options = {})
constructor
A new instance of SSH.
-
- (String) inspect
Provides a concise string representation of the class.
Constructor Details
- (SSH) initialize(options = {})
Returns a new instance of SSH
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lxc/runners/ssh.rb', line 29 def initialize(={}) @ui = ([:ui] || ZTK::UI.new) @use_sudo = ([:use_sudo] || true) @ssh = ([:ssh]) @sftp = ([:sftp]) # If the @ssh object is an instance of ZTK::SSH then use it for our # SFTP object as well. if @ssh.is_a?(ZTK::SSH) @sftp = @ssh end @ssh.nil? and raise SSHError, "You must supply a ZTK::SSH or Net::SSH instance!" @sftp.nil? and raise SSHError, "You must supply a ZTK::SSH or Net::SFTP instance!" 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/ssh.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.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lxc/runners/ssh.rb', line 57 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 if @ssh.is_a?(ZTK::SSH) output << @ssh.exec(arguments, :silence => true, :ignore_exit_status => true).output else if @ssh.respond_to?(:exec!) output << @ssh.exec!(arguments) else raise SSHError, "The object you assigned to ssh does not respond to #exec!" end end output.join.strip end |
- (Boolean) file(options = {}, &block)
File I/O Wrapper
This method renders the supplied content to the file named name on the LXC host.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/lxc/runners/ssh.rb', line 93 def file(={}, &block) if !@sftp.is_a?(ZTK::SSH) # Massage for Net::SSH flags = ([:flags] || 'w') mode = ([:mode] || nil) target = [:target] chown = [:chown] chmod = [:chmod] @sftp.file.open(target, flags, mode) do |file| yield(file) end chown.nil? or self.exec(%(chown -v #{chown} #{target})) chmod.nil? or self.exec(%(chmod -v #{chmod} #{target})) else # Pass-through for ZTK:SSH @sftp.file(, &block) end end |
- (String) inspect
Provides a concise string representation of the class
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/lxc/runners/ssh.rb', line 117 def inspect if @hostname.nil? if @ssh.is_a?(ZTK::SSH) @hostname ||= @ssh.exec(%(hostname -s)).output.strip else if @ssh.respond_to?(:exec!) @hostname ||= @ssh.exec!(%(hostname -s)).strip else raise SSHError, "The object you assigned to ssh does not respond to #exec!" end end end = Array.new << "host=#{@hostname.inspect}" << "use_sudo=#{@use_sudo.inspect}" = .join(' ') "#<#{self.class} #{}>" end |