lib/engineyard-metadata/chef_dna.rb in engineyard-metadata-0.2.3 vs lib/engineyard-metadata/chef_dna.rb in engineyard-metadata-0.2.4

- old
+ new

@@ -3,22 +3,21 @@ %w{ active_support/json }.each do |active_support_3_requirement| require active_support_3_requirement end if ActiveSupport::VERSION::MAJOR == 3 -require 'eat' module EY class Metadata # An adapter that reads from /etc/chef/dna.json, which is only available on cloud instances. class ChefDna PATH = '/etc/chef/dna.json' include SshAliasHelper def dna # :nodoc: - @dna ||= ActiveSupport::JSON.decode eat(PATH) + @dna ||= ActiveSupport::JSON.decode File.read(PATH) end def application # :nodoc: dna['engineyard']['environment']['apps'].detect { |a| a['name'] == EY.metadata.app_name } end @@ -68,67 +67,67 @@ # SSH password. def ssh_password dna['engineyard']['environment']['ssh_password'] end - # The public hostnames of all the app servers. + # 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. - def app_servers - dna['engineyard']['environment']['instances'].select { |i| %w{ app_master app solo }.include? i['role'] }.map { |i| i['public_hostname'] }.sort + 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 - - # The public hostnames of all the app slaves. - def app_slaves - dna['engineyard']['environment']['instances'].select { |i| %w{ app }.include? i['role'] }.map { |i| i['public_hostname'] }.sort + + # An identifying attribute of each app slave. Defaults to public_hostname. + 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 - - # The public hostnames of all the db servers. + + # 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. - def db_servers - dna['engineyard']['environment']['instances'].select { |i| %w{ db_master db_slave solo }.include? i['role'] }.map { |i| i['public_hostname'] }.sort + 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 - # The public hostnames of all the db slaves. - def db_slaves - dna['engineyard']['environment']['instances'].select { |i| %w{ db_slave }.include? i['role'] }.map { |i| i['public_hostname'] }.sort + # An identifying attribute of each DB slave. Defaults to public_hostname. + 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 - # The public hostnames of all the utility servers. + # 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. - def utilities - dna['engineyard']['environment']['instances'].select { |i| %w{ util solo }.include? i['role'] }.map { |i| i['public_hostname'] }.sort + 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 - # The public hostname of the app_master. + # 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. - def app_master + def app_master(identifier = EY::Metadata::DEFAULT_IDENTIFIER) if x = dna['engineyard']['environment']['instances'].detect { |i| i['role'] == 'app_master' } - x['public_hostname'] + x[normalize_identifier(identifier)] else - solo + solo(identifier) end end - # The public hostname of the db_master. + # An identifying attribute of the db_master. Defaults to public_hostname. # - # If you're on a solo app, it counts the solo as the app_master. - def db_master + # If you're on a solo app, it counts the solo as the db_master. + def db_master(identifier = EY::Metadata::DEFAULT_IDENTIFIER) if x = dna['engineyard']['environment']['instances'].detect { |i| i['role'] == 'db_master' } - x['public_hostname'] + x[normalize_identifier(identifier)] else - solo + solo(identifier) end end - # The public hostname of the solo. - def solo + # An identifying attribute of the solo. Defaults to public_hostname. + def solo(identifier = EY::Metadata::DEFAULT_IDENTIFIER) if x = dna['engineyard']['environment']['instances'].detect { |i| i['role'] == 'solo' } - x['public_hostname'] + x[normalize_identifier(identifier)] end end # The shell command for mysql, including username, password, hostname and database def mysql_command @@ -146,9 +145,13 @@ end # The stack in use, like nginx_passenger. def stack_name dna['engineyard']['environment']['stack_name'] + end + + def normalize_identifier(identifier) + (identifier == 'amazon_id') ? 'id' : identifier end end end end