belt 0.3.32 → 0.3.33
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/lib/belt/cli/destroy_command.rb +49 -1
- data/lib/belt/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: efadc9dd5984c98b474ea0bda5f91617ccb9ad7077582086098d913d1190fb79
|
|
4
|
+
data.tar.gz: 8335192999b2f888a518e3bb542c04e0b23364523ecc788f8e6eda7b4281e136
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f7ae2d5e4669eea772061e523bae6422388e356cbe1ecdf86f7d284b034549ac1127a239ac85cb8b3cf1babab27c0232ec61bcfc18dcac8ff54b2b03cb263435
|
|
7
|
+
data.tar.gz: 190079da2ca430955096baa50ea281da2feaee87e6ae24211aab5c26b559eea6f1d1f45d9c9dc627d7ebf5d28418681c13d0398ef744a970580cfe8441368f3b
|
|
@@ -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
|
|
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.
|
data/lib/belt/version.rb
CHANGED