Class: LXC::Runner::SSH

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (SSH) initialize(options = {})

Returns a new instance of SSH

Parameters:

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

    Options hash.

Options Hash (options):

  • :use_sudo (Boolean) — default: false

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

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

    The SSH object to use for remote connections.

  • :sftp (ZTK::SSH, Net::SFTP) — default: nil

    The SFTP object to use for remote file options.



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(options={})
  @ui       = (options[:ui]       || ZTK::UI.new)
  @use_sudo = (options[:use_sudo] || true)
  @ssh      = (options[:ssh])
  @sftp     = (options[: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.

Overloads:

  • - (Boolean) use_sudo=(value)

    Sets if all executed commands should be prefixed with sudo.

    Parameters:

    • value (Boolean)
  • - (Boolean) use_sudo

    Gets if we are prefixing all executed commands with sudo.

Returns:

  • (Boolean)

    Returns true if we are prefixing commands with “sudo”; otherwise false.



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.

Parameters:

  • args (Array)

    Additional command-line arguments.

Returns:

  • (Array<String>)

    Stripped output text of the executed command.



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.

Parameters:

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

    The options hash.

Options Hash (options):

  • :target (String)

    The target file on the remote host.

  • :chown (String)

    A user:group representation of who to change ownership of the target file to (i.e. ‘root:root’).

  • :chmod (String)

    An octal file mode which to set the target file to (i.e. ‘0755’).

Returns:

  • (Boolean)

    True if successful.



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(options={}, &block)
  if !@sftp.is_a?(ZTK::SSH)
    # Massage for Net::SSH
    flags  = (options[:flags] || 'w')
    mode   = (options[:mode]  || nil)

    target = options[:target]
    chown  = options[:chown]
    chmod  = options[: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(options, &block)
  end
end

- (String) inspect

Provides a concise string representation of the class

Returns:

  • (String)


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

  tags = Array.new
  tags << "host=#{@hostname.inspect}"
  tags << "use_sudo=#{@use_sudo.inspect}"
  tags = tags.join(' ')

  "#<#{self.class} #{tags}>"
end