Skip to content

Commit

Permalink
Merge pull request #131 from zanerock/work-liquid-labs/cloudsite/121
Browse files Browse the repository at this point in the history
Add reminder to update billing after create
  • Loading branch information
zanerock authored Mar 30, 2024
2 parents 58079d1 + a2b6e5d commit 5d9fd20
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 43 deletions.
5 changes: 4 additions & 1 deletion src/cli/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const VALID_FORMATS = ['json', 'terminal', 'text', 'yaml']

const DB_PATH = fsPath.join(process.env.HOME, '.config', 'cloudsite', 'cloudsite-db.json')

const ACTION_CLEANUP = 'cleanup'
const ACTION_SETUP_BILLING = 'setup billing'

const globalOptionsSpec = [
{
name : 'format',
Expand Down Expand Up @@ -345,4 +348,4 @@ const cliSpec = {
]
}

export { cliSpec, DB_PATH, globalOptionsSpec, SOURCE_TYPES, VALID_FORMATS }
export { ACTION_CLEANUP, ACTION_SETUP_BILLING, cliSpec, DB_PATH, globalOptionsSpec, SOURCE_TYPES, VALID_FORMATS }
10 changes: 5 additions & 5 deletions src/cli/lib/handle-cleanup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ const handleCleanup = async ({ argv, db }) => {
clearInterval(intervalID)
progressLogger.write('\n')

const userMessages = []
listOfSitesToCleanup.forEach((apexDomain, i) => {
const cleanupResult = cleanupResults[i]
progressLogger.write(`${apexDomain}: ${cleanupResult === true ? 'CLEANED' : 'NOT cleaned'}\n`)
userMessages.push(`${apexDomain}: ${cleanupResult === true ? 'CLEANED' : 'NOT cleaned'}`)
if (cleanupResult === true) {
delete db.toCleanup[apexDomain]
db.reminders.splice(db.reminders.findIndex(({ apexDomain: testDomain }) => testDomain === apexDomain), 1)
// delete all reminders associated with the site
db.reminders = db.reminders.filter(({ apexDomain: testDomain }) => testDomain !== apexDomain)
}
})

const userMessage = listOfSitesToCleanup.length === 1
? `Site '${listOfSitesToCleanup[0]}' has been successfully cleaned.`
: `Sites '${listOfSitesToCleanup.join("', '")}' have been successfully cleaned.`
const userMessage = userMessages.join('\n')

return { success : true, userMessage }
}
Expand Down
14 changes: 11 additions & 3 deletions src/cli/lib/handle-create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import commandLineArgs from 'command-line-args'
import { awsS3TABucketNameRE, awsS3TABucketNameREString } from 'regex-repo'
import { Questioner } from 'question-and-answer'

import { ACTION_SETUP_BILLING, cliSpec } from '../constants'
import { checkAuthentication } from './check-authentication'
import { cliSpec } from '../constants'
import { create } from '../../lib/actions/create'
import { getOptionsSpec } from './get-options-spec'
import * as optionsLib from './options'
Expand Down Expand Up @@ -121,9 +121,17 @@ const handleCreate = async ({ argv, db }) => {
({ stackName, success } = await create({ db, noBuild, noDeleteOnFailure, siteInfo }))

if (success === true) {
return { success, userMessage : `Created stack '${stackName}'.` }
const now = new Date()
const remindAfter = new Date(now.getTime() + 4 * 60 * 60 * 1000) // give it 4 hours
db.reminders.push({
todo : `Setup billing tags for site '${apexDomain}'. Try:\ncloudsite update ${apexDomain} --do-billing`,
remindAfter : remindAfter.toISOString(),
references : apexDomain,
action : ACTION_SETUP_BILLING
})
return { success, userMessage : `Created site '${stackName}'/'www.${stackName}'.` }
} else {
return { success, userMessage : `Failed to create stack '${stackName}'.` }
return { success, userMessage : `Failed to create site '${stackName}'.` }
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/cli/lib/handle-destroy.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import commandLineArgs from 'command-line-args'
import { Questioner } from 'question-and-answer'

import { cliSpec } from '../constants'
import { ACTION_CLEANUP, cliSpec } from '../constants'
import { destroy } from '../../lib/actions/destroy'
import { getOptionsSpec } from './get-options-spec'
import { getSiteInfo } from './get-site-info'
Expand Down Expand Up @@ -40,13 +40,14 @@ const handleDestroy = async ({ argv, db }) => {
return { success : true, userMessage : `${apexDomain} deleted.\nRemoved ${apexDomain} from local DB.\n` }
} else {
const now = new Date()
const remindAfter = new Date(now.getTime() + 2 * 60 * 60 * 1000)
const remindAfter = new Date(now.getTime() + 2 * 60 * 60 * 1000) // give it 2 hours
siteInfo.lastCleanupAttempt = now.toISOString()
db.toCleanup[apexDomain] = siteInfo
db.reminders.push({
todo : `Cleanup partially deleted site '${apexDomain}'. Try:\ncloudsite cleanup`,
remindAfter : remindAfter.toISOString(),
references : apexDomain
references : apexDomain,
action : ACTION_CLEANUP
})
delete db.sites[apexDomain]

Expand Down
9 changes: 7 additions & 2 deletions src/cli/lib/handle-update.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import commandLineArgs from 'command-line-args'

import { cliSpec } from '../constants'
import { ACTION_SETUP_BILLING, cliSpec } from '../constants'
import { getOptionsSpec } from './get-options-spec'
import { getSiteInfo } from './get-site-info'
import { update } from '../../lib/actions/update'
Expand All @@ -18,7 +18,12 @@ const handleUpdate = async ({ argv, db }) => {

const siteInfo = getSiteInfo({ apexDomain, db })

await update({ db, doBilling, doContent, doDNS, doStack, noBuild, noCacheInvalidation, siteInfo })
const { doAll } = await update({ db, doBilling, doContent, doDNS, doStack, noBuild, noCacheInvalidation, siteInfo })

if (doAll === true || doBilling === true) {
db.reminders.splice(db.reminders.findIndex(({ action, apexDomain: testDomain }) =>
action === ACTION_SETUP_BILLING && testDomain === apexDomain), 1)
}

return { success : true, userMessage : `Updated '${apexDomain}' site.` }
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/actions/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ const create = async ({

bucketName = await determineBucketName({ apexDomain, bucketName, credentials, findName : true, siteInfo })
siteInfo.bucketName = bucketName
const stackCreated = await createSiteStack({ credentials, noDeleteOnFailure, siteInfo })
const { success, stackName } = await createSiteStack({ credentials, noDeleteOnFailure, siteInfo })

if (stackCreated === true) {
if (success === true) {
const postUpdateHandlers = Object.keys(siteInfo.plugins || {}).map((pluginKey) =>
[pluginKey, plugins[pluginKey].postUpdateHandler]
)
Expand All @@ -79,9 +79,9 @@ const create = async ({
handleAssociateCostAllocationTagsError({ e, siteInfo })
}

return true
return { success, stackName }
} else {
return false
return { success, stackName }
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/actions/update.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ const update = async ({
}

await Promise.all(secondRoundUpdates)

return { doAll }
}

const invalidateCache = async ({ credentials, siteInfo }) => {
Expand Down
54 changes: 29 additions & 25 deletions src/lib/plugins/contact-handler/lib/setup-contact-handler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const setupContactHandler = async ({
pluginData.contactHandlerFunctionName = contactHandlerFunctionName

const contactHandlerLogGroupName = contactHandlerFunctionName
const contactHandlerPolicyName = contactHandlerFunctionName

const { formFields = 'standard' } = pluginData.settings
const formFieldsSpec = formFields === 'standard'
Expand Down Expand Up @@ -54,31 +55,34 @@ const setupContactHandler = async ({
Path : '/cloudsite/contact-processor/',
Policies : [
{
Version : '2012-10-17',
Statement : [
{
Action : [
'dynamodb:PutItem'
],
Resource : { 'Fn::GetAtt' : ['ContactHandlerDynamoDB', 'Arn'] },
Effect : 'Allow'
},
{
Effect : 'Allow',
Action : 'logs:CreateLogGroup',
Resource : `arn:aws:${region}:${accountID}:*`
},
{
Effect : 'Allow',
Action : [
'logs:CreateLogStream',
'logs:PutLogEvents'
],
Resource : [
`arn:aws:logs:${region}:${accountID}:log-group:${contactHandlerLogGroupName}:*`
]
}
]
PolicyName : contactHandlerPolicyName,
PolicyDocument : {
Version : '2012-10-17',
Statement : [
{
Action : [
'dynamodb:PutItem'
],
Resource : { 'Fn::GetAtt' : ['ContactHandlerDynamoDB', 'Arn'] },
Effect : 'Allow'
},
{
Effect : 'Allow',
Action : 'logs:CreateLogGroup',
Resource : `arn:aws:${region}:${accountID}:*`
},
{
Effect : 'Allow',
Action : [
'logs:CreateLogStream',
'logs:PutLogEvents'
],
Resource : [
`arn:aws:logs:${region}:${accountID}:log-group:${contactHandlerLogGroupName}:*`
]
}
]
}
}
],
Tags : tags
Expand Down

0 comments on commit 5d9fd20

Please sign in to comment.