yle_tf 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.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +26 -0
  5. data/.travis.yml +7 -0
  6. data/CHANGELOG.md +3 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +442 -0
  11. data/Rakefile +6 -0
  12. data/bin/tf +7 -0
  13. data/examples/envs/prod.tfvars +5 -0
  14. data/examples/envs/test.tfvars +2 -0
  15. data/examples/main.tf +10 -0
  16. data/examples/tf.yaml +4 -0
  17. data/examples/tf_hooks/pre/get_current_hash.sh +26 -0
  18. data/examples/variables.tf +22 -0
  19. data/lib/yle_tf.rb +70 -0
  20. data/lib/yle_tf/action.rb +29 -0
  21. data/lib/yle_tf/action/builder.rb +9 -0
  22. data/lib/yle_tf/action/command.rb +25 -0
  23. data/lib/yle_tf/action/copy_root_module.rb +22 -0
  24. data/lib/yle_tf/action/generate_vars_file.rb +27 -0
  25. data/lib/yle_tf/action/load_config.rb +17 -0
  26. data/lib/yle_tf/action/terraform_init.rb +63 -0
  27. data/lib/yle_tf/action/tf_hooks.rb +48 -0
  28. data/lib/yle_tf/action/tmpdir.rb +35 -0
  29. data/lib/yle_tf/action/verify_terraform_version.rb +41 -0
  30. data/lib/yle_tf/action/verify_tf_env.rb +24 -0
  31. data/lib/yle_tf/backend_config.rb +41 -0
  32. data/lib/yle_tf/cli.rb +88 -0
  33. data/lib/yle_tf/config.rb +45 -0
  34. data/lib/yle_tf/config/defaults.rb +35 -0
  35. data/lib/yle_tf/config/erb.rb +22 -0
  36. data/lib/yle_tf/config/file.rb +26 -0
  37. data/lib/yle_tf/config/loader.rb +108 -0
  38. data/lib/yle_tf/error.rb +4 -0
  39. data/lib/yle_tf/logger.rb +48 -0
  40. data/lib/yle_tf/plugin.rb +55 -0
  41. data/lib/yle_tf/plugin/action_hook.rb +23 -0
  42. data/lib/yle_tf/plugin/loader.rb +59 -0
  43. data/lib/yle_tf/plugin/manager.rb +49 -0
  44. data/lib/yle_tf/system.rb +22 -0
  45. data/lib/yle_tf/tf_hook.rb +90 -0
  46. data/lib/yle_tf/tf_hook/runner.rb +48 -0
  47. data/lib/yle_tf/vars_file.rb +42 -0
  48. data/lib/yle_tf/version.rb +3 -0
  49. data/lib/yle_tf/version_requirement.rb +25 -0
  50. data/lib/yle_tf_plugins/backends/file/command.rb +31 -0
  51. data/lib/yle_tf_plugins/backends/file/config.rb +17 -0
  52. data/lib/yle_tf_plugins/backends/file/plugin.rb +16 -0
  53. data/lib/yle_tf_plugins/backends/s3/command.rb +19 -0
  54. data/lib/yle_tf_plugins/backends/s3/plugin.rb +16 -0
  55. data/lib/yle_tf_plugins/commands/__default/command.rb +14 -0
  56. data/lib/yle_tf_plugins/commands/__default/plugin.rb +14 -0
  57. data/lib/yle_tf_plugins/commands/_config/command.rb +11 -0
  58. data/lib/yle_tf_plugins/commands/_config/plugin.rb +19 -0
  59. data/lib/yle_tf_plugins/commands/_shell/command.rb +15 -0
  60. data/lib/yle_tf_plugins/commands/_shell/plugin.rb +14 -0
  61. data/lib/yle_tf_plugins/commands/help/command.rb +55 -0
  62. data/lib/yle_tf_plugins/commands/help/plugin.rb +18 -0
  63. data/lib/yle_tf_plugins/commands/version/command.rb +20 -0
  64. data/lib/yle_tf_plugins/commands/version/plugin.rb +18 -0
  65. data/vendor/hash_deep_merge.rb +59 -0
  66. data/vendor/logger_level_patch.rb +29 -0
  67. data/vendor/middleware/LICENSE +23 -0
  68. data/vendor/middleware/builder.rb +149 -0
  69. data/vendor/middleware/runner.rb +69 -0
  70. data/yle_tf.gemspec +37 -0
  71. metadata +160 -0
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2012 Mitchell Hashimoto
2
+ Copyright (c) 2017 Teemu Matilainen
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,149 @@
1
+ require_relative 'runner'
2
+
3
+ module Middleware
4
+ # This provides a DSL for building up a stack of middlewares.
5
+ #
6
+ # This code is based heavily off of `Rack::Builder` and
7
+ # `ActionDispatch::MiddlewareStack` in Rack and Rails, respectively.
8
+ #
9
+ # # Usage
10
+ #
11
+ # Building a middleware stack is very easy:
12
+ #
13
+ # app = Middleware::Builder.new do
14
+ # use A
15
+ # use B
16
+ # end
17
+ #
18
+ # # Call the middleware
19
+ # app.call(7)
20
+ #
21
+ class Builder
22
+ # Initializes the builder. An optional block can be passed which
23
+ # will be evaluated in the context of the instance.
24
+ #
25
+ # Example:
26
+ #
27
+ # Builder.new do
28
+ # use A
29
+ # use B
30
+ # end
31
+ #
32
+ # @param [Hash] opts Options hash
33
+ # @option opts [Class] :runner_class The class to wrap the middleware stack
34
+ # in which knows how to run them.
35
+ # @yield [] Evaluated in this instance which allows you to use methods
36
+ # like {#use} and such.
37
+ def initialize(opts=nil, &block)
38
+ opts ||= {}
39
+ @runner_class = opts[:runner_class] || Runner
40
+ instance_eval(&block) if block_given?
41
+ end
42
+
43
+ # Returns a mergeable version of the builder. If `use` is called with
44
+ # the return value of this method, then the stack will merge, instead
45
+ # of being treated as a separate single middleware.
46
+ def flatten
47
+ lambda do |env|
48
+ self.call(env)
49
+ end
50
+ end
51
+
52
+ # Adds a middleware class to the middleware stack. Any additional
53
+ # args and a block, if given, are saved and passed to the initializer
54
+ # of the middleware.
55
+ #
56
+ # @param [Class] middleware The middleware class
57
+ def use(middleware, *args, &block)
58
+ if middleware.kind_of?(Builder)
59
+ # Merge in the other builder's stack into our own
60
+ self.stack.concat(middleware.stack)
61
+ else
62
+ self.stack << [middleware, args, block]
63
+ end
64
+
65
+ self
66
+ end
67
+
68
+ # Inserts a middleware at the given index or directly before the
69
+ # given middleware object.
70
+ def insert(index, middleware, *args, &block)
71
+ index = self.index(index) unless index.is_a?(Integer)
72
+ raise "no such middleware to insert before: #{index.inspect}" unless index
73
+
74
+ if middleware.kind_of?(Builder)
75
+ middleware.stack.reverse.each do |stack_item|
76
+ stack.insert(index, stack_item)
77
+ end
78
+ else
79
+ stack.insert(index, [middleware, args, block])
80
+ end
81
+ end
82
+
83
+ alias_method :insert_before, :insert
84
+
85
+ # Inserts a middleware after the given index or middleware object.
86
+ def insert_after(index, middleware, *args, &block)
87
+ index = self.index(index) unless index.is_a?(Integer)
88
+ raise "no such middleware to insert after: #{index.inspect}" unless index
89
+ insert(index + 1, middleware, *args, &block)
90
+ end
91
+
92
+ # Replaces the given middlware object or index with the new
93
+ # middleware.
94
+ def replace(index, middleware, *args, &block)
95
+ if index.is_a?(Integer)
96
+ delete(index)
97
+ insert(index, middleware, *args, &block)
98
+ else
99
+ insert_before(index, middleware, *args, &block)
100
+ delete(index)
101
+ end
102
+ end
103
+
104
+ # Deletes the given middleware object or index
105
+ def delete(index)
106
+ index = self.index(index) unless index.is_a?(Integer)
107
+ stack.delete_at(index)
108
+ end
109
+
110
+ # Runs the builder stack with the given environment.
111
+ def call(env=nil)
112
+ to_app.call(env)
113
+ end
114
+
115
+ # Returns truish if the given middleware object exists in the stack.
116
+ def include?(object)
117
+ index(object)
118
+ end
119
+
120
+ protected
121
+
122
+ # Returns the numeric index for the given middleware object.
123
+ #
124
+ # @param [Object] object The item to find the index for
125
+ # @return [Integer]
126
+ def index(object)
127
+ stack.each_with_index do |item, i|
128
+ return i if item[0] == object
129
+ end
130
+
131
+ nil
132
+ end
133
+
134
+ # Returns the current stack of middlewares. You probably won't
135
+ # need to use this directly, and it's recommended that you don't.
136
+ #
137
+ # @return [Array]
138
+ def stack
139
+ @stack ||= []
140
+ end
141
+
142
+ # Converts the builder stack to a runnable action sequence.
143
+ #
144
+ # @return [Object] A callable object
145
+ def to_app
146
+ @runner_class.new(stack.dup)
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,69 @@
1
+ module Middleware
2
+ # This is a basic runner for middleware stacks. This runner does
3
+ # the default expected behavior of running the middleware stacks
4
+ # in order, then reversing the order.
5
+ class Runner
6
+ # A middleware which does nothing
7
+ EMPTY_MIDDLEWARE = lambda { |env| }
8
+
9
+ # Build a new middleware runner with the given middleware
10
+ # stack.
11
+ #
12
+ # Note: This class usually doesn't need to be used directly.
13
+ # Instead, take a look at using the {Builder} class, which is
14
+ # a much friendlier way to build up a middleware stack.
15
+ #
16
+ # @param [Array] stack An array of the middleware to run.
17
+ def initialize(stack)
18
+ # We need to take the stack of middleware and initialize them
19
+ # all so they call the proper next middleware.
20
+ @kickoff = build_call_chain(stack)
21
+ end
22
+
23
+ # Run the middleware stack with the given state bag.
24
+ #
25
+ # @param [Object] env The state to pass into as the initial
26
+ # environment data. This is usual a hash of some sort.
27
+ def call(env)
28
+ # We just call the kickoff middleware, which is responsible
29
+ # for properly calling the next middleware, and so on and so
30
+ # forth.
31
+ @kickoff.call(env)
32
+ end
33
+
34
+ protected
35
+
36
+ # This takes a stack of middlewares and initializes them in a way
37
+ # that each middleware properly calls the next middleware.
38
+ def build_call_chain(stack)
39
+ # We need to instantiate the middleware stack in reverse
40
+ # order so that each middleware can have a reference to
41
+ # the next middleware it has to call. The final middleware
42
+ # is always the empty middleware, which does nothing but return.
43
+ stack.reverse.inject(EMPTY_MIDDLEWARE) do |next_middleware, current_middleware|
44
+ # Unpack the actual item
45
+ klass, args, block = current_middleware
46
+
47
+ # Default the arguments to an empty array. Otherwise in Ruby 1.8
48
+ # a `nil` args will actually pass `nil` into the class. Not what
49
+ # we want!
50
+ args ||= []
51
+
52
+ if klass.is_a?(Class)
53
+ # If the klass actually is a class, then instantiate it with
54
+ # the app and any other arguments given.
55
+ klass.new(next_middleware, *args, &block)
56
+ elsif klass.respond_to?(:call)
57
+ # Make it a lambda which calls the item then forwards up
58
+ # the chain.
59
+ lambda do |env|
60
+ klass.call(env)
61
+ next_middleware.call(env)
62
+ end
63
+ else
64
+ raise "Invalid middleware, doesn't respond to `call`: #{action.inspect}"
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
data/yle_tf.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'yle_tf/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'yle_tf'
9
+ spec.version = YleTf::VERSION
10
+ spec.summary = 'Tooling for Terraform'
11
+ spec.description = 'Tooling for Terraform to support environments, hooks, etc.'
12
+ spec.homepage = 'https://github.com/Yleisradio/yle_tf'
13
+ spec.license = 'MIT'
14
+
15
+ spec.authors = [
16
+ 'Yleisradio',
17
+ 'Teemu Matilainen',
18
+ 'Antti Forsell',
19
+ ]
20
+ spec.email = [
21
+ 'devops@yle.fi',
22
+ 'teemu.matilainen@iki.fi',
23
+ 'antti.forsell@iki.fi',
24
+ ]
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+
30
+ spec.bindir = 'bin'
31
+ spec.executables = ['tf']
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_development_dependency 'bundler', '~> 1.13'
35
+ spec.add_development_dependency 'rake', '~> 12.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.5'
37
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yle_tf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yleisradio
8
+ - Teemu Matilainen
9
+ - Antti Forsell
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2017-08-29 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.13'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.13'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '12.0'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '12.0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: rspec
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.5'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '3.5'
57
+ description: Tooling for Terraform to support environments, hooks, etc.
58
+ email:
59
+ - devops@yle.fi
60
+ - teemu.matilainen@iki.fi
61
+ - antti.forsell@iki.fi
62
+ executables:
63
+ - tf
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - ".gitignore"
68
+ - ".rspec"
69
+ - ".rubocop.yml"
70
+ - ".travis.yml"
71
+ - CHANGELOG.md
72
+ - CODE_OF_CONDUCT.md
73
+ - Gemfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - bin/tf
78
+ - examples/envs/prod.tfvars
79
+ - examples/envs/test.tfvars
80
+ - examples/main.tf
81
+ - examples/tf.yaml
82
+ - examples/tf_hooks/pre/get_current_hash.sh
83
+ - examples/variables.tf
84
+ - lib/yle_tf.rb
85
+ - lib/yle_tf/action.rb
86
+ - lib/yle_tf/action/builder.rb
87
+ - lib/yle_tf/action/command.rb
88
+ - lib/yle_tf/action/copy_root_module.rb
89
+ - lib/yle_tf/action/generate_vars_file.rb
90
+ - lib/yle_tf/action/load_config.rb
91
+ - lib/yle_tf/action/terraform_init.rb
92
+ - lib/yle_tf/action/tf_hooks.rb
93
+ - lib/yle_tf/action/tmpdir.rb
94
+ - lib/yle_tf/action/verify_terraform_version.rb
95
+ - lib/yle_tf/action/verify_tf_env.rb
96
+ - lib/yle_tf/backend_config.rb
97
+ - lib/yle_tf/cli.rb
98
+ - lib/yle_tf/config.rb
99
+ - lib/yle_tf/config/defaults.rb
100
+ - lib/yle_tf/config/erb.rb
101
+ - lib/yle_tf/config/file.rb
102
+ - lib/yle_tf/config/loader.rb
103
+ - lib/yle_tf/error.rb
104
+ - lib/yle_tf/logger.rb
105
+ - lib/yle_tf/plugin.rb
106
+ - lib/yle_tf/plugin/action_hook.rb
107
+ - lib/yle_tf/plugin/loader.rb
108
+ - lib/yle_tf/plugin/manager.rb
109
+ - lib/yle_tf/system.rb
110
+ - lib/yle_tf/tf_hook.rb
111
+ - lib/yle_tf/tf_hook/runner.rb
112
+ - lib/yle_tf/vars_file.rb
113
+ - lib/yle_tf/version.rb
114
+ - lib/yle_tf/version_requirement.rb
115
+ - lib/yle_tf_plugins/backends/file/command.rb
116
+ - lib/yle_tf_plugins/backends/file/config.rb
117
+ - lib/yle_tf_plugins/backends/file/plugin.rb
118
+ - lib/yle_tf_plugins/backends/s3/command.rb
119
+ - lib/yle_tf_plugins/backends/s3/plugin.rb
120
+ - lib/yle_tf_plugins/commands/__default/command.rb
121
+ - lib/yle_tf_plugins/commands/__default/plugin.rb
122
+ - lib/yle_tf_plugins/commands/_config/command.rb
123
+ - lib/yle_tf_plugins/commands/_config/plugin.rb
124
+ - lib/yle_tf_plugins/commands/_shell/command.rb
125
+ - lib/yle_tf_plugins/commands/_shell/plugin.rb
126
+ - lib/yle_tf_plugins/commands/help/command.rb
127
+ - lib/yle_tf_plugins/commands/help/plugin.rb
128
+ - lib/yle_tf_plugins/commands/version/command.rb
129
+ - lib/yle_tf_plugins/commands/version/plugin.rb
130
+ - vendor/hash_deep_merge.rb
131
+ - vendor/logger_level_patch.rb
132
+ - vendor/middleware/LICENSE
133
+ - vendor/middleware/builder.rb
134
+ - vendor/middleware/runner.rb
135
+ - yle_tf.gemspec
136
+ homepage: https://github.com/Yleisradio/yle_tf
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.6.11
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Tooling for Terraform
160
+ test_files: []