ey_cloud_awareness 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ey_cloud_awareness}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Seamus Abshere"]
@@ -24,7 +24,6 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "ey_cloud_awareness.gemspec",
27
- "lib/engine_yard_cloud_chef.rb",
28
27
  "lib/engine_yard_cloud_instance.rb",
29
28
  "lib/ey_cloud_awareness.rb",
30
29
  "spec/ey_cloud_awareness_spec.rb",
@@ -1,19 +1,29 @@
1
1
  class EngineYardCloudInstance
2
- INSTANCE_DESCRIPTIONS_CACHE_PATH = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/engine_yard_instance_descriptions.yml" : '/etc/engine_yard_instance_descriptions.yml'
2
+ CURRENT_INSTANCE_ID_CACHE_PATH = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/engine_yard_cloud_instance_id" : '/etc/engine_yard_cloud_instance_id'
3
+ INSTANCE_DESCRIPTIONS_CACHE_PATH = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/engine_yard_cloud_instance_descriptions.yml" : '/etc/engine_yard_cloud_instance_descriptions.yml'
4
+ DNA_PATH = '/etc/chef/dna.json'
3
5
 
4
- attr_accessor :instance_id
6
+ attr_reader :instance_id
5
7
  def initialize(instance_id)
6
8
  @instance_id = instance_id.to_sym
7
9
  end
8
10
 
11
+ def valid?
12
+ data.present?
13
+ end
14
+
9
15
  def refresh
10
16
  self.class.refresh
11
17
  end
12
18
 
19
+ def data
20
+ self.class.data[instance_id]
21
+ end
22
+
13
23
  def method_missing(name, *args, &block)
14
24
  name = name.to_sym
15
- if me = self.class.data[instance_id] and me.has_key?(name)
16
- me[name]
25
+ if data and data.has_key?(name)
26
+ data[name]
17
27
  else
18
28
  super
19
29
  end
@@ -36,12 +46,16 @@ class EngineYardCloudInstance
36
46
  data.map { |k, _| new k }
37
47
  end
38
48
 
49
+ def current
50
+ new cached_current_instance_id
51
+ end
52
+
39
53
  def first
40
54
  new data.to_a.first.first
41
55
  end
42
56
 
43
57
  def find_by_instance_id(instance_id)
44
- new instance_id.to_sym
58
+ new instance_id
45
59
  end
46
60
 
47
61
  def find_all_by_instance_roles(*args)
@@ -51,16 +65,16 @@ class EngineYardCloudInstance
51
65
  def refresh
52
66
  @_data = nil
53
67
  @_dna = nil
54
- refresh_instance_descriptions true
68
+ cached_instance_descriptions true
69
+ cached_current_instance_id true
55
70
  end
56
71
 
57
72
  def data
58
73
  return @_data if @_data
59
74
  hash = Hash.new
60
75
  cached_instance_descriptions.each do |instance_description|
61
- key = instance_description[:aws_instance_id].dup
62
- hash[key] ||= Hash.new
63
- current = hash[key]
76
+ hash[instance_description[:aws_instance_id]] ||= Hash.new
77
+ current = hash[instance_description[:aws_instance_id]]
64
78
  # using current as a pointer
65
79
  if dna[:db_host] == instance_description[:dns_name] or dna[:db_host] == instance_description[:private_dns_name]
66
80
  current[:instance_role] = :db
@@ -73,24 +87,30 @@ class EngineYardCloudInstance
73
87
  end
74
88
  current[:private_dns_name] = instance_description[:private_dns_name]
75
89
  current[:dns_name] = instance_description[:dns_name]
76
- current[:aws_instance_id] = instance_description[:aws_instance_id]
77
90
  current[:aws_state] = instance_description[:aws_state]
78
91
  end
79
92
  @_data = hash.recursive_symbolize_keys!
80
93
  end
81
94
 
82
- private
83
-
84
95
  def dna
85
- @_dna ||= EngineYardCloudChef.dna
96
+ @_dna ||= JSON.load(IO.read(DNA_PATH)).recursive_symbolize_keys!
86
97
  end
