class SyncWrap::Context

A single-thread execution context for a single Host.

Context implements much of the interface and behavior defined by Component, via use of the Shell and Rsync module mixins.

Constants

SH_OPT_KEYS

Attributes

host[R]

The current Host of this context

state[R]

A Hash-like interface of keys/values backed read-only by the host properties.

Public Class Methods

current() click to toggle source

Return the current (thread local) Context, or nil.

# File lib/syncwrap/context.rb, line 32
def current
  Thread.current[:syncwrap_context]
end
new( host, opts = {} ) click to toggle source

Construct given host and default_options to use for all sh and rput calls.

Calls superclass method
# File lib/syncwrap/context.rb, line 54
def initialize( host, opts = {} )
  @host = host
  @state = StateHash.new( host )
  reset_queue
  @queue_locked = false
  @default_options = opts

  super()
end
swap( ctx ) click to toggle source

Set the current Context to ctx and return any prior Context or nil.

# File lib/syncwrap/context.rb, line 38
def swap( ctx )
  old = current
  Thread.current[:syncwrap_context] = ctx
  old
end

Public Instance Methods

capture( command, opts = {} ) click to toggle source

See SyncWrap::Component#capture for interface details.

# File lib/syncwrap/context.rb, line 130
def capture( command, opts = {} )
  opts = @default_options.merge( coalesce: false, dryrun: false ).merge( opts )
  flush
  capture_shell( command, opts )
end
check_install?() click to toggle source

Return any value of :check_install set in constructed default options.

# File lib/syncwrap/context.rb, line 87
def check_install?
  @default_options[ :check_install ]
end
dryrun?() click to toggle source

Return true if being executed, by constructed default options, in dryrun mode.

# File lib/syncwrap/context.rb, line 76
def dryrun?
  @default_options[ :dryrun ]
end
find_source( src, opts = {} ) click to toggle source

Returns the path to the the specified src, first found in :sync_paths option. Returns nil if not found.

# File lib/syncwrap/context.rb, line 181
def find_source( src, opts = {} )
  opts = @default_options.merge( opts )
  resolve_source( src, Array( opts[ :sync_paths ] ) )
end
flush() click to toggle source

See SyncWrap::Component#flush for interface details

# File lib/syncwrap/context.rb, line 115
def flush
  if @queued_cmd.length > 0
    begin
      if @queue_locked
        raise NestingError, 'Queue at flush: ' + @queued_cmd.join( '\n' )
      end
      run_shell( @queued_cmd, @queued_opts )
    ensure
      reset_queue
    end
  end
  nil
end
rput( *args ) click to toggle source

See SyncWrap::Component#rput for interface details.

# File lib/syncwrap/context.rb, line 137
def rput( *args )
  opts = @default_options
  opts = opts.merge( args.pop ) if args.last.is_a?( Hash )
  opts = opts.merge( coalesce: false )

  flush

  srcs, target = expand_implied_target( args )

  srcs = resolve_sources( srcs, Array( opts[ :sync_paths ] ) )

  changes = []

  if opts[:erb_process] != false
    sdirs, sfiles =   srcs.partition { |src| File.directory?( src ) }
    serbs, sfiles = sfiles.partition { |src| src =~ /\.erb$/ }
    plains = sdirs + sfiles #might not have/is not templates
    maybes = sdirs + serbs  #might have/is templates

    if maybes.empty?
      changes = rsync( plains, target, opts ) unless plains.empty?
    else
      if ssh_host_name == 'localhost' && opts[ :user ]
        # tmpdir needs to be visable to alt. opts[ :user ]
        opts[ :tmpdir_mode ] = 0755
      end
      process_templates( maybes, opts ) do |processed|
        unless processed.empty? || plains.empty?
          opts = opts.dup
          opts[ :excludes ] = Array( opts[ :excludes ] ) + [ '*.erb' ]
        end
        new_srcs = plains + processed
        changes = rsync( new_srcs, target, opts ) unless new_srcs.empty?
      end
    end
  else
    changes = rsync( srcs, target, opts ) unless srcs.empty?
  end

  changes
end
sh( command, opts = {} ) { || ... } click to toggle source

See SyncWrap::Component#sh for interface details

# File lib/syncwrap/context.rb, line 92
def sh( command, opts = {} )
  opts = @default_options.merge( opts )
  close = opts.delete( :close )

  flush unless sh_opts_equal?( @queued_opts, opts ) #may still be a no-op

  @queued_cmd << command
  @queued_opts = opts

  if close
    prev, @queue_locked = @queue_locked, true
  end

  begin
    yield if block_given?
    @queued_cmd << close if close
  ensure
    @queue_locked = prev if close
  end
  nil
end
verbose?() click to toggle source

Return true if :verbose is set in constructed default options.

# File lib/syncwrap/context.rb, line 81
def verbose?
  @default_options[ :verbose ]
end
with() { || ... } click to toggle source

Set (thread local) current context to self, yield to block, then flush and reset the context.

# File lib/syncwrap/context.rb, line 66
def with
  prior = Context.swap( self )
  yield
  flush
ensure
  Context.swap( prior )
end