class Chef::Provisioning::Transport

Constants

DEFAULT_TIMEOUT

Public Instance Methods

available?() click to toggle source
# File lib/chef/provisioning/transport.rb, line 53
def available?
  raise "available? not overridden on #{self.class}"
end
config() click to toggle source

Config hash, including :log_level and :logger as keys

# File lib/chef/provisioning/transport.rb, line 58
def config
  raise "config not overridden on #{self.class}"
end
disconnect() click to toggle source
# File lib/chef/provisioning/transport.rb, line 49
def disconnect
  raise "disconnect not overridden on #{self.class}"
end
download_file(path, local_path) click to toggle source
# File lib/chef/provisioning/transport.rb, line 37
def download_file(path, local_path)
  IO.write(local_path, read_file(path))
end
execute(command, options = {}) click to toggle source

Execute a program on the remote host.

Arguments

command: command to run. May be a shell-escaped string or a pre-split

array containing [PROGRAM, ARG1, ARG2, ...].

options: hash of options, including but not limited to:

:timeout => NUM_SECONDS - time to wait before program finishes
            (throws an exception otherwise).  Set to nil or 0 to
            run with no timeout.  Defaults to 15 minutes.
:stream => BOOLEAN - true to stream stdout and stderr to the console.
:stream => BLOCK - block to stream stdout and stderr to
           (block.call(stdout_chunk, stderr_chunk))
:stream_stdout => FD - FD to stream stdout to (defaults to IO.stdout)
:stream_stderr => FD - FD to stream stderr to (defaults to IO.stderr)
:read_only => BOOLEAN - true if command is guaranteed not to
              change system state (useful for Docker)
# File lib/chef/provisioning/transport.rb, line 24
def execute(command, options = {})
  raise "execute not overridden on #{self.class}"
end
make_url_available_to_remote(local_url) click to toggle source
# File lib/chef/provisioning/transport.rb, line 45
def make_url_available_to_remote(local_url)
  raise "make_url_available_to_remote not overridden on #{self.class}"
end
read_file(path) click to toggle source

TODO: make exceptions for these instead of just returning nil / silently failing

# File lib/chef/provisioning/transport.rb, line 29
def read_file(path)
  raise "read_file not overridden on #{self.class}"
end
upload_file(local_path, path) click to toggle source
# File lib/chef/provisioning/transport.rb, line 41
def upload_file(local_path, path)
  write_file(path, IO.read(local_path))
end
write_file(path, content) click to toggle source
# File lib/chef/provisioning/transport.rb, line 33
def write_file(path, content)
  raise "write_file not overridden on #{self.class}"
end

Protected Instance Methods

execute_timeout(options) click to toggle source
# File lib/chef/provisioning/transport.rb, line 95
def execute_timeout(options)
  options.has_key?(:timeout) ? options[:timeout] : DEFAULT_TIMEOUT
end
stream_chunk(options, stdout_chunk, stderr_chunk) click to toggle source

Helper to implement stdout/stderr streaming in execute

# File lib/chef/provisioning/transport.rb, line 65
def stream_chunk(options, stdout_chunk, stderr_chunk)
  if options[:stream].is_a?(Proc)
    options[:stream].call(stdout_chunk, stderr_chunk)
  else
    if stdout_chunk
      if options.has_key?(:stream_stdout)
        stream = options[:stream_stdout]
      elsif options[:stream] || config[:log_level] == :debug
        stream = config[:stdout] || STDOUT
      end

      stream.print stdout_chunk if stream
    end

    if stderr_chunk
      if options.has_key?(:stream_stderr)
        stream = options[:stream_stderr]
      elsif options[:stream] || config[:log_level] == :debug
        stream = config[:stderr] || STDERR
      end

      stream.print stderr_chunk if stream
    end
  end
end
with_execute_timeout(options, &block) click to toggle source
# File lib/chef/provisioning/transport.rb, line 91
def with_execute_timeout(options, &block)
  Timeout::timeout(execute_timeout(options), &block)
end