class NixAdm::Command

Attributes

port[R]
sys[R]
throw_on_fail[RW]
user[R]

Public Class Methods

new(host, port=22, user='root', options: {}) click to toggle source
Calls superclass method NixAdm::Status::new
# File src/lib/nixadm/pipeline.rb, line 391
def initialize(host, port=22, user='root', options: {})
  super()

  @sys  = NixAdm::Pipeline.new()
  @host = host
  @user = user
  @port = port
  @throw_on_fail = true

  @sys.debug = options[:debug] || false
end

Public Instance Methods

debug() click to toggle source
# File src/lib/nixadm/pipeline.rb, line 403
def debug()
  return @sys.debug
end
debug=(value) click to toggle source
# File src/lib/nixadm/pipeline.rb, line 407
def debug=(value)
  @sys.debug = value
end
exec(*args)

Some derivative classes have their own separate notion of run(). Make exec() and alias to help with this.

Alias for: run
resolveCommand(command, host_to_run_on = nil) click to toggle source

This command wraps a command in an SSH call. It checks for the existence of the host_to_run_on argument as well as the value in @host member (which takes precedence) and if either exist, it prepends the command with 'ssh' to run command remotely on that machine.

# File src/lib/nixadm/pipeline.rb, line 438
def resolveCommand(command, host_to_run_on = nil)

  # Convert array of commands into pipeline then into string
  if command.is_a?(Array)
    command = command.join(' | ')
  end

  # You have to put quotes around command otherwise shell might
  # misinterpret. For example, if we have:
  #
  #    command = "zfs send data/jails/cron@1 | zfs receive root@storage -d data"
  #
  # then without double quotes around command it would be interpreted as
  #
  #    ssh root@jan zfs send data/jails/cron@1 | zfs receive root@storage -d data
  #
  # This would set up the pipline is run locally on this the current machine
  # the script is running and the "zfs send" output would be piped here and
  # then from here on to storage. This is a waste of bandwidth. By putting
  # command in double quotes it will be as follows:
  #
  #    ssh root@jan "zfs send data/jails/cron@1 | zfs receive root@storage -d data"
  #
  # Here the entire pipeline is run remotely on jan and is piped directly to
  # storage. This is what we want.

  if not host_to_run_on.nil?
    command = %Q{ssh -p #{@port} #{@user}@#{host_to_run_on} "#{command}"}
  end

  if not @host.nil?
    command = %Q{ssh -p #{@port} #{@user}@#{@host} "#{command}"}
  end

  return command.gsub("\n", ' ').strip()
end
run(*args) { |line| ... } click to toggle source
# File src/lib/nixadm/pipeline.rb, line 411
def run(*args)
  if @sys.run(*args) == false
    failure -1, "Command failed: #{args}"
    if @throw_on_fail == true
      raise @status.msg
    else
      return status()
    end
  end

  if block_given?
    @sys.out.each_line do |line|
      yield line
    end
  end

  return success()
end
Also aliased as: exec