require 'picsolve_docker_builder/helpers/ssh_forward' require 'picsolve_docker_builder/base' require 'net/ssh' module PicsolveDockerBuilder module Helpers # Ruby class that can forward a remote port over SSH class SshConnection include PicsolveDockerBuilder::Base attr_reader :options def initialize(opts) @options = opts end def connection @connection ||= Net::SSH.start( ssh_host, ssh_user, port: ssh_port ) end def forward(*args) SshForward.forward(connection, *args) end def start log.debug "Connecting via ssh to host '#{ssh_user}@#{ssh_host}'" # start thread with ssh @thread = Thread.new do connection.loop { true } end end def stop return if @thread.nil? @thread.kill @thread.join log.debug \ "Disconnected ssh connection to host '#{ssh_user}@#{ssh_host}'" end def ssh_user options[:ssh_user] = 'core' end def ssh_host options[:ssh_host] end def ssh_port options[:ssh_port] || 22 end def remote_host options[:remote_host] end def remote_port options[:remote_port] end def local_port options[:local_port] || remote_port end end end end