aws-ec2 0.9.0 → 1.0.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 +4 -4
- data/.gitignore +2 -1
- data/.gitmodules +0 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +18 -10
- data/LICENSE.txt +1 -1
- data/README.md +74 -7
- data/Rakefile +1 -1
- data/aws-ec2.gemspec +7 -5
- data/lib/aws-ec2.rb +5 -2
- data/lib/aws_ec2/ami.rb +1 -1
- data/lib/aws_ec2/base.rb +34 -1
- data/lib/aws_ec2/cli.rb +20 -1
- data/lib/aws_ec2/command.rb +34 -5
- data/lib/aws_ec2/completer.rb +161 -0
- data/lib/aws_ec2/completer/script.rb +6 -0
- data/lib/aws_ec2/completer/script.sh +10 -0
- data/lib/aws_ec2/config.rb +4 -2
- data/lib/aws_ec2/core.rb +5 -1
- data/lib/aws_ec2/create.rb +11 -8
- data/lib/aws_ec2/create/error_messages.rb +1 -1
- data/lib/aws_ec2/create/params.rb +2 -6
- data/lib/aws_ec2/help/completion.md +22 -0
- data/lib/aws_ec2/help/completion_script.md +3 -0
- data/lib/aws_ec2/profile.rb +26 -19
- data/lib/aws_ec2/script.rb +1 -0
- data/lib/aws_ec2/script/compile.rb +15 -6
- data/lib/aws_ec2/script/compress.rb +62 -0
- data/lib/aws_ec2/script/upload.rb +75 -9
- data/lib/aws_ec2/setting.rb +41 -0
- data/lib/aws_ec2/template.rb +13 -0
- data/lib/aws_ec2/template/context.rb +32 -0
- data/lib/aws_ec2/template/helper.rb +17 -0
- data/lib/aws_ec2/{template_helper → template/helper}/ami_helper.rb +8 -3
- data/lib/aws_ec2/template/helper/core_helper.rb +88 -0
- data/lib/aws_ec2/{template_helper → template/helper}/partial_helper.rb +2 -2
- data/lib/aws_ec2/template/helper/script_helper.rb +53 -0
- data/lib/aws_ec2/template/helper/ssh_key_helper.rb +21 -0
- data/lib/aws_ec2/version.rb +1 -1
- data/spec/lib/cli_spec.rb +14 -0
- data/spec/spec_helper.rb +16 -6
- metadata +54 -14
- data/lib/aws_ec2/template_helper.rb +0 -18
- data/lib/aws_ec2/template_helper/core_helper.rb +0 -98
@@ -0,0 +1,21 @@
|
|
1
|
+
module AwsEc2::Template::Helper::SshKeyHelper
|
2
|
+
def add_ssh_key(user="ec2-user")
|
3
|
+
key_path = "#{ENV['HOME']}/.ssh/id_rsa.pub"
|
4
|
+
if File.exist?(key_path)
|
5
|
+
public_key = IO.read(key_path).strip
|
6
|
+
end
|
7
|
+
if public_key
|
8
|
+
<<-SCRIPT
|
9
|
+
# Automatically add user's public key from #{key_path}
|
10
|
+
cp /home/#{user}/.ssh/authorized_keys{,.bak}
|
11
|
+
echo #{public_key} >> /home/#{user}/.ssh/authorized_keys
|
12
|
+
chown #{user}:#{user} /home/#{user}/.ssh/authorized_keys
|
13
|
+
SCRIPT
|
14
|
+
else
|
15
|
+
<<-SCRIPT
|
16
|
+
# WARN: unable to find a ~/.ssh/id_rsa.pub locally on your machine. user: #{ENV}['USER']
|
17
|
+
# Unable to automatically add the public key
|
18
|
+
SCRIPT
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/aws_ec2/version.rb
CHANGED
data/spec/lib/cli_spec.rb
CHANGED
@@ -20,5 +20,19 @@ describe AwsEc2::CLI do
|
|
20
20
|
out = execute("exe/aws-ec2 ami myimage #{@args}")
|
21
21
|
expect(out).to include("Creating EC2 instance")
|
22
22
|
end
|
23
|
+
|
24
|
+
commands = {
|
25
|
+
"am" => "ami",
|
26
|
+
"compile" => "--profile",
|
27
|
+
"create -" => "--profile",
|
28
|
+
"create" => "name",
|
29
|
+
"create name --" => "--profile",
|
30
|
+
}
|
31
|
+
commands.each do |command, expected_word|
|
32
|
+
it "completion #{command}" do
|
33
|
+
out = execute("exe/aws-ec2 completion #{command}")
|
34
|
+
expect(out).to include(expected_word) # only checking for one word for simplicity
|
35
|
+
end
|
36
|
+
end
|
23
37
|
end
|
24
38
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,20 +4,30 @@ ENV["AWS_EC2_ROOT"] = "spec/fixtures/demo_project"
|
|
4
4
|
# Ensures aws api never called. Fixture home folder does not contain ~/.aws/credentails
|
5
5
|
ENV['HOME'] = "spec/fixtures/home"
|
6
6
|
|
7
|
-
|
7
|
+
# CodeClimate test coverage: https://docs.codeclimate.com/docs/configuring-test-coverage
|
8
|
+
# require 'simplecov'
|
9
|
+
# SimpleCov.start
|
8
10
|
|
9
|
-
|
11
|
+
require "pp"
|
12
|
+
require "byebug"
|
13
|
+
root = File.expand_path("../", File.dirname(__FILE__))
|
10
14
|
require "#{root}/lib/aws-ec2"
|
11
15
|
|
12
|
-
module
|
16
|
+
module Helper
|
13
17
|
def execute(cmd)
|
14
|
-
puts "Running: #{cmd}" if
|
18
|
+
puts "Running: #{cmd}" if show_command?
|
15
19
|
out = `#{cmd}`
|
16
|
-
puts out if
|
20
|
+
puts out if show_command?
|
17
21
|
out
|
18
22
|
end
|
23
|
+
|
24
|
+
# Added SHOW_COMMAND because DEBUG is also used by other libraries like
|
25
|
+
# bundler and it shows its internal debugging logging also.
|
26
|
+
def show_command?
|
27
|
+
ENV['DEBUG'] || ENV['SHOW_COMMAND']
|
28
|
+
end
|
19
29
|
end
|
20
30
|
|
21
31
|
RSpec.configure do |c|
|
22
|
-
c.include
|
32
|
+
c.include Helper
|
23
33
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aws-ec2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tung Nguyen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-02-
|
11
|
+
date: 2018-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: aws-sdk-ec2
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: filesize
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: hashie
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
@@ -95,13 +95,13 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
98
|
+
name: render_me_pretty
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
|
-
type: :
|
104
|
+
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
@@ -109,7 +109,21 @@ dependencies:
|
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
112
|
+
name: thor
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: bundler
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
114
128
|
requirements:
|
115
129
|
- - ">="
|
@@ -123,7 +137,7 @@ dependencies:
|
|
123
137
|
- !ruby/object:Gem::Version
|
124
138
|
version: '0'
|
125
139
|
- !ruby/object:Gem::Dependency
|
126
|
-
name:
|
140
|
+
name: byebug
|
127
141
|
requirement: !ruby/object:Gem::Requirement
|
128
142
|
requirements:
|
129
143
|
- - ">="
|
@@ -178,6 +192,20 @@ dependencies:
|
|
178
192
|
- - ">="
|
179
193
|
- !ruby/object:Gem::Version
|
180
194
|
version: '0'
|
195
|
+
- !ruby/object:Gem::Dependency
|
196
|
+
name: rake
|
197
|
+
requirement: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
type: :development
|
203
|
+
prerelease: false
|
204
|
+
version_requirements: !ruby/object:Gem::Requirement
|
205
|
+
requirements:
|
206
|
+
- - ">="
|
207
|
+
- !ruby/object:Gem::Version
|
208
|
+
version: '0'
|
181
209
|
description: Simple tool to create AWS ec2 instances consistently with pre-configured
|
182
210
|
settings
|
183
211
|
email:
|
@@ -188,6 +216,7 @@ extensions: []
|
|
188
216
|
extra_rdoc_files: []
|
189
217
|
files:
|
190
218
|
- ".gitignore"
|
219
|
+
- ".gitmodules"
|
191
220
|
- ".rspec"
|
192
221
|
- CHANGELOG.md
|
193
222
|
- Gemfile
|
@@ -212,6 +241,9 @@ files:
|
|
212
241
|
- lib/aws_ec2/base.rb
|
213
242
|
- lib/aws_ec2/cli.rb
|
214
243
|
- lib/aws_ec2/command.rb
|
244
|
+
- lib/aws_ec2/completer.rb
|
245
|
+
- lib/aws_ec2/completer/script.rb
|
246
|
+
- lib/aws_ec2/completer/script.sh
|
215
247
|
- lib/aws_ec2/config.rb
|
216
248
|
- lib/aws_ec2/core.rb
|
217
249
|
- lib/aws_ec2/create.rb
|
@@ -221,19 +253,27 @@ files:
|
|
221
253
|
- lib/aws_ec2/help.rb
|
222
254
|
- lib/aws_ec2/help/ami.md
|
223
255
|
- lib/aws_ec2/help/compile.md
|
256
|
+
- lib/aws_ec2/help/completion.md
|
257
|
+
- lib/aws_ec2/help/completion_script.md
|
224
258
|
- lib/aws_ec2/help/create.md
|
225
259
|
- lib/aws_ec2/help/upload.md
|
226
260
|
- lib/aws_ec2/hook.rb
|
227
261
|
- lib/aws_ec2/profile.rb
|
228
262
|
- lib/aws_ec2/script.rb
|
229
263
|
- lib/aws_ec2/script/compile.rb
|
264
|
+
- lib/aws_ec2/script/compress.rb
|
230
265
|
- lib/aws_ec2/script/upload.rb
|
231
266
|
- lib/aws_ec2/scripts/ami_creation.sh
|
232
267
|
- lib/aws_ec2/scripts/auto_terminate.sh
|
233
|
-
- lib/aws_ec2/
|
234
|
-
- lib/aws_ec2/
|
235
|
-
- lib/aws_ec2/
|
236
|
-
- lib/aws_ec2/
|
268
|
+
- lib/aws_ec2/setting.rb
|
269
|
+
- lib/aws_ec2/template.rb
|
270
|
+
- lib/aws_ec2/template/context.rb
|
271
|
+
- lib/aws_ec2/template/helper.rb
|
272
|
+
- lib/aws_ec2/template/helper/ami_helper.rb
|
273
|
+
- lib/aws_ec2/template/helper/core_helper.rb
|
274
|
+
- lib/aws_ec2/template/helper/partial_helper.rb
|
275
|
+
- lib/aws_ec2/template/helper/script_helper.rb
|
276
|
+
- lib/aws_ec2/template/helper/ssh_key_helper.rb
|
237
277
|
- lib/aws_ec2/version.rb
|
238
278
|
- spec/fixtures/demo_project/config/test.yml
|
239
279
|
- spec/fixtures/demo_project/profiles/default.yml
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require "active_support" # for autoload
|
2
|
-
require "active_support/core_ext/string"
|
3
|
-
|
4
|
-
module AwsEc2
|
5
|
-
module TemplateHelper
|
6
|
-
# auto load all the template_helpers
|
7
|
-
template_helper_path = File.expand_path("../template_helper", __FILE__)
|
8
|
-
Dir.glob("#{template_helper_path}/*").each do |path|
|
9
|
-
next if File.directory?(path)
|
10
|
-
filename = File.basename(path, '.rb')
|
11
|
-
class_name = filename.classify
|
12
|
-
instance_eval do
|
13
|
-
autoload class_name.to_sym, "aws_ec2/template_helper/#{filename}"
|
14
|
-
include const_get(class_name)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,98 +0,0 @@
|
|
1
|
-
require "base64"
|
2
|
-
require "erb"
|
3
|
-
|
4
|
-
module AwsEc2::TemplateHelper::CoreHelper
|
5
|
-
def user_data(name, base64=true)
|
6
|
-
# allow user to specify the path also
|
7
|
-
if File.exist?(name)
|
8
|
-
name = File.basename(name) # normalize name, change path to name
|
9
|
-
end
|
10
|
-
name = File.basename(name, '.sh')
|
11
|
-
path = "#{AwsEc2.root}/app/user-data/#{name}.sh"
|
12
|
-
result = erb_result(path)
|
13
|
-
result = append_scripts(result)
|
14
|
-
|
15
|
-
# save the unencoded user-data script for easy debugging
|
16
|
-
temp_path = "#{AwsEc2.root}/tmp/user-data.txt"
|
17
|
-
FileUtils.mkdir_p(File.dirname(temp_path))
|
18
|
-
IO.write(temp_path, result)
|
19
|
-
|
20
|
-
base64 ? Base64.encode64(result).strip : result
|
21
|
-
end
|
22
|
-
|
23
|
-
# provides access to config/* settings as variables
|
24
|
-
# AWS_EC2_ENV=development => config/development.yml
|
25
|
-
# AWS_EC2_ENV=production => config/production.yml
|
26
|
-
def config
|
27
|
-
AwsEc2.config
|
28
|
-
end
|
29
|
-
|
30
|
-
# pretty timestamp that is useful for ami ids.
|
31
|
-
# the timestamp is generated once and cached.
|
32
|
-
def timestamp
|
33
|
-
@timestamp ||= Time.now.strftime("%Y-%m-%d-%H-%M-%S")
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
def append_scripts(user_data)
|
38
|
-
# assuming user-data script is a bash script for simplicity
|
39
|
-
script = AwsEc2::Script.new(@options)
|
40
|
-
user_data += script.auto_terminate if @options[:auto_terminate]
|
41
|
-
user_data += script.create_ami if @options[:ami_name]
|
42
|
-
user_data
|
43
|
-
end
|
44
|
-
|
45
|
-
# Load custom helper methods from the project repo
|
46
|
-
def load_custom_helpers
|
47
|
-
Dir.glob("#{AwsEc2.root}/app/helpers/**/*_helper.rb").each do |path|
|
48
|
-
filename = path.sub(%r{.*/},'').sub('.rb','')
|
49
|
-
module_name = filename.classify
|
50
|
-
|
51
|
-
require path
|
52
|
-
self.class.send :include, module_name.constantize
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
def erb_result(path)
|
58
|
-
load_custom_helpers
|
59
|
-
template = IO.read(path)
|
60
|
-
|
61
|
-
# Allow a way to bypass the custom ERB error handling in case
|
62
|
-
# the error is in the lambdagem code.
|
63
|
-
if ENV['DEBUG']
|
64
|
-
return ERB.new(template, nil, "-").result(binding)
|
65
|
-
end
|
66
|
-
|
67
|
-
begin
|
68
|
-
ERB.new(template, nil, "-").result(binding)
|
69
|
-
rescue Exception => e
|
70
|
-
puts e
|
71
|
-
|
72
|
-
# how to know where ERB stopped? - https://www.ruby-forum.com/topic/182051
|
73
|
-
# syntax errors have the (erb):xxx info in e.message
|
74
|
-
# undefined variables have (erb):xxx info in e.backtrac
|
75
|
-
error_info = e.message.split("\n").grep(/\(erb\)/)[0]
|
76
|
-
error_info ||= e.backtrace.grep(/\(erb\)/)[0]
|
77
|
-
raise unless error_info # unable to find the (erb):xxx: error line
|
78
|
-
line = error_info.split(':')[1].to_i
|
79
|
-
puts "Error evaluating ERB template on line #{line.to_s.colorize(:red)} of: #{path.sub(/^\.\//, '')}"
|
80
|
-
|
81
|
-
template_lines = template.split("\n")
|
82
|
-
context = 5 # lines of context
|
83
|
-
top, bottom = [line-context-1, 0].max, line+context-1
|
84
|
-
spacing = template_lines.size.to_s.size
|
85
|
-
template_lines[top..bottom].each_with_index do |line_content, index|
|
86
|
-
line_number = top+index+1
|
87
|
-
if line_number == line
|
88
|
-
printf("%#{spacing}d %s\n".colorize(:red), line_number, line_content)
|
89
|
-
else
|
90
|
-
printf("%#{spacing}d %s\n", line_number, line_content)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
puts "\nIf the this error does not make sense and the error is not in the ERB template. Run the command again with DEBUG=1 to show the full lambdagem backtrace"
|
95
|
-
exit 1 unless ENV['TEST']
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|