Skip to content

Commit 7700638

Browse files
authored
Custom prefix support, add Valheim support and QoL update (#64)
- Add Custom prefix support - Add shared utilities between main code base and configuration package - Add Valheim config pack - Bump version to 1.3.0
1 parent 1fe7861 commit 7700638

30 files changed

+218
-41
lines changed

lambda/discord_command_processer/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Context } from 'aws-lambda';
22
import * as AWS from 'aws-sdk';
33
import axios from 'axios';
4+
import { getFullDiscordCommand } from '../shared_util';
45

56
const InstanceIds = [process.env.INSTANCE_ID!];
67
const ec2_instance_region = process.env.EC2_REGION;
@@ -20,7 +21,7 @@ exports.handler = async (event: any, context: Context) => {
2021
const commandName = body.data.name;
2122
console.log('commandName: ', commandName);
2223

23-
if (commandName == 'mc_start') {
24+
if (commandName == getFullDiscordCommand('start')) {
2425
try {
2526
const result = await ec2.startInstances({ InstanceIds }).promise();
2627
console.log('startInstances succeed, result: \n', result);
@@ -34,7 +35,7 @@ exports.handler = async (event: any, context: Context) => {
3435
}
3536
}
3637

37-
if (commandName == 'mc_stop') {
38+
if (commandName == getFullDiscordCommand('stop')) {
3839
try {
3940
const result = await ec2.stopInstances({ InstanceIds }).promise();
4041
console.log('stopInstance suceeed, result: \n', result);
@@ -47,7 +48,7 @@ exports.handler = async (event: any, context: Context) => {
4748
}
4849
}
4950

50-
if (commandName == 'mc_restart') {
51+
if (commandName == getFullDiscordCommand('restart')) {
5152
try {
5253
const result = await sendCommands(['sudo systemctl restart minecloud']);
5354
console.log('mc_restart result: ', result);
@@ -60,7 +61,7 @@ exports.handler = async (event: any, context: Context) => {
6061
}
6162
}
6263

63-
if (commandName == 'mc_backup') {
64+
if (commandName == getFullDiscordCommand('backup')) {
6465
try {
6566
const result = await sendCommands([
6667
'cd /opt/minecloud/',
@@ -76,7 +77,7 @@ exports.handler = async (event: any, context: Context) => {
7677
}
7778
}
7879

79-
if (commandName == 'mc_backup_download') {
80+
if (commandName == getFullDiscordCommand('backup_download')) {
8081
const s3 = new AWS.S3({ signatureVersion: 'v4' });
8182

8283
const bucketName: string = process.env.BACKUP_BUCKET_NAME as string;

lambda/discord_commands_register/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@ import {
44
Context
55
} from 'aws-lambda';
66
import axios from 'axios';
7+
import { getFullDiscordCommand } from '../shared_util';
78

89
// The deployment will fail if exception was thrown.
910
exports.handler = async (event: CdkCustomResourceEvent, context: Context) => {
1011
if (event.RequestType !== 'Delete') {
1112
const apiEndpoint = `https://discord.com/api/v10/applications/${process.env.APP_ID}/commands`;
12-
await registerCommand('mc_start', 'Start the server', apiEndpoint);
13-
await registerCommand('mc_stop', 'Stop the server', apiEndpoint);
13+
await registerCommand(getFullDiscordCommand('start'), 'Start the server', apiEndpoint);
14+
await registerCommand(getFullDiscordCommand('stop'), 'Stop the server', apiEndpoint);
1415
await registerCommand(
15-
'mc_restart',
16+
getFullDiscordCommand('restart'),
1617
'Restart the server system service',
1718
apiEndpoint
1819
);
1920
await registerCommand(
20-
'mc_backup',
21+
getFullDiscordCommand('backup'),
2122
'Stop the server and make a backup',
2223
apiEndpoint
2324
);
2425
await registerCommand(
25-
'mc_backup_download',
26+
getFullDiscordCommand('backup_download'),
2627
'Get the latest backup',
2728
apiEndpoint
2829
);
@@ -64,4 +65,4 @@ async function registerCommand(
6465
console.log('register request: ', request);
6566
const response = await axios(request);
6667
console.log('register response: ', response.data);
67-
}
68+
}

lambda/shared_util.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { DISCORD_COMMAND_PREFIX } from "../minecloud_configs/MineCloud-Configs";
2+
3+
export function getFullDiscordCommand(command: string){
4+
return `${DISCORD_COMMAND_PREFIX}_${command}`
5+
}

lib/instance-init.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,12 @@ import {
1919
MINECLOUD_BASE_DIR,
2020
MINECLOUD_SERVER_DIR
2121
} from './const/minecloud-dir';
22+
import { setUpEnviromentVariable, setUpShellScript } from '../shared_lib/minecloud-utilities';
2223

2324
const MINECLOUD_USER = 'minecloud';
2425
// Not the same name since cfn-init can't figure it out for some reason
2526
const MINECLOUD_GROUP = 'minecloud-group';
2627

27-
function setUpShellScript(
28-
targetDir: string,
29-
targetFileName: string,
30-
localFilePath: string
31-
) {
32-
return [
33-
InitFile.fromFileInline(`${targetDir}/${targetFileName}`, localFilePath),
34-
InitCommand.shellCommand(`sudo chmod +x ${targetFileName}`, {
35-
cwd: targetDir
36-
}),
37-
// To convert Windows's EOL to Linux
38-
InitCommand.shellCommand(`sed -i 's/\r//' ${targetFileName}`, {
39-
cwd: targetDir
40-
})
41-
];
42-
}
43-
44-
function setUpEnviromentVariable(name: string, value: string) {
45-
return [
46-
InitCommand.shellCommand(`echo '${name}=${value}' >> /etc/environment`)
47-
];
48-
}
4928

5029
export function getInitConfig(backupBucketName: string) {
5130
return CloudFormationInit.fromConfigSets({

minecloud-version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.3
1+
1.3.0

minecloud_configs/MineCloud-Configs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// ------------- CloudFormation ------------- //
22
export const STACK_NAME = 'MinecraftExample';
33

4+
export const DISCORD_COMMAND_PREFIX = 'mc';
5+
46
// -------------- Server Executable ------------- //
57
// If set to true, /minecloud_configs/server/server.zip will be deployed
68
export const DEPLOY_LOCAL_SERVER_EXECUTABLE = false;

minecloud_configs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ The folders hierarchy should look like this after installation:
44
```
55
- /MineCloud-x.x.x
66
- /minecloud_configs
7+
- config-pack-info.json
78
- MineCloud-Configs.ts
89
- /server
910
- ...
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
function get_current_connection_count()
22
{
3-
# Check for TCP connection on port 25565
43
local mcCons=$(netstat -anp | grep :25565 | grep ESTABLISHED | wc -l)
54
echo "$mcCons"
65
}

minecloud_configs/advanced_configs/get_connection_count_udp_template.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
function get_current_connection_count()
22
{
3-
# If there's no udp package recieved on port 34197 for 20 second,
4-
# return an empty string, else return the package info
53
local mcCons=$(sudo timeout 300 tcpdump -c 1 udp port 25565 2>/dev/null)
64

7-
# Return 0 if "mcCons" is an empty string, else 1
85
if [[ -z $mcCons ]]; then
96
echo 0
107
else
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"name": "example-config-pack",
3-
"MineCloudVersion": "1.2.3"
2+
"name": "Minecraft_Example",
3+
"MineCloudVersion": "1.3.0"
44
}

0 commit comments

Comments
 (0)