cardtapp-cloudformation-ruby-dsl 0.0.1.pre.pre1

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.
@@ -0,0 +1,161 @@
1
+ require 'fileutils'
2
+ require 'json'
3
+ require 'open3'
4
+
5
+ ##
6
+ # Error encapsulating information about a failed command
7
+ class CommandError < StandardError
8
+ attr_reader :command, :status, :stderr
9
+
10
+ def initialize(command, status, stderr)
11
+ @command = command
12
+ @status = status
13
+ @stderr = stderr
14
+ super "FAILURE (#{status}) #{stderr}"
15
+ end
16
+ end
17
+
18
+ ##
19
+ # Helpers for dealing with pathing from specs
20
+ module PathHelpers
21
+ ##
22
+ # Returns an absolute path for the specified path that
23
+ # is relative to the project root irregardless of where
24
+ # rspec/ruby is invoked
25
+ def from_project_root(relative_path)
26
+ source_dir = File.expand_path(File.dirname(__FILE__))
27
+ File.join(source_dir, "..", relative_path)
28
+ end
29
+ end
30
+
31
+ ##
32
+ # Mixin containing helper methods for working with
33
+ # commands executed in a subshell
34
+ module CommandHelpers
35
+ include PathHelpers
36
+
37
+ ##
38
+ # Logs the command that failed and raises an exception
39
+ # inlcuding status and stderr
40
+ def cmd_failed(command, status, stderr)
41
+ STDERR.puts("FAILURE executing #{command}")
42
+ raise CommandError.new(command, status, stderr)
43
+ end
44
+
45
+ ##
46
+ # execute a command within a specified directory and
47
+ # return results or yield them to a block if one is
48
+ # given to this method. Results are an array of
49
+ # strings:
50
+ #
51
+ # [stdout, token1, token2, ..., tokenN]
52
+ #
53
+ # where stdout is the captured standard output and
54
+ # the tokens are the words extracted from the output.
55
+ #
56
+ # If the command has a non-zero exit status, an
57
+ # exception is raised including the exit code
58
+ # and stderr.
59
+ #
60
+ # example call:
61
+ # exec_cmd("ls", :within => "/")
62
+ def exec_cmd(cmd, opts={:within => "."})
63
+ exec_dir = from_project_root(opts[:within])
64
+ Dir.chdir(exec_dir) do
65
+ stdout, stderr, status = Open3.capture3(cmd)
66
+ results = stdout.split(" ").unshift(stdout)
67
+
68
+ cmd_failed(cmd, status, stderr) if status != 0
69
+ if (block_given?)
70
+ yield results
71
+ else
72
+ results
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ ##
79
+ # Mixin with helper methods for dealing with JSON generated
80
+ # from cloudformation-ruby-dsl
81
+ module JsonHelpers
82
+ ##
83
+ # Parse the json string, making sure to write all of it to
84
+ # STDERR if parsing fails to make it easier to troubleshoot
85
+ # failures in generated json.
86
+ def jsparse(json_string)
87
+ begin
88
+ JSON.parse(json_string)
89
+ rescue StandardError => e
90
+ STDERR.puts "Error parsing JSON:"
91
+ STDERR.puts json_string
92
+ raise e
93
+ end
94
+ end
95
+ end
96
+
97
+ ##
98
+ # Mixin with helper methods for dealing with files generated
99
+ # from a test/spec
100
+ module FileHelpers
101
+ include PathHelpers
102
+
103
+ ##
104
+ # Delete a file from the spec/tmp directory
105
+ def delete_test_file(filename)
106
+ abs_path = File.join(from_project_root("spec/tmp"), filename)
107
+ FileUtils.rm(abs_path) if File.exist?(abs_path)
108
+ end
109
+
110
+ ##
111
+ # Write a file to the spec/tmp directory
112
+ def write_test_file(filename, contents)
113
+ dest_dir = from_project_root("spec/tmp")
114
+ dest_file = File.join(dest_dir, filename)
115
+
116
+ FileUtils.mkdir_p(dest_dir)
117
+ File.open(dest_file, "w") { |f| f.write(contents) }
118
+ dest_file
119
+ end
120
+ end
121
+
122
+ ##
123
+ # Mixin to assist in using aws cli for validating results of
124
+ # cloudformation-ruby-dsl
125
+ module AwsHelpers
126
+ include CommandHelpers
127
+
128
+ ##
129
+ # Validate a cloudformation template within the spec/tmp directory
130
+ # using aws cli
131
+ def validate_cfn_template(template_name)
132
+ template_path = File.join(from_project_root("spec/tmp"), template_name)
133
+ command = validation_command(template_path)
134
+ exec_cmd(command) do |output|
135
+ begin
136
+ JSON.parse(output.first)
137
+ rescue JSON::ParserError
138
+ STDERR.puts "ERROR parsing output of: #{command}"
139
+ raise
140
+ end
141
+ end
142
+ end
143
+
144
+ def profile
145
+ ENV["AWS_PROFILE"] || "default"
146
+ end
147
+
148
+ def region
149
+ ENV["AWS_REGION"] || "us-east-1"
150
+ end
151
+
152
+ private
153
+
154
+ def validation_command(template_path)
155
+ return <<-EOF
156
+ aws cloudformation validate-template --template-body file://#{template_path} \
157
+ --region #{region} \
158
+ --profile #{profile}
159
+ EOF
160
+ end
161
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.shared_examples "template acceptance validations" do
4
+ include CommandHelpers
5
+ include JsonHelpers
6
+ include FileHelpers
7
+ include AwsHelpers
8
+
9
+ it "should create a valid JSON template from the example ruby template" do
10
+ delete_test_file(json_template)
11
+ json = exec_cmd("./#{ruby_template} expand", :within => "examples").first
12
+ write_test_file(json_template, json)
13
+ validate_cfn_template(json_template)
14
+ end
15
+ end
16
+
17
+ describe "cloudformation-ruby-dsl" do
18
+ context "simplest template" do
19
+ let(:ruby_template) { "simple_template.rb" }
20
+ let(:json_template) { "simple_template.json" }
21
+
22
+ include_examples "template acceptance validations"
23
+ end
24
+
25
+ # TODO validate examples/cloudformation-ruby-script.rb
26
+ end
metadata ADDED
@@ -0,0 +1,234 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cardtapp-cloudformation-ruby-dsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.pre.pre1
5
+ platform: ruby
6
+ authors:
7
+ - Shawn Smith
8
+ - Dave Barcelo
9
+ - Morgan Fletcher
10
+ - Csongor Gyuricza
11
+ - Igor Polishchuk
12
+ - Nathaniel Eliot
13
+ - Jona Fenocchi
14
+ - Tony Cui
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+ date: 2019-07-29 00:00:00.000000000 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: detabulator
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ type: :runtime
28
+ prerelease: false
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ - !ruby/object:Gem::Dependency
35
+ name: json
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ - !ruby/object:Gem::Dependency
49
+ name: bundler
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: aws-sdk-s3
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ type: :runtime
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ - !ruby/object:Gem::Dependency
77
+ name: aws-sdk-cloudformation
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ - !ruby/object:Gem::Dependency
91
+ name: diffy
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ type: :runtime
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ - !ruby/object:Gem::Dependency
105
+ name: highline
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ type: :runtime
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ - !ruby/object:Gem::Dependency
119
+ name: rake
120
+ requirement: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ type: :runtime
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ - !ruby/object:Gem::Dependency
133
+ name: rspec
134
+ requirement: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ type: :development
140
+ prerelease: false
141
+ version_requirements: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ - !ruby/object:Gem::Dependency
147
+ name: pry
148
+ requirement: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ type: :development
154
+ prerelease: false
155
+ version_requirements: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ description: Ruby DSL library that provides a wrapper around the CloudFormation.
161
+ email:
162
+ - Shawn.Smith@bazaarvoice.com
163
+ - Dave.Barcelo@bazaarvoice.com
164
+ - Morgan.Fletcher@bazaarvoice.com
165
+ - Csongor.Gyuricza@bazaarvoice.com
166
+ - Igor.Polishchuk@bazaarvoice.com
167
+ - Nathaniel.Eliot@bazaarvoice.com
168
+ - Jona.Fenocchi@bazaarvoice.com
169
+ - Tony.Cui@bazaarvoice.com
170
+ executables:
171
+ - cfntemplate-to-ruby
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".travis.yml"
177
+ - Gemfile
178
+ - LICENSE
179
+ - OWNERS
180
+ - PULL_REQUEST_TEMPLATE.md
181
+ - README.md
182
+ - Rakefile
183
+ - bin/cfntemplate-to-ruby
184
+ - cloudformation-ruby-dsl.gemspec
185
+ - docs/Contributing.md
186
+ - docs/Releasing.md
187
+ - examples/cloudformation-ruby-script.rb
188
+ - examples/maps/domains.txt
189
+ - examples/maps/map.json
190
+ - examples/maps/map.rb
191
+ - examples/maps/map.yaml
192
+ - examples/maps/more_maps/map1.json
193
+ - examples/maps/more_maps/map2.json
194
+ - examples/maps/more_maps/map3.json
195
+ - examples/maps/table.txt
196
+ - examples/maps/vpc.txt
197
+ - examples/simple_template.rb
198
+ - examples/userdata.sh
199
+ - initial_contributions.md
200
+ - lib/cloudformation-ruby-dsl.rb
201
+ - lib/cloudformation-ruby-dsl/cfntemplate.rb
202
+ - lib/cloudformation-ruby-dsl/dsl.rb
203
+ - lib/cloudformation-ruby-dsl/spotprice.rb
204
+ - lib/cloudformation-ruby-dsl/table.rb
205
+ - lib/cloudformation-ruby-dsl/version.rb
206
+ - spec/spec_helper.rb
207
+ - spec/validation_spec.rb
208
+ homepage: http://github.com/bazaarvoice/cloudformation-ruby-dsl
209
+ licenses: []
210
+ metadata: {}
211
+ post_install_message:
212
+ rdoc_options: []
213
+ require_paths:
214
+ - lib
215
+ - bin
216
+ required_ruby_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ required_rubygems_version: !ruby/object:Gem::Requirement
222
+ requirements:
223
+ - - ">"
224
+ - !ruby/object:Gem::Version
225
+ version: 1.3.1
226
+ requirements: []
227
+ rubygems_version: 3.0.1
228
+ signing_key:
229
+ specification_version: 4
230
+ summary: Ruby DSL library that provides a wrapper around the CloudFormation. Written
231
+ by [Bazaarvoice](http://www.bazaarvoice.com).
232
+ test_files:
233
+ - spec/spec_helper.rb
234
+ - spec/validation_spec.rb