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
The current Host of this context
A Hash-like interface of keys/values backed read-only by the host properties.
Public Class Methods
Return the current (thread local) Context, or nil.
# File lib/syncwrap/context.rb, line 32 def current Thread.current[:syncwrap_context] end
Public Instance Methods
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
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
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
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
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
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
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
Return true if :verbose is set in constructed default options.
# File lib/syncwrap/context.rb, line 81 def verbose? @default_options[ :verbose ] end
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