tflat 0.1.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
+ SHA256:
3
+ metadata.gz: 1189d180d63682ea5c7309d7845769052627f5d415952568bcdb915a88c72a31
4
+ data.tar.gz: 67a9c1e389e40bd8c7b344b9bd3dc65831976b06fd1fe211d4f8a035f57086cd
5
+ SHA512:
6
+ metadata.gz: 6ef47e4daa22441e2b489f97f7e3f54cb50aadae05e825c75129b767ca367d39f0085839bfc3be57843401467b434c081e503c3620801f6b2cd9d33ff1626a8f
7
+ data.tar.gz: 8b90644eec0736eda05f8fdb4de93c9e62dfed9aaa2cbe3c2d2e4fd337d7b442b656464989cd96c2bca400de37a1e3966a441d9ef63705081cd956ed188557be
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.3
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at paulo@parruda.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in tflat.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tflat (0.1.0)
5
+ ptools (~> 1.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ ptools (1.3.5)
12
+ rake (10.5.0)
13
+ rspec (3.8.0)
14
+ rspec-core (~> 3.8.0)
15
+ rspec-expectations (~> 3.8.0)
16
+ rspec-mocks (~> 3.8.0)
17
+ rspec-core (3.8.0)
18
+ rspec-support (~> 3.8.0)
19
+ rspec-expectations (3.8.2)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-mocks (3.8.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-support (3.8.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.16)
32
+ rake (~> 10.0)
33
+ rspec (~> 3.0)
34
+ tflat!
35
+
36
+ BUNDLED WITH
37
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Paulo Arruda
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,122 @@
1
+ # TFlat
2
+
3
+ Aren't you tired of copy and pasting?
4
+
5
+ I love Terraform, but HCL really gets in the way sometimes.
6
+
7
+ How many times you wish you could just write a simple IF or CASE statement inside a .tf file? Any attempt of having minimal flow control with HCL results in a massive oneliner mess. Sometimes it feels like writing PERL one-liners. Hard to read equals hard to debug.
8
+
9
+ Also, you can't use subfolders with Terraform, so you often end up at one of the three scenarios below:
10
+ - You have a few `.tf` files with a lot of code in it. Ugly and not organized.
11
+ - You create lots of different `.tf` files in a single directory, which makes it really hard to stay organized.
12
+ - You separate everything in modules, so you have to keep passing variables downstream. And if you try to be *DRY*, good luck passing 1 million variables downstream to submodules!
13
+
14
+ TFlat does 2 things to solve this problem:
15
+ * Separate your Terraform code in subdirectories.
16
+ * It allows you to write Ruby code in `.tf` files using ERB templates. Hurray!
17
+
18
+ ## How does TFlat make Terraform read subdirectories?
19
+ It doesn't. This is what happens when you run it:
20
+ 1. Create a folder `.tflat` inside the current folder if it doesn't exist yet. If it does exist, delete all files from this folder non-recursively.
21
+ 2. Make a recursive list of all files in the current directory and move one by one to the `.tflat` folder.
22
+ 3. Replace all non-binary files in `.tflat/` with its ERB rendered version.
23
+ 4. Execute `terraform` with the arguments you passed to `tflat`.
24
+
25
+ For example, say you have the following file structure:
26
+ ```
27
+ |_ config
28
+ |_ providers.tf
29
+ |_ state.tf
30
+ |_ ec2
31
+ |_ files
32
+ |_ user_data.sh
33
+ |_ instances.tf
34
+ |_ keypairs.tf
35
+ |_ outputs.tf
36
+ |_ variables.tf
37
+ ```
38
+ TFlat will create the following files inside `./.tflat/`
39
+
40
+ ```
41
+ config#providers.tf
42
+ config#state.tf
43
+ ec2#files#user_data.sh
44
+ ec2#instances.tf
45
+ ec2#keypairs.tf
46
+ outputs.tf
47
+ variables.tf
48
+ ```
49
+
50
+ Then it will `cd` into `.tflat` and run `terraform` with the arguments you passed to `tflat`.
51
+
52
+ *IMPORTANT* The `.terraform` folder will live at `.tflat/.terraform`, so make sure you don't delete that folder if you are storing the terraform state locally!
53
+
54
+ There's only one more thing you have to pay attention to: handling file references.
55
+
56
+ ### Handling file references
57
+ Because TFlat will actually copy and rename files to make it work with subdirectories, you need to pass file references in a different way. For example, imagine you are rendering a terraform template like this:
58
+
59
+ files/userdata.tpl
60
+ ```
61
+ #!/bin/bash
62
+ # ...
63
+ CONSUL_ADDRESS=${consul_address}
64
+ # ...
65
+ ```
66
+ main.tf
67
+ ```
68
+ # ...
69
+ data "template_file" "ec2_userdata" {
70
+ template = "${file("files/userdata.tpl")}"
71
+
72
+ vars {
73
+ consul_address = "${aws_instance.consul.private_ip}"
74
+ }
75
+ }
76
+
77
+ # Create a web server
78
+ resource "aws_instance" "web" {
79
+ # ...
80
+
81
+ user_data = "${data.template_file.ec2_userdata.rendered}"
82
+ }
83
+ # ...
84
+ ```
85
+
86
+ The line `template = "${file("files/userdata.tpl")}"` has to be written the in one of the following ways to work with TFlat:
87
+
88
+ ```
89
+ # Let Ruby load the file content using the 'file' helper method (easier to read)
90
+ template = "<%= file('files/userdata.tpl') %>"
91
+
92
+ # Let Terraform load the file content using the 'f' helper method (quoting nightmare!)
93
+ template = "${file("<%= f('files/userdata.tpl') %>")}"
94
+ ```
95
+ It is up to you to choose what you like. Try both and look inside `.tflat/main.tf` to see the difference between the two ways.
96
+
97
+ ## Installation
98
+
99
+ ```
100
+ $ gem install tflat
101
+ ```
102
+
103
+ ## Usage
104
+ TFlat takes the same arguments from Terraform. It actually hands off the execution to Terraform after processing the files. So:
105
+
106
+ ```
107
+ terraform plan
108
+ ```
109
+ Becomes:
110
+ ```
111
+ tflat plan
112
+ ```
113
+ That's it!
114
+
115
+
116
+ ## Contributing
117
+
118
+ Bug reports and pull requests are welcome on GitHub at https://github.com/parruda/tflat. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
119
+
120
+ ## License
121
+
122
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tflat"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/tflat ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'tflat'
3
+
4
+ Tflat::Terraform.new(args: ARGV, directory: '.').run!
data/lib/tflat.rb ADDED
@@ -0,0 +1,76 @@
1
+ require "tflat/version"
2
+ require 'erb'
3
+ require 'fileutils'
4
+ require 'ptools'
5
+
6
+ module Tflat
7
+ class Terraform
8
+ attr_accessor :destination, :all_files, :args, :directory
9
+ def initialize(args:, directory: '.')
10
+ @args = args.join(' ')
11
+ @directory = directory
12
+ @all_files = Dir.glob("#{directory}/**/*").select{|e| File.file?(e) && !e.match(/^\.tflat\/.+$/)}
13
+ @destination = "#{directory}/.tflat"
14
+ end
15
+ def run!
16
+ if args.empty?
17
+ puts `terraform`
18
+ puts "\n\n- [tflat] Notice: You must run tflat with terraform arguments.\n\n"
19
+ return
20
+ end
21
+ setup
22
+ print "- [tflat] Generating files..."
23
+ flatten_directories
24
+ parse_erb
25
+ puts " done!"
26
+ puts "- [tflat] Running: terraform #{args}"
27
+ execute
28
+ end
29
+
30
+ def setup
31
+ FileUtils.mkdir_p(destination)
32
+ Dir.glob('.tflat/*').each do |entry|
33
+ next unless File.file?(entry)
34
+ FileUtils.rm_f entry
35
+ end
36
+ end
37
+
38
+ def flatten_directories
39
+ all_files.each do |entry|
40
+ new_name = entry.sub(/^#{directory}\//,'').gsub('/', '#')
41
+ if new_name =~ /^#/ # Skip files/directories that start with a hash sign
42
+ puts "- [tflat] Skipping: #{entry}"
43
+ next
44
+ end
45
+ FileUtils.cp(entry, ".tflat/#{new_name}")
46
+ end
47
+ end
48
+
49
+ def parse_erb
50
+ Dir.glob(".tflat/*").each do |entry|
51
+ next unless File.file?(entry)
52
+ next if File.binary?(entry)
53
+ rendered = render(entry)
54
+ File.write(entry, rendered)
55
+ end
56
+ end
57
+
58
+ def f(file)
59
+ file.sub(/^\.\//, "").gsub('/', '#')
60
+ end
61
+
62
+ def file(file)
63
+ f = ".tflat/#{f(file)}"
64
+ File.read(f).inspect[1...-1]
65
+ end
66
+
67
+ def render(file)
68
+ template = File.read(file)
69
+ ERB.new(template).result(binding)
70
+ end
71
+
72
+ def execute
73
+ exec "cd .tflat && terraform #{args}"
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,3 @@
1
+ module Tflat
2
+ VERSION = "0.1.0"
3
+ end
data/tflat.gemspec ADDED
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "tflat/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tflat"
8
+ spec.version = Tflat::VERSION
9
+ spec.authors = ["Paulo Arruda"]
10
+ spec.email = ["parrudaj@gmail.com"]
11
+
12
+ spec.summary = %q{Terraform + Ruby + ERB = DRY infrastructure code!}
13
+ spec.description = %q{tflat is a Terraform wrapper that allows you to write Ruby code in .tf files. It also allows you to use subdirectories! Huray!}
14
+ spec.homepage = "https://github.com/parruda/tflat"
15
+ spec.license = "MIT"
16
+ spec.add_runtime_dependency 'ptools', "~> 1.3"
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ # if spec.respond_to?(:metadata)
21
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
22
+ # else
23
+ # raise "RubyGems 2.0 or newer is required to protect against " \
24
+ # "public gem pushes."
25
+ # end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.16"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "rspec", "~> 3.0"
39
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tflat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paulo Arruda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ptools
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: tflat is a Terraform wrapper that allows you to write Ruby code in .tf
70
+ files. It also allows you to use subdirectories! Huray!
71
+ email:
72
+ - parrudaj@gmail.com
73
+ executables:
74
+ - tflat
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - exe/tflat
90
+ - lib/tflat.rb
91
+ - lib/tflat/version.rb
92
+ - tflat.gemspec
93
+ homepage: https://github.com/parruda/tflat
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.7.7
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Terraform + Ruby + ERB = DRY infrastructure code!
117
+ test_files: []