ec2_userdata 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ec2_userdata.rb CHANGED
@@ -5,17 +5,26 @@ module EC2
5
5
  class UserData
6
6
  def self.[](key)
7
7
  if @userdata.nil?
8
- if EC2.ec2?
8
+ if EC2.ec2? && !use_local_config?
9
9
  logger.info "Running on EC2. Reading user data from http://169.254.169.254/1.0/user-data" if logger
10
10
  @userdata = get_ec2_userdata
11
11
  else
12
- logger.info "Not running on EC2. Reading user data from #{app_root}/config/ec2_userdata.yml." if logger
12
+ logger.info "Reading user data from #{app_root}/config/ec2_userdata.yml." if logger
13
13
  @userdata = get_local_userdata
14
14
  end
15
15
  end
16
16
 
17
17
  @userdata[key]
18
18
  end
19
+
20
+ # Force use of local configuration file even when running on EC2
21
+ def self.use_local_config!
22
+ @use_local_config = true
23
+ end
24
+
25
+ def self.use_local_config?
26
+ @use_local_config == true
27
+ end
19
28
 
20
29
  private
21
30
  def self.get_ec2_userdata
@@ -44,7 +53,12 @@ module EC2
44
53
 
45
54
  def self.app_root
46
55
  if defined?(Rails)
47
- RAILS_ROOT
56
+ if Rails.respond_to?(:root)
57
+ Rails.root
58
+ else
59
+ RAILS_ROOT
60
+ end
61
+
48
62
  elsif defined?(Merb)
49
63
  Merb.root
50
64
  elsif defined?(APP_ROOT)
@@ -77,4 +91,4 @@ end
77
91
 
78
92
  class NilClass
79
93
  def blank?; true; end
80
- end
94
+ end
@@ -9,8 +9,8 @@ APP_ROOT = "File.dirname(__FILE__) + '/../.."
9
9
  class Ec2UserDataTest < Test::Unit::TestCase
10
10
  def setup
11
11
  EC2::UserData.class_eval("@userdata = nil")
12
+ EC2::UserData.class_eval("@use_local_config = nil")
12
13
  EC2::module_eval("@running_on_ec2 = nil")
13
-
14
14
  end
15
15
 
16
16
  def test_get_on_ec2
@@ -48,14 +48,20 @@ class Ec2UserDataTest < Test::Unit::TestCase
48
48
  assert_raises(RuntimeError) { EC2.ec2? }
49
49
  end
50
50
 
51
+ def test_use_local_config!
52
+ EC2::UserData.use_local_config!
53
+ assert EC2::UserData.use_local_config?
54
+ EC2::UserData.expects(:get_local_userdata).returns({'foo'=>'bar'})
55
+ assert_equal 'bar', EC2::UserData['foo']
56
+ end
51
57
 
52
58
  ## Helpers ##
53
59
  def ec2_json_userdata
54
- '{"queue_host":"lisa-queue.defensio.net","cluster":"lisa","queue_port":11300}'
60
+ '{"queue_host":"lisa-queue.example.net","cluster":"lisa","queue_port":11300}'
55
61
  end
56
62
 
57
63
  def local_yaml_userdata
58
- "--- \nqueue_host: lisa-queue.defensio.net\nqueue_port: 11300\ncluster: lisa\n"
64
+ "--- \nqueue_host: lisa-queue.example.net\nqueue_port: 11300\ncluster: lisa\n"
59
65
  end
60
66
 
61
67
  def nslookup_on_ec2
@@ -65,4 +71,4 @@ class Ec2UserDataTest < Test::Unit::TestCase
65
71
  def nslookup_on_local
66
72
  "Server: 192.168.1.1\nAddress: 192.168.1.1#53\n\n** server can't find 254.169.254.169.in-addr.arpa.: NXDOMAIN\n"
67
73
  end
68
- end
74
+ 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
+ hash: 13
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
- - 0
8
- version: "1.0"
8
+ - 1
9
+ version: "1.1"
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: 2010-04-26 00:00:00 -04:00
17
+ date: 2010-11-18 00:00:00 -05:00
17
18
  default_executable:
18
19
  dependencies: []
19
20
 
@@ -38,23 +39,27 @@ rdoc_options: []
38
39
  require_paths:
39
40
  - lib
40
41
  required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
41
43
  requirements:
42
44
  - - ">="
43
45
  - !ruby/object:Gem::Version
46
+ hash: 3
44
47
  segments:
45
48
  - 0
46
49
  version: "0"
47
50
  required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
48
52
  requirements:
49
53
  - - ">="
50
54
  - !ruby/object:Gem::Version
55
+ hash: 3
51
56
  segments:
52
57
  - 0
53
58
  version: "0"
54
59
  requirements: []
55
60
 
56
61
  rubyforge_project:
57
- rubygems_version: 1.3.6
62
+ rubygems_version: 1.3.7
58
63
  signing_key:
59
64
  specification_version: 3
60
65
  summary: A simple Ruby library that reads UserData on EC2 with graceful fallback when not running on EC2