TerraformDevKit 0.2.2 → 0.2.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a0a6b6bc02df8351a7ce6b5fa4184335d77e9058
4
- data.tar.gz: 65ad8fb0acce6af24e09bfe3023be632f0b49a46
3
+ metadata.gz: 3b16b145303034cd5bb4067a2067cb6375fd2671
4
+ data.tar.gz: 2ba0084ba9439bfc8b225f0692bd4ee56f3065cd
5
5
  SHA512:
6
- metadata.gz: 509b73c78dcb20c729f5d952d2c7cf0b33dd748e9c3fe5ce0bc6854b5c93ccbc8963b743c533eca5f75267c4fe36d80bfb5f2d65f920376f7a9e57850b0ca6f4
7
- data.tar.gz: 98d862ea73043dbcdd3e8e18c9676d4d24729829f2f94c2342a711bce560cfab10a2fca0d6eaba44bf0c786ed34471bdb183fe7bb1776aa6c6d847e4990df568
6
+ metadata.gz: 5dceda5b12de14d4b694d15c91b605f42063b4cf81ae05768b794d709b96335fd1009e602f4e2c989c39d5cb49c792f12d0734613cba3edd83312f4afb51e1f6
7
+ data.tar.gz: d9cd74d4249954c8596ea643eb5d0ed41e630c31d5218bb4046f870e841a2c0fb7ab58f124c8ac669a52fcc7244fd7b8dc6cd78a0a9a47f779a0bab7cb8f22f5
data/README.md CHANGED
@@ -20,6 +20,8 @@ The script collection includes support for:
20
20
 
