Module: EngineYard::VisualVM::Helpers

Included in:
CLI
Defined in:
lib/engineyard-visualvm/cli.rb

Constant Summary collapse

STARTING_PORT =
5900

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/engineyard-visualvm/cli.rb', line 16

def self.included(base)
  unless base.instance_methods.map(&:to_s).include?("options")
    base.class_eval do
      def options
        @_opts ||= { :host => "localhost" }
      end
    end
  end
end

.next_free_port(start = STARTING_PORT) ⇒ Object



39
40
41
42
43
# File 'lib/engineyard-visualvm/cli.rb', line 39

def self.next_free_port(start = STARTING_PORT)
  (start...start+100).each do |i|
    return i if port_available?(i)
  end
end

.port_available?(port) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
# File 'lib/engineyard-visualvm/cli.rb', line 26

def self.port_available?(port)
  begin
    tcps = TCPServer.new("127.0.0.1", port)
    true
  rescue Errno::EADDRINUSE
    false
  ensure
    tcps.close if tcps
  end
end

Instance Method Details

#agent_jar_pathObject



112
113
114
# File 'lib/engineyard-visualvm/cli.rb', line 112

def agent_jar_path
  File.expand_path('../agent.jar', __FILE__)
end

#environmentObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/engineyard-visualvm/cli.rb', line 45

def environment
  @environment ||= begin
                     fetch_environment(options[:environment], options[:account]).tap {|env|
                       @user = env.username
                       @host = fetch_public_ip(env)
                     }
                   rescue EY::Error
                     raise if options[:environment]
                     nil
                   end
end

#fetch_public_ip(environment) ⇒ Object

Return the public IP or instance hostname assigned to an environment (which may or may not be a booted cluster of instances) Displays error and exits if no public IP assigned to the environment



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/engineyard-visualvm/cli.rb', line 128

def fetch_public_ip(environment)
  if environment.load_balancer_ip_address
    return environment.load_balancer_ip_address
  end

  if environment.instances.length == 1 && environment.instances.first.public_hostname
    return environment.instances.first.public_hostname
  end

  warn "#{environment..name}/#{environment.name} has no assigned public IP address or hostname."
  nil
end

#find_executable?(exe) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
# File 'lib/engineyard-visualvm/cli.rb', line 106

def find_executable?(exe)
  ENV['PATH'].split(File::PATH_SEPARATOR).detect do |path|
    File.exist?(File.join(path, exe))
  end
end

#find_tools_jarObject



116
117
118
119
120
121
122
# File 'lib/engineyard-visualvm/cli.rb', line 116

def find_tools_jar
  java_home = `java -classpath #{agent_jar_path} org.jruby.ext.jmx.agent.JavaHome`
  [File.expand_path('./lib/tools.jar', java_home),
   File.expand_path('../lib/tools.jar', java_home)].detect do |path|
    File.readable?(path)
  end
end

#hostObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/engineyard-visualvm/cli.rb', line 73

def host
  @host ||= begin
              match = /(.*)?@(.*)/.match options[:host]
              if match
                @user = match[1]
                match[2]
              else
                @user = nil
                options[:host]
              end
            end
end

#jmx_service_urlObject



102
103
104
# File 'lib/engineyard-visualvm/cli.rb', line 102

def jmx_service_url
  "service:jmx:rmi://#{host}:#{port}/jndi/rmi://#{host}:#{port}/jmxrmi"
end

#jvm_argumentsObject



94
95
96
97
98
99
100
# File 'lib/engineyard-visualvm/cli.rb', line 94

def jvm_arguments
  tools_jar = find_tools_jar
  args = "-Dorg.jruby.jmx.agent.port=#{next_free_port} -javaagent:#{agent_jar_path}"
  args = "-Dorg.jruby.jmx.agent.hostname=#{host} #{args}" if host != "localhost"
  args = "-Xbootclasspath/a:#{tools_jar} #{args}" if tools_jar
  args
end

#next_free_portObject



86
87
88
# File 'lib/engineyard-visualvm/cli.rb', line 86

def next_free_port
  Helpers.next_free_port(port)
end

#portObject



90
91
92
# File 'lib/engineyard-visualvm/cli.rb', line 90

def port
  @port ||= Numeric === options[:port] && options[:port] || STARTING_PORT
end

#socks_proxy?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/engineyard-visualvm/cli.rb', line 65

def socks_proxy?
  options[:socks]
end

#ssh?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/engineyard-visualvm/cli.rb', line 61

def ssh?
  environment || (host && user) || options[:ssh] || options[:socks]
end

#ssh_hostObject



69
70
71
# File 'lib/engineyard-visualvm/cli.rb', line 69

def ssh_host
  user ? "#{user}@#{host}" : host
end

#userObject



57
58
59
# File 'lib/engineyard-visualvm/cli.rb', line 57

def user
  @user
end