ec2-host 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/docs/EC2/Host/Config.html +7 -7
- data/docs/file.README.html +1 -1
- data/docs/index.html +1 -1
- data/docs/method_list.html +1 -1
- data/ec2-host.gemspec +1 -1
- data/lib/ec2/host/config.rb +18 -11
- data/lib/ec2/host/ec2_client.rb +24 -17
- data/lib/ec2/host/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad6de8f55b767b6131c58d34025197f7d50b5bf9
|
4
|
+
data.tar.gz: '0586edf0954b7480db15634a168ae2eced6a0acf'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5353b0fe64005d1f69f3197ac767d87058aafe0a03f9d101859491d6486c4d828761b1969571bffd69c9d4714b7e1267dad0971cfebccbca409defaac2d66fc6
|
7
|
+
data.tar.gz: b6576721559e850cf1db0abe94aaf1bbb83272a7be7e562957df7cfab0e5d09fa7c19190db2e5428571de28943c3c6a1bff3ac66abfa2d59cfd57fea77ae1ae3
|
data/CHANGELOG.md
CHANGED
data/docs/EC2/Host/Config.html
CHANGED
@@ -134,7 +134,7 @@
|
|
134
134
|
<li class="public ">
|
135
135
|
<span class="summary_signature">
|
136
136
|
|
137
|
-
<a href="#
|
137
|
+
<a href="#aws_credentials_file-class_method" title="aws_credentials_file (class method)">.<strong>aws_credentials_file</strong> ⇒ Object </a>
|
138
138
|
|
139
139
|
|
140
140
|
|
@@ -658,11 +658,11 @@
|
|
658
658
|
</div>
|
659
659
|
|
660
660
|
|
661
|
-
<span id="
|
661
|
+
<span id="aws_credentials_file=-class_method"></span>
|
662
662
|
<div class="method_details ">
|
663
|
-
<h3 class="signature " id="
|
663
|
+
<h3 class="signature " id="aws_credentials_file-class_method">
|
664
664
|
|
665
|
-
.<strong>
|
665
|
+
.<strong>aws_credentials_file</strong> ⇒ <tt>Object</tt>
|
666
666
|
|
667
667
|
|
668
668
|
|
@@ -684,8 +684,8 @@
|
|
684
684
|
<td>
|
685
685
|
<pre class="code"><span class="info file"># File 'lib/ec2/host/config.rb', line 55</span>
|
686
686
|
|
687
|
-
<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier
|
688
|
-
<span class='ivar'>@
|
687
|
+
<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_aws_credentials_file'>aws_credentials_file</span>
|
688
|
+
<span class='ivar'>@aws_credentials_file</span> <span class='op'>||=</span>
|
689
689
|
<span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>AWS_CREDENTIALS_FILE</span><span class='tstring_end'>'</span></span><span class='rbracket'>]</span> <span class='op'>||</span> <span class='id identifier rubyid_config'>config</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>AWS_CREDENTIALS_FILE</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='kw'>nil</span><span class='rparen'>)</span> <span class='op'>||</span>
|
690
690
|
<span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>AWS_CREDENTIAL_FILE</span><span class='tstring_end'>'</span></span><span class='rbracket'>]</span> <span class='op'>||</span> <span class='id identifier rubyid_config'>config</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>AWS_CREDENTIAL_FILE</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='kw'>nil</span><span class='rparen'>)</span> <span class='op'>||</span> <span class='comment'># ref. aws cli (supported lately)
|
691
691
|
</span> <span class='const'>File</span><span class='period'>.</span><span class='id identifier rubyid_expand_path'>expand_path</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>~/.aws/credentials</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
|
@@ -1373,4 +1373,4 @@
|
|
1373
1373
|
|
1374
1374
|
</div>
|
1375
1375
|
</body>
|
1376
|
-
</html>
|
1376
|
+
</html>
|
data/docs/file.README.html
CHANGED
data/docs/index.html
CHANGED
data/docs/method_list.html
CHANGED
@@ -94,7 +94,7 @@
|
|
94
94
|
|
95
95
|
<li class="odd ">
|
96
96
|
<div class="item">
|
97
|
-
<span class='object_link'><a href="EC2/Host/Config.html#
|
97
|
+
<span class='object_link'><a href="EC2/Host/Config.html#aws_credentials_file-class_method" title="EC2::Host::Config.aws_credentials_file (method)">aws_credentials_file</a></span>
|
98
98
|
<small>EC2::Host::Config</small>
|
99
99
|
</div>
|
100
100
|
</li>
|
data/ec2-host.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
|
|
18
18
|
|
19
19
|
gem.add_runtime_dependency 'aws-sdk-ec2'
|
20
20
|
gem.add_runtime_dependency 'dotenv'
|
21
|
-
gem.add_runtime_dependency '
|
21
|
+
gem.add_runtime_dependency 'inifile'
|
22
22
|
|
23
23
|
gem.add_development_dependency 'yard'
|
24
24
|
gem.add_development_dependency 'rdoc'
|
data/lib/ec2/host/config.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'dotenv'
|
2
|
-
require '
|
2
|
+
require 'inifile'
|
3
3
|
Dotenv.load
|
4
4
|
|
5
5
|
class EC2
|
@@ -11,7 +11,7 @@ class EC2
|
|
11
11
|
:aws_profile,
|
12
12
|
:aws_access_key_id,
|
13
13
|
:aws_secret_access_key,
|
14
|
-
:
|
14
|
+
:aws_credentials_file,
|
15
15
|
:log_level,
|
16
16
|
:hostname_tag,
|
17
17
|
:roles_tag,
|
@@ -33,7 +33,7 @@ class EC2
|
|
33
33
|
@aws_region ||=
|
34
34
|
ENV['AWS_REGION'] || config.fetch('AWS_REGION', nil) || # ref. old aws cli
|
35
35
|
ENV['AWS_DEFAULT_REGION'] || config.fetch('AWS_DEFAULT_REGION', nil) || # ref. aws cli and terraform
|
36
|
-
aws_config['region'] || raise('AWS_REGION nor AWS_DEFAULT_REGION is not set')
|
36
|
+
aws_config['region'] || raise('AWS_REGION nor AWS_DEFAULT_REGION nor reagion in ~/.aws/config is not set')
|
37
37
|
end
|
38
38
|
|
39
39
|
def self.aws_profile
|
@@ -52,12 +52,16 @@ class EC2
|
|
52
52
|
@aws_secret_access_key ||= ENV['AWS_SECRET_ACCESS_KEY'] || config.fetch('AWS_SECRET_ACCESS_KEY', nil)
|
53
53
|
end
|
54
54
|
|
55
|
-
def self.
|
56
|
-
@
|
57
|
-
ENV['AWS_CREDENTIALS_FILE'] || config.fetch('AWS_CREDENTIALS_FILE', nil) ||
|
58
|
-
ENV['AWS_CREDENTIAL_FILE'] || config.fetch('AWS_CREDENTIAL_FILE', nil) || #
|
55
|
+
def self.aws_credentials_file
|
56
|
+
@aws_credentials_file ||=
|
57
|
+
ENV['AWS_CREDENTIALS_FILE'] || config.fetch('AWS_CREDENTIALS_FILE', nil) || # old
|
58
|
+
ENV['AWS_CREDENTIAL_FILE'] || config.fetch('AWS_CREDENTIAL_FILE', nil) || # old
|
59
|
+
ENV['AWS_SHARED_CREDENTIALS_FILE'] || config.fetch('AWS_SHARED_CREDENTIALS_FILE', nil) || # ref. https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-envvars.html
|
59
60
|
File.expand_path('~/.aws/credentials')
|
60
61
|
end
|
62
|
+
class << self
|
63
|
+
alias_method :aws_credential_file, :aws_credentials_file # for backward compatibility
|
64
|
+
end
|
61
65
|
|
62
66
|
def self.aws_config_file
|
63
67
|
@aws_config_file ||= ENV['AWS_CONFIG_FILE'] || config.fetch('AWS_CONFIG_FILE', nil) || File.expand_path('~/.aws/config')
|
@@ -65,10 +69,13 @@ class EC2
|
|
65
69
|
|
66
70
|
def self.aws_config
|
67
71
|
return @aws_config if @aws_config
|
68
|
-
if File.readable?(aws_config_file)
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
+
if File.readable?(aws_config_file)
|
73
|
+
ini = IniFile.load(aws_config_file).to_h
|
74
|
+
if aws_profile == 'default'
|
75
|
+
@aws_config = ini['default']
|
76
|
+
else
|
77
|
+
@aws_config = ini["profile #{aws_profile}"]
|
78
|
+
end
|
72
79
|
end
|
73
80
|
@aws_config ||= {}
|
74
81
|
end
|
data/lib/ec2/host/ec2_client.rb
CHANGED
@@ -35,25 +35,32 @@ class EC2
|
|
35
35
|
@ec2 ||= Aws::EC2::Client.new(region: Config.aws_region, credentials: credentials)
|
36
36
|
end
|
37
37
|
|
38
|
-
def
|
39
|
-
|
38
|
+
def ec2_instance?
|
39
|
+
(not instance_id.nil?) rescue false
|
40
|
+
end
|
41
|
+
|
42
|
+
def raw_credentials
|
43
|
+
if Config.aws_config['credential_source'] == 'Ec2InstanceMetadata'
|
44
|
+
Aws::InstanceProfileCredentials.new
|
45
|
+
elsif Config.aws_access_key_id and Config.aws_secret_access_key
|
40
46
|
Aws::Credentials.new(Config.aws_access_key_id, Config.aws_secret_access_key)
|
47
|
+
elsif File.readable?(Config.aws_credentials_file)
|
48
|
+
Aws::SharedCredentials.new(profile_name: Config.aws_profile, path: Config.aws_credentials_file)
|
49
|
+
elsif ec2_instance? # fallback to instance profile
|
50
|
+
Aws::InstanceProfileCredentials.new
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def credentials
|
55
|
+
if Config.aws_config['role_arn']
|
56
|
+
# wrapped by assume role if necessary
|
57
|
+
Aws::AssumeRoleCredentials.new(
|
58
|
+
client: Aws::STS::Client.new(raw_credentials),
|
59
|
+
role_arn: Config.aws_config['role_arn'],
|
60
|
+
role_session_name: "ec2-host-session-#{Time.now.to_i}"
|
61
|
+
)
|
41
62
|
else
|
42
|
-
|
43
|
-
if aws_config[:role_arn]
|
44
|
-
Aws::AssumeRoleCredentials.new(
|
45
|
-
client: Aws::STS::Client.new(aws_config.config_hash),
|
46
|
-
role_arn: aws_config[:role_arn],
|
47
|
-
role_session_name: "ec2-host-session-#{Time.now.to_i}"
|
48
|
-
)
|
49
|
-
elsif aws_config[:credential_source] == "Ec2InstanceMetadata"
|
50
|
-
Aws::InstanceProfileCredentials.new
|
51
|
-
else
|
52
|
-
Aws::SharedCredentials.new(
|
53
|
-
profile_name: Config.aws_profile,
|
54
|
-
path: Config.aws_credential_file
|
55
|
-
)
|
56
|
-
end
|
63
|
+
raw_credentials
|
57
64
|
end
|
58
65
|
end
|
59
66
|
|
data/lib/ec2/host/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ec2-host
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Naotoshi Seo
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: inifile
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|