TerraformDevKit 0.1.13 → 0.1.14

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: 43d9a31480f56ff1f3f54e95c92b6938cf206c53
4
- data.tar.gz: f795680f411b57a400ae4de2748d7343667ec0ee
3
+ metadata.gz: a94f0642cf970c5994db6fc663a2f2add47810e4
4
+ data.tar.gz: 8fa17cf1bf78f83abe26094616904692db12fdf2
5
5
  SHA512:
6
- metadata.gz: 1d61c3537b6b0892e6ee0fe2ebfa3c5ae9f995a9062a227da300087e1312656a2df13172732424436adfba57bbc44c7693d6cfc56e958aea7ad84275e74f8123
7
- data.tar.gz: e5cb3bcb16a29cfb10dcd390dbf9c6bc6e2f8fccc6a35ca3a1851b6d6be52ef46b08c52ca4f1104cd02319c7a0576038446486cd7f5e622673101b462e4ccf4b
6
+ metadata.gz: daf674ba9d45403f922f157eccb704ac0581470c801c9d8c86d90b1c3dab5a3899993706931b42712b9994fe3b104d232b8ddb17d816184a887ff5f844ff3285
7
+ data.tar.gz: 9171fd82c7de355a543053b1fd95bbd7f868ec2cb39547b4dec5faeb6510a820ec929e3931fec624683de7f8f924b694e767e44f20bb8c837708b0708bdbf64a
data/README.md CHANGED
@@ -89,6 +89,27 @@ task :custom_test, [:env] do |_, args|
89
89
  end
90
90
  ```
91
91
 
92
+ ### Tasks and Hooks
93
+
94
+ TerraformDevKit provides a set of generic tasks to perform:
95
+
96
+ * `prepare`: prepares the environment
97
+ * `plan`: shows the plan to create the infrastructure
98
+ * `apply`: creates the infrastructure
99
+ * `destroy`: destroys the infrastructure
100
+ * `clean`: cleans the environment (after destroying the infrastructure)
101
+ * `test`: tests a local environment
102
+ * `preflight`: creates a temporary infrastructure and runs the test task
103
+
104
+ Additionally, TerraformDevKit allows users to define a set of hooks that will be called during the different steps required to complete the previous list of tasks. The following hooks are available:
105
+
106
+ * `pre_apply`: invoked before `apply` task runs
107
+ * `post_apply`: invoked after `apply` task runs
108
+ * `pre_destroy`: invoked before `destroy` task runs
109
+ * `post_destroy`: invoked after `destroy` task runs
110
+ * `custom_prepare`: invoked during the preparation process, before terraform is initialized
111
+ * `custom_test`: invoked during as part of the `test` task, right after `apply` completes.
112
+
92
113
  ### Sample Terraform/Terragrunt Templates
93
114
 
94
115
  The following file (`main.tf.mustache`) contains the infrastructure configuration (a single S3 bucket) as well as information related to the AWS provider.
@@ -148,9 +169,7 @@ terragrunt = {
148
169
 
149
170
  ### Injecting Additional Variables into Template Files
150
171
 
151
- In addition to the default variables that are passed to Mustache when rendering
152
- a template file, users can provide additional variables. To do so, users must register a procedure that receives the environment as a parameter and returns
153
- a map with the extra variables and their values. An example is shown next:
172
+ In addition to the default variables that are passed to Mustache when rendering a template file, users can provide additional variables. To do so, users must register a procedure that receives the environment as a parameter and returns a map with the extra variables and their values. An example is shown next:
154
173
 
155
174
  ```ruby
