ruby_terraform 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fcd14aca27aa2e663ef70a25683b91c35e14fb13
4
+ data.tar.gz: b15fbab24d3bd7f7c7dd1dfa232d4289ddc07002
5
+ SHA512:
6
+ metadata.gz: 7d43f90458fbbe0dea58de37d77994f6a3746b258798fe8228a4adecfcaadc5e38c65b4f94c78595a6fdc5f9a9ca22f0cb6d83210f2f1a38604e6585a00832e8
7
+ data.tar.gz: e292c532fb6958f078b061c9635122cc6535eeb82a45240aa18b245c0fbba2b184cdcb63995f259dee42efb6f040b7068de4c5f3532da95ca0ed26c4d909e591
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,14 @@
1
+ LineLength:
2
+ Max: 130
3
+
4
+ MethodLength:
5
+ Max: 20
6
+
7
+ Documentation:
8
+ Enabled: false
9
+
10
+ AbcSize:
11
+ Enabled: false
12
+
13
+ Style/RegexpLiteral:
14
+ Enabled: false
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_terraform.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'spring'
8
+ gem 'spring-commands-rspec'
9
+
10
+ gem 'guard'
11
+ gem 'guard-rubocop'
12
+
13
+ gem 'rspec'
14
+ gem 'rspec-mocks'
15
+ gem 'guard-rspec', require: false
16
+ end
@@ -0,0 +1,52 @@
1
+ directories %w(lib spec) \
2
+ .select { |d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist") }
3
+
4
+ guard :rspec, cmd: 'bundle exec rspec' do
5
+ require 'guard/rspec/dsl'
6
+ dsl = Guard::RSpec::Dsl.new(self)
7
+
8
+ # Feel free to open issues for suggestions and improvements
9
+
10
+ # RSpec files
11
+ rspec = dsl.rspec
12
+ watch(rspec.spec_helper) { rspec.spec_dir }
13
+ watch(rspec.spec_support) { rspec.spec_dir }
14
+ watch(rspec.spec_files)
15
+
16
+ # Ruby files
17
+ ruby = dsl.ruby
18
+ dsl.watch_spec_files_for(ruby.lib_files)
19
+
20
+ # Rails files
21
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
22
+ dsl.watch_spec_files_for(rails.app_files)
23
+ dsl.watch_spec_files_for(rails.views)
24
+
25
+ watch(rails.controllers) do |m|
26
+ [
27
+ rspec.spec.call("routing/#{m[1]}_routing"),
28
+ rspec.spec.call("controllers/#{m[1]}_controller"),
29
+ rspec.spec.call("acceptance/#{m[1]}")
30
+ ]
31
+ end
32
+
33
+ # Rails config changes
34
+ watch(rails.spec_helper) { rspec.spec_dir }
35
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
36
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
37
+
38
+ # Capybara features specs
39
+ watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
40
+ watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
41
+
42
+ # Turnip features and steps
43
+ watch(%r{^spec/acceptance/(.+)\.feature$})
44
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
45
+ Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
46
+ end
47
+ end
48
+
49
+ guard :rubocop do
50
+ watch(/.+\.rb$/)
51
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
52
+ end
@@ -0,0 +1,39 @@
1
+ # RubyTerraform
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ruby_terraform`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ruby_terraform'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ruby_terraform
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/[my-github-username]/ruby_terraform/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
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -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
@@ -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,5 @@
1
+ require 'ruby_terraform/version'
2
+ require 'ruby_terraform/client'
3
+
4
+ module RubyTerraform
5
+ end
@@ -0,0 +1,115 @@
1
+ require 'systemu'
2
+
3
+ module RubyTerraform
4
+ class Client
5
+ def initialize(directory, options)
6
+ @directory = directory
7
+ @options = options
8
+ end
9
+
10
+ def get(variables = {}, options = {})
11
+ status, _stdout, stderr = run('get', variables, options)
12
+ fail "Execute terraform get has been failed\n#{stderr}" unless status.success?
13
+ true
14
+ end
15
+
16
+ def plan(variables = {}, options = {})
17
+ options = { input: false, 'no-color' => nil }.merge(options)
18
+
19
+ status, stdout, stderr = run('plan', variables, options)
20
+ fail "Execute terraform plan has been failed\n#{stderr}" unless [0, 2].include?(status.exitstatus)
21
+
22
+ { add: {}, destroy: {}, change: {} }.tap do |hash|
23
+ stdout.split("\n").each do |line|
24
+ case line
25
+ when /^\-\/\+ (.*)/
26
+ build_hash(hash[:add], Regexp.last_match(1))
27
+ build_hash(hash[:destroy], Regexp.last_match(1))
28
+ when /^\+ (.*)/
29
+ build_hash(hash[:add], Regexp.last_match(1))
30
+ when /^\- (.*)/
31
+ build_hash(hash[:destroy], Regexp.last_match(1))
32
+ when /^\~ (.*)/
33
+ build_hash(hash[:change], Regexp.last_match(1))
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def apply(variables = {}, options = {})
40
+ options = { input: false, 'no-color' => nil }.merge(options)
41
+
42
+ status, _stdout, stderr = run('apply', variables, options)
43
+ fail "Execute terraform apply has been failed\n#{stderr}" unless status.success?
44
+ true
45
+ end
46
+
47
+ def show(variables = {}, options = {})
48
+ options = { 'no-color' => nil }.merge(options)
49
+
50
+ status, stdout, stderr = run('show', variables, options)
51
+ fail "Execute terraform show has been failed\n#{stderr}" unless [0, 2].include?(status.exitstatus)
52
+
53
+ # Remove outputs
54
+ stdout = stdout.gsub(/^Outputs:$.*/m, '')
55
+
56
+ {}.tap do |hash|
57
+ stdout.split("\n").each do |line|
58
+ next unless (match = line.match(/^(\S*):$/))
59
+ build_hash(hash, match[1])
60
+ end
61
+ end
62
+ end
63
+
64
+ def output(variables = {}, options = {})
65
+ options = { 'no-color' => nil }.merge(options)
66
+
67
+ status, stdout, stderr = run('output', variables, options)
68
+ fail "Execute terraform output has been failed\n#{stderr}" unless status.success?
69
+
70
+ Hash[stdout.split("\n").map { |line| line.split(' = ') }]
71
+ end
72
+
73
+ def destroy(variables = {}, options = {})
74
+ options = { force: nil, 'no-color' => nil }.merge(options)
75
+
76
+ status, _stdout, stderr = run('destroy', variables, options)
77
+ fail "Execute terraform destroy has been failed\n#{stderr}" unless status.success?
78
+ true
79
+ end
80
+
81
+ private
82
+
83
+ def run(subcommand, variables = {}, options = {})
84
+ terraform_path = @options[:terraform_path] || 'terraform'
85
+ command = "#{terraform_path} #{subcommand} #{options2command(options)} #{vars2command(variables)}"
86
+ systemu(command, cwd: @directory).tap do |args|
87
+ yield(*args) if block_given?
88
+ end
89
+ end
90
+
91
+ def vars2command(variables)
92
+ variables.map { |key, value| "-var #{key.to_s.shellescape}=#{value.to_s.shellescape}" }.join(' ')
93
+ end
94
+
95
+ def options2command(options)
96
+ commands = options.map do |key, value|
97
+ if value.nil?
98
+ "-#{key.to_s.shellescape}"
99
+ elsif value.is_a?(Array)
100
+ value.map { |v| "-#{key.to_s.shellescape}=#{v.to_s.shellescape}" }.join(' ')
101
+ else
102
+ "-#{key.to_s.shellescape}=#{value.to_s.shellescape}"
103
+ end
104
+ end
105
+ commands.join(' ')
106
+ end
107
+
108
+ def build_hash(hash, dotted_string)
109
+ dotted_string.split('.').inject(hash) do |h, key|
110
+ h[key] ||= {}
111
+ end
112
+ hash
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,3 @@
1
+ module RubyTerraform
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_terraform/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ruby_terraform'
8
+ spec.version = RubyTerraform::VERSION
9
+ spec.authors = ['Yo Takezawa']
10
+ spec.email = ['takezawa.yo@tis.co.jp']
11
+
12
+ spec.summary = 'Execute terraform from ruby'
13
+ spec.description = 'This gem can execute terraform plan, apply or destroy that are provided by hashicorp'
14
+ spec.homepage = 'https://github.com/cloudconductor/ruby_terraform'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.9'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'systemu', '~> 2.6'
24
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_terraform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yo Takezawa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-30 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: systemu
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.6'
55
+ description: This gem can execute terraform plan, apply or destroy that are provided
56
+ by hashicorp
57
+ email:
58
+ - takezawa.yo@tis.co.jp
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".rubocop.yml"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - Guardfile
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/ruby_terraform.rb
74
+ - lib/ruby_terraform/client.rb
75
+ - lib/ruby_terraform/version.rb
76
+ - ruby_terraform.gemspec
77
+ homepage: https://github.com/cloudconductor/ruby_terraform
78
+ licenses: []
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Execute terraform from ruby
100
+ test_files: []