aws-data 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27e8ce1b196c30054d66b267351e0eddaf1ad653
4
- data.tar.gz: 10731dd7bc8e8e2edc1898d15ccc45c2f60d8e4e
3
+ metadata.gz: 5e475268b6b72f7eb862131e4cd37243a283d0d4
4
+ data.tar.gz: 8ff49f8b175a99cdd0717c6b44b0188871eb690b
5
5
  SHA512:
6
- metadata.gz: 396378fe485c201d3d836367c7a0baf4da0f5b54920acbacc4ec89689198be920633a180b04e25bedb85405e566d5916b2ae77eb3c91af85389a77a5a2c56a10
7
- data.tar.gz: f4ceb355dffe3e9325c9eb7a72a50600060f5b94c8bb9eee565b6cab73000a90ea4cbb944c9d4cc7f6796fe1e3f65a37eb3c16a675b4a6d06722f95fbcd96d91
6
+ metadata.gz: b27ad408bbe7ae779fe4a7a7e219f8fce2641e4d4069739b41b26ed6ec64bf8e914766330122afbf5b76c782f08a54403ab11dc9503e3505e4e1b1464e3ee658
7
+ data.tar.gz: 3b8c305c5c372d637142f27d18870ea149b96dd6b04abeb18bb2bbab7ef1f5ea51e2d430e2dd800299fdbafe704c2a0fc284573c6461ce76a766922f572ba38e
data/README.md CHANGED
@@ -3,4 +3,53 @@ aws-data
3
3
 
4
4
  A rubygem for fetching data from AWS's user and metadata endpoints from ec2.
5
5
 
6
+ ### API
7
+
8
+ ```ruby
9
+
10
+ data = AWSData::Metadata.new
11
+
12
+ =begin
13
+
14
+ The constructor reaches out to the network to enumerate all the endpoints.
15
+ It'll block for a bit and then throw exceptions if you're not on EC2, or better
16
+ still do random undefined things if someone's chosen to make 169.254/24
17
+ routable on your network
18
+
19
+ =end
20
+
21
+ data.instance-id #=> i-xxxxxxx
22
+
23
+ data.methods #=>
24
+ # ...
25
+ # 'ami_id',
26
+ # 'ami_launch_index',
27
+ # 'ami_manifest_path',
28
+ # 'block_device_mapping',
29
+ # 'hostname',
30
+ # 'instance_action',
31
+ # 'instance_id',
32
+ # 'instance_type',
33
+ # 'kernel_id',
34
+ # 'local_hostname',
35
+ # 'local_ipv4',
36
+ # 'mac',
37
+ # 'metrics',
38
+ # 'network',
39
+ # 'placement',
40
+ # 'profile',
41
+ # 'public_hostname',
42
+ # 'public_ipv4',
43
+ # 'public_keys',
44
+ # 'reservation_id',
45
+ # ...
46
+
47
+ =begin
48
+
49
+ Note that some of these will return a new Transport object, eg network, which has finder methods for it's keys
50
+
51
+ =end
52
+ ```
53
+
54
+
6
55
  See: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "aws-data"
5
- s.version = "0.0.0"
5
+ s.version = "0.1.0"
6
6
  s.authors = ["Richo Healey"]
7
7
  s.email = ["richo@psych0tik.net"]
8
8
  s.homepage = "http://github.com/richo/aws-data"
@@ -1,5 +1,7 @@
1
1
  %w[
2
2
  aws-data/transport
3
+
4
+ aws-data/base
3
5
  aws-data/metadata
4
6
  ].each do |f|
5
7
  require File.expand_path("../#{f}", __FILE__)
@@ -0,0 +1,72 @@
1
+ module AWSData
2
+
3
+ Record = Struct.new("Record", :name, :finder)
4
+
5
+ class Base
6
+
7
+ class << self
8
+ @base_path = nil
9
+ def base_path(path)
10
+ @base_path = path
11
+ end
12
+
13
+ def _base_path
14
+ @base_path
15
+ end
16
+ end
17
+
18
+ def initialize
19
+ # Setup the finders, or throw an exception if their IP isn't routable
20
+ unless self.class._base_path
21
+ raise "Can't instanciate without a base_path"
22
+ end
23
+
24
+ methods = parse_endpoint_data(transport.get(self.class._base_path))
25
+
26
+ methods.each do |m|
27
+ method_name = m.name.gsub(/-/, "_").to_sym
28
+ if m.finder
29
+ eigenclass.send(:define_method, method_name) do
30
+ Finder.new("#{self.class._base_path}/#{m.name}/")
31
+ end
32
+ else
33
+ eigenclass.send(:define_method, method_name) do
34
+ transport.get("#{self.class._base_path}/#{m.name}")
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def transport
41
+ @transport ||= Transport.new
42
+ end
43
+
44
+ def parse_endpoint_data(data)
45
+ ret = []
46
+ data.split("\n").each do |el|
47
+ if el.end_with? "/"
48
+ el.gsub!(/\/$/, "")
49
+ ret << Record.new(el, true)
50
+ else
51
+ ret << Record.new(el, false)
52
+ end
53
+ end
54
+ return ret
55
+ end
56
+
57
+ def eigenclass
58
+ class << self
59
+ self
60
+ end
61
+ end
62
+
63
+ end
64
+
65
+ class Finder
66
+ def self.new(path)
67
+ Class.new(Base) do
68
+ base_path(path)
69
+ end.new
70
+ end
71
+ end
72
+ end
@@ -1,12 +1,7 @@
1
1
  module AWSData
2
- class Metadata
2
+ class Metadata < Base
3
3
 
4
- def latest
5
- transport.get("/latest/meta-data")
6
- end
4
+ base_path "/latest/meta-data/"
7
5
 
8
- def transport
9
- @transport ||= Transport.new
10
- end
11
6
  end
12
7
  end
@@ -1,15 +1,20 @@
1
1
  require 'net/http'
2
2
 
3
3
  module AWSData
4
+ class EndpointUnavailable < Exception
5
+ end
6
+
4
7
  class Transport
5
8
 
9
+ ENDPOINT_IP = "169.254.169.254"
10
+
6
11
  def get(path)
7
12
  uri = uri_for(path)
8
13
  res = Net::HTTP.get_response(uri)
9
14
  if res.is_a?(Net::HTTPSuccess)
10
- return JSON.parse(res.body)
15
+ return res.body
11
16
  else
12
- raise "oops"
17
+ raise EndpointUnavailable
13
18
  end
14
19
  end
15
20
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richo Healey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-05 00:00:00.000000000 Z
11
+ date: 2013-12-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Fetch instance data from AWS
14
14
  email:
@@ -20,6 +20,7 @@ files:
20
20
  - README.md
21
21
  - aws-data.gemspec
22
22
  - lib/aws-data.rb
23
+ - lib/aws-data/base.rb
23
24
  - lib/aws-data/metadata.rb
24
25
  - lib/aws-data/transport.rb
25
26
  homepage: http://github.com/richo/aws-data