Class: EY::Metadata::EngineYardCloudApi

Inherits:
Object
  • Object
show all
Includes:
SshAliasHelper
Defined in:
lib/engineyard-metadata/engine_yard_cloud_api.rb

Overview

An adapter that reads from the public EngineYard Cloud API (cloud.engineyard.com). Available from anywhere.

See README for what environment variables and/or files you need to have in place for this to work.

Constant Summary collapse

URL =
'https://cloud.engineyard.com/api/v2/environments'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SshAliasHelper

#ssh_alias, #ssh_aliases

Constructor Details

#initialize(last_used_ey_cloud_token) ⇒ EngineYardCloudApi

Returns a new instance of EngineYardCloudApi.



23
24
25
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 23

def initialize(last_used_ey_cloud_token)
  @last_used_ey_cloud_token = last_used_ey_cloud_token
end

Instance Attribute Details

#last_used_ey_cloud_tokenObject (readonly)

Returns the value of attribute last_used_ey_cloud_token.



18
19
20
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 18

def last_used_ey_cloud_token
  @last_used_ey_cloud_token
end

Instance Method Details

#app_master(identifier = EY::Metadata::DEFAULT_IDENTIFIER) ⇒ Object

An identifying attribute of the app_master. Defaults to public_hostname.

If you’re on a solo app, it counts the solo as the app_master.



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

def app_master(identifier = EY::Metadata::DEFAULT_IDENTIFIER)
  if x = environment['instances'].detect { |i| i['role'] == 'app_master' }
    x[identifier]
  else
    solo(identifier)
  end
end

#app_nameObject

The name of the app we’re looking at.



130
131
132
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 130

def app_name
  application['name']
end

#app_servers(identifier = EY::Metadata::DEFAULT_IDENTIFIER) ⇒ Object

An identifying attribute of each app server. Defaults to public_hostname.

If you’re on a solo app, it counts the solo as an app server.



63
64
65
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 63

def app_servers(identifier = EY::Metadata::DEFAULT_IDENTIFIER)
  environment['instances'].select { |i| %w{ app_master app solo }.include? i['role'] }.map { |i| i[identifier] }.sort
end

#app_slaves(identifier = EY::Metadata::DEFAULT_IDENTIFIER) ⇒ Object

An identifying attribute of each app slave. Defaults to public_hostname.



82
83
84
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 82

def app_slaves(identifier = EY::Metadata::DEFAULT_IDENTIFIER)
  environment['instances'].select { |i| %w{ app }.include? i['role'] }.map { |i| i[identifier] }.sort
end

#applicationObject

Used internally to store data about the specific application we’re working with.

Raises:

  • (RuntimeError)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 143

def application
  hit = if EY..preset_app_name?
    catch(:found_it) do
      environments.each do |e|
        if hit = e['apps'].detect { |a| a['name'] == EY..app_name }
          throw :found_it, hit
        end
      end
    end
  elsif possible_to_detect_app_from_environment_name?
    environment['apps'][0]
  elsif possible_to_detect_app_from_git_config?
    catch(:found_it) do
      environments.each do |e|
        if hit = e['apps'].detect { |a| a['repository_uri'] == repository_uri_from_git_config }
          throw :found_it, hit
        end
      end
    end
  end
  raise RuntimeError, "[engineyard-metadata gem] Couldn't find a matching application. Please set EY.metadata.app_name= or ENV['EY_APP_NAME']" unless hit
  hit
end

#database_hostObject

The hostname of the database host.



45
46
47
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 45

def database_host
  db_master
end

#database_nameObject

Currently the same as the app name, at least for recently-created environments.

This is less reliable that the answer you would get running from an instance, because databases used to be named after environments.



40
41
42
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 40

def database_name
  EY..app_name
end

#database_usernameObject

Currently the same as the SSH username.



28
29
30
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 28

def database_username
  environment['ssh_username']
end

#db_master(identifier = EY::Metadata::DEFAULT_IDENTIFIER) ⇒ Object

An identifying attribute of the db_master. Defaults to public_hostname.

If you’re on a solo app, it counts the solo as the db_master.



52
53
54
55
56
57
58
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 52

def db_master(identifier = EY::Metadata::DEFAULT_IDENTIFIER)
  if x = environment['instances'].detect { |i| i['role'] == 'db_master' }
    x[identifier]
  else
    solo(identifier)
  end
end

#db_servers(identifier = EY::Metadata::DEFAULT_IDENTIFIER) ⇒ Object

An identifying attribute of each DB server. Defaults to public_hostname.

If you’re on a solo app, it counts the solo as a db server.



70
71
72
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 70

def db_servers(identifier = EY::Metadata::DEFAULT_IDENTIFIER)
  environment['instances'].select { |i| %w{ db_master db_slave solo }.include? i['role'] }.map { |i| i[identifier] }.sort
end

