Class: Rosh

Inherits:
Object
  • Object
show all
Defined in:
lib/rosh.rb,
lib/rosh/version.rb

Constant Summary collapse

VERSION =
'0.9.8'

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Rosh

Returns a new instance of Rosh.



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
59
60
61
# File 'lib/rosh.rb', line 10

def initialize(*args)
  @interval = 3
  @ssh_opts = []
  @base_ssh_opts = []
  @local_forward_specs = []
  @remote_forward_specs = []
  alive_interval = 5
  @escape = '^t'
  @tmux_socket_name = nil
  OptionParser.new("test").tap do |opt|
    opt.banner = '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}
    opt.on('-L socket-name'){|v| @tmux_socket_name = v}
  end.parse! args
  @host, @name = *args, :default
  abort 'hostname is required' if @host == :default
  @base_ssh_opts << "-o ServerAliveInterval=#{alive_interval}"
  @base_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
  @oom_reported = false
  @last_exit_status = nil
  @local_forward_specs = local_forwards(alias_name)
  @remote_forward_specs = remote_forwards(alias_name)
  @host = config[:host_name] if config[:host_name]
  @base_ssh_opts << "-l #{config[:user]}" if config[:user]
  @base_ssh_opts << "-p #{config[:port]}" if config[:port]
  if proxy_option = ssh_proxy_option(config[:proxy])
    @base_ssh_opts << proxy_option
  end
  if keys = config[:keys]
    keys.each{|k| @base_ssh_opts << "-i #{k}"}
  end
  refresh_ssh_opts!
  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

#connectObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rosh.rb', line 63

def connect
  if @verbose
    puts "connecting to #{@host}..."
  end
  cmd = nil
  begin
    reconnect
    cmd = attach_command
    puts cmd if @verbose
  end until execute_attach(cmd)
  report_session_end(cmd)
end

#reconnectObject



76
77
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
# File 'lib/rosh.rb', line 76

def reconnect
  refresh_ssh_opts!
  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