Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve user descriptions on delete and actually remove from DB #73

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/cli/lib/handle-destroy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ const handleDestroy = async ({ argv, globalOptions, sitesInfo }) => {
process.exit(3) // eslint-disable-line no-process-exit
}

await destroy({ globalOptions, siteInfo, verbose : true })
const deleted = await destroy({ globalOptions, siteInfo, verbose : true })

if (deleted === true) {
process.stdout.write(`Removing ${apexDomain} from local DB.\n`)
delete sitesInfo[apexDomain]
}
}

export { handleDestroy }
37 changes: 33 additions & 4 deletions src/lib/actions/destroy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,47 @@ const destroy = async ({ globalOptions, siteInfo, verbose }) => {
const s3Client = new S3Client({ credentials })

// this method provides user udptaes
await emptyBucket({ bucketName, s3Client, verbose })
try {
progressLogger?.write('Deleting site bucket...\n')
await emptyBucket({ bucketName, s3Client, verbose })
} catch (e) {
if (e.name === 'NoSuchBucket') {
progressLogger?.write('Bucket already deleted.\n')
} else {
throw e
}
}

const siteTemplate = new SiteTemplate({ credentials, siteInfo })
await siteTemplate.destroyPlugins()

progressLogger.write('Deleting stack...\n')
progressLogger.write('Deleting stack...')
const cloudFormationClient = new CloudFormationClient({ credentials })
const deleteStackCommand = new DeleteStackCommand({ StackName : stackName })
await cloudFormationClient.send(deleteStackCommand)

const finalStatus = await trackStackStatus({ cloudFormationClient, noDeleteOnFailure : true, stackName })
progressLogger?.write('Final status: ' + finalStatus + '\n')
// the delete command is doesn't mind if the bucket doesn't exist, but trackStackStatus does
try {
const finalStatus = await trackStackStatus({ cloudFormationClient, noDeleteOnFailure : true, stackName })
progressLogger?.write('Final status: ' + finalStatus + '\n')

if (finalStatus === 'DELETE_FAILED' && progressLogger !== undefined) {
progressLogger.write('\nThe delete is expected to fail at first because the \'replicated Lambda functions\' take a while to clear and the stack cannot be fully deleted until AWS clears the replicated functions. Give it at least 30 min and up to a few hours and try again.')
return false
} else if (finalStatus === 'DELETE_COMPLETE') {
return true
}
} catch (e) {
// oddly, if the stack does not exist we get a ValidationError; which means it's already deleted
if (e.name === 'ValidationError') {
progressLogger.write(' already deleted.\n')
return true
} else {
throw e
}
} finally {
progressLogger?.write('\n')
}
}

export { destroy }
9 changes: 7 additions & 2 deletions src/lib/shared/site-template.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import yaml from 'js-yaml'

import { S3Client, DeleteBucketCommand } from '@aws-sdk/client-s3'

import * as plugins from '../plugins'
import { determineBucketName } from './determine-bucket-name'
import { determineOACName } from './determine-oac-name'
import * as plugins from '../plugins'
import { progressLogger } from './progress-logger'

/**
* Class encapsulating site stack configuration. Any enabled plugins are loaded and processed by this class.
Expand Down Expand Up @@ -153,6 +154,7 @@ const SiteTemplate = class {
const { siteInfo } = this
const { sharedLoggingBucket } = siteInfo

progressLogger.write('Deleting shared logging bucket...\n')
const s3Client = new S3Client({ credentials : this.credentials })
const deleteBucketCommand = new DeleteBucketCommand({ Bucket : sharedLoggingBucket })
await s3Client.send(deleteBucketCommand)
Expand Down Expand Up @@ -197,7 +199,10 @@ const SiteTemplate = class {
throw new Error(`Unknown plugin found in '${apexDomain}' plugin settings.`)
}

await plugin.preStackDestroyHandler({ siteTemplate : this, settings })
const { preStackDestroyHandler } = plugin
if (preStackDestroyHandler !== undefined) {
await preStackDestroyHandler({ siteTemplate : this, settings })
}
}
}

Expand Down