rake-terraform 0.2.2 → 0.3.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.
- checksums.yaml +5 -5
- data/README.md +12 -9
- data/lib/rake-terraform/basetask.rb +0 -9
- data/lib/rake-terraform/default_tasks.rb +0 -3
- data/lib/rake-terraform/plan_task/config.rb +1 -10
- data/lib/rake-terraform/plan_task/task.rb +2 -10
- data/lib/rake-terraform/terraformcmd.rb +1 -12
- data/lib/rake-terraform/version.rb +1 -1
- data/spec/fixtures/set_all_variables_nil.env +0 -1
- data/spec/unit/plantask_spec.rb +0 -2
- data/spec/unit/terraformcmd_spec.rb +3 -24
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9b77ebb2dfff7f44ddd481c47e1af8df9c2bd36923ed6381e92f4d4e323924e2
|
4
|
+
data.tar.gz: b921ee652eda8d825a7ceaf2713e26420b313c77301a11067cccd3b591f9c942
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19e2250063b7a03bdf09171515a3ea32f631870488fee8ac5c1f5cc1ecd0d14edc7e526fc740ffff4940d43c0e6015ea7b30ef48ae4508f42a22f085d35a9811
|
7
|
+
data.tar.gz: 33e9185deb94ab07efec9eaa738c01c4389ebf82616eb0264b03c999786366bd2b7155fc963b59911828d6fae8002d3971cb7af27740f16e6bfe7fbd0a236942
|
data/README.md
CHANGED
@@ -31,6 +31,8 @@ Or install it yourself as:
|
|
31
31
|
|
32
32
|
## Usage
|
33
33
|
|
34
|
+
**Note**: Versions < 0.3.0 handled credentials as part of this gem/default rake tasks. This is no longer the case with 0.3.0 which defaults to Terraform to handle provider credentials.
|
35
|
+
|
34
36
|
This gem currently provides two different types of tasks:
|
35
37
|
|
36
38
|
### `terraform_plan`
|
@@ -40,7 +42,6 @@ You can set the following configuration for the task:
|
|
40
42
|
|
41
43
|
* `t.input_dir` - the directory from which to read terraform config (default: `./terraform`)
|
42
44
|
* `t.output_file` - the path and name of the plan file to generate (default: `./output/terraform/plan.tf`)
|
43
|
-
* `t.credentials` - the path of the [AWS credentials file](http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-config-files) to read from (default: `~/.aws/credentials`)
|
44
45
|
* `t.aws_project` - the name of the project to use from your credentials file (default: `default`)
|
45
46
|
|
46
47
|
### `terraform_apply`
|
@@ -76,7 +77,7 @@ Given, the following terraform hierarchy:
|
|
76
77
|
output.tf
|
77
78
|
```
|
78
79
|
|
79
|
-
It
|
80
|
+
It will automatically generate the following rake tasks:
|
80
81
|
|
81
82
|
```bash
|
82
83
|
rake terraform:all # Plan and migrate all environments
|
@@ -84,6 +85,9 @@ rake terraform:all # Plan and migrate all environments
|
|
84
85
|
rake terraform:apply_us-east-1 # Execute plan for us-east-1
|
85
86
|
rake terraform:apply_us-west-1 # Execute plan for us-west-1
|
86
87
|
|
88
|
+
rake terraform:init_us-east-1 # Execute init for us-east-1
|
89
|
+
rake terraform:init_us-west-1 # Execute init for us-west-1
|
90
|
+
|
87
91
|
rake terraform:plan_us-east-1 # Plan migration of us-east-1
|
88
92
|
rake terraform:plan_us-west-1 # Plan migration of us-west-1
|
89
93
|
|
@@ -95,16 +99,15 @@ The following environment variables can be set to tweak `default_task`'s behavio
|
|
95
99
|
* `ENV['TERRAFORM_AWS_PROJECT']` - Sets `t.aws_project` on the `terraform_plan` tasks (default: `default`)
|
96
100
|
* `ENV['TERRAFORM_ENVIRONMENT_GLOB']` - Dir glob used to discover terraform environments (default: `terraform/**/*.tf`)
|
97
101
|
* `ENV['TERRAFORM_OUTPUT_BASE']` - Directory to which plan files are saved/read. The environment name is appended to this automatically (default: `output/terraform`)
|
98
|
-
* `ENV['
|
99
|
-
* `ENV['
|
100
|
-
* `ENV['TERRAFORM_STATE_FILE']` - The full path to a state file to use for this run. Only used when `TERRAFORM_UNIQUE_STATE` is true, and cannot be used in conjunction with `TERRAFORM_STATE_DIR_VAR`.
|
102
|
+
* `ENV['TERRAFORM_UNIQUE_STATE']` - Whether to use a unique state for this run. Requires `TERRAFORM_STATE_FILE` OR `TERRAFORM_STATE_DIR_VAR`. Can be any truthy or falsey looking string from [this list][wannabe_bool_string] (e.g `TRUE` or `FALSE`)
|
103
|
+
* `ENV['TERRAFORM_STATE_FILE']` - The full path to a state file to use for this run. Only used when `TERRAFORM_UNIQUE_STATE` is true, and cannot be used in conjunction with `TERRAFORM_STATE_DIR_VAR`.
|
101
104
|
* `ENV['TERRAFORM_STATE_DIR_VAR']` - The name of an environment variable that holds a variable that will be used to reference a directory in which to store state files in for this run. This directory will be a subdirectory within the terraform environment. Only used when `TERRAFORM_STATE_DIR` is true, and cannot be used in conjunction with `TERRAFORM_STATE_FILE`
|
102
105
|
|
103
106
|
[wannabe_bool_string]: https://github.com/prodis/wannabe_bool#string
|
104
107
|
|
105
108
|
#### Unique States
|
106
109
|
|
107
|
-
|
110
|
+
By default, `rake-terraform` stores state within a given environment directory.
|
108
111
|
|
109
112
|
Sometimes, you will have several infrastructure environments ("infrastructure
|
110
113
|
environment" in this block here taken to mean e.g "staging" or "production"
|
@@ -129,7 +132,7 @@ commits between them. Again, depends on clean commit hygiene and easy to mess
|
|
129
132
|
up manual steps.
|
130
133
|
|
131
134
|
By using a unique state file for each of your infrastructure environments,
|
132
|
-
whilst
|
135
|
+
whilst utilizing a single terraform environment, you can avoid repeating
|
133
136
|
yourself and manage roll out changes to each of your infrastructure
|
134
137
|
environments better.
|
135
138
|
|
@@ -169,7 +172,7 @@ This would result in a directory layout resembling the following:
|
|
169
172
|
staging
|
170
173
|
terraform.tfstate
|
171
174
|
terraform.tfstate.backup
|
172
|
-
production
|
175
|
+
production
|
173
176
|
terraform.tfstate
|
174
177
|
terraform.tfstate.backup
|
175
178
|
app_tier
|
@@ -180,7 +183,7 @@ This would result in a directory layout resembling the following:
|
|
180
183
|
staging
|
181
184
|
terraform.tfstate
|
182
185
|
terraform.tfstate.backup
|
183
|
-
production
|
186
|
+
production
|
184
187
|
terraform.tfstate
|
185
188
|
terraform.tfstate.backup
|
186
189
|
|
@@ -17,14 +17,5 @@ module RakeTerraform
|
|
17
17
|
rescue => _
|
18
18
|
false
|
19
19
|
end
|
20
|
-
|
21
|
-
def get_aws_credentials(creds_file, project = 'default')
|
22
|
-
error = "Could not locate AWS credentials in #{creds_file}!"
|
23
|
-
raise error unless File.exist? File.expand_path(creds_file)
|
24
|
-
|
25
|
-
credentials = IniParse.parse(File.read(File.expand_path(creds_file)))
|
26
|
-
{ accesskey: credentials[project]['aws_access_key_id'],
|
27
|
-
secretkey: credentials[project]['aws_secret_access_key'] }
|
28
|
-
end
|
29
20
|
end
|
30
21
|
end
|
@@ -8,8 +8,6 @@ namespace :terraform do
|
|
8
8
|
# classes
|
9
9
|
env_glob = ENV['TERRAFORM_ENVIRONMENT_GLOB'] || 'terraform/**/*.tf'
|
10
10
|
output_base = ENV['TERRAFORM_OUTPUT_BASE'] || 'output/terraform'
|
11
|
-
credential_file = ENV['TERRAFORM_CREDENTIAL_FILE']
|
12
|
-
credential_file ||= "#{Dir.home}/.aws/credentials"
|
13
11
|
aws_project = ENV['TERRAFORM_AWS_PROJECT'] || 'default'
|
14
12
|
|
15
13
|
# Set to string 'false' instead of bool so users can more-easily override
|
@@ -40,7 +38,6 @@ namespace :terraform do
|
|
40
38
|
t.input_dir = env
|
41
39
|
t.aws_project = aws_project
|
42
40
|
t.output_file = plan_path
|
43
|
-
t.credentials = credential_file
|
44
41
|
end
|
45
42
|
|
46
43
|
desc "Execute plan for #{short_name}" if hide_tasks == 'false'
|
@@ -6,7 +6,7 @@ module RakeTerraform
|
|
6
6
|
class Config
|
7
7
|
prepend RakeTerraform::EnvProcess
|
8
8
|
|
9
|
-
attr_writer :aws_project, :
|
9
|
+
attr_writer :aws_project, :output_file
|
10
10
|
|
11
11
|
def initialize
|
12
12
|
# initialize RakeTerraform::EnvProcess
|
@@ -17,10 +17,6 @@ module RakeTerraform
|
|
17
17
|
@aws_project ||= 'default'
|
18
18
|
end
|
19
19
|
|
20
|
-
def credentials
|
21
|
-
@credentials ||= File.expand_path(default_credentials)
|
22
|
-
end
|
23
|
-
|
24
20
|
def output_file
|
25
21
|
@output_file ||= File.expand_path(default_output)
|
26
22
|
end
|
@@ -41,7 +37,6 @@ module RakeTerraform
|
|
41
37
|
def opts
|
42
38
|
Map.new(input_dir: input_dir,
|
43
39
|
output_file: output_file,
|
44
|
-
credentials: credentials,
|
45
40
|
aws_project: aws_project,
|
46
41
|
unique_state: unique_state,
|
47
42
|
state_file: state_file)
|
@@ -52,10 +47,6 @@ module RakeTerraform
|
|
52
47
|
def default_output
|
53
48
|
File.join('output', 'terraform', 'plan.tf')
|
54
49
|
end
|
55
|
-
|
56
|
-
def default_credentials
|
57
|
-
File.join('~', '.aws', 'credentials')
|
58
|
-
end
|
59
50
|
end
|
60
51
|
end
|
61
52
|
end
|
@@ -5,12 +5,9 @@ module RakeTerraform
|
|
5
5
|
# Custom rake task to run `terraform plan`
|
6
6
|
class Task < BaseTask
|
7
7
|
include RakeTerraform::TerraformCmd
|
8
|
-
attr_accessor :creds_file
|
9
8
|
|
10
9
|
def initialize(opts)
|
11
|
-
@creds_file = opts.get(:credentials)
|
12
10
|
@opts = opts
|
13
|
-
creds
|
14
11
|
end
|
15
12
|
|
16
13
|
def execute
|
@@ -18,18 +15,13 @@ module RakeTerraform
|
|
18
15
|
Dir.chdir(@opts.get(:input_dir)) do
|
19
16
|
puts "=> Generating plan for #{@opts.get(:input_dir)}..."
|
20
17
|
if @opts[:unique_state]
|
21
|
-
tf_plan(@
|
22
|
-
@opts[:output_file], @opts[:state_file])
|
18
|
+
tf_plan(@opts[:output_file], @opts[:state_file])
|
23
19
|
else
|
24
|
-
tf_plan(@
|
20
|
+
tf_plan(@opts[:output_file])
|
25
21
|
end
|
26
22
|
end
|
27
23
|
end
|
28
24
|
|
29
|
-
def creds
|
30
|
-
@creds ||= get_aws_credentials(@creds_file, @opts.get(:aws_project))
|
31
|
-
end
|
32
|
-
|
33
25
|
private
|
34
26
|
|
35
27
|
# run pre execution checks
|
@@ -23,21 +23,10 @@ module RakeTerraform
|
|
23
23
|
|
24
24
|
# perform a 'terraform plan'
|
25
25
|
#
|
26
|
-
|
27
|
-
# library have supported the standard AWS_PROFILE env var for a while
|
28
|
-
#
|
29
|
-
def tf_plan(access_key = nil, secret_key = nil,
|
30
|
-
output_file = nil, state_file = nil, module_depth = 2)
|
26
|
+
def tf_plan(output_file = nil, state_file = nil, module_depth = 2)
|
31
27
|
cmd = 'terraform plan'
|
32
28
|
cmd << " -module-depth #{module_depth}"
|
33
29
|
state_file && cmd << " -state #{state_file}"
|
34
|
-
if access_key && secret_key
|
35
|
-
# TODO: additional escaped quotes required?
|
36
|
-
cmd << " -var access_key=\"#{access_key}\""
|
37
|
-
cmd << " -var secret_key=\"#{secret_key}\""
|
38
|
-
elsif access_key || secret_key
|
39
|
-
raise ArgumentError, 'Only one of access_key or secret_key given'
|
40
|
-
end
|
41
30
|
output_file && cmd << " -out #{output_file}"
|
42
31
|
system(cmd)
|
43
32
|
end
|
data/spec/unit/plantask_spec.rb
CHANGED
@@ -18,12 +18,10 @@ module RakeTerraform
|
|
18
18
|
let(:default_output_file_str) do
|
19
19
|
"#{PROJECT_ROOT}/output/terraform/plan.tf"
|
20
20
|
end
|
21
|
-
let(:default_credentials_str) { File.expand_path('~/.aws/credentials') }
|
22
21
|
let(:default_opts_hash) do
|
23
22
|
{
|
24
23
|
input_dir: default_input_dir_str,
|
25
24
|
output_file: default_output_file_str,
|
26
|
-
credentials: default_credentials_str,
|
27
25
|
aws_project: default_aws_project_str,
|
28
26
|
unique_state: false,
|
29
27
|
state_file: nil
|
@@ -61,46 +61,25 @@ module RakeTerraform
|
|
61
61
|
test_class_inst.tf_plan
|
62
62
|
end
|
63
63
|
end
|
64
|
-
context 'with just an access_key' do
|
65
|
-
it 'should raise an ArgumentError' do
|
66
|
-
expect { test_class_inst.tf_plan(access_key) }
|
67
|
-
.to raise_error(ArgumentError,
|
68
|
-
'Only one of access_key or secret_key given')
|
69
|
-
end
|
70
|
-
end
|
71
|
-
context 'with just a secret_key' do
|
72
|
-
it 'should raise an ArgumentError' do
|
73
|
-
expect { test_class_inst.tf_plan(nil, secret_key) }
|
74
|
-
.to raise_error(ArgumentError,
|
75
|
-
'Only one of access_key or secret_key given')
|
76
|
-
end
|
77
|
-
end
|
78
|
-
context 'with both an access_key and a secret_key' do
|
79
|
-
it 'should call terraform plan with those vars configured' do
|
80
|
-
expect(test_class_inst).to receive(:system)
|
81
|
-
.with(cred_plan_cmd)
|
82
|
-
test_class_inst.tf_plan(access_key, secret_key)
|
83
|
-
end
|
84
|
-
end
|
85
64
|
context 'with an output file' do
|
86
65
|
it 'should call terraform plan with an output file' do
|
87
66
|
expect(test_class_inst).to receive(:system)
|
88
67
|
.with(output_plan_cmd)
|
89
|
-
test_class_inst.tf_plan(
|
68
|
+
test_class_inst.tf_plan(default_output_file)
|
90
69
|
end
|
91
70
|
end
|
92
71
|
context 'with an state file' do
|
93
72
|
it 'should call terraform plan with a state file' do
|
94
73
|
expect(test_class_inst).to receive(:system)
|
95
74
|
.with(state_file_cmd)
|
96
|
-
test_class_inst.tf_plan(nil,
|
75
|
+
test_class_inst.tf_plan(nil, state_file)
|
97
76
|
end
|
98
77
|
end
|
99
78
|
context 'where module_depth is given as an argument' do
|
100
79
|
it 'should call terraform plan with updated module-depth argument' do
|
101
80
|
expect(test_class_inst).to receive(:system)
|
102
81
|
.with(module_arg_cmd)
|
103
|
-
test_class_inst.tf_plan(nil, nil,
|
82
|
+
test_class_inst.tf_plan(nil, nil, module_arg)
|
104
83
|
end
|
105
84
|
end
|
106
85
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rake-terraform
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Norm MacLennan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -208,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
208
208
|
version: '0'
|
209
209
|
requirements: []
|
210
210
|
rubyforge_project:
|
211
|
-
rubygems_version: 2.
|
211
|
+
rubygems_version: 2.7.5
|
212
212
|
signing_key:
|
213
213
|
specification_version: 4
|
214
214
|
summary: Rake tasks for use with Terraform.
|