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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3748671ba7cadd0a528d33b4066e929df30817f1741cedd4126088e3148a1edb
4
- data.tar.gz: 475c07f7d293f759082c4138d7bccb6cc2c04493fae45b2751225cdc98b6775e
3
+ metadata.gz: 00bb3571502743499452f7444772ce2eb2b83fec4ed88152f9badde93946bf0c
4
+ data.tar.gz: 37310310f729f91745266f0a21dd86c0db0af95b04525b1e871746b81479b752
5
5
  SHA512:
6
- metadata.gz: d87307602bb62b5be1e8686c7ea3a8315ba10ffd972bc20a930296281de520b025b96b3312af2bebb6317944544b0bb3caea35aba702c4dddfe4fa4a7ca8534d
7
- data.tar.gz: 9371bb009d008ec46cf2ceb0c2542dca1ce424b5ddac7555d696ea72e12a4e0a4c3c3614a8a9e8b2388c6552a98df4f2d6439ef39262f3f640a6098bfe9bdf5c
6
+ metadata.gz: 3363c54464747c089dbdbea6f0fdffa1ba73a83bba8c5af3679206b2e8a4a8e906dc93426a596ca870219750808e4f9c1810826d11539d8aa32e1a0826c2b2db
7
+ data.tar.gz: 65f7838f5431964c253b59f1d7d3564c5498a43a39deb534797ba1a20ecd3731940956572aa1354171f84d8ccf2eef992c8e59e7b898a85bd55eccdf324e6a21
@@ -1,8 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- $LOAD_PATH << File.expand_path(File.join(__dir__, '..', 'lib'))
4
+ begin
5
+ $LOAD_PATH << File.expand_path(File.join(__dir__, "..", "lib"))
5
6
 
6
- require 'mux_tf'
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
- $LOAD_PATH << File.expand_path(File.join(__dir__, '..', 'lib'))
4
+ begin
5
+ $LOAD_PATH << File.expand_path(File.join(__dir__, "..", "lib"))
5
6
 
6
- require 'mux_tf'
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
@@ -1,8 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- $LOAD_PATH << File.expand_path(File.join(__dir__, '..', 'lib'))
4
+ begin
5
+ $LOAD_PATH << File.expand_path(File.join(__dir__, "..", "lib"))
5
6
 
6
- require 'mux_tf'
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
@@ -0,0 +1,5 @@
1
+ require "bundler/inline"
2
+
3
+ gemfile do
4
+ gemspec(path: File.join(__dir__, ".."))
5
+ end
@@ -1,30 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'English'
4
-
5
- require 'shellwords'
6
- require 'optparse'
7
- require 'json'
8
- require 'open3'
9
-
10
- require 'piotrb_cli_utils'
11
- require 'stateful_parser'
12
-
13
- require 'active_support/core_ext'
14
-
15
- require 'paint'
16
- require 'pastel'
17
- require 'tty-prompt'
18
- require 'tty-table'
19
- require 'dotenv'
20
-
21
- require_relative './mux_tf/version'
22
- require_relative './mux_tf/cli'
23
- require_relative './mux_tf/tmux'
24
- require_relative './mux_tf/terraform_helpers'
25
- require_relative './mux_tf/plan_formatter'
26
- require_relative './mux_tf/version_check'
27
- require_relative './mux_tf/yaml_cache'
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
@@ -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 = 'foo.tfplan'
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] == 'cli'
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['TF_IN_AUTOMATION'] = '1'
26
- ENV['TF_INPUT'] = '0'
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['TF_UPGRADE']
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 'no changes, exiting', depth: 1
41
+ log "no changes, exiting", depth: 1
40
42
  when :error
41
- log 'something went wrong', depth: 1
43
+ log "something went wrong", depth: 1
42
44
  launch_cmd_loop(plan_status)
43
45
  when :changes
44
- log 'Printing Plan Summary ...', depth: 1
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['Unhandled Exception!', :red]
52
- puts '=' * 20
53
+ puts Paint["Unhandled Exception!", :red]
54
+ puts "=" * 20
53
55
  puts e.full_message
54
56
  puts
55
- puts '< press enter to continue >'
57
+ puts "< press enter to continue >"
56
58
  gets
57
59
  exit 1
58
60
  end
59
61
 
60
- private
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 'Running terraform init ...', depth: 2
81
- tf_init
82
- remedies = PlanFormatter.process_validation(validate)
83
- process_remedies(remedies)
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 'Validating module ...', depth: 1
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 'Preparing Plan ...', depth: 1
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['NO_CMD']
123
+ return if ENV["NO_CMD"]
115
124
 
116
125
  case status
117
126
  when :error, :unknown
118
- log Paint['Dropping to command line so you can fix the issue!', :red]
127
+ log Paint["Dropping to command line so you can fix the issue!", :red]
119
128
  when :changes
120
- log Paint['Dropping to command line so you can review the changes.', :yellow]
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('plan', summary: 'Re-run plan') do |_opts, _args, _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('apply', summary: 'Apply the current plan') do |_opts, _args, _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 'Apply Failed!'
183
+ log "Apply Failed!"
174
184
  end
175
185
  end
176
186
  end
177
187
 
178
188
  def shell_cmd
179
- define_cmd('shell', summary: 'Open your default terminal in the current folder') do |_opts, _args, _cmd|
180
- log Paint['Launching shell ...', :yellow]
181
- log Paint['When it exits you will be back at this prompt.', :yellow]
182
- system ENV['SHELL']
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('force-unlock', summary: 'Force unlock state after encountering a lock error!') do
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 << ['Lock ID', @plan_meta['ID']]
192
- table << ['Operation', @plan_meta['Operation']]
193
- table << ['Who', @plan_meta['Who']]
194
- table << ['Created', @plan_meta['Created']]
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['error'] == 'lock'
199
- done = catch(:abort) do
200
- if @plan_meta['Operation'] != 'OperationTypePlan'
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['Operation']}",
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
- 'Are you sure you want to force unlock this lock?',
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['ID'])
222
+ status = tf_force_unlock(id: @plan_meta["ID"])
213
223
  if status.success?
214
- log 'Done!'
224
+ log "Done!"
215
225
  else
216
226
  log Paint["Failed with status: #{status}", :red]
217
227
  end
218
228
 
219
229
  true
220
- end
230
+ }
221
231
 
222
- log Paint['Aborted', :yellow] unless done
232
+ log Paint["Aborted", :yellow] unless done
223
233
  else
224
- log Paint['No lock error or no plan ran!', :red]
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('upgrade', summary: 'Upgrade modules/plguins') do |_opts, _args, _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 'Upgrade Failed!'
244
+ log "Upgrade Failed!"
235
245
  end
236
246
  end
237
247
  end
238
248
 
239
- def interactive_cmd
240
- define_cmd('interactive', summary: 'Apply interactively') do |_opts, _args, _cmd|
241
- status = run_shell([tf_plan_summrary_cmd, PLAN_FILENAME, '-i'], return_status: true)
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 'Interactive Apply Failed!'
244
- else
245
- run_plan
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 'no changes', depth: 1
283
+ log "no changes", depth: 1
256
284
  when :error
257
- log 'something went wrong', depth: 1
285
+ log "something went wrong", depth: 1
258
286
  when :changes
259
- log 'Printing Plan Summary ...', depth: 1
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.process_upgrade
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
- run_with_each_line([tf_plan_summrary_cmd, filename]) do |raw_line|
287
- log raw_line.rstrip, depth: 2
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