pec2 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pec2/core.rb +23 -0
  3. data/lib/pec2/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4163010c695efbf95f5a4f1b6effbba828afa98
4
- data.tar.gz: 63497eff0b0c1f2a881d79fbb7bb1ecd56a90022
3
+ metadata.gz: 9511d7463442954a44245ab355178e32088c9aed
4
+ data.tar.gz: b02546a6e4a0b69a4ff537a4af44665d7cfa473b
5
5
  SHA512:
6
- metadata.gz: 8cb7edce5b870a3e94066fc45b56f2aa15a73457b3f6b9b56af114da478cca71a046a437a95aa39cae3c2dea4d4c18f98e08abaade9294b0346713db7826b3d7
7
- data.tar.gz: 6cc1c66c3d9c675cb697858b7efbb576638a4bf2d37e2882d1add07d5d4955301c1cde573bdee73122b95b51a62ab29ed5e905302dd20aba330fab2b58424e9e
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
@@ -1,4 +1,4 @@
1
1
  module Pec2
2
2
  # pec2 version
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pec2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - toyama0919