rake-terraform-wrapper 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 89c88b4d3fb51f2b11cac6e38f3750935ff678eedf6f8be9a4d57a50c0d7c18c
4
+ data.tar.gz: 16f80114887a769b5473439f61009df0da52cc9683339ba97c9f9ddd195a3080
5
+ SHA512:
6
+ metadata.gz: 477ad93228587c2a6522725056f46ff8e76399069a2deb61516d3b27410d94e2c44f93aa92a66f2bffd15bd13b2fa6ec84f141663b27a833b00bc799f12bd418
7
+ data.tar.gz: c03895337c9fbad834e96ab9c604505549e85744f0240b309b875ee402c135d2a8abff578799cb4c0ff4e7370d4e1019a3778439e8f21d00cc29c9a05c36c39a
@@ -0,0 +1,33 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ - name: Set up Ruby
24
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
+ # uses: ruby/setup-ruby@v1
27
+ uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
28
+ with:
29
+ ruby-version: 2.6
30
+ - name: Install dependencies
31
+ run: bundle install
32
+ - name: Run tests
33
+ run: bundle exec rake
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.terraform-binary/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.2
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 rake-terraform-wrapper.gemspec
6
+ gemspec
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rake-terraform-wrapper (0.1.0)
5
+ mixlib-shellout (~> 3.1)
6
+ os (~> 1.1)
7
+ rake (~> 13.0)
8
+ rubyzip (~> 2.3)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ chef-utils (16.5.64)
14
+ coderay (1.1.3)
15
+ diff-lcs (1.4.4)
16
+ ffi (1.13.1)
17
+ formatador (0.2.5)
18
+ guard (2.16.2)
19
+ formatador (>= 0.2.4)
20
+ listen (>= 2.7, < 4.0)
21
+ lumberjack (>= 1.0.12, < 2.0)
22
+ nenv (~> 0.1)
23
+ notiffany (~> 0.0)
24
+ pry (>= 0.9.12)
25
+ shellany (~> 0.0)
26
+ thor (>= 0.18.1)
27
+ guard-compat (1.2.1)
28
+ guard-rspec (4.7.3)
29
+ guard (~> 2.1)
30
+ guard-compat (~> 1.1)
31
+ rspec (>= 2.99.0, < 4.0)
32
+ listen (3.2.1)
33
+ rb-fsevent (~> 0.10, >= 0.10.3)
34
+ rb-inotify (~> 0.9, >= 0.9.10)
35
+ lumberjack (1.2.8)
36
+ method_source (1.0.0)
37
+ mixlib-shellout (3.1.6)
38
+ chef-utils
39
+ nenv (0.3.0)
40
+ notiffany (0.1.3)
41
+ nenv (~> 0.1)
42
+ shellany (~> 0.0)
43
+ os (1.1.1)
44
+ pry (0.13.1)
45
+ coderay (~> 1.1)
46
+ method_source (~> 1.0)
47
+ rake (13.0.1)
48
+ rb-fsevent (0.10.4)
49
+ rb-inotify (0.10.1)
50
+ ffi (~> 1.0)
51
+ rspec (3.9.0)
52
+ rspec-core (~> 3.9.0)
53
+ rspec-expectations (~> 3.9.0)
54
+ rspec-mocks (~> 3.9.0)
55
+ rspec-core (3.9.2)
56
+ rspec-support (~> 3.9.3)
57
+ rspec-expectations (3.9.2)
58
+ diff-lcs (>= 1.2.0, < 2.0)
59
+ rspec-support (~> 3.9.0)
60
+ rspec-mocks (3.9.1)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.9.0)
63
+ rspec-support (3.9.3)
64
+ rubyzip (2.3.0)
65
+ shellany (0.0.1)
66
+ thor (1.0.1)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ bundler (~> 1.17)
73
+ guard-rspec (~> 4.7)
74
+ rake-terraform-wrapper!
75
+ rspec (~> 3.0)
76
+
77
+ BUNDLED WITH
78
+ 1.17.2
@@ -0,0 +1,5 @@
1
+ guard :rspec, cmd: 'bundle exec rspec', notification: true do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 ThoughtWorks, Inc.
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.
@@ -0,0 +1,55 @@
1
+ # Ruby::Terraform
2
+
3
+ A ruby wrapper around terraform.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rake-terraform-wrapper'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ruby-terraform
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'rake-terraform-wrapper'
25
+
26
+ namespace :network do
27
+ Ruby::Terraform::Tasks::AllTasks.new(:network) do |tf|
28
+ tf.dir = File.absolute_path("infra/network")
29
+ tf.backend = 's3'
30
+ tf.backend_config = {
31
+ bucket: 'some-bucket',
32
+ key: 'network.tfstate',
33
+ region: 'us-west-2'
34
+ }
35
+ tf.vars = {
36
+ image_id: 'ami-something',
37
+ availability_zone_names: ['us-west-2a', 'us-west-2b']
38
+ }
39
+ end
40
+ end
41
+ ```
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ketan/rake-terraform-wrapper.
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruby/terraform"
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__)
@@ -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
@@ -0,0 +1,27 @@
1
+ require "ruby/terraform/version"
2
+ require "ruby/terraform/configuration"
3
+ require 'ruby/terraform/executable'
4
+ require 'ruby/terraform/decompressor'
5
+ require 'ruby/terraform/execution_support'
6
+
7
+ require 'ruby/terraform/commands/init_command'
8
+ require 'ruby/terraform/commands/apply_command'
9
+ require 'ruby/terraform/commands/plan_command'
10
+ require 'ruby/terraform/commands/destroy_command'
11
+ require 'ruby/terraform/commands/output_command'
12
+ require 'ruby/terraform/commands/validate_command'
13
+
14
+ require 'ruby/terraform/tasks/base_task'
15
+ require 'ruby/terraform/tasks/apply_task'
16
+ require 'ruby/terraform/tasks/destroy_task'
17
+ require 'ruby/terraform/tasks/init_task'
18
+ require 'ruby/terraform/tasks/output_task'
19
+ require 'ruby/terraform/tasks/plan_task'
20
+ require 'ruby/terraform/tasks/validate_task'
21
+ require 'ruby/terraform/tasks/all_tasks'
22
+
23
+ module Ruby
24
+ module Terraform
25
+ class Error < StandardError; end
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Commands
4
+ class ApplyCommand
5
+ include ExecutionSupport
6
+
7
+ attr_accessor :dir
8
+ attr_accessor :state
9
+ attr_accessor :vars
10
+ attr_accessor :auto_approve
11
+
12
+ def initialize(opts = {})
13
+ @dir = opts[:dir]
14
+ @state = opts[:state]
15
+ @vars = opts[:vars]
16
+ @auto_approve = if opts[:auto_approve].nil?
17
+ true
18
+ else
19
+ opts[:auto_approve]
20
+ end
21
+ end
22
+
23
+ def command
24
+ cmd = %W[#{tf_binary} apply]
25
+
26
+ cmd += ['-state', state] if state
27
+ cmd += ['-auto-approve'] if auto_approve
28
+
29
+ if vars
30
+ vars.each do |key, value|
31
+ var_value = value.is_a?(String) ? value : JSON.generate(value)
32
+ cmd += ['-var', "#{key}=#{var_value}"]
33
+ end
34
+ end
35
+
36
+ cmd += [dir] if dir
37
+ cmd
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Commands
4
+ class DestroyCommand
5
+ include ExecutionSupport
6
+
7
+ attr_accessor :dir
8
+ attr_accessor :state
9
+ attr_accessor :vars
10
+ attr_accessor :auto_approve
11
+
12
+ def initialize(opts = {})
13
+ @dir = opts[:dir]
14
+ @state = opts[:state]
15
+ @vars = opts[:vars]
16
+ @auto_approve = if opts[:auto_approve].nil?
17
+ true
18
+ else
19
+ opts[:auto_approve]
20
+ end
21
+ end
22
+
23
+ def command
24
+ cmd = %W[#{tf_binary} destroy]
25
+
26
+ cmd += ['-state', state] if state
27
+ cmd += ['-auto-approve'] if auto_approve
28
+
29
+ if vars
30
+ vars.each do |key, value|
31
+ var_value = value.is_a?(String) ? value : JSON.generate(value)
32
+ cmd += ['-var', "#{key}=#{var_value}"]
33
+ end
34
+ end
35
+
36
+ cmd += [dir] if dir
37
+ cmd
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,33 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Commands
4
+ class InitCommand
5
+ include ExecutionSupport
6
+
7
+ attr_accessor :dir
8
+ attr_accessor :backend
9
+ attr_accessor :backend_config
10
+
11
+ def initialize(opts = {})
12
+ @dir = opts[:dir]
13
+ @backend = opts[:backend]
14
+ @backend_config = opts[:backend_config]
15
+ end
16
+
17
+ def command
18
+ cmd = %W[#{tf_binary} init]
19
+ cmd += ['-backend', backend] if backend
20
+
21
+ if backend_config
22
+ backend_config.each do |key, value|
23
+ cmd += ['-backend-config', "#{key}=#{value}"]
24
+ end
25
+ end
26
+
27
+ cmd += [dir] if dir
28
+ cmd
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Commands
4
+ class OutputCommand
5
+ include ExecutionSupport
6
+
7
+ attr_accessor :dir
8
+ attr_accessor :state
9
+ attr_accessor :json
10
+ attr_accessor :name
11
+
12
+ def initialize(opts = {})
13
+ @dir = opts[:dir]
14
+ @state = opts[:state]
15
+ @json = opts[:json]
16
+ @name = opts[:name]
17
+ end
18
+
19
+ def command
20
+ cmd = %W[#{tf_binary} output]
21
+
22
+ cmd += ['-state', state] if state
23
+ cmd += ['-json'] if json
24
+ cmd += [name] if name
25
+ cmd
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Commands
4
+ class PlanCommand
5
+ include ExecutionSupport
6
+
7
+ attr_accessor :dir
8
+ attr_accessor :state
9
+ attr_accessor :vars
10
+
11
+ def initialize(opts = {})
12
+ @dir = opts[:dir]
13
+ @state = opts[:state]
14
+ @vars = opts[:vars]
15
+ end
16
+
17
+ def command
18
+ cmd = %W[#{tf_binary} plan]
19
+
20
+ cmd += ['-state', state] if state
21
+
22
+ if vars
23
+ vars.each do |key, value|
24
+ var_value = value.is_a?(String) ? value : JSON.generate(value)
25
+ cmd += ['-var', "#{key}=#{var_value}"]
26
+ end
27
+ end
28
+
29
+ cmd += [dir] if dir
30
+ cmd
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Commands
4
+ class ValidateCommand
5
+ include ExecutionSupport
6
+
7
+ attr_accessor :dir
8
+ attr_accessor :json
9
+
10
+ def initialize(opts = {})
11
+ @dir = opts[:dir]
12
+ @json = opts[:json]
13
+ end
14
+
15
+ def command
16
+ cmd = %W[#{tf_binary} validate]
17
+
18
+ cmd += ['-json'] if json
19
+
20
+ cmd += [dir] if dir
21
+ cmd
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ module Ruby
2
+ module Terraform
3
+ class Configuration
4
+ attr_accessor :terraform_version
5
+ attr_accessor :download_path
6
+
7
+ def initialize(opts = {})
8
+ @terraform_version = opts[:terraform_version] || Ruby::Terraform::TERRAFORM_VERSION
9
+ @download_path = opts[:download_path] || File.expand_path("~/.terraform-bin/#{terraform_version}")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ require 'zip'
2
+
3
+ module Ruby
4
+ module Terraform
5
+ class Decompressor
6
+ class << self
7
+ def extract(input_file, output_dir, verbose = false)
8
+ Zip::File.open(input_file) do |zip_file|
9
+ zip_file.each do |entry|
10
+ dest_file = "#{output_dir}/#{entry.name}"
11
+ $stderr.puts("Extracting to #{dest_file}") if verbose
12
+ entry.extract(dest_file)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,75 @@
1
+ require 'os'
2
+ require 'open-uri'
3
+
4
+ module Ruby
5
+ module Terraform
6
+ module_function
7
+
8
+ def config
9
+ @config ||= Configuration.new
10
+ end
11
+
12
+ class Executable
13
+ class PlatformUnsupported < StandardError; end
14
+
15
+ def initialize(config = Ruby::Terraform.config)
16
+ @terraform_version = config.terraform_version
17
+ @download_path = config.download_path
18
+ @download_filename = "#{@terraform_version}-terraform.zip"
19
+
20
+ FileUtils.mkdir_p(@download_path)
21
+
22
+ raise PlatformUnsupported unless supported?
23
+ end
24
+
25
+ def binary
26
+ if OS.windows?
27
+ "#{@download_path}/terraform.exe"
28
+ else
29
+ "#{@download_path}/terraform"
30
+ end
31
+ end
32
+
33
+ def download(opts = {})
34
+ return if binary_exist?
35
+ opts[:uri] ||= ENV['TERRAFORM_DOWNLOAD_URL'] || "https://releases.hashicorp.com/#{download_uri}"
36
+
37
+ $stderr.puts("Downloading terraform binary from #{uri}") if opts[:verbose]
38
+
39
+ open("#{@download_path}/#{@download_filename}", 'wb') do |saved_file|
40
+ open(opts[:uri], 'rb') do |read_file|
41
+ saved_file.write(read_file.read)
42
+ end
43
+ end
44
+ end
45
+
46
+ def extract(verbose = false)
47
+ return if binary_exist?
48
+
49
+ Decompressor.extract("#{@download_path}/#{@download_filename}", @download_path, verbose)
50
+ make_exe
51
+ end
52
+
53
+ private
54
+
55
+ def binary_exist?
56
+ File.exist?(binary)
57
+ end
58
+
59
+ def supported?
60
+ OS.freebsd? || OS.mac? || OS.windows? || OS.linux?
61
+ end
62
+
63
+ def make_exe
64
+ FileUtils.chmod('a+x', binary) if binary_exist? && !OS.windows?
65
+ end
66
+
67
+ def download_uri
68
+ return "terraform/#{@terraform_version}/terraform_#{@terraform_version}_freebsd_amd64.zip" if OS.freebsd?
69
+ return "terraform/#{@terraform_version}/terraform_#{@terraform_version}_darwin_amd64.zip" if OS.mac?
70
+ return "terraform/#{@terraform_version}/terraform_#{@terraform_version}_windows_amd64.zip" if OS.windows?
71
+ return "terraform/#{@terraform_version}/terraform_#{@terraform_version}_linux_amd64.zip" if OS.linux?
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,37 @@
1
+ require 'mixlib/shellout'
2
+ require 'logger'
3
+
4
+ module Ruby
5
+ module Terraform
6
+ module ExecutionSupport
7
+ def execute(opts = {})
8
+ working_dir = opts[:dir] || (self.dir if self.respond_to?(:dir))
9
+
10
+ shellout_opts = {}
11
+ shellout_opts[:cwd] = working_dir if working_dir
12
+ if opts[:live]
13
+ shellout_opts[:live_stdout] = STDOUT
14
+ shellout_opts[:live_stderr] = STDERR
15
+ end
16
+
17
+ if opts[:show_command]
18
+ formatter = proc do |severity, datetime, progname, msg|
19
+ "#{msg}\n"
20
+ end
21
+ shellout_opts[:logger] = Logger.new(STDERR, formatter: formatter)
22
+ end
23
+
24
+ cmd = Mixlib::ShellOut.new(command, shellout_opts)
25
+ cmd.run_command
26
+ cmd.error!
27
+ end
28
+
29
+ def tf_binary
30
+ executable = Executable.new
31
+ executable.download
32
+ executable.extract
33
+ executable.binary
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,68 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Tasks
4
+ class AllTasks < BaseTask
5
+
6
+ attr_accessor :state
7
+ attr_accessor :vars
8
+ attr_accessor :auto_approve
9
+
10
+ attr_accessor :backend
11
+ attr_accessor :backend_config
12
+
13
+ attr_accessor :json
14
+
15
+ def define
16
+ InitTask.new(configuration_name, :init) do |task|
17
+ task.dir = dir
18
+ task.backend = backend
19
+ task.backend_config = backend_config
20
+ task.show_output = show_output
21
+ task.show_command = show_command
22
+ end
23
+
24
+ ApplyTask.new(configuration_name, :apply => :init) do |task|
25
+ task.dir = dir
26
+ task.state = state
27
+ task.vars = vars
28
+ task.auto_approve = auto_approve
29
+ task.show_output = show_output
30
+ task.show_command = show_command
31
+ end
32
+
33
+ DestroyTask.new(configuration_name, :destroy => :init) do |task|
34
+ task.dir = dir
35
+ task.state = state
36
+ task.vars = vars
37
+ task.auto_approve = auto_approve
38
+ task.show_output = show_output
39
+ task.show_command = show_command
40
+ end
41
+
42
+ OutputTask.new(configuration_name, :output => :init) do |task|
43
+ task.dir = dir
44
+ task.state = state
45
+ task.json = json
46
+ task.show_output = show_output
47
+ task.show_command = show_command
48
+ end
49
+
50
+ PlanTask.new(configuration_name, :plan => :init) do |task|
51
+ task.dir = dir
52
+ task.state = state
53
+ task.vars = vars
54
+ task.show_output = show_output
55
+ task.show_command = show_command
56
+ end
57
+
58
+ ValidateTask.new(configuration_name, :validate => :init) do |task|
59
+ task.dir = dir
60
+ task.json = json
61
+ task.show_output = show_output
62
+ task.show_command = show_command
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,18 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Tasks
4
+ class ApplyTask < BaseTask
5
+ attr_accessor :state
6
+ attr_accessor :vars
7
+ attr_accessor :auto_approve
8
+
9
+ def define
10
+ desc description || "Apply #{configuration_name} using terraform"
11
+ task task_name => Array(deps) do
12
+ Commands::ApplyCommand.new(dir: dir, state: state, vars: vars, auto_approve: auto_approve).execute(show_command: show_command, live: show_output)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,44 @@
1
+ require 'rake/tasklib'
2
+
3
+ module Ruby
4
+ module Terraform
5
+ module Tasks
6
+ class BaseTask < ::Rake::TaskLib
7
+ # name of configuration
8
+ attr_accessor :configuration_name
9
+
10
+ # name of task
11
+ attr_accessor :task_name
12
+
13
+ # the working dir of `terraform` command
14
+ attr_accessor :dir
15
+
16
+ # the description of the task
17
+ attr_accessor :description
18
+
19
+ # should the terraform command be shown on the console. Defaults to `false`.
20
+ attr_accessor :show_command
21
+
22
+ # should the terraform output be shown on the console. Defaults to `false`.
23
+ attr_accessor :show_output
24
+
25
+ # the task dependencies
26
+ attr_accessor :deps
27
+
28
+ def initialize(configuration_name, task_name = nil)
29
+ @configuration_name = configuration_name
30
+ @task_name = task_name
31
+ @deps = []
32
+
33
+ if @task_name.is_a?(Hash)
34
+ @deps = @task_name.values.first
35
+ @task_name = @task_name.keys.first
36
+ end
37
+
38
+ yield self if block_given?
39
+ define
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,18 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Tasks
4
+ class DestroyTask < BaseTask
5
+ attr_accessor :state
6
+ attr_accessor :vars
7
+ attr_accessor :auto_approve
8
+
9
+ def define
10
+ desc description || "Destroy #{configuration_name} using terraform"
11
+ task task_name => Array(deps) do
12
+ Commands::DestroyCommand.new(dir: dir, state: state, vars: vars, auto_approve: auto_approve).execute(show_command: show_command, live: show_output)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Tasks
4
+ class InitTask < BaseTask
5
+ attr_accessor :backend
6
+ attr_accessor :backend_config
7
+
8
+ def define
9
+ desc description || "Initialize #{configuration_name} using terraform"
10
+ task task_name => Array(deps) do
11
+ Commands::InitCommand.new(dir: dir, backend: backend, backend_config: backend_config).execute(show_command: show_command, live: show_output)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Tasks
4
+ class OutputTask < BaseTask
5
+ attr_accessor :state
6
+ attr_accessor :json
7
+ attr_accessor :name
8
+
9
+ def define
10
+ desc description || "Output #{configuration_name} using terraform"
11
+ task task_name => Array(deps) do
12
+ Commands::OutputCommand.new(dir: dir, state: state, json: json, name: name).execute(show_command: show_command, live: show_output)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Tasks
4
+ class PlanTask < BaseTask
5
+ attr_accessor :state
6
+ attr_accessor :vars
7
+
8
+ def define
9
+ desc description || "Plan #{configuration_name} using terraform"
10
+ task task_name => Array(deps) do
11
+ Commands::PlanCommand.new(dir: dir, state: state, vars: vars).execute(show_command: show_command, live: show_output)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module Ruby
2
+ module Terraform
3
+ module Tasks
4
+ class ValidateTask < BaseTask
5
+ attr_accessor :json
6
+
7
+ def define
8
+ desc description || "Validate #{configuration_name} using terraform"
9
+ task task_name => Array(deps) do
10
+ Commands::ValidateCommand.new(dir: dir, json: json).execute(show_command: show_command, live: show_output)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module Ruby
2
+ module Terraform
3
+ module TerraformDownloadSupport
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module Ruby
2
+ module Terraform
3
+ VERSION = "0.1.0".freeze
4
+ TERRAFORM_VERSION = '0.13.3'.freeze
5
+ end
6
+ end
@@ -0,0 +1,46 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "ruby/terraform/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rake-terraform-wrapper"
7
+ spec.version = Ruby::Terraform::VERSION
8
+ spec.authors = ["Ruby Terraform"]
9
+ spec.email = ["ketan@thoughtworks.com"]
10
+
11
+ spec.summary = %q{A ruby wrapper around terraform.}
12
+ spec.description = %q{A ruby wrapper around terraform.}
13
+ spec.homepage = "https://github.com/ketan/rake-terraform-wrapper"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/ketan/rake-terraform-wrapper"
23
+ spec.metadata["changelog_uri"] = "https://github.com/ketan/rake-terraform-wrapper"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_dependency "mixlib-shellout", "~> 3.1"
39
+ spec.add_dependency "os", "~> 1.1"
40
+ spec.add_dependency "rubyzip", "~> 2.3"
41
+ spec.add_dependency "rake", "~> 13.0"
42
+
43
+ spec.add_development_dependency "bundler", "~> 1.17"
44
+ spec.add_development_dependency "rspec", "~> 3.0"
45
+ spec.add_development_dependency "guard-rspec", "~> 4.7"
46
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rake-terraform-wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ruby Terraform
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mixlib-shellout
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: os
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubyzip
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.17'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.17'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.7'
111
+ description: A ruby wrapper around terraform.
112
+ email:
113
+ - ketan@thoughtworks.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".github/workflows/ruby.yml"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - Guardfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - lib/ruby/terraform.rb
131
+ - lib/ruby/terraform/commands/apply_command.rb
132
+ - lib/ruby/terraform/commands/destroy_command.rb
133
+ - lib/ruby/terraform/commands/init_command.rb
134
+ - lib/ruby/terraform/commands/output_command.rb
135
+ - lib/ruby/terraform/commands/plan_command.rb
136
+ - lib/ruby/terraform/commands/validate_command.rb
137
+ - lib/ruby/terraform/configuration.rb
138
+ - lib/ruby/terraform/decompressor.rb
139
+ - lib/ruby/terraform/executable.rb
140
+ - lib/ruby/terraform/execution_support.rb
141
+ - lib/ruby/terraform/tasks/all_tasks.rb
142
+ - lib/ruby/terraform/tasks/apply_task.rb
143
+ - lib/ruby/terraform/tasks/base_task.rb
144
+ - lib/ruby/terraform/tasks/destroy_task.rb
145
+ - lib/ruby/terraform/tasks/init_task.rb
146
+ - lib/ruby/terraform/tasks/output_task.rb
147
+ - lib/ruby/terraform/tasks/plan_task.rb
148
+ - lib/ruby/terraform/tasks/validate_task.rb
149
+ - lib/ruby/terraform/terraform_download_support.rb
150
+ - lib/ruby/terraform/version.rb
151
+ - rake-terraform-wrapper.gemspec
152
+ homepage: https://github.com/ketan/rake-terraform-wrapper
153
+ licenses:
154
+ - MIT
155
+ metadata:
156
+ allowed_push_host: https://rubygems.org
157
+ homepage_uri: https://github.com/ketan/rake-terraform-wrapper
158
+ source_code_uri: https://github.com/ketan/rake-terraform-wrapper
159
+ changelog_uri: https://github.com/ketan/rake-terraform-wrapper
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubygems_version: 3.0.3
176
+ signing_key:
177
+ specification_version: 4
178
+ summary: A ruby wrapper around terraform.
179
+ test_files: []