Class: EY::Metadata::ChefDna

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

Overview

An adapter that reads from /etc/chef/dna.json, which is only available on cloud instances.

Constant Summary collapse

PATH =
'/etc/chef/dna.json'

Instance Method Summary collapse

Methods included from SshAliasHelper

#ssh_alias, #ssh_aliases

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.



106
107
108
109
110
111
112
# File 'lib/engineyard-metadata/chef_dna.rb', line 106

def app_master(identifier = EY::Metadata::DEFAULT_IDENTIFIER)
  if x = dna['engineyard']['environment']['instances'].detect { |i| i['role'] == 'app_master' }
    x[normalize_identifier(identifier)]
  else
    solo(identifier)
  end
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.



75
76
77
# File 'lib/engineyard-metadata/chef_dna.rb', line 75

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

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

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



80
81
82
# File 'lib/engineyard-metadata/chef_dna.rb', line 80

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

#applicationObject

:nodoc:



21
22
23
# File 'lib/engineyard-metadata/chef_dna.rb', line 21

def application # :nodoc:
  dna['engineyard']['environment']['apps'].detect { |a| a['name'] == EY..app_name }
end

#database_hostObject

Public hostname where you should connect to the database.

Currently the db master public hostname.



58
59
60
# File 'lib/engineyard-metadata/chef_dna.rb', line 58

def database_host
  db_master
end

#database_nameObject

For newly deployed applications, equal to the application name.



46
47
48
# File 'lib/engineyard-metadata/chef_dna.rb', line 46

def database_name
  application['database_name']
end

#database_passwordObject

Currently the same as the SSH password.



36
37
38
# File 'lib/engineyard-metadata/chef_dna.rb', line 36

def database_password
  dna['users'][0]['password']
end

#database_usernameObject

Currently the same as the SSH username.



41
42
43
# File 'lib/engineyard-metadata/chef_dna.rb', line 41

def database_username
  dna['users'][0]['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.



117
118
119
120
121
122
123
# File 'lib/engineyard-metadata/chef_dna.rb', line 117

def db_master(identifier = EY::Metadata::DEFAULT_IDENTIFIER)
  if x = dna['engineyard']['environment']['instances'].detect { |i| i['role'] == 'db_master' }
    x[normalize_identifier(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.



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

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

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

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



92
93
94
# File 'lib/engineyard-metadata/chef_dna.rb', line 92

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

#dnaObject

:nodoc:



17
18
19
# File 'lib/engineyard-metadata/chef_dna.rb', line 17

def dna # :nodoc:
  @dna ||= ActiveSupport::JSON.decode File.read(PATH)
end

#environment_nameObject

The name of the EngineYard AppCloud environment.



143
144
145
# File 'lib/engineyard-metadata/chef_dna.rb', line 143

def environment_name
  dna['environment']['name']
end

#mysql_commandObject

The shell command for mysql, including username, password, hostname and database



133
134
135
# File 'lib/engineyard-metadata/chef_dna.rb', line 133

def mysql_command
  "/usr/bin/mysql -h #{database_host} -u #{database_username} -p#{database_password} #{database_name}"
end

#mysqldump_commandObject

The shell command for mysql, including username, password, hostname and database



138
139
140
# File 'lib/engineyard-metadata/chef_dna.rb', line 138

def mysqldump_command
  "/usr/bin/mysqldump -h #{database_host} -u #{database_username} -p#{database_password} #{database_name}"
end

#normalize_identifier(identifier) ⇒ Object



152
153
154
# File 'lib/engineyard-metadata/chef_dna.rb', line 152

def normalize_identifier(identifier)
  (identifier == 'amazon_id') ? 'id' : identifier
end

#present_instance_roleObject

The present instance’s role



26
27
28
# File 'lib/engineyard-metadata/chef_dna.rb', line 26

def present_instance_role
  dna['instance_role']
end

#present_public_hostnameObject

The present instance’s public hostname.



31
32
33
# File 'lib/engineyard-metadata/chef_dna.rb', line 31

def present_public_hostname
  dna['engineyard']['environment']['instances'].detect { |i| i['id'] == EY..present_instance_id }['public_hostname']
end

#repository_uriObject

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



51
52
53
# File 'lib/engineyard-metadata/chef_dna.rb', line 51

def repository_uri
  application['repository_name']
end

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

An identifying attribute of the solo. Defaults to public_hostname.



126
127
128
129
130
# File 'lib/engineyard-metadata/chef_dna.rb', line 126

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

#ssh_passwordObject

SSH password.



68
69
70
# File 'lib/engineyard-metadata/chef_dna.rb', line 68

def ssh_password
  dna['engineyard']['environment']['ssh_password']
end

#ssh_usernameObject

SSH username.



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

def ssh_username
  dna['engineyard']['environment']['ssh_username']
end

#stack_nameObject

The stack in use, like nginx_passenger.



148
149
150
# File 'lib/engineyard-metadata/chef_dna.rb', line 148

def stack_name
  dna['engineyard']['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.



99
100
101
# File 'lib/engineyard-metadata/chef_dna.rb', line 99

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