#db_slaves(identifier = EY::Metadata::DEFAULT_IDENTIFIER) ⇒ Object

An identifying attribute of each DB slave. Defaults to public_hostname.



87
88
89
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 87

def db_slaves(identifier = EY::Metadata::DEFAULT_IDENTIFIER)
  environment['instances'].select { |i| %w{ db_slave }.include? i['role'] }.map { |i| i[identifier] }.sort
end

#environmentObject

Used internally to store data about the specific environment we’re working with.

Raises:

  • (RuntimeError)


197
198
199
200
201
202
203
204
205
206
207
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 197

def environment
  hit = if EY..preset_environment_name?
    environments.detect { |e| e['name'] == EY..environment_name }
  elsif possible_to_detect_environment_from_app_name?
    environments.detect { |e| e['apps'].any? { |a| a['name'] == EY..app_name } }
  elsif possible_to_detect_environment_from_git_config?
    environments.detect { |e| e['apps'].any? { |a| a['repository_uri'] == repository_uri_from_git_config } }
  end
  raise RuntimeError, "[engineyard-metadata gem] Couldn't find a matching environment. Please set EY.metadata.environment_name= or ENV['EY_ENVIRONMENT_NAME']" unless hit
  hit
end

#environment_nameObject

The name of the EngineYard AppCloud environment.



110
111
112
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 110

def environment_name
  environment['name']
end

#environment_namesObject

A list of all the environment names belonging to this account.



125
126
127
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 125

def environment_names
  environments.map { |environment| environment['name'] }
end

#environmentsObject

Used internally to store the full list of environments that the API gives us.



135
136
137
138
139
140
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 135

def environments
  return @environments if @environments.is_a? Array
  raw_json = REST.get(URL, 'X-EY-Cloud-Token' => last_used_ey_cloud_token).body
  raw_data = ActiveSupport::JSON.decode raw_json
  @environments = raw_data['environments']
end

#possible_to_detect_app_from_environment_name?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 177

def possible_to_detect_app_from_environment_name?
  environment['apps'].length == 1
end

#possible_to_detect_app_from_git_config?Boolean

Returns:

  • (Boolean)


186
187
188
189
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 186

def possible_to_detect_app_from_git_config?
  return false unless repository_uri_from_git_config
  environments.sum { |e| e['apps'].sum { |a| (a['repository_uri'] == repository_uri_from_git_config) ? 1 : 0 } } == 1
end

#possible_to_detect_environment_from_app_name?Boolean

Returns:

  • (Boolean)


181
182
183
184
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 181

def possible_to_detect_environment_from_app_name?
  return false unless EY..preset_app_name?
  environments.sum { |e| e['apps'].sum { |a| (a['name'] == EY..app_name) ? 1 : 0 } } == 1
end

#possible_to_detect_environment_from_git_config?Boolean

Returns:

  • (Boolean)


191
192
193
194
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 191

def possible_to_detect_environment_from_git_config?
  return false unless repository_uri_from_git_config
  environments.any? { |e| e['apps'].any? { |a| a['repository_uri'] == repository_uri_from_git_config } }
end

#repository_uriObject

The git repository that you told EngineYard to use for this application.



120
121
122
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 120

def repository_uri
  application['repository_uri']
end

#repository_uri_from_git_configObject



167
168
169
170
171
172
173
174
175
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 167

def repository_uri_from_git_config
  return @repository_uri_from_git_config if @repository_uri_from_git_config.is_a? String
  git_config_path = File.join Dir.pwd, '.git', 'config'
  if File.exist? git_config_path
    git_config = File.read git_config_path
    git_config =~ /^\[remote.*?\burl = (.*?)\n/m
    @repository_uri_from_git_config = $1
  end
end

#solo(identifier = EY::Metadata::DEFAULT_IDENTIFIER) ⇒ Object

An identifying attribute of the solo. Defaults to public_hostname.



103
104
105
106
107
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 103

def solo(identifier = EY::Metadata::DEFAULT_IDENTIFIER)
  if x = environment['instances'].detect { |i| i['role'] == 'solo' }
    x[identifier]
  end
end

#ssh_usernameObject

The username for connecting by SSH.



33
34
35
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 33

def ssh_username
  environment['ssh_username']
end

#stack_nameObject

The stack in use, like nginx_passenger.



115
116
117
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 115

def stack_name
  environment['stack_name']
end

#utilities(identifier = EY::Metadata::DEFAULT_IDENTIFIER) ⇒ Object

An identifying attribute of each utility server. Defaults to public_hostname.

If you’re on a solo app, it counts the solo as a utility.



77
78
79
# File 'lib/engineyard-metadata/engine_yard_cloud_api.rb', line 77

def utilities(identifier = EY::Metadata::DEFAULT_IDENTIFIER)
  environment['instances'].select { |i| %w{ util solo }.include? i['role'] }.map { |i| i[identifier] }.sort
end