pec2 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pec2/core.rb +23 -0
- data/lib/pec2/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9511d7463442954a44245ab355178e32088c9aed
|
4
|
+
data.tar.gz: b02546a6e4a0b69a4ff537a4af44665d7cfa473b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2fb7df4e7944cdb94987602b9e5fccfee0dbbaf33c3f185debf8f3d34f1fc1a05a7de820980f9d2c4e91b295436cb84402bd62302cade6a3d76562df1a06ce6
|
7
|
+
data.tar.gz: 17da78af4212b91ed740fa4bb77e0291fe2c74d71a0e410bfd367d9634b766158f1c69294e4ad4453dab4779be6eef9a57108b1a26c8aad47cdc19e99771426c
|
data/lib/pec2/core.rb
CHANGED
@@ -1,12 +1,18 @@
|
|
1
1
|
require 'aws-sdk'
|
2
2
|
require 'hashie'
|
3
3
|
require 'logger'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'json'
|
6
|
+
require 'timeout'
|
7
|
+
|
4
8
|
|
5
9
|
module Pec2
|
6
10
|
class Core
|
11
|
+
TIME_OUT = 3
|
7
12
|
|
8
13
|
def initialize
|
9
14
|
@logger = Logger.new(STDOUT)
|
15
|
+
ENV['AWS_REGION'] = ENV['AWS_REGION'] || get_document['region']
|
10
16
|
@ec2 = Aws::EC2::Client.new
|
11
17
|
end
|
12
18
|
|
@@ -20,5 +26,22 @@ module Pec2
|
|
20
26
|
filters: filter
|
21
27
|
).data.to_h[:reservations].map { |instance| Hashie::Mash.new(instance[:instances].first) }
|
22
28
|
end
|
29
|
+
|
30
|
+
def get_document
|
31
|
+
JSON.parse(get_metadata('/latest/dynamic/instance-identity/document/'))
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_metadata(path)
|
35
|
+
begin
|
36
|
+
result = {}
|
37
|
+
Timeout.timeout(TIME_OUT) {
|
38
|
+
body = open('http://169.254.169.254' + path).read
|
39
|
+
return body
|
40
|
+
}
|
41
|
+
return result
|
42
|
+
rescue Timeout::Error => e
|
43
|
+
raise "not EC2 instance"
|
44
|
+
end
|
45
|
+
end
|
23
46
|
end
|
24
47
|
end
|
data/lib/pec2/version.rb
CHANGED