Skip to content

Commit

Permalink
1.2.2 Update (#57)
Browse files Browse the repository at this point in the history
- Add support to specify backup directories
- Fixes incorrect lambda packing
- Add MineCloud Configuration Package support
  • Loading branch information
314pies committed May 28, 2023
1 parent 4bd7017 commit 43f9821
Show file tree
Hide file tree
Showing 34 changed files with 296 additions and 24 deletions.
3 changes: 1 addition & 2 deletions lambda/discord_interactions_endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Context } from 'aws-lambda';
import { sign } from 'tweetnacl';
import { Lambda } from 'aws-sdk';
import { STACK_PREFIX } from '../../lib/mine-cloud-stack';

exports.handler = async (event: any, context: Context) => {
const PUBLIC_KEY = process.env.PUBLIC_KEY!;
Expand Down Expand Up @@ -37,7 +36,7 @@ exports.handler = async (event: any, context: Context) => {
const lambda = new Lambda();
const res = await lambda
.invokeAsync({
FunctionName: `${STACK_PREFIX}_discord_command_processor_lambda`,
FunctionName: process.env.DISCORD_COMMAND_PROCESSOR_FUNCTION_NAME!,
InvokeArgs: JSON.stringify(event)
})
.promise();
Expand Down
34 changes: 18 additions & 16 deletions lib/discord-interactions-endpoint-construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import path = require('path');
import { PolicyStatement, Policy } from 'aws-cdk-lib/aws-iam';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Duration } from 'aws-cdk-lib';
import { DISCORD_APP_ID } from '../minecloud_configs/MineCloud-Configs';
import { Bucket } from 'aws-cdk-lib/aws-s3';

export interface DiscordInteractionsEndpointConstructProps {
instanceId: string;
ec2Region: string;
discordAppId: string;
discordPublicKey: string;
backUpBucket: Bucket;
}
Expand All @@ -31,44 +31,46 @@ export class DiscordInteractionsEndpointConstruct extends Construct {
) {
super(scope, id);

this.discordInteractionsEndpoint = new NodejsFunction(
this.discordCommandProcesser = new NodejsFunction(
this,
`${STACK_PREFIX}_discord_interactions_endpoint_lambda`,
`${STACK_PREFIX}_discord_command_processor_lambda`,
{
functionName: `${STACK_PREFIX}_discord_interactions_endpoint_lambda`,
functionName: `${STACK_PREFIX}_discord_command_processor_lambda`,
runtime: Runtime.NODEJS_18_X,
handler: 'index.handler',
entry: path.join(
__dirname,
'/../lambda/discord_interactions_endpoint/index.ts'
'/../lambda/discord_command_processer/index.ts'
),
environment: {
PUBLIC_KEY: props.discordPublicKey
INSTANCE_ID: props.instanceId,
EC2_REGION: props.ec2Region,
APP_ID: props.discordAppId,
BACKUP_BUCKET_NAME: props.backUpBucket.bucketName
},
memorySize: 1024 // To reduce cold start time
timeout: Duration.seconds(15)
}
);

this.discordCommandProcesser = new NodejsFunction(
this.discordInteractionsEndpoint = new NodejsFunction(
this,
`${STACK_PREFIX}_discord_command_processor_lambda`,
`${STACK_PREFIX}_discord_interactions_endpoint_lambda`,
{
functionName: `${STACK_PREFIX}_discord_command_processor_lambda`,
functionName: `${STACK_PREFIX}_discord_interactions_endpoint_lambda`,
runtime: Runtime.NODEJS_18_X,
handler: 'index.handler',
entry: path.join(
__dirname,
'/../lambda/discord_command_processer/index.ts'
'/../lambda/discord_interactions_endpoint/index.ts'
),
environment: {
INSTANCE_ID: props.instanceId,
EC2_REGION: props.ec2Region,
APP_ID: DISCORD_APP_ID,
BACKUP_BUCKET_NAME: props.backUpBucket.bucketName
PUBLIC_KEY: props.discordPublicKey,
DISCORD_COMMAND_PROCESSOR_FUNCTION_NAME: this.discordCommandProcesser.functionName
},
timeout: Duration.seconds(15)
memorySize: 1024 // To reduce cold start time
}
);

this.discordCommandProcesser.grantInvoke(this.discordInteractionsEndpoint);
props.backUpBucket.grantReadWrite(this.discordCommandProcesser);

Expand Down
8 changes: 8 additions & 0 deletions lib/instance-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ export function getInitConfig(backupBucketName: string) {
InitCommand.shellCommand('systemctl start minecloud.service')
]),
setupBackupScripts: new InitConfig([
InitFile.fromFileInline(
`${MINECLOUD_BASE_DIR}/backup-folders.txt`,
'minecloud_configs/advanced_configs/backup-folders.txt'
),
// To convert Windows's EOL to Linux
InitCommand.shellCommand(`sed -i 's/\r//' backup-folders.txt`, {
cwd: MINECLOUD_BASE_DIR
}),
...setUpEnviromentVariable('BACKUP_BUCKET_NAME', backupBucketName),
...setUpShellScript(
MINECLOUD_BASE_DIR,
Expand Down
1 change: 1 addition & 0 deletions lib/mine-cloud-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class MineCloud extends Stack {
{
instanceId: this.ec2Instance.instanceId,
ec2Region: this.region,
discordAppId: DISCORD_APP_ID,
discordPublicKey: DISCORD_PUBLIC_KEY,
backUpBucket: this.backupBucket
}
Expand Down
8 changes: 5 additions & 3 deletions minecloud_configs/MineCloud-Configs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// MineCloud Version: 1.2.2

// ---------------- Required -------------------- //
export const AWS_ACCOUNT_ID = '';
export const AWS_REGION = '';
Expand All @@ -8,15 +10,15 @@ export const DISCORD_BOT_TOKEN = '';
export const DISCORD_CHANNEL_WEB_HOOK = '';

// ------------- CloudFormation ------------- //
export const STACK_NAME = 'Minecraft';
export const STACK_NAME = 'Minecraft';

// -------------- Server Executable ------------- //
// If set to true, /minecloud_configs/server/server.zip will be deployed
export const DEPLOY_LOCAL_SERVER_EXECUTABLE = false;
export const DEPLOY_LOCAL_SERVER_EXECUTABLE = false;

// ----------------EC2 Machine Settings-------------------- //
// EC2 max price per hours, in dollars
export const MAX_PRICE = 0.1;
export const MAX_PRICE = 0.1;
// EC2 instance type, refer to https://aws.amazon.com/ec2/instance-types/ for more info
export const EC2_INSTANCE_TYPE = 't2.large';
// Disk size, in GB
Expand Down
19 changes: 19 additions & 0 deletions minecloud_configs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Please download and unzip MineCloud Configuration Package to this folder.
The folders hierarchy should look like this:
```
- /MineCloud_root_folder
- /minecloud_configs
- MineCloud-Configs.ts
- /server
- ...
- /advanced_configs
- ...
- /bin
- /lambda
- /lib
- /server_init_assets
- ...
```
The pairing MineCloud version of the Configuration Package can be found in `MineCloud-Configs.ts`.

An out-of-the-box Minecraft server example, `minecraft_example_config_pack.zip`, can also be found in this folder.
1 change: 1 addition & 0 deletions minecloud_configs/advanced_configs/backup-folders.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/opt/minecloud/server
Binary file not shown.
3 changes: 3 additions & 0 deletions minecloud_configuration_packages/Minecraft/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# MineCloud: Minecraft Configuration Package

// To-do
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// MineCloud Version: 1.2.2

// ---------------- Required -------------------- //
export const AWS_ACCOUNT_ID = '';
export const AWS_REGION = '';

export const DISCORD_APP_ID = '';
export const DISCORD_PUBLIC_KEY = '';
export const DISCORD_BOT_TOKEN = '';
export const DISCORD_CHANNEL_WEB_HOOK = '';

// ------------- CloudFormation ------------- //
export const STACK_NAME = 'Minecraft';

// -------------- Server Executable ------------- //
// If set to true, /minecloud_configs/server/server.zip will be deployed
export const DEPLOY_LOCAL_SERVER_EXECUTABLE = false;

// ----------------EC2 Machine Settings-------------------- //
// EC2 max price per hours, in dollars
export const MAX_PRICE = 0.1;
// EC2 instance type, refer to https://aws.amazon.com/ec2/instance-types/ for more info
export const EC2_INSTANCE_TYPE = 't2.large';
// Disk size, in GB
export const EC2_VOLUME = 16;
// Init time out, in minutes
export const EC2_INIT_TIMEOUT = 15;

// --------------- Backup Settings------------------ //
// At most how many backups
export const MAX_BACKUP_COUNT = 3;
export const BACKUP_INTERVAL_IN_SECONDS = 10800;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/opt/minecloud/server
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
InitCommand,
InitConfig,
InitFile,
InitPackage
} from 'aws-cdk-lib/aws-ec2';
import { MINECLOUD_SERVER_DIR } from '../../lib/const/minecloud-dir';
import { DEPLOY_LOCAL_SERVER_EXECUTABLE } from '../MineCloud-Configs';
import { MINECRAFT_SERVER_DOWNLOAD_URL } from './minecraft-server-download-url';

export const CUSTOM_INIT_CONFIG: InitConfig = getCustomInitConfig();

function getCustomInitConfig(): InitConfig {
let configs: (InitPackage | InitCommand | InitFile)[] = [
// Install an Amazon Java package using yum
InitPackage.yum('java-17-amazon-corretto-headless'),
InitCommand.shellCommand("echo 'eula=true' > eula.txt", {
cwd: MINECLOUD_SERVER_DIR
})
];

if (!DEPLOY_LOCAL_SERVER_EXECUTABLE) {
configs.push(
InitFile.fromUrl(
`${MINECLOUD_SERVER_DIR}/server.jar`,
MINECRAFT_SERVER_DOWNLOAD_URL
)
);
}

return new InitConfig(configs);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function get_current_connection_count()
{
local mcCons=$(netstat -anp | grep :25565 | grep ESTABLISHED | wc -l)
echo "$mcCons"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function get_current_connection_count()
{
local mcCons=$(sudo timeout 300 tcpdump -c 1 udp port 25565 2>/dev/null)

if [[ -z $mcCons ]]; then
echo 0
else
echo 1
fi
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MINECRAFT_SERVER_DOWNLOAD_URL =
'https://piston-data.mojang.com/v1/objects/8f3112a1049751cc472ec13e397eade5336ca7ae/server.jar';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Set to true so to avoid roll back when EC2 init fail, mainly used for debugging.
export const IGNORE_FAILURE_ON_INSTANCE_INIT = false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Peer, Port } from 'aws-cdk-lib/aws-ec2';

export const PORT_CONFIGS = [
{
peer: Peer.anyIpv4(),
port: Port.tcp(25565),
description: 'Allows Minecraft connection'
}
];
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

echo "Starting Minecraft server"
# You can adjust your server start up command here
/usr/bin/env java -Xmx6144M -Xms1024M -jar server.jar nogui
echo "Minecraft server stop"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

/usr/bin/screen -S mc_server -X stuff 'say Server shutting down (closing by the system service)^M'
/usr/bin/screen -S mc_server -X stuff 'save-all^M'
/bin/sleep 10
/usr/bin/screen -S mc_server -X stuff 'stop^M'
/opt/minecloud/send_discord_message_to_webhook.sh "Shutting Minecraft server down..."
/bin/sleep 10
/opt/minecloud/send_discord_message_to_webhook.sh "(Minecraft server shut down)"
Binary file not shown.
3 changes: 3 additions & 0 deletions minecloud_configuration_packages/Terraria/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# MineCloud: Terraria Configuration Package

// To-do
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// MineCloud Version: 1.2.2

// ---------------- Required -------------------- //
export const AWS_ACCOUNT_ID = '';
export const AWS_REGION = '';

export const DISCORD_APP_ID = '';
export const DISCORD_PUBLIC_KEY = '';
export const DISCORD_BOT_TOKEN = '';
export const DISCORD_CHANNEL_WEB_HOOK = '';

// ------------- CloudFormation ------------- //
export const STACK_NAME = 'Terraria';

// -------------- Server Executable ------------- //
// If set to true, /minecloud_configs/server/server.zip will be deployed
export const DEPLOY_LOCAL_SERVER_EXECUTABLE = false;

// ----------------EC2 Machine Settings-------------------- //
// EC2 max price per hours, in dollars
export const MAX_PRICE = 0.1;
// EC2 instance type, refer to https://aws.amazon.com/ec2/instance-types/ for more info
export const EC2_INSTANCE_TYPE = 't2.large';
// Disk size, in GB
export const EC2_VOLUME = 16;
// Init time out, in minutes
export const EC2_INIT_TIMEOUT = 15;

// --------------- Backup Settings------------------ //
// At most how many backups
export const MAX_BACKUP_COUNT = 3;
export const BACKUP_INTERVAL_IN_SECONDS = 10800;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/opt/minecloud/server
/opt/minecloud/TerrariaSaves
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
InitCommand,
InitConfig,
InitFile,
InitPackage
} from 'aws-cdk-lib/aws-ec2';
import { MINECLOUD_SERVER_DIR } from '../../lib/const/minecloud-dir';
import { DEPLOY_LOCAL_SERVER_EXECUTABLE } from '../MineCloud-Configs';

const TERRARIA_SERVER_DOWNLOAD_URL =
'https://terraria.org/api/download/pc-dedicated-server/terraria-server-1449.zip';

export const CUSTOM_INIT_CONFIG: InitConfig = getCustomInitConfig();

function getCustomInitConfig(): InitConfig {

if (!DEPLOY_LOCAL_SERVER_EXECUTABLE) {
// Download and setup Terraria server
return new InitConfig([
InitFile.fromUrl(
`${MINECLOUD_SERVER_DIR}/terraria-server-1449.zip`,
TERRARIA_SERVER_DOWNLOAD_URL
),
InitCommand.shellCommand(`sudo unzip terraria-server-1449.zip`, {
cwd: MINECLOUD_SERVER_DIR
}),
InitCommand.shellCommand(`sudo rm -f terraria-server-1449.zip`, {
cwd: MINECLOUD_SERVER_DIR
}),
InitCommand.shellCommand(`sudo mv 1449/Linux/* .`, {
cwd: MINECLOUD_SERVER_DIR
}),
InitCommand.shellCommand(`sudo rm -rf 1449`, {
cwd: MINECLOUD_SERVER_DIR
}),
InitCommand.shellCommand(`sudo chmod +x TerrariaServer.bin.x86*`, {
cwd: MINECLOUD_SERVER_DIR
}),
InitFile.fromFileInline(`${MINECLOUD_SERVER_DIR}/serverconfig.txt`,'minecloud_configs/advanced_configs/serverconfig.txt')
]);
} else {
return new InitConfig([
InitCommand.shellCommand(`sudo chmod +x TerrariaServer.bin.x86*`, {
cwd: MINECLOUD_SERVER_DIR
})
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function get_current_connection_count()
{
# Check how many TCP connections on port 7777
local mcCons=$(netstat -anp | grep :7777 | grep ESTABLISHED | wc -l)
echo "$mcCons"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Set to true so to avoid roll back when EC2 init fail, mainly used for debugging.
export const IGNORE_FAILURE_ON_INSTANCE_INIT = false;
Loading

0 comments on commit 43f9821

Please sign in to comment.