87
98
 
88
- def cached_instance_descriptions(refresh = false)
89
- refresh_instance_descriptions refresh
90
- @_cached_instance_descriptions ||= YAML.load(IO.read(INSTANCE_DESCRIPTIONS_CACHE_PATH))
99
+ private
100
+
101
+ def cached_current_instance_id(refresh = false)
102
+ if refresh or !File.readable?(CURRENT_INSTANCE_ID_CACHE_PATH)
103
+ @_cached_current_instance_id = open("http://169.254.169.254/latest/meta-data/instance-id").gets
104
+ begin
105
+ File.open(CURRENT_INSTANCE_ID_CACHE_PATH, 'w') { |f| f.write @_cached_current_instance_id }
106
+ rescue Errno::EACCES
107
+ $stderr.puts "[EY CLOUD AWARENESS GEM] Not caching current instance id because #{CURRENT_INSTANCE_ID_CACHE_PATH} can't be written to"
108
+ end
109
+ end
110
+ @_cached_current_instance_id ||= IO.read(CURRENT_INSTANCE_ID_CACHE_PATH)
91
111
  end
92
112
 
93
- def refresh_instance_descriptions(refresh)
113
+ def cached_instance_descriptions(refresh = false)
94
114
  if refresh or !File.readable?(INSTANCE_DESCRIPTIONS_CACHE_PATH)
95
115
  ec2 = RightAws::Ec2.new dna[:aws_secret_id], dna[:aws_secret_key]
96
116
  @_cached_instance_descriptions = ec2.describe_instances.map(&:recursive_symbolize_keys!)
@@ -100,6 +120,7 @@ class EngineYardCloudInstance
100
120
  $stderr.puts "[EY CLOUD AWARENESS GEM] Not caching instance data because #{INSTANCE_DESCRIPTIONS_CACHE_PATH} can't be written to"
101
121
  end
102
122
  end
123
+ @_cached_instance_descriptions ||= YAML.load(IO.read(INSTANCE_DESCRIPTIONS_CACHE_PATH))
103
124
  end
104
125
  end
105
126
  end
@@ -1,9 +1,7 @@
1
+ require 'open-uri'
1
2
  require 'active_support'
2
3
  require 'right_aws'
3
-
4
- %w{ engine_yard_cloud_instance engine_yard_cloud_chef }.each do |lib|
5
- require File.expand_path(File.join(File.dirname(__FILE__), lib))
6
- end
4
+ require File.expand_path(File.join(File.dirname(__FILE__), 'engine_yard_cloud_instance'))
7
5
 
8
6
  # http://pragmatig.wordpress.com/2009/04/14/recursive-symbolize_keys/
9
7
  class Hash
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ey_cloud_awareness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seamus Abshere
@@ -59,7 +59,6 @@ files:
59
59
  - Rakefile
60
60
  - VERSION
61
61
  - ey_cloud_awareness.gemspec
62
- - lib/engine_yard_cloud_chef.rb
63
62
  - lib/engine_yard_cloud_instance.rb
64
63
  - lib/ey_cloud_awareness.rb
65
64
  - spec/ey_cloud_awareness_spec.rb
@@ -1,9 +0,0 @@
1
- module EngineYardCloudChef
2
- DNA_PATH = '/etc/chef/dna.json'
3
- # Load the Chef DNA file from /etc/chef/dna.json
4
- # >> JSON.load(IO.read('/etc/chef/dna.json')).keys
5
- # => ["aws_secret_key", "db_slaves", "user_ssh_key", "admin_ssh_key", "backup_interval", "instance_role", "mailserver", "utility_instances", "crons", "backup_window", "removed_applications", "alert_email", "applications", "gems_to_install", "members", "reporting_url", "aws_secret_id", "environment", "users", "master_app_server", "db_host", "packages_to_install", "haproxy"]
6
- def self.dna
7
- JSON.load(IO.read(DNA_PATH)).recursive_symbolize_keys!
8
- end
9
- end