aws-ec2 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +1 -4
- data/lib/aws_ec2/script/upload.rb +3 -2
- data/lib/aws_ec2/template_helper.rb +0 -96
- data/lib/aws_ec2/template_helper/core.rb +98 -0
- data/lib/aws_ec2/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7eed22f3840f438dbfab2212334abcd8e4337e850f234bd7392e42e95fd0c6e7
|
4
|
+
data.tar.gz: 4d8cb388ffe2b1cbe1e5efdfd28e4d363443b8b4eba97b5f1843220310bbb439
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfb928c51a9c89ded35ab43752a188e99bb5bd1f6f0cd784a20f977a36e4e9e1c9e1134a2379f761d77420342c2b78746bc4b8c162d9946c9a98ad1017c6a811
|
7
|
+
data.tar.gz: 2561e05ead626c13ca00b65f2760c7e34f8b63daafc02145c75e414d275c6d4706ac67f77f410e636a5593ba9a3f9d01f9e42af7e0097b11c1eb6f0cbae440bc
|
data/CHANGELOG.md
CHANGED
@@ -3,6 +3,9 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
|
5
5
|
|
6
|
+
## [0.8.1]
|
7
|
+
- reorganize template helpers into core and update readme
|
8
|
+
|
6
9
|
## [0.8.0]
|
7
10
|
- aws-ec2 upload_scripts command
|
8
11
|
- rename aws-ec2 compile to aws-ec2 compile_scripts
|
data/README.md
CHANGED
@@ -23,10 +23,7 @@ config | Access to the variables set in config/[AWS_EC2_ENV].yml.
|
|
23
23
|
latest_ami | Returns an AMI id by searching the ami name pattern and sorting in reverse older. Example: `latest_ami("ruby-2.5.0_*")`
|
24
24
|
search_ami | Returns a collection of AMI image objects based on a search pattern. The query searches on the AMI name.
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
* [aws_ec2/template_helper.rb](lib/aws_ec2/template_helper.rb).
|
29
|
-
* [aws_ec2/template_helper](lib/aws_ec2/template_helper).
|
26
|
+
For a full list of all the template helpers checkout: [aws_ec2/template_helper](lib/aws_ec2/template_helper).
|
30
27
|
|
31
28
|
You can also define your own custom helpers in the `app/helpers` folder as ruby modules with the naming convention `'*_helper.rb`. Example, the module FooHelper should be defined in `app/helpers/foo_helper.rb`. The custom helpers are first class citizens and have access to the same variables and methods as built in helpers.
|
32
29
|
|
@@ -5,12 +5,13 @@ class AwsEc2::Script
|
|
5
5
|
class Upload < AwsEc2::Base
|
6
6
|
def initialize(options={})
|
7
7
|
@options = options
|
8
|
+
@compile = @options[:compile] ? @options[:compile] : true
|
8
9
|
end
|
9
10
|
|
10
11
|
def upload
|
11
|
-
compiler.compile if @
|
12
|
+
compiler.compile if @compile
|
12
13
|
sync_scripts_to_s3
|
13
|
-
compiler.clean if @
|
14
|
+
compiler.clean if @compile and !ENV['AWS_EC2_KEEP']
|
14
15
|
end
|
15
16
|
|
16
17
|
def sync_scripts_to_s3
|
@@ -1,5 +1,3 @@
|
|
1
|
-
require "base64"
|
2
|
-
require "erb"
|
3
1
|
require "active_support/all"
|
4
2
|
|
5
3
|
module AwsEc2
|
@@ -15,99 +13,5 @@ module AwsEc2
|
|
15
13
|
include const_get(class_name)
|
16
14
|
end
|
17
15
|
end
|
18
|
-
|
19
|
-
def user_data(name, base64=true)
|
20
|
-
# allow user to specify the path also
|
21
|
-
if File.exist?(name)
|
22
|
-
name = File.basename(name) # normalize name, change path to name
|
23
|
-
end
|
24
|
-
name = File.basename(name, '.sh')
|
25
|
-
path = "#{AwsEc2.root}/app/user-data/#{name}.sh"
|
26
|
-
result = erb_result(path)
|
27
|
-
result = append_scripts(result)
|
28
|
-
|
29
|
-
# save the unencoded user-data script for easy debugging
|
30
|
-
temp_path = "/tmp/aws-ec2/user-data.txt"
|
31
|
-
FileUtils.mkdir_p(File.dirname(temp_path))
|
32
|
-
IO.write(temp_path, result)
|
33
|
-
|
34
|
-
base64 ? Base64.encode64(result).strip : result
|
35
|
-
end
|
36
|
-
|
37
|
-
# provides access to config/* settings as variables
|
38
|
-
# AWS_EC2_ENV=development => config/development.yml
|
39
|
-
# AWS_EC2_ENV=production => config/production.yml
|
40
|
-
def config
|
41
|
-
AwsEc2.config
|
42
|
-
end
|
43
|
-
|
44
|
-
# pretty timestamp that is useful for ami ids.
|
45
|
-
# the timestamp is generated once and cached.
|
46
|
-
def timestamp
|
47
|
-
@timestamp ||= Time.now.strftime("%Y-%m-%d-%H-%M-%S")
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
def append_scripts(user_data)
|
52
|
-
# assuming user-data script is a bash script for simplicity
|
53
|
-
script = AwsEc2::Script.new(@options)
|
54
|
-
user_data += script.auto_terminate if @options[:auto_terminate]
|
55
|
-
user_data += script.create_ami if @options[:ami_name]
|
56
|
-
user_data
|
57
|
-
end
|
58
|
-
|
59
|
-
# Load custom helper methods from the project repo
|
60
|
-
def load_custom_helpers
|
61
|
-
Dir.glob("#{AwsEc2.root}/app/helpers/**/*_helper.rb").each do |path|
|
62
|
-
filename = path.sub(%r{.*/},'').sub('.rb','')
|
63
|
-
module_name = filename.classify
|
64
|
-
|
65
|
-
require path
|
66
|
-
self.class.send :include, module_name.constantize
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
def erb_result(path)
|
72
|
-
load_custom_helpers
|
73
|
-
template = IO.read(path)
|
74
|
-
|
75
|
-
# Allow a way to bypass the custom ERB error handling in case
|
76
|
-
# the error is in the lambdagem code.
|
77
|
-
if ENV['DEBUG']
|
78
|
-
return ERB.new(template, nil, "-").result(binding)
|
79
|
-
end
|
80
|
-
|
81
|
-
begin
|
82
|
-
ERB.new(template, nil, "-").result(binding)
|
83
|
-
rescue Exception => e
|
84
|
-
puts e
|
85
|
-
|
86
|
-
# how to know where ERB stopped? - https://www.ruby-forum.com/topic/182051
|
87
|
-
# syntax errors have the (erb):xxx info in e.message
|
88
|
-
# undefined variables have (erb):xxx info in e.backtrac
|
89
|
-
error_info = e.message.split("\n").grep(/\(erb\)/)[0]
|
90
|
-
error_info ||= e.backtrace.grep(/\(erb\)/)[0]
|
91
|
-
raise unless error_info # unable to find the (erb):xxx: error line
|
92
|
-
line = error_info.split(':')[1].to_i
|
93
|
-
puts "Error evaluating ERB template on line #{line.to_s.colorize(:red)} of: #{path.sub(/^\.\//, '')}"
|
94
|
-
|
95
|
-
template_lines = template.split("\n")
|
96
|
-
context = 5 # lines of context
|
97
|
-
top, bottom = [line-context-1, 0].max, line+context-1
|
98
|
-
spacing = template_lines.size.to_s.size
|
99
|
-
template_lines[top..bottom].each_with_index do |line_content, index|
|
100
|
-
line_number = top+index+1
|
101
|
-
if line_number == line
|
102
|
-
printf("%#{spacing}d %s\n".colorize(:red), line_number, line_content)
|
103
|
-
else
|
104
|
-
printf("%#{spacing}d %s\n", line_number, line_content)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
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"
|
109
|
-
exit 1 unless ENV['TEST']
|
110
|
-
end
|
111
|
-
end
|
112
16
|
end
|
113
17
|
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require "base64"
|
2
|
+
require "erb"
|
3
|
+
|
4
|
+
module AwsEc2::TemplateHelper::Core
|
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 = "/tmp/aws-ec2/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
|
data/lib/aws_ec2/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aws-ec2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tung Nguyen
|
@@ -223,6 +223,7 @@ files:
|
|
223
223
|
- lib/aws_ec2/scripts/auto_terminate.sh
|
224
224
|
- lib/aws_ec2/template_helper.rb
|
225
225
|
- lib/aws_ec2/template_helper/ami_helper.rb
|
226
|
+
- lib/aws_ec2/template_helper/core.rb
|
226
227
|
- lib/aws_ec2/template_helper/partial_helper.rb
|
227
228
|
- lib/aws_ec2/version.rb
|
228
229
|
- spec/fixtures/demo_project/config/test.yml
|