Class: Rosh
- Inherits:
-
Object
- Object
- Rosh
- Defined in:
- lib/rosh.rb,
lib/rosh/version.rb
Constant Summary collapse
- VERSION =
'0.9.4'
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(*args) ⇒ Rosh
constructor
A new instance of Rosh.
- #reconnect ⇒ Object
Constructor Details
#initialize(*args) ⇒ Rosh
Returns a new instance of Rosh.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rosh.rb', line 9 def initialize(*args) @interval = 3 @ssh_opts = [] alive_interval = 5 @escape = '^t' OptionParser.new("test").tap do |opt| opt. = 'Usage: rosh [options] hostname [session-name]' opt.on('-a alive-interval'){|v| alive_interval = v.to_i} opt.on('-e escape'){|v| @escape = v} opt.on('-I interval'){|v| @interval = v.to_f} opt.on('-V'){|v| @verbose = true} opt.on('-S'){|v| @screen = true} end.parse! args @host, @name = *args, :default abort 'hostname is required' if @host == :default @ssh_opts << "-o ServerAliveInterval=#{alive_interval}" @ssh_opts << "-o ServerAliveCountMax=1" # check ~/.ssh/config to resolve alias name alias_name = @host config = Net::SSH::Config.for(@host) if @verbose puts "ssh-config: #{config}" end @forward_opts = [] @forwarding_disabled = false @oom_reported = false @last_exit_status = nil local_forwards(alias_name).each do |f| add_forward_option(:local, f) end remote_forwards(alias_name).each do |f| add_forward_option(:remote, f) end @host = config[:host_name] if config[:host_name] @ssh_opts << "-l #{config[:user]}" if config[:user] @ssh_opts << "-p #{config[:port]}" if config[:port] @ssh_opts << "-J #{config[:proxy].jump_proxies}" if config[:proxy] if keys = config[:keys] keys.each{|k| @ssh_opts << "-i #{k}"} end if @verbose puts "host: #{@host}" puts "name: #{@name}" puts "interval: #{@interval}" puts "alive_interval: #{alive_interval}" puts "options: #{@ssh_opts*' '}" end @first_try = true end |
Instance Method Details
#connect ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rosh.rb', line 60 def connect cmd = if @screen ["ssh", *@ssh_opts, resolv, '-t', "'screen -rx #{@name}'", '2>/dev/null']*' ' else ["ssh", *@ssh_opts, resolv, '-t', "'tmux attach -t #{@name}'", '2>/dev/null']*' ' end if @verbose puts "connecting to #{@host}..." puts cmd end begin reconnect end until execute_attach(cmd) report_session_end(cmd) end |
#reconnect ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rosh.rb', line 78 def reconnect if @first_try session_exists = if @screen sh('-p 0 -X echo ok', '2>&1 >/dev/null') else sh_has_session? end unless session_exists type = @screen ? 'screen' : 'tmux' print "creating new #{type} session #{@name}..." new_session = if @screen sh %{-c /dev/null -e "#{@escape*2}" -dm} and sh '-p 0 -X eval "stuff STY=\\040screen\\015"' else sh_new_session? end if new_session puts "done." else puts "failed." end end @first_try = false else sleep [@last_try - Time.now + @interval, 0].max if @last_try puts "reconnecting..." @last_try = Time.now end end |