cumuliform 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e4589cd0a6f35898e49b81876b266f3a9822a111
4
+ data.tar.gz: 7ddd217959b61d317a0ddc0f2def640d2fa51d07
5
+ SHA512:
6
+ metadata.gz: d1c8a3767dac321236b599bffb8a4c05d3111eeb7acac048a8da941d061dc3b3bfe12fa61150dad15bb590596a7414aa9a04a765c09b2f24d4e8673c36ccc0a0
7
+ data.tar.gz: 20524b85307eb0fe35233ccb6c3453dbc6d09a4920af248fa0d22c9df8abfc62e37b316a7b1149306153546d3d0f71387f5328087b39efa063c8cbe60e3920f3
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.2
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ 2.2.2
5
+ addons:
6
+ code_climate:
7
+ repo_token: "ad171accd9e47d577857f84b0d9032042ea70bf942a219bfa4f5582b5e504a60"
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cumuliform.gemspec
4
+ gemspec
5
+
6
+ gem 'codeclimate-test-reporter', group: :test, require: nil
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Matt Patterson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Cumuliform
2
+
3
+ [![Build Status](https://travis-ci.org/tape-tv/cumuliform.svg?branch=master)](https://travis-ci.org/tape-tv/cumuliform) [![Code Climate](https://codeclimate.com/github/tape-tv/cumuliform/badges/gpa.svg)](https://codeclimate.com/github/tape-tv/cumuliform) [![Test Coverage](https://codeclimate.com/github/tape-tv/cumuliform/badges/coverage.svg)](https://codeclimate.com/github/tape-tv/cumuliform/coverage)
4
+
5
+ A DSL and library for the generation and reuse of Amazon's AWS CloudFormation templates, extracted from ops and deployment code at tape.tv
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cumuliform'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cumuliform
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/tape-tv/cumuliform/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ begin
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: [:spec]
6
+ rescue LoadError
7
+ end
8
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cumuliform"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cumuliform/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cumuliform"
8
+ spec.version = Cumuliform::VERSION
9
+ spec.authors = ["Matt Patterson"]
10
+ spec.email = ["matt@reprocessed.org"]
11
+
12
+ spec.summary = %q{DSL library for generating AWS CloudFormation templates}
13
+ spec.description = <<-EOD
14
+ Simple DSL for generation AWS CloudFormation templates with an emphasis
15
+ on ensuring you don't shoot yourself in the foot by, e.g. referencing
16
+ non-existent resources because you have a typo.
17
+ EOD
18
+ spec.homepage = "https://www.github.com/tape-tv/cumuliform"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.9"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3"
29
+ end
@@ -0,0 +1,47 @@
1
+ module Cumuliform
2
+ module Error
3
+ class IDError < StandardError
4
+ attr_reader :id
5
+
6
+ def initialize(id)
7
+ @id = id
8
+ end
9
+ end
10
+
11
+ class DuplicateLogicalID < IDError
12
+ def to_s
13
+ "Existing item with logical ID '#{id}'"
14
+ end
15
+ end
16
+
17
+ class NoSuchLogicalId < IDError
18
+ def to_s
19
+ "No such logical ID '#{id}'"
20
+ end
21
+ end
22
+
23
+ class FragmentAlreadyDefined < IDError
24
+ def to_s
25
+ "Fragment '#{id}' already defined"
26
+ end
27
+ end
28
+
29
+ class FragmentNotFound < IDError
30
+ def to_s
31
+ "No fragment with name '#{id}'"
32
+ end
33
+ end
34
+
35
+ class EmptyItem < IDError
36
+ def to_s
37
+ "Empty item '#{id}'"
38
+ end
39
+ end
40
+
41
+ class NoResourcesDefined < StandardError
42
+ def to_s
43
+ "No resources defined"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,44 @@
1
+ require_relative 'error'
2
+
3
+ module Cumuliform
4
+ module Fragments
5
+ def fragments
6
+ @fragments ||= {}
7
+ end
8
+
9
+ def fragment(name, *args, &block)
10
+ if block_given?
11
+ define_fragment(name, block)
12
+ else
13
+ include_fragment(name, *args)
14
+ end
15
+ end
16
+
17
+ def find_fragment(name)
18
+ local_fragment = fragments[name]
19
+ imports.reverse.reduce(local_fragment) { |fragment, import|
20
+ fragment || import.find_fragment(name)
21
+ }
22
+ end
23
+
24
+ def has_fragment?(name)
25
+ !find_fragment(name).nil?
26
+ end
27
+
28
+ private
29
+
30
+ def define_fragment(name, block)
31
+ if fragments.has_key?(name)
32
+ raise Error::FragmentAlreadyDefined, name
33
+ end
34
+ fragments[name] = block
35
+ end
36
+
37
+ def include_fragment(name, opts = {})
38
+ unless has_fragment?(name)
39
+ raise Error::FragmentNotFound, name
40
+ end
41
+ instance_exec(opts, &find_fragment(name))
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,69 @@
1
+ require_relative 'error'
2
+
3
+ module Cumuliform
4
+ module Functions
5
+ class IntrinsicFunctions
6
+ attr_reader :template
7
+
8
+ def initialize(template)
9
+ @template = template
10
+ end
11
+
12
+ def find_in_map(mapping_logical_id, level_1_key, level_2_key)
13
+ template.verify_mapping_logical_id!(mapping_logical_id)
14
+ {"Fn::FindInMap" => [mapping_logical_id, level_1_key, level_2_key]}
15
+ end
16
+
17
+ def get_att(resource_logical_id, attr_name)
18
+ template.verify_resource_logical_id!(resource_logical_id)
19
+ {"Fn::GetAtt" => [resource_logical_id, attr_name]}
20
+ end
21
+
22
+ def join(separator, args)
23
+ raise ArgumentError, "Second argument must be an Array" unless args.is_a?(Array)
24
+ {"Fn::Join" => [separator, args]}
25
+ end
26
+
27
+ def base64(value)
28
+ {"Fn::Base64" => value}
29
+ end
30
+
31
+ def get_azs(value = "")
32
+ {"Fn::GetAZs" => value}
33
+ end
34
+
35
+ def equals(value, other_value)
36
+ {"Fn::Equals" => [value, other_value]}
37
+ end
38
+
39
+ def if(condition, true_value, false_value)
40
+ {"Fn::If" => [condition, true_value, false_value]}
41
+ end
42
+
43
+ def select(index, array)
44
+ unless index.is_a?(Integer) && index >= 0
45
+ raise ArgumentError, "index must be a positive integer"
46
+ end
47
+ if array.is_a?(Array) && index >= array.length
48
+ raise IndexError, "index must be in the range 0 <= index < array.length"
49
+ end
50
+ {"Fn::Select" => [index.to_s, array]}
51
+ end
52
+ end
53
+
54
+ def xref(logical_id)
55
+ unless has_logical_id?(logical_id)
56
+ raise Error::NoSuchLogicalId, logical_id
57
+ end
58
+ logical_id
59
+ end
60
+
61
+ def ref(logical_id)
62
+ {"Ref" => xref(logical_id)}
63
+ end
64
+
65
+ def fn
66
+ @fn ||= IntrinsicFunctions.new(self)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,62 @@
1
+ require_relative 'error'
2
+
3
+ module Cumuliform
4
+ module Import
5
+ def import(template)
6
+ imports << template
7
+ end
8
+
9
+ def has_logical_id?(logical_id)
10
+ (imports.reverse).inject(logical_ids.include?(logical_id)) { |found, template|
11
+ found || template.has_logical_id?(logical_id)
12
+ }
13
+ end
14
+
15
+ def verify_logical_id!(logical_id)
16
+ raise Error::NoSuchLogicalId, logical_id unless has_logical_id?(logical_id)
17
+ true
18
+ end
19
+
20
+ private
21
+
22
+ def imports
23
+ @imports ||= []
24
+ end
25
+ end
26
+
27
+ class Section
28
+ attr_reader :name, :imports, :items
29
+
30
+ def initialize(name, imports)
31
+ @name = name
32
+ @imports = imports
33
+ @items = {}
34
+ end
35
+
36
+ def []=(name, item)
37
+ items[name] = item
38
+ end
39
+
40
+ def [](name)
41
+ merged[name]
42
+ end
43
+
44
+ def each(&block)
45
+ merged.each(&block)
46
+ end
47
+
48
+ def member?(name)
49
+ merged.member?(name)
50
+ end
51
+
52
+ def empty?
53
+ merged.empty?
54
+ end
55
+
56
+ def merged
57
+ imports.reduce({}) { |merged, import|
58
+ import.get_section(name).merged.merge(merged)
59
+ }.merge(items)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,41 @@
1
+ # Simple AWS CloudFormation. Doesn't try to run the commands, doesn't try to
2
+ # DSLize every last thing. Simple is the watch word
3
+
4
+ require 'json'
5
+ require 'set'
6
+
7
+ module Cumuliform
8
+ module Output
9
+ def to_hash
10
+ output = {}
11
+ SECTIONS.each do |section_name, _|
12
+ section = get_section(section_name)
13
+ output[section_name] = generate_section(section) unless section.empty?
14
+ end
15
+
16
+ raise Error::NoResourcesDefined unless output.has_key?("Resources")
17
+ output
18
+ end
19
+
20
+ def to_json
21
+ JSON.pretty_generate(to_hash)
22
+ end
23
+
24
+ private
25
+
26
+ def generate_section(section)
27
+ output = {}
28
+ section.each do |logical_id, block|
29
+ output[logical_id] = generate_item(logical_id, block)
30
+ end
31
+ output
32
+ end
33
+
34
+ def generate_item(logical_id, block)
35
+ raise Error::EmptyItem, logical_id if block.nil?
36
+ item_body = instance_exec(&block)
37
+ raise Error::EmptyItem, logical_id if item_body.empty?
38
+ item_body
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Cumuliform
2
+ VERSION = "0.3.0"
3
+ end
data/lib/cumuliform.rb ADDED
@@ -0,0 +1,96 @@
1
+ # Simple AWS CloudFormation. Doesn't try to run the commands, doesn't try to
2
+ # DSLize every last thing. Simple is the watch word
3
+
4
+ require 'json'
5
+ require 'set'
6
+ require_relative 'cumuliform/error'
7
+ require_relative 'cumuliform/fragments'
8
+ require_relative 'cumuliform/functions'
9
+ require_relative 'cumuliform/import'
10
+ require_relative 'cumuliform/output'
11
+
12
+ module Cumuliform
13
+ AWS_PSEUDO_PARAMS = %w{
14
+ AWS::AccountId AWS::NotificationARNs AWS::NoValue
15
+ AWS::Region AWS::StackId AWS::StackName
16
+ }
17
+ SECTIONS = {
18
+ "Parameters" => :parameter,
19
+ "Mappings" => :mapping,
20
+ "Conditions" => :condition,
21
+ "Resources" => :resource,
22
+ "Outputs" => :output
23
+ }
24
+
25
+ def self.template(&block)
26
+ template = Template.new
27
+ template.define(&block)
28
+ end
29
+
30
+ class Template
31
+ include Import
32
+ include Fragments
33
+ include Functions
34
+ include Output
35
+
36
+ def initialize
37
+ SECTIONS.each do |section_name, _|
38
+ instance_variable_set(:"@#{section_name}", Section.new(section_name, imports))
39
+ end
40
+ end
41
+
42
+ def helpers(*mods, &block)
43
+ if block_given?
44
+ mods << Module.new(&block)
45
+ end
46
+ mods.each do |mod|
47
+ self.class.include mod
48
+ end
49
+ end
50
+
51
+ def define(&block)
52
+ instance_exec(&block)
53
+ self
54
+ end
55
+
56
+ def logical_ids
57
+ @logical_ids ||= Set.new(AWS_PSEUDO_PARAMS)
58
+ end
59
+
60
+ def get_section(name)
61
+ instance_variable_get(:"@#{name}")
62
+ end
63
+
64
+ SECTIONS.each do |section_name, method_name|
65
+ error_class = Class.new(Error::IDError) do
66
+ def to_s
67
+ "No logical ID '#{id}' in section"
68
+ end
69
+ end
70
+ Error.const_set("NoSuchLogicalIdIn#{section_name}", error_class)
71
+
72
+ define_method method_name, ->(logical_id, &block) {
73
+ add_to_section(section_name, logical_id, block)
74
+ }
75
+
76
+ define_method :"verify_#{method_name}_logical_id!", ->(logical_id) {
77
+ raise error_class, logical_id unless get_section(section_name).member?(logical_id)
78
+ true
79
+ }
80
+ end
81
+
82
+ private
83
+
84
+ def has_local_logical_id?(logical_id)
85
+ logical_ids.include?(logical_id)
86
+ end
87
+
88
+ def add_to_section(section_name, logical_id, block)
89
+ if has_local_logical_id?(logical_id)
90
+ raise Error::DuplicateLogicalID, logical_id
91
+ end
92
+ logical_ids << logical_id
93
+ get_section(section_name)[logical_id] = block
94
+ end
95
+ end
96
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cumuliform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Patterson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ description: |
56
+ Simple DSL for generation AWS CloudFormation templates with an emphasis
57
+ on ensuring you don't shoot yourself in the foot by, e.g. referencing
58
+ non-existent resources because you have a typo.
59
+ email:
60
+ - matt@reprocessed.org
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".ruby-version"
67
+ - ".travis.yml"
68
+ - CODE_OF_CONDUCT.md
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - cumuliform.gemspec
76
+ - lib/cumuliform.rb
77
+ - lib/cumuliform/error.rb
78
+ - lib/cumuliform/fragments.rb
79
+ - lib/cumuliform/functions.rb
80
+ - lib/cumuliform/import.rb
81
+ - lib/cumuliform/output.rb
82
+ - lib/cumuliform/version.rb
83
+ homepage: https://www.github.com/tape-tv/cumuliform
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.4.5
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: DSL library for generating AWS CloudFormation templates
107
+ test_files: []