Class: SSHTunnel::UI::Models::Tunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh-hull/ui/models/tunnel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Tunnel

Returns a new instance of Tunnel.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 11

def initialize(opts = {})
  @uuid        = opts.fetch(:uuid) { SecureRandom.uuid }
  @name        = opts.fetch(:name, '')
  @parent      = opts[:parent]
  @type        = opts[:type]
  @local_host  = opts[:local_host]
  @local_port  = opts[:local_port]
  @remote_host = opts[:remote_host]
  @remote_port = opts[:remote_port]
  @auto_start  = opts[:auto_start]
  @process     = nil
  @started     = false
end

Instance Attribute Details

#auto_startObject

Returns the value of attribute auto_start.



8
9
10
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 8

def auto_start
  @auto_start
end

#local_hostObject

Returns the value of attribute local_host.



8
9
10
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 8

def local_host
  @local_host
end

#local_portObject

Returns the value of attribute local_port.



8
9
10
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 8

def local_port
  @local_port
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 8

def name
  @name
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 8

def parent
  @parent
end

#remote_hostObject

Returns the value of attribute remote_host.



8
9
10
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 8

def remote_host
  @remote_host
end

#remote_portObject

Returns the value of attribute remote_port.



8
9
10
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 8

def remote_port
  @remote_port
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 8

def type
  @type
end

#uuidObject

Returns the value of attribute uuid.



8
9
10
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 8

def uuid
  @uuid
end

Instance Method Details

#auto_start!Object



60
61
62
63
64
65
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 60

def auto_start!
  return false unless auto_start?

  start!
  true
end

#auto_start?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 55

def auto_start?
  @auto_start == true
end

#commandObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 98

def command
  cmd = [
    '/usr/bin/ssh',
    '-N',
    '-t',
    '-x',
    '-o', 'ExitOnForwardFailure=yes',
    "-l#{parent.user}",
    "-L#{local_host}:#{local_port}:#{remote_host}:#{remote_port}",
    "-p#{parent.port}"
  ]

  cmd << "-i#{parent.identity_file}" if parent.identity_file.present?
  cmd << parent.host
  cmd
end

#start!Object



73
74
75
76
77
78
79
80
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 73

def start!
  return if started?

  SSHTunnel.logger.info "Starting tunnel : #{self}"

  @process = Subprocess.popen(command, stdout: '/dev/null', stderr: '/dev/null', stdin: Subprocess::PIPE)
  @started = true
end

#started?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 68

def started?
  @started
end

#stop!Object

See: www.redhat.com/archives/libvir-list/2007-September/msg00106.html to avoid zombie when terminating process



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 85

def stop!
  return unless started?
  return if @process.nil?

  SSHTunnel.logger.info "Stopping tunnel : #{self}"

  @process.terminate
  @process.wait
  @started = false
  @process = nil
end

#to_hashObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 41

def to_hash
  {
    uuid:        uuid,
    name:        name,
    type:        type,
    local_host:  local_host,
    local_port:  @local_port,
    remote_host: remote_host,
    remote_port: @remote_port,
    auto_start:  auto_start,
  }
end

#to_sObject



26
27
28
# File 'lib/ssh-hull/ui/models/tunnel.rb', line 26

def to_s
  "#{parent} - #{name}"
end