mux_tf 0.2.4 → 0.4.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 +4 -4
- data/exe/tf_current +10 -3
- data/exe/tf_mux +10 -3
- data/exe/tf_plan_summary +10 -3
- data/lib/deps.rb +5 -0
- data/lib/mux_tf.rb +28 -25
- data/lib/mux_tf/cli/current.rb +92 -65
- data/lib/mux_tf/cli/mux.rb +47 -24
- data/lib/mux_tf/cli/plan_summary.rb +20 -246
- data/lib/mux_tf/plan_formatter.rb +100 -52
- data/lib/mux_tf/plan_summary_handler.rb +258 -0
- data/lib/mux_tf/terraform_helpers.rb +30 -29
- data/lib/mux_tf/tmux.rb +13 -13
- data/lib/mux_tf/version.rb +1 -1
- data/lib/mux_tf/version_check.rb +6 -6
- data/lib/mux_tf/yaml_cache.rb +1 -1
- data/mux_tf.gemspec +24 -25
- metadata +4 -7
- data/.gitignore +0 -10
- data/Gemfile +0 -8
- data/LICENSE.txt +0 -21
- data/README.md +0 -52
- data/Rakefile +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00bb3571502743499452f7444772ce2eb2b83fec4ed88152f9badde93946bf0c
|
4
|
+
data.tar.gz: 37310310f729f91745266f0a21dd86c0db0af95b04525b1e871746b81479b752
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3363c54464747c089dbdbea6f0fdffa1ba73a83bba8c5af3679206b2e8a4a8e906dc93426a596ca870219750808e4f9c1810826d11539d8aa32e1a0826c2b2db
|
7
|
+
data.tar.gz: 65f7838f5431964c253b59f1d7d3564c5498a43a39deb534797ba1a20ecd3731940956572aa1354171f84d8ccf2eef992c8e59e7b898a85bd55eccdf324e6a21
|
data/exe/tf_current
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
4
|
+
begin
|
5
|
+
$LOAD_PATH << File.expand_path(File.join(__dir__, "..", "lib"))
|
5
6
|
|
6
|
-
require
|
7
|
+
require "mux_tf"
|
7
8
|
|
8
|
-
MuxTf::Cli.run(:current, ARGV)
|
9
|
+
MuxTf::Cli.run(:current, ARGV)
|
10
|
+
rescue Exception => e
|
11
|
+
warn e.full_message
|
12
|
+
warn "<press enter>"
|
13
|
+
gets
|
14
|
+
exit 1
|
15
|
+
end
|
data/exe/tf_mux
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
4
|
+
begin
|
5
|
+
$LOAD_PATH << File.expand_path(File.join(__dir__, "..", "lib"))
|
5
6
|
|
6
|
-
require
|
7
|
+
require "mux_tf"
|
7
8
|
|
8
|
-
MuxTf::Cli.run(:mux, ARGV)
|
9
|
+
MuxTf::Cli.run(:mux, ARGV)
|
10
|
+
rescue Exception => e
|
11
|
+
warn e.full_message
|
12
|
+
warn "<press enter>"
|
13
|
+
gets
|
14
|
+
exit 1
|
15
|
+
end
|
data/exe/tf_plan_summary
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
4
|
+
begin
|
5
|
+
$LOAD_PATH << File.expand_path(File.join(__dir__, "..", "lib"))
|
5
6
|
|
6
|
-
require
|
7
|
+
require "mux_tf"
|
7
8
|
|
8
|
-
MuxTf::Cli.run(:plan_summary, ARGV)
|
9
|
+
MuxTf::Cli.run(:plan_summary, ARGV)
|
10
|
+
rescue Exception => e
|
11
|
+
warn e.full_message
|
12
|
+
warn "<press enter>"
|
13
|
+
gets
|
14
|
+
exit 1
|
15
|
+
end
|
data/lib/deps.rb
ADDED
data/lib/mux_tf.rb
CHANGED
@@ -1,30 +1,33 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
require
|
6
|
-
|
7
|
-
require
|
8
|
-
require
|
9
|
-
|
10
|
-
require
|
11
|
-
|
12
|
-
|
13
|
-
require
|
14
|
-
|
15
|
-
require
|
16
|
-
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
require_relative
|
24
|
-
require_relative
|
25
|
-
require_relative
|
26
|
-
require_relative
|
27
|
-
require_relative
|
3
|
+
require_relative "./deps"
|
4
|
+
|
5
|
+
require "English"
|
6
|
+
|
7
|
+
require "shellwords"
|
8
|
+
require "optparse"
|
9
|
+
require "json"
|
10
|
+
require "open3"
|
11
|
+
|
12
|
+
require "piotrb_cli_utils"
|
13
|
+
require "stateful_parser"
|
14
|
+
|
15
|
+
require "active_support/core_ext"
|
16
|
+
|
17
|
+
require "paint"
|
18
|
+
require "pastel"
|
19
|
+
require "tty-prompt"
|
20
|
+
require "tty-table"
|
21
|
+
require "dotenv"
|
22
|
+
|
23
|
+
require_relative "./mux_tf/version"
|
24
|
+
require_relative "./mux_tf/cli"
|
25
|
+
require_relative "./mux_tf/tmux"
|
26
|
+
require_relative "./mux_tf/terraform_helpers"
|
27
|
+
require_relative "./mux_tf/plan_formatter"
|
28
|
+
require_relative "./mux_tf/version_check"
|
29
|
+
require_relative "./mux_tf/yaml_cache"
|
30
|
+
require_relative "./mux_tf/plan_summary_handler"
|
28
31
|
|
29
32
|
module MuxTf
|
30
33
|
end
|
data/lib/mux_tf/cli/current.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "bundler"
|
4
|
+
|
3
5
|
module MuxTf
|
4
6
|
module Cli
|
5
7
|
module Current
|
@@ -8,13 +10,13 @@ module MuxTf
|
|
8
10
|
extend PiotrbCliUtils::CriCommandSupport
|
9
11
|
extend PiotrbCliUtils::CmdLoop
|
10
12
|
|
11
|
-
PLAN_FILENAME =
|
13
|
+
PLAN_FILENAME = "foo.tfplan"
|
12
14
|
|
13
15
|
class << self
|
14
16
|
def run(args)
|
15
17
|
version_check
|
16
18
|
|
17
|
-
if args[0] ==
|
19
|
+
if args[0] == "cli"
|
18
20
|
cmd_loop
|
19
21
|
return
|
20
22
|
end
|
@@ -22,12 +24,12 @@ module MuxTf
|
|
22
24
|
folder_name = File.basename(Dir.getwd)
|
23
25
|
log "Processing #{Paint[folder_name, :cyan]} ..."
|
24
26
|
|
25
|
-
ENV[
|
26
|
-
ENV[
|
27
|
+
ENV["TF_IN_AUTOMATION"] = "1"
|
28
|
+
ENV["TF_INPUT"] = "0"
|
27
29
|
|
28
30
|
return launch_cmd_loop(:error) unless run_validate
|
29
31
|
|
30
|
-
if ENV[
|
32
|
+
if ENV["TF_UPGRADE"]
|
31
33
|
upgrade_status, upgrade_meta = run_upgrade
|
32
34
|
return launch_cmd_loop(:error) unless upgrade_status == :ok
|
33
35
|
end
|
@@ -36,37 +38,37 @@ module MuxTf
|
|
36
38
|
|
37
39
|
case plan_status
|
38
40
|
when :ok
|
39
|
-
log
|
41
|
+
log "no changes, exiting", depth: 1
|
40
42
|
when :error
|
41
|
-
log
|
43
|
+
log "something went wrong", depth: 1
|
42
44
|
launch_cmd_loop(plan_status)
|
43
45
|
when :changes
|
44
|
-
log
|
46
|
+
log "Printing Plan Summary ...", depth: 1
|
45
47
|
pretty_plan_summary(PLAN_FILENAME)
|
46
48
|
launch_cmd_loop(plan_status)
|
47
49
|
when :unknown
|
48
50
|
launch_cmd_loop(plan_status)
|
49
51
|
end
|
50
52
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
51
|
-
puts Paint[
|
52
|
-
puts
|
53
|
+
puts Paint["Unhandled Exception!", :red]
|
54
|
+
puts "=" * 20
|
53
55
|
puts e.full_message
|
54
56
|
puts
|
55
|
-
puts
|
57
|
+
puts "< press enter to continue >"
|
56
58
|
gets
|
57
59
|
exit 1
|
58
60
|
end
|
59
61
|
|
60
|
-
|
62
|
+
private
|
61
63
|
|
62
64
|
def version_check
|
63
65
|
if VersionCheck.has_updates?
|
64
|
-
log Paint["="*80, :yellow]
|
66
|
+
log Paint["=" * 80, :yellow]
|
65
67
|
log "New version of #{Paint["mux_tf", :cyan]} is available!"
|
66
68
|
log "You are currently on version: #{Paint[VersionCheck.current_gem_version, :yellow]}"
|
67
69
|
log "Latest version found is: #{Paint[VersionCheck.latest_gem_version, :green]}"
|
68
70
|
log "Run `#{Paint["gem install mux_tf", :green]}` to update!"
|
69
|
-
log Paint["="*80, :yellow]
|
71
|
+
log Paint["=" * 80, :yellow]
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
@@ -77,10 +79,17 @@ module MuxTf
|
|
77
79
|
|
78
80
|
def process_remedies(remedies)
|
79
81
|
if remedies.delete? :init
|
80
|
-
log
|
81
|
-
|
82
|
-
|
83
|
-
|
82
|
+
log "Running terraform init ...", depth: 2
|
83
|
+
remedies = PlanFormatter.init_status_to_remedies(*PlanFormatter.run_tf_init)
|
84
|
+
if process_remedies(remedies)
|
85
|
+
remedies = PlanFormatter.process_validation(validate)
|
86
|
+
return false unless process_remedies(remedies)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
if remedies.delete? :reconfigure
|
90
|
+
log "Running terraform init ...", depth: 2
|
91
|
+
remedies = PlanFormatter.init_status_to_remedies(*PlanFormatter.run_tf_init(reconfigure: true))
|
92
|
+
return false unless process_remedies(remedies)
|
84
93
|
end
|
85
94
|
unless remedies.empty?
|
86
95
|
log "unprocessed remedies: #{remedies.to_a}", depth: 1
|
@@ -90,12 +99,12 @@ module MuxTf
|
|
90
99
|
end
|
91
100
|
|
92
101
|
def validate
|
93
|
-
log
|
102
|
+
log "Validating module ...", depth: 1
|
94
103
|
tf_validate.parsed_output
|
95
104
|
end
|
96
105
|
|
97
106
|
def create_plan(filename)
|
98
|
-
log
|
107
|
+
log "Preparing Plan ...", depth: 1
|
99
108
|
exit_code, meta = PlanFormatter.pretty_plan(filename)
|
100
109
|
case exit_code
|
101
110
|
when 0
|
@@ -111,13 +120,13 @@ module MuxTf
|
|
111
120
|
end
|
112
121
|
|
113
122
|
def launch_cmd_loop(status)
|
114
|
-
return if ENV[
|
123
|
+
return if ENV["NO_CMD"]
|
115
124
|
|
116
125
|
case status
|
117
126
|
when :error, :unknown
|
118
|
-
log Paint[
|
127
|
+
log Paint["Dropping to command line so you can fix the issue!", :red]
|
119
128
|
when :changes
|
120
|
-
log Paint[
|
129
|
+
log Paint["Dropping to command line so you can review the changes.", :yellow]
|
121
130
|
end
|
122
131
|
cmd_loop(status)
|
123
132
|
end
|
@@ -138,7 +147,7 @@ module MuxTf
|
|
138
147
|
end
|
139
148
|
|
140
149
|
run_cmd_loop(prompt) do |cmd|
|
141
|
-
throw(:stop, :no_input) if cmd ==
|
150
|
+
throw(:stop, :no_input) if cmd == ""
|
142
151
|
args = Shellwords.split(cmd)
|
143
152
|
root_cmd.run(args, {}, hard_exit: false)
|
144
153
|
end
|
@@ -152,6 +161,7 @@ module MuxTf
|
|
152
161
|
root_cmd.add_command(shell_cmd)
|
153
162
|
root_cmd.add_command(force_unlock_cmd)
|
154
163
|
root_cmd.add_command(upgrade_cmd)
|
164
|
+
root_cmd.add_command(reconfigure_cmd)
|
155
165
|
root_cmd.add_command(interactive_cmd)
|
156
166
|
|
157
167
|
root_cmd.add_command(exit_cmd)
|
@@ -159,90 +169,108 @@ module MuxTf
|
|
159
169
|
end
|
160
170
|
|
161
171
|
def plan_cmd
|
162
|
-
define_cmd(
|
172
|
+
define_cmd("plan", summary: "Re-run plan") do |_opts, _args, _cmd|
|
163
173
|
run_validate && run_plan
|
164
174
|
end
|
165
175
|
end
|
166
176
|
|
167
177
|
def apply_cmd
|
168
|
-
define_cmd(
|
178
|
+
define_cmd("apply", summary: "Apply the current plan") do |_opts, _args, _cmd|
|
169
179
|
status = tf_apply(filename: PLAN_FILENAME)
|
170
180
|
if status.success?
|
171
181
|
throw :stop, :done
|
172
182
|
else
|
173
|
-
log
|
183
|
+
log "Apply Failed!"
|
174
184
|
end
|
175
185
|
end
|
176
186
|
end
|
177
187
|
|
178
188
|
def shell_cmd
|
179
|
-
define_cmd(
|
180
|
-
log Paint[
|
181
|
-
log Paint[
|
182
|
-
system ENV[
|
189
|
+
define_cmd("shell", summary: "Open your default terminal in the current folder") do |_opts, _args, _cmd|
|
190
|
+
log Paint["Launching shell ...", :yellow]
|
191
|
+
log Paint["When it exits you will be back at this prompt.", :yellow]
|
192
|
+
system ENV["SHELL"]
|
183
193
|
end
|
184
194
|
end
|
185
195
|
|
186
196
|
def force_unlock_cmd
|
187
|
-
define_cmd(
|
197
|
+
define_cmd("force-unlock", summary: "Force unlock state after encountering a lock error!") do
|
188
198
|
prompt = TTY::Prompt.new(interrupt: :noop)
|
189
199
|
|
190
200
|
table = TTY::Table.new(header: %w[Field Value])
|
191
|
-
table << [
|
192
|
-
table << [
|
193
|
-
table << [
|
194
|
-
table << [
|
201
|
+
table << ["Lock ID", @plan_meta["ID"]]
|
202
|
+
table << ["Operation", @plan_meta["Operation"]]
|
203
|
+
table << ["Who", @plan_meta["Who"]]
|
204
|
+
table << ["Created", @plan_meta["Created"]]
|
195
205
|
|
196
206
|
puts table.render(:unicode, padding: [0, 1])
|
197
207
|
|
198
|
-
if @plan_meta && @plan_meta[
|
199
|
-
done = catch(:abort)
|
200
|
-
if @plan_meta[
|
208
|
+
if @plan_meta && @plan_meta["error"] == "lock"
|
209
|
+
done = catch(:abort) {
|
210
|
+
if @plan_meta["Operation"] != "OperationTypePlan"
|
201
211
|
throw :abort unless prompt.yes?(
|
202
|
-
"Are you sure you want to force unlock a lock for operation: #{@plan_meta[
|
212
|
+
"Are you sure you want to force unlock a lock for operation: #{@plan_meta["Operation"]}",
|
203
213
|
default: false
|
204
214
|
)
|
205
215
|
end
|
206
216
|
|
207
217
|
throw :abort unless prompt.yes?(
|
208
|
-
|
218
|
+
"Are you sure you want to force unlock this lock?",
|
209
219
|
default: false
|
210
220
|
)
|
211
221
|
|
212
|
-
status = tf_force_unlock(id: @plan_meta[
|
222
|
+
status = tf_force_unlock(id: @plan_meta["ID"])
|
213
223
|
if status.success?
|
214
|
-
log
|
224
|
+
log "Done!"
|
215
225
|
else
|
216
226
|
log Paint["Failed with status: #{status}", :red]
|
217
227
|
end
|
218
228
|
|
219
229
|
true
|
220
|
-
|
230
|
+
}
|
221
231
|
|
222
|
-
log Paint[
|
232
|
+
log Paint["Aborted", :yellow] unless done
|
223
233
|
else
|
224
|
-
log Paint[
|
234
|
+
log Paint["No lock error or no plan ran!", :red]
|
225
235
|
end
|
226
236
|
end
|
227
237
|
end
|
228
238
|
|
229
239
|
def upgrade_cmd
|
230
|
-
define_cmd(
|
240
|
+
define_cmd("upgrade", summary: "Upgrade modules/plguins") do |_opts, _args, _cmd|
|
231
241
|
status, meta = run_upgrade
|
232
242
|
if status != :ok
|
233
243
|
log meta.inspect unless meta.empty?
|
234
|
-
log
|
244
|
+
log "Upgrade Failed!"
|
235
245
|
end
|
236
246
|
end
|
237
247
|
end
|
238
248
|
|
239
|
-
def
|
240
|
-
define_cmd(
|
241
|
-
status =
|
249
|
+
def reconfigure_cmd
|
250
|
+
define_cmd("reconfigure", summary: "Reconfigure modules/plguins") do |_opts, _args, _cmd|
|
251
|
+
status, meta = PlanFormatter.run_tf_init(reconfigure: true)
|
242
252
|
if status != 0
|
243
|
-
log
|
244
|
-
|
245
|
-
|
253
|
+
log meta.inspect unless meta.empty?
|
254
|
+
log "Reconfigure Failed!"
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
def interactive_cmd
|
260
|
+
define_cmd("interactive", summary: "Apply interactively") do |_opts, _args, _cmd|
|
261
|
+
plan = PlanSummaryHandler.from_file(PLAN_FILENAME)
|
262
|
+
begin
|
263
|
+
abort_message = catch :abort do
|
264
|
+
plan.run_interactive
|
265
|
+
end
|
266
|
+
if abort_message
|
267
|
+
log Paint["Aborted: #{abort_message}", :red]
|
268
|
+
else
|
269
|
+
run_plan
|
270
|
+
end
|
271
|
+
rescue Exception => e
|
272
|
+
log e.full_message
|
273
|
+
log "Interactive Apply Failed!"
|
246
274
|
end
|
247
275
|
end
|
248
276
|
end
|
@@ -252,11 +280,11 @@ module MuxTf
|
|
252
280
|
|
253
281
|
case plan_status
|
254
282
|
when :ok
|
255
|
-
log
|
283
|
+
log "no changes", depth: 1
|
256
284
|
when :error
|
257
|
-
log
|
285
|
+
log "something went wrong", depth: 1
|
258
286
|
when :changes
|
259
|
-
log
|
287
|
+
log "Printing Plan Summary ...", depth: 1
|
260
288
|
pretty_plan_summary(PLAN_FILENAME)
|
261
289
|
when :unknown
|
262
290
|
# nothing
|
@@ -264,7 +292,7 @@ module MuxTf
|
|
264
292
|
end
|
265
293
|
|
266
294
|
def run_upgrade
|
267
|
-
exit_code, meta = PlanFormatter.
|
295
|
+
exit_code, meta = PlanFormatter.run_tf_init(upgrade: true)
|
268
296
|
case exit_code
|
269
297
|
when 0
|
270
298
|
[:ok, meta]
|
@@ -278,16 +306,15 @@ module MuxTf
|
|
278
306
|
end
|
279
307
|
end
|
280
308
|
|
281
|
-
def tf_plan_summrary_cmd
|
282
|
-
@tf_plan_summrary_cmd ||= File.expand_path(File.join(__dir__, '..', '..', '..', 'exe', 'tf_plan_summary'))
|
283
|
-
end
|
284
|
-
|
285
309
|
def pretty_plan_summary(filename)
|
286
|
-
|
287
|
-
|
310
|
+
plan = PlanSummaryHandler.from_file(filename)
|
311
|
+
plan.flat_summary.each do |line|
|
312
|
+
log line, depth: 2
|
288
313
|
end
|
314
|
+
log "", depth: 2
|
315
|
+
log plan.summary, depth: 2
|
289
316
|
end
|
317
|
+
end
|
290
318
|
end
|
291
319
|
end
|
292
|
-
end
|
293
320
|
end
|