156
175
  TDK::TerraformConfigManager.register_extra_vars_proc(
@@ -160,14 +179,11 @@ TDK::TerraformConfigManager.register_extra_vars_proc(
160
179
  )
161
180
  ```
162
181
 
163
- ### Skipping Module Updates
182
+ ### Updating Modules
164
183
 
165
- For safety and correctness reasons, TerraformDevKit requests to update every
166
- module when it executes Terraform. This may take a while, depending on the
167
- number of modules used in the infrastructure.
184
+ Terraform will get the necessary modules every time a new environment is created. Once the modules are cached, there is generally no need to keep updating the modules each time Terraform is executed. When using a module repository it is possible to select a specific version to use (as shown [here](https://www.terraform.io/docs/modules/sources.html#ref)). In such a case, Terraform will automatically update the modules whenever the version number is changed.
168
185
 
169
- When users are sure that an update is not necessary, this step can be skipped
170
- by setting the environment variable `TF_DEVKIT_SKIP_MODULE_UPDATE` to `true`.
186
+ When using local modules (e.g., during development process) it might be desirable to update the modules every time Terraform runs. This can be achieved by setting the environment variable `TF_DEVKIT_UPDATE_MODULES` to `true`.
171
187
 
172
188
  ## Development
173
189
 
@@ -17,10 +17,8 @@ module TerraformDevKit
17
17
  end
18
18
 
19
19
  def self.update_modules?
20
- skip_update = ENV.fetch('TF_DEVKIT_SKIP_MODULE_UPDATE', 'false')
21
- .strip
22
- .downcase
23
- skip_update != 'true'
20
+ var = ENV.fetch('TF_DEVKIT_UPDATE_MODULES', 'false')
21
+ var.strip.casecmp('true').zero?
24
22
  end
25
23
 
26
24
  private_class_method
@@ -10,13 +10,21 @@ module TerraformDevKit
10
10
  LOCAL_FILE_NAME = 'terraform.zip'.freeze
11
11
 
12
12
  def self.installed_terraform_version
13
- version = Command.run('terraform --version')[0]
14
- match = /Terraform v(\d+\.\d+\.\d+)/.match(version)
15
- match[1] unless match.nil?
13
+ extract_version(Command.run('terraform --version'))
16
14
  rescue
17
15
  nil
18
16
  end
19
17
 
18
+ def self.extract_version(output)
19
+ # Terraform vx.y.z might be anywhere in the output (warnings may appear
20
+ # before the version does). Therefore we scan all the lines.
21
+
22
+ matches = output.map { |line| /Terraform v(\d+\.\d+\.\d+)/.match(line) }
23
+ .reject(&:nil?)
24
+
25
+ matches.count == 1 ? matches[0][1] : nil
26
+ end
27
+
20
28
  def self.install_local(version, directory: Dir.pwd)
21
29
  if installed_terraform_version == version
22
30
  puts 'Terraform already installed'
@@ -1,3 +1,3 @@
1
1
  module TerraformDevKit
2
- VERSION = '0.1.13'.freeze
2
+ VERSION = '0.1.14'.freeze
3
3
  end
data/tasks/devkit.rake CHANGED
@@ -21,6 +21,12 @@ rescue StandardError => e
21
21
  raise
22
22
  end
23
23
 
24
+ def invoke_if_defined(task_name, env)
25
+ if Rake::Task.task_defined?(task_name)
26
+ task(task_name).invoke(env)
27
+ end
28
+ end
29
+
24
30
  desc 'Prepares the environment to create the infrastructure'
25
31
  task :prepare, [:env] do |_, args|
26
32
  puts "== Configuring environment #{args.env}"
@@ -41,9 +47,7 @@ task :prepare, [:env] do |_, args|
41
47
 
42
48
  TDK::TerraformConfigManager.setup(env)
43
49
 
44
- if Rake::Task.task_defined?('custom_prepare')
45
- task('custom_prepare').invoke(args.env)
46
- end
50
+ invoke_if_defined('custom_prepare', args.env)
47
51
 
48
52
  TDK::Command.run(
49
53
  'terragrunt init -upgrade=false',
@@ -64,13 +68,14 @@ end
64
68
 
65
69
  desc 'Creates the infrastructure'
66
70
  task :apply, [:env] => :prepare do |_, args|
71
+ invoke_if_defined('pre_apply', args.env)
72
+
67
73
  env = TDK::Environment.new(args.env)
68
74
  destroy_if_fails(env) do
69
75
  TDK::Command.run('terragrunt apply', directory: env.working_dir)
70
76
  end
71
- if Rake::Task.task_defined?('post_apply')
72
- task('post_apply').invoke(args.env)
73
- end
77
+
78
+ invoke_if_defined('post_apply', args.env)
74
79
  end
75
80
 
76
81
  desc 'Tests a local environment'
@@ -81,9 +86,7 @@ task :test, [:env] do |_, args|
81
86
  task('apply').invoke(env.name)
82
87
 
83
88
  destroy_if_fails(env) do
84
- if Rake::Task.task_defined?('custom_test')
85
- task('custom_test').invoke(args.env)
86
- end
89
+ invoke_if_defined('custom_test', args.env)
87
90
  end
88
91
  end
89
92
 
@@ -97,13 +100,14 @@ end
97
100
 
98
101
  desc 'Destroys the infrastructure'
99
102
  task :destroy, [:env] => :prepare do |_, args|
103
+ invoke_if_defined('pre_destroy', args.env)
104
+
100
105
  env = TDK::Environment.new(args.env)
101
106
  cmd = 'terragrunt destroy'
102
107
  cmd += ' -force' if env.local_backend?
103
108
  TDK::Command.run(cmd, directory: env.working_dir, close_stdin: false)
104
- if Rake::Task.task_defined?('custom_destroy')
105
- task('custom_destroy').invoke(args.env)
106
- end
109
+
110
+ invoke_if_defined('post_destroy', args.env)
107
111
  end
108
112
 
109
113
  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.1.13
4
+ version: 0.1.14
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-11-21 00:00:00.000000000 Z
11
+ date: 2017-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler