Skip to content

Commit 37ad633

Browse files
authored
Move latest backup download feature to Discord commands processing lambda (#53)
1 parent c4dd723 commit 37ad633

File tree

5 files changed

+28
-41
lines changed

5 files changed

+28
-41
lines changed

lambda/discord_command_processer/index.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,28 @@ exports.handler = async (event: any, context: Context) => {
7777
}
7878

7979
if (commandName == 'mc_backup_download') {
80-
try {
81-
const result = await sendCommands([
82-
'cd /opt/minecloud/',
83-
'sudo ./get_latest_server_backup.sh'
84-
]);
85-
console.log('mc_backup_download result: ', result);
86-
await sendDeferredResponse('OK, contacting server instance!');
87-
} catch (err) {
88-
console.error(`mc_backup error: \n`, err);
80+
const s3 = new AWS.S3({ signatureVersion: 'v4' });
81+
82+
const bucketName: string = process.env.BACKUP_BUCKET_NAME as string;
83+
const s3Objects = await s3.listObjectsV2({ Bucket: bucketName }).promise();
84+
85+
if (s3Objects.Contents && s3Objects.Contents.length > 0) {
86+
let s3ObjectKeys = s3Objects.Contents.map((x) => x.Key);
87+
s3ObjectKeys = s3ObjectKeys.sort((a, b) => (a! > b! ? -1 : 1));
88+
89+
const latestBackupKey = s3ObjectKeys[0];
90+
91+
const params = {
92+
Bucket: bucketName,
93+
Key: latestBackupKey,
94+
Expires: 3600
95+
};
96+
const preSignedUrl = await s3.getSignedUrl('getObject', params);
8997
await sendDeferredResponse(
90-
getAWSErrorMessageTemplate('getting latest backup', err)
98+
`Here's the download link for ${latestBackupKey}:\n ${preSignedUrl}`
9199
);
100+
} else {
101+
await sendDeferredResponse('Hmm... looks like there is no backup yet~');
92102
}
93103
}
94104

lib/discord-interactions-endpoint-construct.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import { PolicyStatement, Policy } from 'aws-cdk-lib/aws-iam';
1010
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
1111
import { Duration } from 'aws-cdk-lib';
1212
import { DISCORD_APP_ID } from '../minecloud_configs/MineCloud-Configs';
13+
import { Bucket } from 'aws-cdk-lib/aws-s3';
1314

1415
export interface DiscordInteractionsEndpointConstructProps {
1516
instanceId: string;
1617
ec2Region: string;
1718
discordPublicKey: string;
19+
backUpBucket: Bucket;
1820
}
1921

2022
export class DiscordInteractionsEndpointConstruct extends Construct {
@@ -61,12 +63,14 @@ export class DiscordInteractionsEndpointConstruct extends Construct {
6163
environment: {
6264
INSTANCE_ID: props.instanceId,
6365
EC2_REGION: props.ec2Region,
64-
APP_ID: DISCORD_APP_ID
66+
APP_ID: DISCORD_APP_ID,
67+
BACKUP_BUCKET_NAME: props.backUpBucket.bucketName
6568
},
6669
timeout: Duration.seconds(15)
6770
}
6871
);
6972
this.discordCommandProcesser.grantInvoke(this.discordInteractionsEndpoint);
73+
props.backUpBucket.grantReadWrite(this.discordCommandProcesser);
7074

7175
const ec2Policy = new PolicyStatement({
7276
actions: ['ec2:*'],

lib/instance-init.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ export function getInitConfig(backupBucketName: string) {
5858
'setupDiscordMessaging',
5959
'setupMineCloudService',
6060
'setupBackupScripts',
61-
'setupAutoShutdown',
62-
'setupGetLatestBackupScript'
61+
'setupAutoShutdown'
6362
]
6463
},
6564
configs: {
@@ -170,13 +169,6 @@ export function getInitConfig(backupBucketName: string) {
170169
`(crontab -l 2>/dev/null; echo "*/30 * * * * ${MINECLOUD_BASE_DIR}/check_user_conn.sh") | crontab -`
171170
)
172171
]),
173-
setupGetLatestBackupScript: new InitConfig([
174-
...setUpShellScript(
175-
MINECLOUD_BASE_DIR,
176-
'get_latest_server_backup.sh',
177-
'server_init_assets/get_latest_server_backup.sh'
178-
)
179-
]),
180172
noAction: new InitConfig([])
181173
}
182174
});

lib/mine-cloud-stack.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ export class MineCloud extends Stack {
8383
{
8484
instanceId: this.ec2Instance.instanceId,
8585
ec2Region: this.region,
86-
discordPublicKey: DISCORD_PUBLIC_KEY
86+
discordPublicKey: DISCORD_PUBLIC_KEY,
87+
backUpBucket: this.backupBucket
8788
}
8889
);
8990
this.discordInteractionsEndpointLambda.node.addDependency(this.ec2Instance);

server_init_assets/get_latest_server_backup.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)