| 
									
										
										
										
											2014-05-23 17:59:06 +04:00
										 |  |  | module SshCommands | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def ssh_test server, params | 
					
						
							|  |  |  |     res, code = ssh_execute(server, "test #{params}") | 
					
						
							|  |  |  |     code == 0
 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def ssh_execute server, cmd | 
					
						
							| 
									
										
										
										
											2015-10-06 13:36:24 +03:00
										 |  |  |     key_path = server[:private_key] | 
					
						
							| 
									
										
										
										
											2015-10-05 13:39:29 +03:00
										 |  |  |     ssh_cmd = "ssh -i #{key_path} #{server[:remote_user]}@#{server[:host]} '#{cmd}'" | 
					
						
							|  |  |  |     DevopsLogger.logger.info "Ssh command: #{ssh_cmd}" | 
					
						
							|  |  |  |     res = `#{ssh_cmd}` | 
					
						
							| 
									
										
										
										
											2014-05-23 17:59:06 +04:00
										 |  |  |     return res, $? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 12:37:56 +03:00
										 |  |  |  def self.ssh_with_status out, cert_path, user, ip, remote_cmd | 
					
						
							|  |  |  |     cmd = "ssh -t -i #{cert_path} #{user}@#{ip} \"#{(user == "root" ? remote_cmd : "sudo #{remote_cmd}")}\"" | 
					
						
							|  |  |  |     out << "\nCommand: '#{cmd}'\n" | 
					
						
							|  |  |  |     out.flush if out.respond_to?(:flush) | 
					
						
							|  |  |  |     status = nil | 
					
						
							|  |  |  |     IO.popen(cmd + " 2>&1") do |c| | 
					
						
							|  |  |  |       buf = "" | 
					
						
							|  |  |  |       while line = c.gets do | 
					
						
							|  |  |  |         out << line | 
					
						
							|  |  |  |         buf = line | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       c.close | 
					
						
							|  |  |  |       status = $?.to_i | 
					
						
							|  |  |  |       r = buf.scan(/exit\scode\s([0-9]{1,3})/)[0] | 
					
						
							|  |  |  |       unless r.nil? | 
					
						
							|  |  |  |         status = r[0].to_i | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     status | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-23 17:59:06 +04:00
										 |  |  | end |