tflat 0.1.7 → 0.1.8

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
  SHA256:
3
- metadata.gz: 329791358e3d0b9aa452c4f36b81f1d885bff26a2da4a2e15c60df541bd36366
4
- data.tar.gz: dde5a28faa85dd402d23aceca6b04940b08ba22a2b8ee8678b09883f6401f50f
3
+ metadata.gz: 42600069942f24bb77fc24d41b850db4ccdbdeac276f4064ea49a4a8d322ff78
4
+ data.tar.gz: d40ec5e868f261109b9a8a260aa123732b04a233cbdda30e67c0dd8411d642b6
5
5
  SHA512:
6
- metadata.gz: 3bf5ac8ca188c97e235e5cdcb0edf48c5fae3562d307b154d1548012a932da1ee4e9072e4e5d222742952b94c617fc58577947cf3a0f2e4c13ba73e300643237
7
- data.tar.gz: 4584ab627c91b6c99748856d8c931655c8f1d8c15204ad61db333cf223312de9575b7daa2bfa14e5668b4f3ee4461b5443dd3169d96779cb70d3d6ea6891fc1c
6
+ metadata.gz: 0b9fca3508a34e031087b032112ae6c02d4cd8aaac2107fdf13a8f244b4c0e734008bfac16998ef3a3af7f87514adb26df069fd511b9db341c65dabebd5bd0fe
7
+ data.tar.gz: d891a1eb93b6cd99c8543a13aeade06b3bde60f9d3f0157efd9affe3023de4629c0584d2a37785bb7a8122ad0f947a7afed456ff432d4de4dbe77ab0dc11648d
@@ -1,6 +1,10 @@
1
1
  # Changelog
2
2
 
3
- ## 0.1.7 - Nov 17 2018
3
+ ## 0.1.8 - Dec 5 2018
4
+ - Set environment variable TFLAT_WORKSPACE to have the `.tflat` folder have a suffix. Useful for running same code on different states, for example.
5
+
6
+
7
+ ## 0.1.7 - Nov 18 2018
4
8
  - Environment variables `TF_VAR_*` are also loaded by Ruby and will overwrite a value set in the JSON file.
5
9
 
6
10
  ## 0.1.6 - Nov 17 2018
data/README.md CHANGED
@@ -56,6 +56,43 @@ Then it will `cd` into `.tflat` and run `terraform` with the arguments you passe
56
56
 
57
57
  **IMPORTANT:** The `.terraform` folder will live at `.tflat/.terraform`, so make sure you don't delete that folder if you are storing the terraform state locally!
58
58
 
59
+ ### TFLAT Workspaces
60
+ Say you want to run the same terraform code against multiple environments and cloud regions.
61
+
62
+ You can do that by setting the environment variable TFLAT_WORKSPACE in your shell, plus any other `TF_VAR_`'s that are required to achieve what you want.
63
+
64
+ For example, say I have some terraform code setup with remote state using S3 + DynamoDB as backend. I want to run it against the `staging` environment on `ca-central-1`.
65
+
66
+ Here is my `state.tf`:
67
+ ```
68
+ terraform {
69
+ backend "s3" {
70
+ encrypt = true
71
+ bucket = "my-s3-bucket"
72
+ region = "ca-central-1"
73
+ dynamodb_table = "my-dynamo-table"
74
+ key = "<%= ENV['TFLAT_WORKSPACE'] %>/terraform.tfstate"
75
+ }
76
+ }
77
+ ```
78
+
79
+ And here is `providers.tf`:
80
+ ```
81
+ variable "region" {}
82
+ provider "aws" {
83
+ version = "~> 1.50"
84
+ region = "${var.region}"
85
+ }
86
+ ```
87
+
88
+ And here is how I would set my shell environment:
89
+ ```
90
+ export TF_VAR_region=ca-central-1
91
+ export TFLAT_WORKSPACE=staging/ca-central-1
92
+ ```
93
+
94
+ When you run `tflat init`, you will notice that the `.tflat` folder is really `.tflat.staging/ca-central-1`, and your state is in the key `staging/ca-central-1/terraform.tfstate` inside of S3!
95
+
59
96
  There's only one more thing you have to pay attention to: handling file references.
