aws_helper 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/awshelper/ec2.rb CHANGED
@@ -1,142 +1,142 @@
1
- #
2
- # Copyright:: Copyright (c) 2009 Opscode, Inc.
3
- # License:: Apache License, Version 2.0
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- require 'json'
19
- require 'open-uri'
20
- require 'syslog'
21
- require 'right_aws'
22
-
23
-
24
- module Awshelper
25
- module Ec2
26
- def find_snapshot_id(volume_id="", find_most_recent=false)
27
- snapshot_id = nil
28
- snapshots = if find_most_recent
29
- ec2.describe_snapshots.sort { |a,b| a[:aws_started_at] <=> b[:aws_started_at] }
30
- else
31
- ec2.describe_snapshots.sort { |a,b| b[:aws_started_at] <=> a[:aws_started_at] }
32
- end
33
- snapshots.each do |snapshot|
34
- if snapshot[:aws_volume_id] == volume_id
35
- snapshot_id = snapshot[:aws_id]
36
- end
37
- end
38
- log("Cannot find snapshot id!",'err') unless snapshot_id
39
- raise "Cannot find snapshot id!" unless snapshot_id
40
- log("Snapshot ID is #{snapshot_id}")
41
- snapshot_id
42
- end
43
-
44
- def ec2
45
- @@ec2 ||= create_aws_interface
46
- #(RightAws::Ec2)
47
- end
48
-
49
- def instance_id
50
- @@instance_id ||= query_instance_id
51
- end
52
-
53
- def ami_id
54
- @@ami_id ||= query_ami_id
55
- end
56
-
57
- def local_ipv4
58
- @@local_ipv4 ||= query_local_ipv4
59
- end
60
-
61
- def instance_availability_zone
62
- @@instance_availability_zone ||= query_instance_availability_zone
63
- end
64
-
65
- private
66
-
67
- def create_aws_interface
68
-
69
- region = instance_availability_zone
70
- region = region[0, region.length-1]
71
-
72
- aws_access_key = ENV['AWS_ACCESS_KEY_ID']
73
- aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
74
-
75
- if aws_access_key and aws_secret_access_key
76
- RightAws::Ec2.new(aws_access_key,aws_secret_access_key, {:region => region}) # :logger => Chef::Log,
77
- else
78
- log("No env var AWS_ACCESS_KEY_ID and 'AWS_SECRET_ACCESS_KEY so trying role credentials")
79
- creds = query_role_credentials
80
- RightAws::Ec2.new(creds['AccessKeyId'], creds['SecretAccessKey'], { :region => region, :token => creds['Token']})
81
- end
82
- end
83
-
84
- def query_role
85
- r = open("http://169.254.169.254/latest/meta-data/iam/security-credentials/",options = {:proxy => false}).readlines.first
86
- r
87
- end
88
-
89
- def query_role_credentials(role = query_role)
90
- log("Instance has no IAM role.",'err') if role.to_s.empty?
91
- fail "Instance has no IAM role." if role.to_s.empty?
92
- creds = open("http://169.254.169.254/latest/meta-data/iam/security-credentials/#{role}",options = {:proxy => false}){|f| JSON.parse(f.string)}
93
- log("Retrieved instance credentials for IAM role #{role}")
94
- creds
95
- end
96
-
97
- def query_instance_id
98
- instance_id = open('http://169.254.169.254/latest/meta-data/instance-id',options = {:proxy => false}){|f| f.gets}
99
- log("Cannot find instance id!",'err') unless instance_id
100
- raise "Cannot find instance id!" unless instance_id
101
- log("Instance ID is #{instance_id}")
102
- instance_id
103
- end
104
-
105
- def query_ami_id
106
- ami_id = open('http://169.254.169.254/latest/meta-data/ami-id',options = {:proxy => false}){|f| f.gets}
107
- log("Cannot find ami id!",'err') unless ami_id
108
- raise "Cannot find instance id!" unless ami_id
109
- log("Aim ID is #{ami_id}")
110
- ami_id
111
- end
112
-
113
- def query_local_ipv4
114
- local_ipv4 = open('http://169.254.169.254/latest/meta-data/local-ipv4',options = {:proxy => false}){|f| f.gets}
115
- log("Cannot find local_ipv4!",'err') unless local_ipv4
116
- raise "Cannot find local_ipv4!" unless local_ipv4
117
- log("local_ipv4 is #{local_ipv4}")
118
- local_ipv4
119
- end
120
-
121
- def query_instance_availability_zone
122
- availability_zone = open('http://169.254.169.254/latest/meta-data/placement/availability-zone/', options = {:proxy => false}){|f| f.gets}
123
- log("Cannot find availability zone!",'err') unless availability_zone
124
- raise "Cannot find availability zone!" unless availability_zone
125
- log("Instance's availability zone is #{availability_zone}")
126
- availability_zone
127
- end
128
-
129
- def log(message,type="info")
130
- # $0 is the current script name
131
- puts message
132
- Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.info message } if type == "info"
133
- Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.info message } if type == "err"
134
- end
135
-
136
- def proxy
137
- p = ENV['HTTP_PROXY'] if ENV['HTTP_PROXY']
138
- p
139
- end
140
- end
141
- end
142
-
1
+ #
2
+ # Copyright:: Copyright (c) 2009 Opscode, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'json'
19
+ require 'open-uri'
20
+ require 'syslog'
21
+ require 'right_aws'
22
+
23
+
24
+ module Awshelper
25
+ module Ec2
26
+ def find_snapshot_id(volume_id="", find_most_recent=false)
27
+ snapshot_id = nil
28
+ snapshots = if find_most_recent
29
+ ec2.describe_snapshots.sort { |a,b| a[:aws_started_at] <=> b[:aws_started_at] }
30
+ else
31
+ ec2.describe_snapshots.sort { |a,b| b[:aws_started_at] <=> a[:aws_started_at] }
32
+ end
33
+ snapshots.each do |snapshot|
34
+ if snapshot[:aws_volume_id] == volume_id
35
+ snapshot_id = snapshot[:aws_id]
36
+ end
37
+ end
38
+ log("Cannot find snapshot id!",'err') unless snapshot_id
39
+ raise "Cannot find snapshot id!" unless snapshot_id
40
+ log("Snapshot ID is #{snapshot_id}")
41
+ snapshot_id
42
+ end
43
+
44
+ def ec2
45
+ @@ec2 ||= create_aws_interface
46
+ #(RightAws::Ec2)
47
+ end
48
+
49
+ def instance_id
50
+ @@instance_id ||= query_instance_id
51
+ end
52
+
53
+ def ami_id
54
+ @@ami_id ||= query_ami_id
55
+ end
56
+
57
+ def local_ipv4
58
+ @@local_ipv4 ||= query_local_ipv4
59
+ end
60
+
61
+ def instance_availability_zone
62
+ @@instance_availability_zone ||= query_instance_availability_zone
63
+ end
64
+
65
+ private
66
+
67
+ def create_aws_interface
68
+
69
+ region = instance_availability_zone
70
+ region = region[0, region.length-1]
71
+
72
+ aws_access_key = ENV['AWS_ACCESS_KEY_ID']
73
+ aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
74
+
75
+ if aws_access_key and aws_secret_access_key
76
+ RightAws::Ec2.new(aws_access_key,aws_secret_access_key, {:region => region}) # :logger => Chef::Log,
77
+ else
78
+ log("No env var AWS_ACCESS_KEY_ID and 'AWS_SECRET_ACCESS_KEY so trying role credentials")
79
+ creds = query_role_credentials
80
+ RightAws::Ec2.new(creds['AccessKeyId'], creds['SecretAccessKey'], { :region => region, :token => creds['Token']})
81
+ end
82
+ end
83
+
84
+ def query_role
85
+ r = open("http://169.254.169.254/latest/meta-data/iam/security-credentials/",options = {:proxy => false}).readlines.first
86
+ r
87
+ end
88
+
89
+ def query_role_credentials(role = query_role)
90
+ log("Instance has no IAM role.",'err') if role.to_s.empty?
91
+ fail "Instance has no IAM role." if role.to_s.empty?
92
+ creds = open("http://169.254.169.254/latest/meta-data/iam/security-credentials/#{role}",options = {:proxy => false}){|f| JSON.parse(f.string)}
93
+ log("Retrieved instance credentials for IAM role #{role}")
94
+ creds
95
+ end
96
+
97
+ def query_instance_id
98
+ instance_id = open('http://169.254.169.254/latest/meta-data/instance-id',options = {:proxy => false}){|f| f.gets}
99
+ log("Cannot find instance id!",'err') unless instance_id
100
+ raise "Cannot find instance id!" unless instance_id
101
+ log("Instance ID is #{instance_id}")
102
+ instance_id
103
+ end
104
+
105
+ def query_ami_id
106
+ ami_id = open('http://169.254.169.254/latest/meta-data/ami-id',options = {:proxy => false}){|f| f.gets}
107
+ log("Cannot find ami id!",'err') unless ami_id
108
+ raise "Cannot find instance id!" unless ami_id
109
+ log("Aim ID is #{ami_id}")
110
+ ami_id
111
+ end
112
+
113
+ def query_local_ipv4
114
+ local_ipv4 = open('http://169.254.169.254/latest/meta-data/local-ipv4',options = {:proxy => false}){|f| f.gets}
115
+ log("Cannot find local_ipv4!",'err') unless local_ipv4
116
+ raise "Cannot find local_ipv4!" unless local_ipv4
117
+ log("local_ipv4 is #{local_ipv4}")
118
+ local_ipv4
119
+ end
120
+
121
+ def query_instance_availability_zone
122
+ availability_zone = open('http://169.254.169.254/latest/meta-data/placement/availability-zone/', options = {:proxy => false}){|f| f.gets}
123
+ log("Cannot find availability zone!",'err') unless availability_zone
124
+ raise "Cannot find availability zone!" unless availability_zone
125
+ log("Instance's availability zone is #{availability_zone}")
126
+ availability_zone
127
+ end
128
+
129
+ def log(message,type="info")
130
+ # $0 is the current script name
131
+ puts message
132
+ Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.info message } if type == "info"
133
+ Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.info message } if type == "err"
134
+ end
135
+
136
+ def proxy
137
+ p = ENV['HTTP_PROXY'] if ENV['HTTP_PROXY']
138
+ p
139
+ end
140
+ end
141
+ end
142
+
data/lib/awshelper/elb.rb CHANGED
@@ -1,12 +1,12 @@
1
- require File.join(File.dirname(__FILE__), 'ec2')
2
-
3
- module AwsHelper
4
- module Elb
5
- include AwsHelper::Ec2
6
-
7
- def elb
8
- @@elb ||= create_aws_interface(RightAws::ElbInterface)
9
- end
10
- end
11
- end
12
- end
1
+ require File.join(File.dirname(__FILE__), 'ec2')
2
+
3
+ module AwsHelper
4
+ module Elb
5
+ include AwsHelper::Ec2
6
+
7
+ def elb
8
+ @@elb ||= create_aws_interface(RightAws::ElbInterface)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
- module Awshelper
2
- VERSION = "0.0.7"
3
- end
1
+ module Awshelper
2
+ VERSION = "0.0.8"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neill Turner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-28 00:00:00.000000000 Z
11
+ date: 2015-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: right_aws
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  version: '0'
75
75
  requirements: []
76
76
  rubyforge_project: "[none]"
77
- rubygems_version: 2.2.3
77
+ rubygems_version: 2.2.2
78
78
  signing_key:
79
79
  specification_version: 4
80
80
  summary: Aws Helper for an instance