tfrb 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tfrb/base.rb +26 -5
- data/lib/tfrb/cli.rb +22 -10
- data/lib/tfrb/resource.rb +3 -1
- data/lib/tfrb/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcf070c3b3ae2566c53df75d4a38a6fdcae411cd0eadaa620062ea0d8c690a91
|
4
|
+
data.tar.gz: 468e9dbf7423807d1fbe9ccca5ea835394013176848f671451eec28a5d439548
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f93320b30f22b5b6101083e3e4d544de336f56abb85d74dbfe1db12e1340b2eb3b48f947b4130fb2156038d57655399895afb73d8dd98c71616b1ef1d7ec85a8
|
7
|
+
data.tar.gz: 30e8349752c48e7a16cdc11070a752f0f79560aa1d3a8417f5946b2335d4a42db1968f44c49f212ab12cd25202129c99032e6d553a3d54cfbf17d48f68aaeae8
|
data/lib/tfrb/base.rb
CHANGED
@@ -8,16 +8,20 @@ class Tfrb::Base
|
|
8
8
|
attr_accessor :block
|
9
9
|
attr_accessor :path
|
10
10
|
attr_accessor :temp_path
|
11
|
+
attr_accessor :local_state_path
|
11
12
|
attr_accessor :environments
|
12
13
|
attr_accessor :state
|
13
14
|
attr_accessor :skip_import
|
15
|
+
attr_accessor :dry_run
|
14
16
|
|
15
17
|
def initialize(block, environments)
|
16
18
|
@skip_import = false
|
19
|
+
@dry_run = false
|
17
20
|
@block = block
|
18
21
|
@path = Tfrb::Config[:path]
|
19
22
|
@temp_path = File.join(Tfrb::Config[:temp_path], block)
|
20
23
|
Dir.mkdir(@temp_path) unless Dir.exist?(@temp_path)
|
24
|
+
@local_state_path = File.join(@temp_path, '.terraform', 'local.tfstate')
|
21
25
|
@environments = environments.each_with_object({}) do |environment, hash|
|
22
26
|
hash[environment] = Tfrb::Block.load(environment)
|
23
27
|
end
|
@@ -44,6 +48,7 @@ class Tfrb::Base
|
|
44
48
|
tf_pullstate = Mixlib::ShellOut.new('terraform', 'state', 'pull', { cwd: temp_path })
|
45
49
|
tf_pullstate.run_command
|
46
50
|
tf_pullstate.error!
|
51
|
+
File.write(local_state_path, tf_pullstate.stdout) if dry_run
|
47
52
|
pulled_state = JSON.parse(tf_pullstate.stdout)
|
48
53
|
if pulled_state['modules']
|
49
54
|
pulled_state['modules'].each do |state_module|
|
@@ -76,12 +81,18 @@ class Tfrb::Base
|
|
76
81
|
end
|
77
82
|
write!
|
78
83
|
end
|
84
|
+
tf_import_args = ['terraform', 'import']
|
85
|
+
if dry_run
|
86
|
+
tf_import_args << "-lock=false"
|
87
|
+
tf_import_args << "-state=#{local_state_path}"
|
88
|
+
end
|
79
89
|
providers = @environments.find { |_, e| e['resource'] && e['resource'][resource] && e['resource'][resource][name] && e['resource'][resource][name]['provider'] }
|
80
90
|
if providers && provider = providers[1]['resource'][resource][name]['provider']
|
81
|
-
|
82
|
-
else
|
83
|
-
tf_import = Mixlib::ShellOut.new('terraform', 'import', "#{resource}.#{name}", id, cwd: temp_path)
|
91
|
+
tf_import_args << "-provider=#{provider}"
|
84
92
|
end
|
93
|
+
tf_import_args << "#{resource}.#{name}"
|
94
|
+
tf_import_args << id
|
95
|
+
tf_import = Mixlib::ShellOut.new(*tf_import_args, cwd: temp_path)
|
85
96
|
tf_import.run_command
|
86
97
|
tf_import.error!
|
87
98
|
state[resource] ||= {}
|
@@ -116,7 +127,14 @@ class Tfrb::Base
|
|
116
127
|
|
117
128
|
def plan!
|
118
129
|
printf "\033[1;32mCalculating plan...\033[0m\n"
|
119
|
-
|
130
|
+
tf_plan_args = ['terraform', 'plan']
|
131
|
+
if dry_run
|
132
|
+
tf_plan_args << "-lock=false"
|
133
|
+
tf_plan_args << "-state=#{local_state_path}"
|
134
|
+
else
|
135
|
+
tf_plan_args << '-out=plan.cache'
|
136
|
+
end
|
137
|
+
tf_plan = Mixlib::ShellOut.new(*tf_plan_args, shell_opts)
|
120
138
|
tf_plan.run_command
|
121
139
|
tf_plan.error!
|
122
140
|
end
|
@@ -152,7 +170,7 @@ class Tfrb::Base
|
|
152
170
|
end
|
153
171
|
|
154
172
|
class << self
|
155
|
-
def load(block, environments, skip_import = false)
|
173
|
+
def load(block, environments, skip_import = false, dry_run = false)
|
156
174
|
Tfrb::Resource.load_helpers!
|
157
175
|
|
158
176
|
printf "\033[1;32mLoading %s...\033[0m\n", block
|
@@ -161,6 +179,9 @@ class Tfrb::Base
|
|
161
179
|
# Set skip_import
|
162
180
|
tfrb.skip_import = skip_import
|
163
181
|
|
182
|
+
# Set dry_run
|
183
|
+
tfrb.dry_run = dry_run
|
184
|
+
|
164
185
|
# Clean temporary files before starting
|
165
186
|
tfrb.clean!
|
166
187
|
|
data/lib/tfrb/cli.rb
CHANGED
@@ -12,14 +12,26 @@ class Tfrb::CLI < Thor
|
|
12
12
|
puts Tfrb::VERSION
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
desc 'init', 'Runs a terraform init'
|
16
|
+
def init
|
17
|
+
load_tfrb.init!
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'plan', 'Runs a terraform plan'
|
21
|
+
method_option :dry_run, aliases: '-n', type: :boolean, desc: 'Dry run using local copy of statefile', default: false
|
22
|
+
method_option :skip_import, aliases: '-s', type: :boolean, desc: 'Skip automatic terraform import', default: false
|
23
|
+
def plan
|
24
|
+
tfrb = load_tfrb(options[:skip_import], options[:dry_run])
|
25
|
+
tfrb.plan!
|
26
|
+
tfrb.clean!
|
27
|
+
end
|
28
|
+
|
29
|
+
desc 'apply', 'Runs a terraform apply'
|
30
|
+
method_option :skip_import, aliases: '-s', type: :boolean, desc: 'Skip automatic terraform import', default: false
|
31
|
+
def apply
|
32
|
+
tfrb = load_tfrb(options[:skip_import])
|
33
|
+
tfrb.apply!
|
34
|
+
tfrb.clean!
|
23
35
|
end
|
24
36
|
|
25
37
|
desc 'import TYPE NAME ID', 'Runs a terraform import'
|
@@ -48,7 +60,7 @@ class Tfrb::CLI < Thor
|
|
48
60
|
|
49
61
|
private
|
50
62
|
|
51
|
-
def load_tfrb(skip_import = true)
|
63
|
+
def load_tfrb(skip_import = true, dry_run = false)
|
52
64
|
unless File.exist?('tfrb.rb')
|
53
65
|
puts 'Missing tfrb.rb file'
|
54
66
|
exit(false)
|
@@ -63,6 +75,6 @@ class Tfrb::CLI < Thor
|
|
63
75
|
|
64
76
|
require 'tfrb/base'
|
65
77
|
|
66
|
-
Tfrb::Base.load(Tfrb::Config[:environment_name], Tfrb::Config[:files], skip_import)
|
78
|
+
Tfrb::Base.load(Tfrb::Config[:environment_name], Tfrb::Config[:files], skip_import, dry_run)
|
67
79
|
end
|
68
80
|
end
|
data/lib/tfrb/resource.rb
CHANGED
@@ -67,7 +67,9 @@ module Tfrb::Resource
|
|
67
67
|
end
|
68
68
|
|
69
69
|
def get_state(base, resource, name)
|
70
|
-
|
70
|
+
tf_state_args = ['terraform', 'state', 'show', "#{resource}.#{name}"]
|
71
|
+
tf_state_args << "-state=#{base.local_state_path}" if base.dry_run
|
72
|
+
tf_state = Mixlib::ShellOut.new(*tf_state_args, cwd: base.temp_path)
|
71
73
|
tf_state.run_command
|
72
74
|
tf_state.error!
|
73
75
|
tf_state.stdout.split("\n").each_with_object({}) { |line, hash| key, value = line.split(' = '); hash[key.strip] = value }
|
data/lib/tfrb/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tfrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 'Matt Kasa
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2019-05-
|
13
|
+
date: 2019-05-02 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|