-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloudsite.mjs
269 lines (245 loc) · 10.3 KB
/
cloudsite.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import * as fs from 'node:fs/promises'
import * as fsPath from 'node:path'
import commandLineArgs from 'command-line-args'
import { commandLineHelp } from 'command-line-help'
import isEqual from 'lodash/isEqual'
import { cliSpec, DB_PATH, globalOptionsSpec } from './constants'
import { checkReminders } from './lib/check-reminders'
import { configureLogger, progressLogger } from '../lib/shared/progress-logger'
import { createCommandGroupHandler } from './lib/create-command-group-handler'
import { getGlobalOptions } from './lib/get-global-options'
import { handleCleanup } from './lib/handle-cleanup'
import { handleCreate } from './lib/handle-create'
import { handleDestroy } from './lib/handle-destroy'
import { handleDetail } from './lib/handle-detail'
import { handleDocument } from './lib/handle-document'
import { handleGetIAMPolicy } from './lib/handle-get-iam-policy'
import { handleList } from './lib/handle-list'
import { handleImport } from './lib/handle-import'
import { handler as handleSetup } from './handlers/setup'
import { handleUpdateContents } from './lib/handle-update-contents'
import { handleUpdateDNS } from './lib/handle-update-dns'
import { handleUpdateStack } from './lib/handle-update-stack'
import { handleVerify } from './lib/handle-verify'
// billing handlers
import { handleBillingConfigureTags } from './lib/billing/handle-billing-configure-tags'
// configuration handlers
import { handleConfigurationSetupLocal } from './lib/configuration/handle-configuration-setup-local'
import { handleConfigurationShow } from './lib/configuration/handle-configuration-show'
// plugin-settings handlers
import { handlePluginSettingsSet } from './lib/plugin-settings/handle-plugin-settings-set'
import { handlePluginSettingsShow } from './lib/plugin-settings/handle-plugin-settings-show'
// reminders handlers
import { handleRemindersList } from './lib/reminders/handle-reminders-list'
// sso handlers
import { handler as handleSSODetail } from './handlers/sso/detail'
const initialDB = {
account : { settings : {} },
sso : {
details : {},
groups : {}
},
reminders : [],
sites : {},
toCleanup : {}
}
const cloudsite = async () => {
// we can 'stopAtFirstUnknown' because the globals are defined at the root level
const mainOptions = commandLineArgs(cliSpec.mainOptions, { partial : true })
const argv = mainOptions._unknown || []
const { command, help /*, quiet */ } = mainOptions
const noReminders = mainOptions['no-reminders']
let db
try {
const dbContents = await fs.readFile(DB_PATH, { encoding : 'utf8' })
db = dbContents.trim() === ''
? initialDB
: JSON.parse(dbContents)
} catch (e) {
if (e.code !== 'ENOENT') {
throw e
}
// otherwise, it's fine, there just are no options
db = initialDB
}
const origDB = structuredClone(db)
const globalOptions = getGlobalOptions({ db })
const { format } = globalOptions
const noColor = globalOptions['no-color']
const ssoProfile = globalOptions['sso-profile']
const throwError = globalOptions['throw-error']
configureLogger({ allowOverflow : true, ...globalOptions })
if (help === true) {
const commands = command === undefined
? []
: [command, ...(mainOptions._unknown?.filter((option) => !option.startsWith('--')) || [])]
const clhOptions = { cliSpec, commands, mainOptionsGlobal : true, noColor }
if (commands.length !== 0) { // then we inject the globalOptions
clhOptions.globalOptions = globalOptionsSpec
}
const help = commandLineHelp(clhOptions)
progressLogger.write(help + '\n', { hangingIndent : 4 })
process.exit(0) // eslint-disable-line no-process-exit
}
if (noReminders !== true) {
checkReminders({ reminders : db.reminders })
}
let exitCode = 0
let data, userMessage, success
try {
switch (command) {
case 'billing': {
const handleBilling = createCommandGroupHandler({
commandHandlerMap : { 'configure-tags' : handleBillingConfigureTags },
groupPath : ['billing']
});
({ success, userMessage } = await handleBilling({ argv, db })); break
}
case 'cleanup':
({ success, userMessage } = await handleCleanup({ argv, db })); break
case 'configuration': {
const handleConfiguration = createCommandGroupHandler({
commandHandlerMap : {
'setup-local' : handleConfigurationSetupLocal,
show : handleConfigurationShow
},
groupPath : ['configuration']
});
({ data, success, userMessage } = await handleConfiguration({ argv, db, globalOptions })); break
}
case 'create':
({ success, userMessage } = await handleCreate({ argv, db, globalOptions })); break
case 'destroy':
({ success, userMessage } = await handleDestroy({ argv, db, globalOptions })); break
case 'detail':
({ data, success } = await handleDetail({ argv, db })); break
case 'document':
({ data, success } = handleDocument({ argv, db }))
break
case 'get-iam-policy':
await handleGetIAMPolicy({ argv, db, globalOptions })
return // get-iam-policy is handles it's own output as the IAM policy is always in JSON format
case 'list':
({ data, success } = await handleList({ argv, db })); break
case 'import':
({ success, userMessage } = await handleImport({ argv, db, globalOptions })); break
/* case 'permissions': {
const handlePermissions = createCommandGroupHandler({
commandHandlerMap : {
sso : createCommandGroupHandler({
commandHandlerMap : {
create : permissionsSSOCreate
},
groupPath : ['permissions', 'sso']
})
},
groupPath : ['permissions']
});
({ data, success } = await handlePermissions({ argv, db, globalOptions })); break
} */
case 'plugin-settings': {
const handlePluginSettings = createCommandGroupHandler({
commandHandlerMap : {
set : handlePluginSettingsSet,
show : handlePluginSettingsShow
},
groupPath : ['plugin-settings']
});
({ data, success, userMessage } = await handlePluginSettings({ argv, db, globalOptions })); break
}
case 'reminders': {
const handleReminders = createCommandGroupHandler({
commandHandlerMap : {
list : handleRemindersList
},
groupPath : ['reminders']
});
({ data, success } = await handleReminders({ argv, db, globalOptions })); break
}
case 'setup':
({ success, userMessage } = await handleSetup({ argv, db, globalOptions })); break
case 'sso': {
const handleSSO = createCommandGroupHandler({
commandHandlerMap : {
detail : handleSSODetail
},
groupPath : ['sso']
});
({ data, success, userMessage } = await handleSSO({ argv, db, globalOptions })); break
}
case 'update-contents':
({ success, userMessage } = await handleUpdateContents({ argv, db, globalOptions })); break
case 'update-dns':
({ success, userMessage } = await handleUpdateDNS({ argv, db, globalOptions })); break
case 'update-stack':
({ success, userMessage } = await handleUpdateStack({ argv, db, globalOptions })); break
case 'verify':
({ data } = await handleVerify({ argv, db, globalOptions })); break
case undefined:
throw new Error("Must specify command or '--help' option.", { exitCode : 1 })
default:
throw new Error('Unknown command: ' + command, { exitCode : 10 })
}
} catch (e) {
if (e.cause === 'setup required') {
process.exit(1) // eslint-disable-line no-process-exit
}
if (throwError === true) {
throw e
} else if (e.name === 'CredentialsProviderError') {
userMessage = 'Your AWS login credentials may have expired. Update your credentials or try refreshing with:\n\n<em>aws sso login'
if (ssoProfile !== undefined) {
userMessage += ' --profile ' + ssoProfile
}
userMessage += '<rst>'
exitCode = 2
} else if (e.name === 'InvalidAccessKeyId') {
userMessage = `It looks like you're using AWS access keys which are no longer valid. These may have been deleted on the AWS end. There are two options:\n\n1) Try singing in with:\n\n <code>aws sso login${ssoProfile === undefined ? '' : ' --profile'}<rst>\n\n2) If don't have SSO setup, you will need to regenerate the access keys. Refer to the following instructions:\n\n<em>https://cloudsitehosting.org/docs/get-started/authentication#initial-authentication-with-access-keys<rst>`
exitCode = 3
} else {
userMessage = e.message +
`\n\nFor more information, try:\n<em>cloudsite --help${command === undefined ? '' : ' <command(s)>'}<rst>`
exitCode = e.exitCode || 11
}
} finally {
await checkAndUpdateSitesInfo({ origDB, db })
}
globalOptions.quiet = false // always send the final status message
const actionStatus = {
success : exitCode === 0 && success === true,
status : exitCode !== 0 ? 'ERROR' : (success === true ? 'SUCCESS' : 'FAILURE'),
userMessage
}
if (data !== undefined) {
actionStatus.data = data
}
// is it a data format
if (format === 'json' || format === 'yaml') {
progressLogger.writeWithOptions({ width : -1 }, actionStatus, '')
} else { // then it's a 'human' format
if (data !== undefined) {
progressLogger.writeWithOptions({ width : -1 }, data, '')
}
if (userMessage !== undefined) {
const { status, userMessage } = actionStatus
let message = userMessage
if (status === 'ERROR') {
message = '<error>!! ERROR !!<rst>: ' + message
} else if (status === 'FAILURE') {
message = '<warn>PARTIAL success: <rst>' + message
}
progressLogger.write('\n' + message + '\n')
}
}
process.exit(exitCode) // eslint-disable-line no-process-exit
}
const checkAndUpdateSitesInfo = async ({ origDB, db }) => {
if (!isEqual(origDB, db)) {
progressLogger.write('Updating Cloudsite DB... ')
await fs.mkdir(fsPath.dirname(DB_PATH), { recursive : true })
const dbContents = JSON.stringify(db, null, ' ')
await fs.writeFile(DB_PATH, dbContents, { encoding : 'utf8' })
progressLogger.write('SUCCESS\n')
}
}
export { cloudsite }