lib/symphony/tasks/ssh.rb
changeset 11 ffa70066522c
parent 7 4321943b8db5
child 17 5db18679edcf
equal deleted inserted replaced
10:7013280e62fa 11:ffa70066522c
    21 ###    opts:    (optional) Explicit SSH client options
    21 ###    opts:    (optional) Explicit SSH client options
    22 ###    user:    (optional) The user to connect as (defaults to root)
    22 ###    user:    (optional) The user to connect as (defaults to root)
    23 ###    key:     (optional) The path to an SSH private key
    23 ###    key:     (optional) The path to an SSH private key
    24 ###
    24 ###
    25 ###
    25 ###
    26 ### Additionally, this class responds to the 'symphony_ssh' configurability
    26 ### Additionally, this class responds to the 'symphony.ssh' configurability
    27 ### key.  Currently, you can set the 'path' argument, which is the
    27 ### key.  Currently, you can set the 'path' argument, which is the
    28 ### full path to the local ssh binary (defaults to '/usr/bin/ssh') and
    28 ### full path to the local ssh binary (defaults to '/usr/bin/ssh') and
    29 ### override the default ssh user, key, and client opts.
    29 ### override the default ssh user, key, and client opts.
    30 ###
    30 ###
    31 ### Textual output of the command is stored in the @output instance variable.
    31 ### Textual output of the command is stored in the @output instance variable.
    45 ###        end
    45 ###        end
    46 ###    end
    46 ###    end
    47 ###
    47 ###
    48 class Symphony::Task::SSH < Symphony::Task
    48 class Symphony::Task::SSH < Symphony::Task
    49 	extend Configurability
    49 	extend Configurability
    50 	config_key :symphony_ssh
       
    51 
    50 
    52 	# SSH default options.
    51 	# The default set of ssh command line flags.
    53 	#
    52 	#
    54 	CONFIG_DEFAULTS = {
    53 	DEFAULT_SSH_OPTS = %w[
    55 		:path => '/usr/bin/ssh',
    54 			-e none
    56 		:opts => [
    55 			-T
    57 			'-e', 'none',
    56 			-x
    58 			'-T',
    57 			-q
    59 			'-x',
    58 			-o CheckHostIP=no
    60 			'-q',
    59 			-o BatchMode=yes
    61 			'-o', 'CheckHostIP=no',
    60 			-o StrictHostKeyChecking=no
    62 			'-o', 'BatchMode=yes',
    61 	]
    63 			'-o', 'StrictHostKeyChecking=no'
       
    64 		],
       
    65 		:user => 'root',
       
    66 		:key  => nil
       
    67 	}
       
    68 
    62 
    69 	# SSH "informative" stdout output that should be cleaned from the
    63 	# SSH "informative" stdout output that should be cleaned from the
    70 	# command output.
    64 	# command output.
    71 	SSH_CLEANUP = %r/Warning: no access to tty|Thus no job control in this shell/
    65 	SSH_CLEANUP = %r/Warning: no access to tty|Thus no job control in this shell/
    72 
    66 
    73 	class << self
    67 
       
    68 	# Configurability API
       
    69 	#
       
    70 	configurability( :symphony__ssh ) do
       
    71 
    74 		# The full path to the ssh binary.
    72 		# The full path to the ssh binary.
    75 		attr_reader :path
    73 		setting :path, default: '/usr/bin/ssh'
    76 
    74 
    77 		# A default set of ssh client options when connecting
    75 		# The default user to use when connecting.
       
    76 		setting :user, default: 'root'
       
    77 
       
    78 		# A default Array of ssh client options when connecting
    78 		# to remote hosts.
    79 		# to remote hosts.
    79 		attr_reader :opts
    80 		setting :opts, default: DEFAULT_SSH_OPTS do |val|
    80 
    81 			Array( val )
    81 		# The default user to use when connecting.  If unset, 'root' is used.
    82 		end
    82 		attr_reader :user
       
    83 
    83 
    84 		# An absolute path to a password-free ssh private key.
    84 		# An absolute path to a password-free ssh private key.
    85 		attr_reader :key
    85 		setting :key
    86 	end
       
    87 
       
    88 	### Configurability API.
       
    89 	###
       
    90 	def self::configure( config=nil )
       
    91 		config = Symphony::Task::SSH.defaults.merge( config || {} )
       
    92 		@path  = config.delete( :path )
       
    93 		@opts  = config.delete( :opts )
       
    94 		@user  = config.delete( :user )
       
    95 		@key   = config.delete( :key )
       
    96 		super
       
    97 	end
    86 	end
    98 
    87 
    99 
    88 
   100 	### Perform the ssh connection, passing the command to the pipe
    89 	### Perform the ssh connection, passing the command to the pipe
   101 	### and retreiving any output from the remote end.
    90 	### and retreiving any output from the remote end.