Class: LXC::Runner::Shell

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Shell) initialize(options = {})

Returns a new instance of Shell

Parameters:

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

    Options hash.

Options Hash (options):

  • :use_sudo (Boolean) — default: false

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



25
26
27
28
29
30
# File 'lib/lxc/runners/shell.rb', line 25

def initialize(options={})
  @hostname = Socket.gethostname.split('.').first.strip

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

Parameters:

  • args (Array)

    Additional command-line arguments.

Returns:

  • (Array<String>)

    Stripped output text of the executed command.



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.

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.



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, options={}, &block)
  flags  = (options[:flags] || 'w')
  mode   = (options[:mode]  || nil)

  target = options[:target]
  chown  = options[:chown]
  chmod  = options[: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

Returns:

  • (String)


102
103
104
105
106
107
108
109
# File 'lib/lxc/runners/shell.rb', line 102

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

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