21
21
  Most of these scripts exist to provide support to a module development and testing environment for Terraform: [TerraformModules](https://github.com/vistaprint/TerraformModules). But, they might be useful for other purposes too.
22
22
 
23
+ Currently this repository is tightly coupled with AWS and has not been tested to work with other providers. We are actively working to change this and hope to have a more generic solution soon. If you would like to see support for your favourite cloud provider please have submit a pull request implementing support and we will be more than happy to merge your changes in.
24
+
23
25
  ## Installation
24
26
 
25
27
  Add this line to your application's Gemfile:
@@ -89,6 +91,16 @@ task :custom_test, [:env] do |_, args|
89
91
  end
90
92
  ```
91
93
 
94
+ #### Overrides
95
+
96
+ It's possible to override the location of your config files by setting the variable `CONFIG_FILE` in the top level `Rakefile`
97
+
98
+ ```ruby
99
+ # %s will be substituted with the environment name.
100
+ # File is exected to live in /c/path/to/root/config/config-dev.yml
101
+ CONFIG_FILE = File.join(ROOT_PATH, 'config', 'config-%s.yml')
102
+ ```
103
+
92
104
  ### Tasks and Hooks
93
105
 
94
106
  TerraformDevKit provides a set of generic tasks to perform:
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'rspec', '~> 3.0'
27
27
  spec.add_development_dependency 'webmock', '~> 3.0'
28
28
 
29
- spec.add_runtime_dependency 'aws-sdk', '~> 2.9'
29
+ spec.add_runtime_dependency 'aws-sdk', '~> 3'
30
30
  spec.add_runtime_dependency 'mustache', '~> 1.0'
31
31
  spec.add_runtime_dependency 'rainbow', '~> 3.0'
32
32
  spec.add_runtime_dependency 'rubyzip', '~> 1.2'
@@ -1,26 +1,26 @@
1
- require 'open3'
1
+ require 'open3'
2
+
3
+ module TerraformDevKit
4
+ class Command
5
+ def self.run(cmd, directory: Dir.pwd, print_output: true)
6
+ Open3.popen2e(cmd, chdir: directory) do |_, stdout_and_stderr, thread|
7
+ output = process_output(stdout_and_stderr, print_output)
2
8
 
3
- module TerraformDevKit
4
- class Command
5
- def self.run(cmd, directory: Dir.pwd, print_output: true)
6
- Open3.popen2e(cmd, chdir: directory) do |_, stdout_and_stderr, thread|
7
- output = process_output(stdout_and_stderr, print_output)
8
-
9
- thread.join
10
- raise "Error running command #{cmd}" unless thread.value.success?
11
- return output
12
- end
13
- end
14
-
15
- private_class_method
16
- def self.process_output(stream, print_output)
17
- lines = []
18
-
19
- until (line = stream.gets).nil?
20
- print line if print_output
21
- lines << line.strip
22
- end
23
- lines
24
- end
25
- end
26
- end
9
+ thread.join
10
+ raise "Error running command #{cmd}" unless thread.value.success?
11
+ return output
12
+ end
13
+ end
14
+
15
+ private_class_method
16
+ def self.process_output(stream, print_output)
17
+ lines = []
18
+
19
+ until (line = stream.gets).nil?
20
+ print line if print_output
21
+ lines << line.strip
22
+ end
23
+ lines
24
+ end
25
+ end
26
+ end
@@ -30,7 +30,7 @@ module TerraformDevKit
30
30
  end
31
31
 
32
32
  def working_dir
33
- "envs/#{@name}"
33
+ File.join(ROOT_PATH, 'envs', @name)
34
34
  end
35
35
 
36
36
  def self.temp_name
@@ -1,3 +1,3 @@
1
1
  module TerraformDevKit
2
- VERSION = '0.2.2'.freeze
2
+ VERSION = '0.2.5'.freeze
3
3
  end
data/tasks/devkit.rake CHANGED
@@ -6,6 +6,7 @@ TDK = TerraformDevKit
6
6
 
7
7
  raise 'ROOT_PATH is not defined' if defined?(ROOT_PATH).nil?
8
8
  BIN_PATH = File.join(ROOT_PATH, 'bin')
9
+ CONFIG_FILE ||= File.join(ROOT_PATH, 'config', 'config-%s.yml')
9
10
 
10
11
  # Ensure terraform is in the PATH
11
12
  ENV['PATH'] = TDK::OS.join_env_path(
@@ -15,17 +16,28 @@ ENV['PATH'] = TDK::OS.join_env_path(
15
16
 
16
17
  PLAN_FILE = 'plan.tfplan'.freeze
17
18
 
18
- def destroy_if_fails(env)
19
+ def destroy_if_fails(env, task)
19
20
  yield
20
- rescue StandardError => e
21
+ rescue Exception => e
21
22
  puts "ERROR: #{e.message}"
22
23
  puts e.backtrace.join("\n")
23
- task('destroy').invoke(env.name) if env.local_backend?
24
+ invoke('destroy', task, env.name) if env.local_backend?
24
25
  raise
25
26
  end
26
27
 
27
- def invoke_if_defined(task_name, env)
28
- task(task_name).invoke(env) if Rake::Task.task_defined?(task_name)
28
+ def invoke(task_name, task_context, env, safe_invoke: false)
29
+ task_in_context = task_in_current_namespace(task_name, task_context)
30
+ should_invoke = !safe_invoke || Rake::Task.task_defined?(task_in_context)
31
+ Rake::Task[task_in_context].invoke(env) if should_invoke
32
+ end
33
+
34
+ def task_in_current_namespace(task_name, current_task)
35
+ namespace = current_task.scope.path.to_s
36
+ if namespace.empty?
37
+ return task_name
38
+ end
39
+
40
+ return "#{namespace}:#{task_name}"
29
41
  end
30
42
 
31
43
  def remote_state
@@ -46,7 +58,7 @@ task :prepare, [:env] do |_, args|
46
58
  puts "== Configuring environment #{args.env}"
47
59
  env = TDK::Environment.new(args.env)
48
60
 
49
- config_file = "config/config-#{env.config}.yml"
61
+ config_file = CONFIG_FILE % env.config
50
62
  puts "== Loading configuration from #{config_file}"
51
63
  TDK::Configuration.init(config_file)
52
64
 
@@ -65,7 +77,7 @@ task :prepare, [:env] do |_, args|
65
77
  remote_state.init(env, project_config)
66
78
  end
67
79
 
68
- invoke_if_defined('custom_prepare', args.env)
80
+ invoke('custom_prepare', task, args.env, safe_invoke: true)
69
81
 
70
82
  if File.exist?(File.join(env.working_dir, '.terraform'))
71
83
  get_cmd = 'terraform get'
@@ -88,12 +100,12 @@ task :plan, [:env] => :prepare do |_, args|
88
100
  end
89
101
 
90
102
  desc 'Creates the infrastructure'
91
- task :apply, [:env] => :prepare do |_, args|
92
- invoke_if_defined('pre_apply', args.env)
103
+ task :apply, [:env] => :prepare do |task, args|
104
+ invoke('pre_apply', task, args.env, safe_invoke: true)
93
105
 
94
106
  env = TDK::Environment.new(args.env)
95
107
 
96
- task('plan').invoke(env.name)
108
+ invoke('plan', task, env.name)
97
109
 
98
110
  unless env.local_backend?
99
111
  puts Rainbow("Are you sure you want to apply the above plan?\n" \
@@ -105,38 +117,40 @@ task :apply, [:env] => :prepare do |_, args|
105
117
  end
106
118
  end
107
119
 
108
- destroy_if_fails(env) do
120
+ destroy_if_fails(env, task) do
109
121
  Dir.chdir(env.working_dir) do
110
122
  system("terraform apply \"#{PLAN_FILE}\"")
111
123
  end
112
124
  end
113
125
 
114
- invoke_if_defined('post_apply', args.env)
126
+ invoke('post_apply', task, args.env, safe_invoke: true)
115
127
  end
116
128
 
117
129
  desc 'Tests a local environment'
118
- task :test, [:env] do |_, args|
130
+ task :test, [:env] do |task, args|
119
131
  env = TDK::Environment.new(args.env)
120
132
  env.local_backend? || (raise 'Testing is only allowed for local environments')
121
133
 
122
- task('apply').invoke(env.name, true)
134
+ invoke('apply', task, env.name)
123
135
 
124
- destroy_if_fails(env) do
125
- invoke_if_defined('custom_test', args.env)
136
+ destroy_if_fails(env, task) do
137
+ invoke('custom_test', task, args.env, safe_invoke: true)
126
138
  end
127
139
  end
128
140
 
129
- desc 'Creates the infrastructure and run the tests'
130
- task :preflight, [:teardown] do |_, args|
141
+ desc 'Creates the infrastructure and runs the tests'
142
+ task :preflight, [:prefix, :teardown] do |task, args|
131
143
  args.with_defaults(teardown: 'true')
132
- env = TDK::Environment.new(TDK::Environment.temp_name)
133
- task('test').invoke(env.name)
134
- task('clean').invoke(env.name) if args.teardown == 'true'
144
+ args.with_defaults(prefix: TDK::Environment.temp_name)
145
+ env = TDK::Environment.new(args.prefix)
146
+
147
+ invoke('test', task, env.name)
148
+ invoke('clean', task, env.name) if args.teardown == 'true'
135
149
  end
136
150
 
137
151
  desc 'Destroys the infrastructure'
138
- task :destroy, [:env] => :prepare do |_, args|
139
- invoke_if_defined('pre_destroy', args.env)
152
+ task :destroy, [:env] => :prepare do |task, args|
153
+ invoke('pre_destroy', task, args.env, safe_invoke: true)
140
154
 
141
155
  env = TDK::Environment.new(args.env)
142
156
  cmd = 'terraform destroy'
@@ -159,7 +173,7 @@ task :destroy, [:env] => :prepare do |_, args|
159
173
  Dir.chdir(env.working_dir) do
160
174
  system(cmd)
161
175
  end
162
- invoke_if_defined('pre_destroy', args.env)
176
+ invoke('pre_destroy', task, args.env, safe_invoke: true)
163
177
 
164
178
  unless env.local_backend?
165
179
  project_config = TDK::TerraformProjectConfig.new(
@@ -168,7 +182,7 @@ task :destroy, [:env] => :prepare do |_, args|
168
182
  remote_state.destroy(env, project_config)
169
183
  end
170
184
 
171
- invoke_if_defined('post_destroy', args.env)
185
+ invoke('post_destroy', task, args.env, safe_invoke: true)
172
186
  end
173
187
 
174
188
  desc 'Cleans an environment (infrastructure is destroyed too)'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: TerraformDevKit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Jimenez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-19 00:00:00.000000000 Z
11
+ date: 2017-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '2.9'
75
+ version: '3'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '2.9'
82
+ version: '3'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: mustache
85
85
  requirement: !ruby/object:Gem::Requirement