60
97
 
61
98
  ### Handling file references
@@ -9,30 +9,40 @@ module Tflat
9
9
  class Terraform
10
10
  attr_accessor :destination, :all_files, :args, :directory
11
11
  def initialize(args:, directory: '.')
12
+ @workspace = ENV['TFLAT_WORKSPACE']
12
13
  @args = args.join(' ')
13
14
  @directory = directory
14
- @all_files = Dir.glob("#{directory}/**/*").select{|e| File.file?(e) && !e.match(/^\.tflat\/.+$/)}
15
- @destination = "#{directory}/.tflat"
15
+ @all_files = Dir.glob("#{directory}/**/*").select{|e| File.file?(e) && !e.match(/^\.tflat.*\/.+$/)}
16
+ if @workspace
17
+ @destination = "#{directory}/.tflat.#{@workspace}"
18
+ @tag = "[tflat | #{@workspace}]"
19
+ @tflat_folder = ".tflat.#{@workspace}"
20
+ else
21
+ @destination = "#{directory}/.tflat"
22
+ @tag = "[tflat]"
23
+ @tflat_folder = ".tflat"
24
+ end
25
+
16
26
  end
17
27
  def run!
18
28
  if args.empty?
19
29
  puts `terraform`
20
- puts "\n\n- [tflat] Notice: You must run tflat with terraform arguments.\n\n"
30
+ puts "\n\n- #{@tag} Notice: You must run tflat with terraform arguments.\n\n"
21
31
  return
22
32
  end
23
33
  setup
24
34
  read_variables
25
- puts "- [tflat] Generating files"
35
+ puts "- #{@tag} Generating files"
26
36
  flatten_directories
27
37
  parse_erb
28
38
  puts " done!"
29
- puts "- [tflat] Running: terraform #{args}"
39
+ puts "- #{@tag} Running: terraform #{args}"
30
40
  execute
31
41
  end
32
42
 
33
43
  def setup
34
44
  FileUtils.mkdir_p(destination)
35
- Dir.glob('.tflat/*').each do |entry|
45
+ Dir.glob("#{@tflat_folder}/*").each do |entry|
36
46
  next unless File.file?(entry)
37
47
  FileUtils.rm_f entry
38
48
  end
@@ -53,22 +63,22 @@ module Tflat
53
63
  next if entry =~ /#/
54
64
  new_name = entry.sub(/^#{directory}\//,'').gsub('/', '#')
55
65
  if new_name =~ /^#/ # Skip files/directories that start with a hash sign
56
- puts "- [tflat] Skipping: #{entry}"
66
+ puts "- #{@tag} Skipping: #{entry}"
57
67
  next
58
68
  end
59
- FileUtils.cp(entry, ".tflat/#{new_name}")
69
+ FileUtils.cp(entry, "#{@tflat_folder}/#{new_name}")
60
70
  end
61
71
  end
62
72
 
63
73
  def parse_erb
64
- Dir.glob(".tflat/*").each do |entry|
74
+ Dir.glob("#{@tflat_folder}/*").each do |entry|
65
75
  next unless File.file?(entry)
66
76
  next if File.binary?(entry)
67
- puts "- [tflat] -> #{entry}"
77
+ puts "- #{@tag} -> #{entry}"
68
78
  begin
69
79
  rendered = render(entry)
70
80
  rescue Exception => e
71
- puts "- [tflat] ERROR: Could not parse ERB on file #{entry}"
81
+ puts "- #{@tag} ERROR: Could not parse ERB on file #{entry}"
72
82
  puts e.full_message
73
83
  exit 1
74
84
  end
@@ -95,7 +105,7 @@ module Tflat
95
105
  end
96
106
 
97
107
  def execute
98
- exec "cd .tflat && terraform #{args}"
108
+ exec "cd #{@tflat_folder} && terraform #{args}"
99
109
  end
100
110
  end
101
111
  end
@@ -1,3 +1,3 @@
1
1
  module Tflat
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tflat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Arruda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-18 00:00:00.000000000 Z
11
+ date: 2018-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ptools