Class: CFTools::Tunnel::Base

Inherits:
CF::CLI
  • Object
show all
Defined in:
lib/tools-cf-plugin/tunnel/base.rb

Direct Known Subclasses

TunnelNATS, WatchLogs

Constant Summary collapse

BOSH_CONFIG =
"~/.bosh_config"

Instance Method Summary collapse

Instance Method Details

#authenticate_with_director(director, remote_director, auth) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tools-cf-plugin/tunnel/base.rb', line 39

def authenticate_with_director(director, remote_director, auth)
  if auth && (director, auth["username"], auth["password"])
    return true
  end

  while true
    line unless quiet?
    user = ask("Director Username")
    pass = ask("Director Password", :echo => "*", :forget => true)
    break if (director, user, pass)
  end

  save_auth(remote_director, "username" => user, "password" => pass)

  true
end

#connected_director(director_host, gateway) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/tools-cf-plugin/tunnel/base.rb', line 28

def connected_director(director_host, gateway)
  director = director(director_host, gateway)

  authenticate_with_director(
    director,
    "https://#{director_host}:25555",
    director_credentials(director_host))

  director
end

#current_deployment(director) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tools-cf-plugin/tunnel/base.rb', line 75

def current_deployment(director)
  deployments =
    with_progress("Getting deployments") do
      director.list_deployments
    end

  fail "No deployments." if deployments.empty?

  cf_deployments = deployments.select do |d|
    d["releases"].any? { |r| ["cf-release", "cf"].include?(r["name"]) }
  end

  return cf_deployments.first if cf_deployments.size == 1

  ask("Which deployment?",
      :choices => deployments,
      :display => proc { |x| x["name"] })
end

#director(director_host, gateway) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tools-cf-plugin/tunnel/base.rb', line 15

def director(director_host, gateway)
  if address_reachable?(director_host, 25555)
    director_for(25555, director_host)
  else
    dport =
      with_progress("Opening local tunnel to director") do
        tunnel_to(director_host, 25555, gateway)
      end

    director_for(dport)
  end
end

#login_to_director(director, user, pass) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/tools-cf-plugin/tunnel/base.rb', line 56

def (director, user, pass)
  director.user = user
  director.password = pass

  with_progress("Authenticating as #{c(user, :name)}") do |s|
    director.authenticated? || s.fail
  end
end

#preconditionObject



11
12
13
# File 'lib/tools-cf-plugin/tunnel/base.rb', line 11

def precondition
  require "cli"
end

#tunnel_to(address, remote_port, gateway) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/tools-cf-plugin/tunnel/base.rb', line 65

def tunnel_to(address, remote_port, gateway)
  local_port = grab_ephemeral_port

  Process.spawn("ssh -o StrictHostKeyChecking=no -N -L #{local_port}:#{address}:#{remote_port} #{gateway}")

  wait_for_port_open(local_port)

  local_port
end