-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
554 lines (443 loc) · 14.2 KB
/
app.js
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
const { App } = require('@slack/bolt');
require('dotenv').config();
const schedule = require('node-schedule');
// Initialization
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
socketMode: true, // enable the following to use socket mode
appToken: process.env.APP_TOKEN,
});
// kinsta API utilities
const KinstaAPIUrl = 'https://api.kinsta.com/v2';
const getHeaders = {
Authorization: `Bearer ${process.env.KINSTA_API_KEY}`,
};
const postHeaders = {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.KINSTA_API_KEY}`,
};
// querying Kinsta API
async function getAllSites() {
const query = new URLSearchParams({
company: process.env.KINSTA_COMPANY_ID,
}).toString();
const resp = await fetch(`${KinstaAPIUrl}/sites?${query}`, {
method: 'GET',
headers: getHeaders,
});
const data = await resp.json();
// console.log(data);
return data;
}
async function getEnvironmentId(siteId) {
const resp = await fetch(`${KinstaAPIUrl}/sites/${siteId}/environments`, {
method: 'GET',
headers: getHeaders,
});
const data = await resp.json();
return data;
}
async function CheckOperationStatus(operationId) {
const resp = await fetch(`${KinstaAPIUrl}/operations/${operationId}`, {
method: 'GET',
headers: getHeaders,
});
const data = await resp.json();
return data;
}
async function clearSiteCache(environmentId) {
const resp = await fetch(`${KinstaAPIUrl}/sites/tools/clear-cache`, {
method: 'POST',
headers: postHeaders,
body: JSON.stringify({
environment_id: environmentId,
}),
});
const data = await resp.json();
return data;
}
async function restartPHPEngine(environmentId) {
const resp = await fetch(`${KinstaAPIUrl}/sites/tools/restart-php`, {
method: 'POST',
headers: postHeaders,
body: JSON.stringify({
environment_id: environmentId,
}),
});
const data = await resp.json();
return data;
}
async function getSiteLogs(environmentId, fileName, lines) {
const query = new URLSearchParams({
file_name: fileName || 'error',
lines: lines || 1000,
}).toString();
const resp = await fetch(
`https://api.kinsta.com/v2/sites/environments/${environmentId}/logs?${query}`,
{
method: 'GET',
headers: getHeaders,
}
);
const data = await resp.json();
return data;
}
// --- Handling backups with Kinsta API --- //
// Get manual, schedule and system generated backups
async function getBackups(environmentId) {
const resp = await fetch(
`${KinstaAPIUrl}/sites/environments/${environmentId}/backups`,
{
method: 'GET',
headers: getHeaders,
}
);
const data = await resp.json();
return data;
}
async function getDownloadableBackups(environmentId) {
const resp = await fetch(
`${KinstaAPIUrl}/sites/environments/${environmentId}/downloadable-backups`,
{
method: 'GET',
headers: getHeaders,
}
);
const data = await resp.json();
return data;
}
async function restoreBackup(targetEnvironmentId, backupId, environmentName) {
const resp = await fetch(
`${KinstaAPIUrl}/sites/environments/${targetEnvironmentId}/backups/restore`,
{
method: 'POST',
headers: postHeaders,
body: JSON.stringify({
backup_id: backupId,
env_display_name_of_backup: environmentName,
}),
}
);
const data = await resp.json();
return data;
}
async function addManualBackup(environmentId, tag) {
const resp = await fetch(
`${KinstaAPIUrl}/sites/environments/${environmentId}/manual-backups`,
{
method: 'POST',
headers: postHeaders,
body: JSON.stringify({
tag,
}),
}
);
const data = await resp.json();
return data;
}
async function deleteBackup(backupId) {
const resp = await fetch(
`${KinstaAPIUrl}/sites/environments/backups/${backupId}`,
{
method: 'DELETE',
headers: getHeaders,
}
);
const data = await resp.json();
return data;
}
// -------- SLASH COMMANDS ---------- //
// creating slash commands
app.command('/environment_id', async ({ command, ack, say }) => {
await ack();
const siteName = command.text;
const response = await getAllSites();
if (response) {
const mySites = response.company.sites;
const currentSite = mySites.find((site) => site.name === siteName);
const envIdResponse = await getEnvironmentId(currentSite.id);
const envId = envIdResponse.site.environments[0].id;
if (envId) {
say(`Hey 👋,\n\nThe environment ID for "${siteName}" is 👉 ${envId}`);
}
}
});
app.command('/site_id', async ({ command, ack, say }) => {
await ack();
const siteName = command.text;
const response = await getAllSites();
const mySites = response.company.sites;
const currentSite = mySites.find((site) => site.name === siteName);
const siteId = currentSite.id;
if (siteId) {
say(`Hey 👋, \n\nThe site ID for "${siteName}" is 👉 ${siteId}`);
}
});
app.command('/operation_status', async ({ command, ack, say }) => {
await ack();
const operationId = command.text;
const response = await CheckOperationStatus(operationId);
const operationMessage = response.message;
if (operationMessage) {
say(`Hey 👋, \n\n${operationMessage}`);
}
});
app.command('/clear_site_cache', async ({ command, ack, say }) => {
await ack();
const environmentId = command.text;
const response = await clearSiteCache(environmentId);
if (response) {
say(
`Hey 👋, \n\n${response.message} by using the /operation_status slack commmand. \n\nOperation Id is ${response.operation_id}`
);
}
});
app.command('/restart_php_engine', async ({ command, ack, say }) => {
await ack();
const environmentId = command.text;
const response = await restartPHPEngine(environmentId);
if (response) {
say(
`Hey 👋, \n\n${response.message} by using the /operation_status slack commmand. \n\nOperation Id is ${response.operation_id}`
);
}
});
app.command('/get_backups', async ({ command, ack, say }) => {
await ack();
const environmentId = command.text;
const response = await getBackups(environmentId);
const backups = response.environment.backups;
const backupDetails = backups
.map((backup) => {
return `Backup ID: ${backup.id}\nName: ${backup.name}\nNote: ${
backup.note
}\nType: ${backup.type}\nCreated At: ${new Date(backup.created_at)}\n\n`;
})
.join('');
if (backupDetails) {
say(
`Hey 👋, here are the backup details for environment ID ${environmentId}:\n\n${backupDetails}`
);
} else {
say(`No backups found for environment ID ${environmentId}`);
}
});
app.command('/get_downloadable_backups', async ({ command, ack, say }) => {
await ack();
const environmentId = command.text;
const response = await getDownloadableBackups(environmentId);
const backups = response.environment.downloadable_backups;
const downloadable_backupDetails = backups
.map((backup) => {
return `Backup ID: ${backup.id}\nDownload Link: ${
backup.download_link
}\nCreated At: ${new Date(backup.created_at)}\nExpires At: ${new Date(
backup.expires_at
)}\nIs Generation in Progress: ${backup.is_generation_in_progress}\n\n`;
})
.join('');
if (downloadable_backupDetails) {
say(
`Hey 👋, here are the downloadable backup details for environment ${environmentId}:\n\n${downloadable_backupDetails}`
);
} else {
say(`No downloadable backups found for environment ${environmentId}`);
}
});
app.command('/restore_backup', async ({ command, ack, say }) => {
await ack();
const [targetEnvironmentId, backupId, environmentName] =
command.text.split(' ');
const response = await restoreBackup(
targetEnvironmentId,
backupId,
environmentName
);
if (response) {
say(
`Hey 👋, \n\n${response.message}. You can use the /operation_status slack commmand to check the status of this Operation Id ${response.operation_id}`
);
}
});
app.command('/add_manual_backup', async ({ command, ack, say }) => {
await ack();
const [environmentId, tag] = command.text.split(' ');
const response = await addManualBackup(environmentId, tag);
if (response) {
say(
`Hey 👋, \n\n${response.message}. You can use the /operation_status slack commmand to check the status of this Operation Id ${response.operation_id}`
);
}
});
app.command('/delete_backup', async ({ command, ack, say }) => {
await ack();
const backupId = command.text;
const response = await deleteBackup(backupId);
if (response) {
say(`Hey 👋, \n\n${response.message}`);
}
});
app.command('/get_site_logs', async ({ command, ack, say }) => {
await ack();
const [environmentId, fileName, lines] = command.text.split(' ');
const response = await getSiteLogs(environmentId, fileName, lines);
if (response) {
const logs = response.environment.container_info.logs.split('\n');
const formattedLogs = logs.join('\n\n'); // or any other formatting needed
say(`Hey 👋, \n\nHere are the logs for ${fileName}:\n\n${formattedLogs}`);
} else {
say(`Sorry, no logs found for ${fileName}.`);
}
});
// Command to schedule backup at a specific time
app.command('/schedule_backup', async ({ command, ack, say }) => {
await ack();
// Extract environment ID, tag, and time from the Slack command text
const [environmentId, tag, time] = command.text.split(' ');
// Validate time (expecting HH:MM in 24-hour format)
const timeRegex = /^([01]\d|2[0-3]):([0-5]\d)$/;
if (!time || !timeRegex.test(time)) {
say('Invalid time format. Please use HH:MM (24-hour format).');
return;
}
const [hour, minute] = time.split(':').map(Number);
// Schedule the backup
const date = new Date();
date.setHours(hour, minute, 0, 0); // Set the time for the scheduled backup
// Schedule the backup using node-schedule
schedule.scheduleJob(date, async () => {
try {
// Call the function to create a manual backup
await addManualBackup(environmentId, tag);
// Notify the user via Slack when the backup is created
await say(
`Backup with tag *${tag}* has been successfully created for environment *${environmentId}*.`
);
} catch (error) {
console.error('Error creating scheduled backup:', error);
await say(
`Failed to create backup for environment *${environmentId}*. Please try again.`
);
}
});
// Acknowledge scheduling in Slack immediately
say(
`Backup for environment *${environmentId}* has been scheduled at *${time}* with tag *${tag}*.`
);
});
app.command('/backup_all_sites', async ({ command, ack, say }) => {
await ack();
const tag = command.text || 'default-backup-tag'; // Optionally take a tag from the command or use a default
// Fetch all sites for the company
const sitesResponse = await getAllSites(); // This function is already defined in your code
if (sitesResponse && sitesResponse.company && sitesResponse.company.sites) {
const sites = sitesResponse.company.sites;
// Iterate through each site and retrieve the environment ID
for (const site of sites) {
const envResponse = await getEnvironmentId(site.id); // Fetch the environment ID of the site
if (envResponse && envResponse.site.environments) {
const environments = envResponse.site.environments;
// Create a backup for each environment of the site
for (const environment of environments) {
try {
await addManualBackup(environment.id, tag);
await say(
`Backup successfully created for environment *${environment.display_name}* of site *${site.name}* with tag *${tag}*.`
);
} catch (error) {
console.error(
`Error creating backup for site ${site.name}:`,
error
);
say(
`Failed to create backup for environment *${environment.display_name}* of site *${site.name}*.`
);
}
}
} else {
say(`No environments found for site *${site.name}*.`);
}
}
} else {
say('No sites found for your company.');
}
});
app.command('/backup_multiple_envs', async ({ command, ack, say }) => {
await ack();
const inputText = command.text;
const [tag, ...envIds] = inputText.split(' '); // The first part of the command is the tag, and the rest are environment IDs
if (!envIds.length) {
say('Please provide at least one environment ID.');
return;
}
// Loop through the environment IDs and create a backup for each
for (const envId of envIds) {
try {
await addManualBackup(envId, tag);
await say(
`Backup successfully created for environment ID *${envId}* with tag *${tag}*.`
);
} catch (error) {
console.error(
`Error creating backup for environment ID ${envId}:`,
error
);
say(`Failed to create backup for environment ID *${envId}*.`);
}
}
});
app.command('/schedule_backup_all_sites', async ({ command, ack, say }) => {
await ack();
const [tag, time] = command.text.split(' '); // Get the backup tag and the time
const timeRegex = /^([01]\d|2[0-3]):([0-5]\d)$/; // Validate time format (24-hour HH:MM)
if (!time || !timeRegex.test(time)) {
say('Invalid time format. Please use HH:MM (24-hour format).');
return;
}
const [hour, minute] = time.split(':').map(Number);
// Schedule the backup
const date = new Date();
date.setHours(hour, minute, 0, 0);
schedule.scheduleJob(date, async () => {
const sitesResponse = await getAllSites();
if (sitesResponse && sitesResponse.company && sitesResponse.company.sites) {
const sites = sitesResponse.company.sites;
for (const site of sites) {
const envResponse = await getEnvironmentId(site.id);
if (envResponse && envResponse.site.environments) {
const environments = envResponse.site.environments;
for (const environment of environments) {
try {
await addManualBackup(environment.id, tag);
await say(
`Scheduled backup successfully created for environment *${environment.display_name}* of site *${site.name}* with tag *${tag}*.`
);
} catch (error) {
console.error(
`Error creating backup for site ${site.name}:`,
error
);
say(
`Failed to create scheduled backup for environment *${environment.display_name}* of site *${site.name}*.`
);
}
}
} else {
say(`No environments found for site *${site.name}*.`);
}
}
} else {
say('No sites found for your company.');
}
});
// Respond immediately that scheduling is done
say(`Backup for all sites scheduled at *${time}* with tag *${tag}*.`);
});
(async () => {
// Start your app
await app.start(process.env.PORT || 3000);
console.log(
`⚡️ Kinsta Bot app is running on port ${process.env.PORT || 3000}!`
);
})();