belt 0.3.32 → 0.3.34

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: d6661697f19873dc93fa45b49d27d6665e370d771b947a6ea5539fb33a0970c9
4
- data.tar.gz: ad996f0d38638b90ae2ebb1c8c761487b9bc4939fe3dba009dbed4f8ea4a6b4d
3
+ metadata.gz: 01f5648c9621e3feace4ceafc8f6683575c9f52a17ec59c60c2eb1f9202f3f65
4
+ data.tar.gz: 86a781e22c010419b21f09d00115db32ad9c13f40bf5be02a391ae146fd70ec6
5
5
  SHA512:
6
- metadata.gz: 5eda51e4ed021da4d55135db7d5945c8370bbd6ae2d378e760aab837cdd353fc70dd34f2ae18c35dca6bd5d57eb8222bc0646b017a2199bdd01c747c94d6c6ee
7
- data.tar.gz: 0e3e599b0972fb45145735ff8d390ff23bca425521c1fdaba2c5758071017b1857aad884a57fcf124a39bdba2f66845ca85b8890f21d9b5e0f3bf2ab9ae6e1be
6
+ metadata.gz: 6f3aca3d970a65d95fc9028b9eb7bb8117043e7c00fffaf481fbe3bf80d4a603c501f6321e438934c6cc8622e897c84034657fb01f6cc9c8ac2c3ae9b7604404
7
+ data.tar.gz: 50f18ff13e6cb0767a57bc7af4b1963de6bd4681fdd2030db8e5ed34872bc031bf4761bf23065a27e7115bb6f405ece4c0078a30dd86ba17b077cb3b0d6eb8cd
@@ -117,7 +117,8 @@ module Belt
117
117
 
118
118
  Environment options:
119
119
  --full, -y Non-interactive teardown: ALWAYS run terraform
120
- destroy, then delete local files, no prompts
120
+ destroy, then delete local files AND purge the
121
+ remote terraform state from S3, no prompts
121
122
  (use this for CI / agents tearing down ephemeral envs)
122
123
  --force, -f Skip all prompts but SKIP terraform destroy —
123
124
  only deletes local files (leaves cloud resources)
@@ -274,6 +275,9 @@ module Belt
274
275
  end
275
276
  end
276
277
 
278
+ # Purge remote state from S3 when --full is used (complete ephemeral cleanup)
279
+ purge_terraform_state(dir) if @full
280
+
277
281
  FileUtils.rm_rf(dir)
278
282
  @removed << dir
279
283
  puts " remove #{dir}/"
@@ -402,6 +406,50 @@ module Belt
402
406
  puts ' ✓ Infrastructure destroyed.'
403
407
  end
404
408
 
409
+ # Purge the terraform state file from S3 after a full destroy.
410
+ # This leaves zero trace of ephemeral environments in the state bucket.
411
+ def purge_terraform_state(dir)
412
+ backend_file = File.join(dir, 'backend.tf')
413
+ return unless File.exist?(backend_file)
414
+
415
+ content = File.read(backend_file)
416
+
417
+ # Extract bucket and key from backend config
418
+ bucket_match = content.match(/bucket\s*=\s*"([^"]+)"/)
419
+ key_match = content.match(/key\s*=\s*"([^"]+)"/)
420
+
421
+ return unless bucket_match && key_match
422
+
423
+ bucket = bucket_match[1]
424
+ key = key_match[1]
425
+
426
+ puts ' Purging terraform state from S3...'
427
+
428
+ # Delete the state file
429
+ result = `aws s3 rm "s3://#{bucket}/#{key}" 2>&1`
430
+ if Process.last_status.success?
431
+ puts " ✓ Deleted s3://#{bucket}/#{key}"
432
+ else
433
+ puts " ⚠ Could not delete state file: #{result.strip}" unless result.include?('delete: s3://')
434
+ end
435
+
436
+ # Also try to delete the state lock file if it exists (DynamoDB-based locking
437
+ # uses a separate mechanism, but some setups have .tflock files)
438
+ lock_key = key.sub(/\.tfstate$/, '.tfstate.lock')
439
+ `aws s3 rm "s3://#{bucket}/#{lock_key}" 2>/dev/null`
440
+
441
+ # Try to clean up the environment's directory in the bucket if empty
442
+ # (e.g., if the key is "myapp/fizzy123/terraform.tfstate", try to remove the "fizzy123" prefix)
443
+ env_prefix = key.sub(%r{/[^/]+$}, '')
444
+ return unless env_prefix != key
445
+
446
+ # List remaining objects under this prefix
447
+ remaining = `aws s3 ls "s3://#{bucket}/#{env_prefix}/" 2>/dev/null`
448
+ return unless Process.last_status.success? && remaining.strip.empty?
449
+
450
+ puts " ✓ State directory s3://#{bucket}/#{env_prefix}/ is now empty"
451
+ end
452
+
405
453
  # Load infrastructure/<env>/belt.rb and apply its aws_profile + env vars to
406
454
  # the current process so terraform (and any aws CLI calls during destroy)
407
455
  # authenticate against the right account. Mirrors DeployCommand / TerraformCommand.
@@ -167,12 +167,15 @@ module Belt
167
167
 
168
168
  if auto
169
169
  run_terraform('apply', env_config, 'tfplan')
170
+ cleanup_plan
170
171
  else
171
172
  print "\nApply this plan? [y/N] "
172
173
  answer = $stdin.gets&.strip&.downcase
173
174
  if %w[y yes].include?(answer)
174
175
  run_terraform('apply', env_config, 'tfplan')
176
+ cleanup_plan
175
177
  else
178
+ cleanup_plan
176
179
  puts 'Aborted.'
177
180
  exit 1
178
181
  end
@@ -180,6 +183,10 @@ module Belt
180
183
  end
181
184
  end
182
185
 
186
+ def cleanup_plan
187
+ FileUtils.rm_f('tfplan')
188
+ end
189
+
183
190
  # --- Add Environment ---
184
191
  def add_environment(args)
185
192
  env_name = args.shift
data/lib/belt/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Belt
4
- VERSION = '0.3.32'
4
+ VERSION = '0.3.34'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: belt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.32
4
+ version: 0.3.34
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stowzilla