ec2_userdata 1.2 → 1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ec2_userdata.rb +19 -28
  2. metadata +8 -5
@@ -1,37 +1,38 @@
1
1
  require 'net/http'
2
2
  require 'json/pure' unless defined?(JSON)
3
3
  require 'yaml' unless defined?(YAML)
4
+ require 'resolv' unless defined?(Resolv)
4
5
 
5
6
  module EC2
6
7
  class UserData
7
8
  def self.[](key)
8
- unless defined?(@@userdata)
9
+ unless defined?(@userdata)
9
10
  if EC2.ec2? && !use_local_config?
10
11
  logger.info "Running on EC2. Reading user data from http://169.254.169.254/1.0/user-data" if logger
11
- @@userdata = get_ec2_userdata
12
+ @userdata = get_ec2_userdata
12
13
  else
13
14
  logger.info "Reading user data from #{app_root}/config/ec2_userdata.yml." if logger
14
- @@userdata = get_local_userdata
15
+ @userdata = get_local_userdata
15
16
  end
16
17
  end
17
18
 
18
- @@userdata[key]
19
+ @userdata[key]
19
20
  end
20
21
 
21
22
  # Force use of local configuration file even when running on EC2
22
23
  def self.use_local_config!
23
- @@use_local_config = true
24
+ @use_local_config = true
24
25
  end
25
-
26
+
26
27
  def self.use_local_config?
27
- @@use_local_config == true
28
+ @use_local_config == true
28
29
  end
29
-
30
+
30
31
  private
31
32
  def self.get_ec2_userdata
32
33
  JSON.parse(Net::HTTP.get(URI.parse("http://169.254.169.254/1.0/user-data")))
33
34
  end
34
-
35
+
35
36
  def self.get_local_userdata
36
37
  if app_root
37
38
  YAML.load_file("#{app_root}/config/ec2_userdata.yml")
@@ -39,7 +40,7 @@ module EC2
39
40
  raise "Cannot find app_root. Don't know what to do!"
40
41
  end
41
42
  end
42
-
43
+
43
44
  def self.logger
44
45
  if defined?(Rails)
45
46
  Rails.logger
@@ -51,7 +52,7 @@ module EC2
51
52
  nil
52
53
  end
53
54
  end
54
-
55
+
55
56
  def self.app_root
56
57
  if defined?(Rails)
57
58
  if Rails.respond_to?(:root)
@@ -72,24 +73,14 @@ module EC2
72
73
 
73
74
  # Returns true if the current instance is running on the EC2 cloud
74
75
  def self.ec2?
75
- return @@running_on_ec2 if defined?(@@running_on_ec2)
76
- raise("nslookup must be in the path") if cmd_exec("which nslookup").blank?
77
- @@running_on_ec2 = (cmd_exec("nslookup 169.254.169.254").match(/NXDOMAIN/) || []).size < 1
78
- end
76
+ return @running_on_ec2 if defined?(@running_on_ec2)
79
77
 
80
- private
81
- def self.cmd_exec(cmd)
82
- `#{cmd}`
83
- end
84
- end
78
+ begin
79
+ @running_on_ec2 = Resolv.getname("169.254.169.254").include?(".ec2.internal")
80
+ rescue Resolv::ResolvError
81
+ @running_on_ec2 = false
82
+ end
85
83
 
86
- ### Active Support ###
87
- class String
88
- def blank?
89
- self.strip.length == 0
84
+ @running_on_ec2
90
85
  end
91
86
  end
92
-
93
- class NilClass
94
- def blank?; true; end
95
- end
metadata CHANGED
@@ -1,11 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2_userdata
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 9
5
+ prerelease:
5
6
  segments:
6
7
  - 1
7
- - 2
8
- version: "1.2"
8
+ - 3
9
+ version: "1.3"
9
10
  platform: ruby
10
11
  authors:
11
12
  - Carl Mercier
@@ -13,7 +14,7 @@ autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
16
 
16
- date: 2011-01-26 00:00:00 -05:00
17
+ date: 2011-01-27 00:00:00 -05:00
17
18
  default_executable:
18
19
  dependencies: []
19
20
 
@@ -42,6 +43,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
42
43
  requirements:
43
44
  - - ">="
44
45
  - !ruby/object:Gem::Version
46
+ hash: 3
45
47
  segments:
46
48
  - 0
47
49
  version: "0"
@@ -50,13 +52,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
52
  requirements:
51
53
  - - ">="
52
54
  - !ruby/object:Gem::Version
55
+ hash: 3
53
56
  segments:
54
57
  - 0
55
58
  version: "0"
56
59
  requirements: []
57
60
 
58
61
  rubyforge_project:
59
- rubygems_version: 1.3.7
62
+ rubygems_version: 1.4.2
60
63
  signing_key:
61
64
  specification_version: 3
62
65
  summary: A simple Ruby library that reads UserData on EC2 with graceful fallback when not running on EC2