Multiple changes. v0.3.0
authorMahlon E. Smith <mahlon@martini.nu>
Thu, 09 Jul 2020 15:05:21 -0700
changeset 17 5db18679edcf
parent 16 420037982f78
child 18 d629b9939df4
Multiple changes. - Migrate to gem.deps.rb from .gems - Bump dependency versions - Clear environment before connecting to remote server - Allow option for environment modifications - Place commands in args, rather than via the IO pipe. (won't spawn remote shell)
.gems
.hgignore
.ruby-version
.rvm.gems
Rakefile
gem.deps.rb
lib/symphony/tasks/ssh.rb
spec/symphony/tasks/ssh_spec.rb
--- a/.gems	Tue Apr 07 16:42:31 2020 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-configurability -v3.2.0
-symphony -v0.11.1
-inversion -v1.1.1
-net-ssh -v5.0.2
-net-sftp -v2.1.2
-rspec -v3.7.0
-simplecov -v0.16.1
-pry -v0.11.3
--- a/.hgignore	Tue Apr 07 16:42:31 2020 -0700
+++ b/.hgignore	Thu Jul 09 15:05:21 2020 -0700
@@ -7,4 +7,5 @@
 ^ChangeLog$
 ^docs/
 ^coverage/
+^gem.deps.rb.lock$
 Session.vim
--- a/.ruby-version	Tue Apr 07 16:42:31 2020 -0700
+++ b/.ruby-version	Thu Jul 09 15:05:21 2020 -0700
@@ -1,1 +1,1 @@
-2.6
+2.7
--- a/.rvm.gems	Tue Apr 07 16:42:31 2020 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-configurability -v2.1.2
-inversion -v0.12.3
-net-sftp -v2.1.2
-net-ssh -v2.9.0
-rake -v0.9.6
-rdoc -v4.0.0
-rdoc-generator-fivefish -v0.1.0
-rspec -v3.0.0.beta2
-rspec-core -v3.0.0.beta2
-rspec-expectations -v3.0.0.beta2
-rspec-mocks -v3.0.0.beta2
-rspec-support -v3.0.0.beta2
-simplecov -v0.7.1
-symphony -v0.6.0
--- a/Rakefile	Tue Apr 07 16:42:31 2020 -0700
+++ b/Rakefile	Thu Jul 09 15:05:21 2020 -0700
@@ -31,7 +31,7 @@
 	s.platform     = Gem::Platform::RUBY
 	s.summary      = "Base classes for using Symphony with ssh."
 	s.name         = 'symphony-ssh'
-	s.version      = '0.2.1'
+	s.version      = '0.3.0'
 	s.license      = 'BSD-3-Clause'
 	s.has_rdoc     = true
 	s.require_path = 'lib'
@@ -49,12 +49,12 @@
 
 	s.add_dependency 'configurability', [ '>= 3.2', '<= 4.99' ]
 	s.add_dependency 'symphony', '~> 0.13'
-	s.add_dependency 'inversion', '~> 1.2'
-	s.add_dependency 'net-ssh', '~> 5.0'
-	s.add_dependency 'net-sftp', '~> 2.1'
+	s.add_dependency 'inversion', '~> 1.3'
+	s.add_dependency 'net-ssh', '~> 6.0'
+	s.add_dependency 'net-sftp', '~> 3.0'
 
-	s.add_development_dependency 'rspec',     '~> 3.7'
-	s.add_development_dependency 'simplecov', '~> 0.16'
+	s.add_development_dependency 'rspec',     '~> 3.9'
+	s.add_development_dependency 'simplecov', '~> 0.18'
 end
 
 Gem::PackageTask.new( spec ) do |pkg|
@@ -119,4 +119,3 @@
 __END__
 lib/symphony/tasks/ssh.rb
 lib/symphony/tasks/sshscript.rb
-README.rdoc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gem.deps.rb	Thu Jul 09 15:05:21 2020 -0700
@@ -0,0 +1,16 @@
+# vim: set nosta noet ts=4 sw=4:
+source "https://rubygems.org/"
+
+gem 'configurability', '~> 4.0'
+gem 'symphony',        '~> 0.13'
+gem 'inversion',       '~> 1.3'
+gem 'net-ssh',         '~> 6.1'
+gem 'net-sftp',        '~> 3.0'
+
+group( :development ) do
+	gem 'rake',      '~> 13.0'
+	gem 'rspec',     '~> 3.9'
+	gem 'simplecov', '~> 0.18'
+	gem 'pry',       '~> 0.13'
+end
+
--- a/lib/symphony/tasks/ssh.rb	Tue Apr 07 16:42:31 2020 -0700
+++ b/lib/symphony/tasks/ssh.rb	Thu Jul 09 15:05:21 2020 -0700
@@ -19,6 +19,7 @@
 ###    command: (required) The command to run on the remote host
 ###    port:    (optional) The port to connect to (defaults to 22)
 ###    opts:    (optional) Explicit SSH client options
+###    env:     (optional) A hash of environment vars to set for the connection.
 ###    user:    (optional) The user to connect as (defaults to root)
 ###    key:     (optional) The path to an SSH private key
 ###
@@ -86,17 +87,16 @@
 	end
 
 
-	### Perform the ssh connection, passing the command to the pipe
-	### and retreiving any output from the remote end.
+	### Perform the ssh connection in 'exec' mode, and retrieve any
+	### output from the remote end.
 	###
 	def work( payload, metadata )
-		command = payload[ 'command' ]
-		raise ArgumentError, "Missing required option 'command'" unless command
+		raise ArgumentError, "Missing required option 'command'" unless payload[ 'command' ]
 		raise ArgumentError, "Missing required option 'host'"    unless payload[ 'host' ]
 
 		exitcode = self.open_connection( payload, metadata ) do |reader, writer|
-			self.log.debug "Writing command #{command}..."
-			writer.puts( command )
+			#self.log.debug "Writing command #{command}..."
+			#writer.puts( command )
 			self.log.debug "  closing child's writer."
 			writer.close
 			self.log.debug "  reading from child."
@@ -123,6 +123,7 @@
 		opts = payload[ 'opts' ] || Symphony::Task::SSH.opts
 		user = payload[ 'user' ] || Symphony::Task::SSH.user
 		key  = payload[ 'key'  ] || Symphony::Task::SSH.key
+		env  = payload[ 'env'  ] || {}
 
 		cmd = []
 		cmd << Symphony::Task::SSH.path
@@ -132,23 +133,30 @@
 		cmd << '-i' << key if key
 		cmd << '-l' << user
 		cmd << payload[ 'host' ]
+		cmd << payload[ 'command' ]
 		cmd.flatten!
 		self.log.debug "Running SSH command with: %p" % [ Shellwords.shelljoin(cmd) ]
 
 		parent_reader, child_writer = IO.pipe
 		child_reader, parent_writer = IO.pipe
 
-		pid = Process.spawn( *cmd, :out => child_writer, :in => child_reader, :close_others => true )
+		pid = Process.spawn( env, *cmd,
+			out:             child_writer,
+			in:              child_reader,
+			close_others:    true,
+			unsetenv_others: true
+		)
+
 		child_writer.close
 		child_reader.close
 
 		self.log.debug "Yielding back to the run block."
 		@output = yield( parent_reader, parent_writer )
-		@output = @output.split("\n").reject{|l| l =~ SSH_CLEANUP }.join
+		@output = @output.split( /\r?\n/ ).reject{|l| l =~ SSH_CLEANUP }.join
 		self.log.debug "  run block done."
 
-		status = nil
-
+	rescue => err
+		self.log.error( err.message )
 	ensure
 		if pid
 			active = Process.kill( 0, pid ) rescue false
--- a/spec/symphony/tasks/ssh_spec.rb	Tue Apr 07 16:42:31 2020 -0700
+++ b/spec/symphony/tasks/ssh_spec.rb	Thu Jul 09 15:05:21 2020 -0700
@@ -38,12 +38,12 @@
 			allow( IO ).to receive( :pipe ).and_return([ pipe, pipe ])
 
 			args = [
-				'-p', '22', '-i', '/tmp/sekrit.rsa', '-l', 'symphony', 'example.com'
+				'-p', '22', '-i', '/tmp/sekrit.rsa', '-l', 'symphony', 'example.com', 'woohoo'
 			]
 
 			expect( Process ).to receive( :spawn ).with(
-				*[ ssh.to_s, described_class.opts, args ].flatten,
-				:out => pipe, :in => pipe, :close_others => true
+				*[ {}, ssh.to_s, described_class.opts, args ].flatten,
+				out: pipe, in: pipe, close_others: true, unsetenv_others: true
 			).and_return( 12 )
 
 			expect( Process ).to receive( :waitpid2 ).with( 12 ).and_return([ 12, 1 ])