Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #22 from cpolanec/support-minecraft-v17
Browse files Browse the repository at this point in the history
upgrade to Minecraft version 17
  • Loading branch information
cpolanec committed Aug 15, 2021
2 parents df4aee3 + c809888 commit 50a5ebd
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 17 deletions.
4 changes: 4 additions & 0 deletions lib/game-server-def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
export default interface GameServerDefinition {
name: string;
initSnapshot: string;
papermc?: {
version?: string;
build?: number;
};
}

/**
Expand Down
24 changes: 21 additions & 3 deletions lib/game-server-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as iam from '@aws-cdk/aws-iam';
import NetworkStack from './network-stack';
import AppParameters from './app-parameters';
import GameServerDefinition from './game-server-def';
import PaperMCApiClient from './papermc-api-client';

/**
* Definition of a nested CloudFormation stack that contains the AWS resources
Expand All @@ -16,10 +17,24 @@ class GameServerStack extends cdk.NestedStack {
// CONSTRUCTORS & INITIALIZATION
//---------------------------------------------------------------------------

constructor(
public static create = async (
scope: cdk.Construct,
definition: GameServerDefinition,
network: NetworkStack,
): Promise<GameServerStack> => {
const papermcInfo = definition.papermc || {};
const version = papermcInfo.version || '1.17.1';
const build = papermcInfo.build || await PaperMCApiClient.gatherLatestBuildNumber(version);
const downloadUrl = await PaperMCApiClient.createDownloadUrl(version, build);

return new GameServerStack(scope, definition, network, downloadUrl);
};

private constructor(
scope: cdk.Construct,
definition: GameServerDefinition,
network: NetworkStack,
downloadUrl: string,
) {
super(scope, definition.name);

Expand All @@ -32,8 +47,10 @@ class GameServerStack extends cdk.NestedStack {

const userData = ec2.UserData.forLinux();
userData.addCommands(
'rpm --import https://yum.corretto.aws/corretto.key',
'curl -L -o /etc/yum.repos.d/corretto.repo https://yum.corretto.aws/corretto.repo',
'yum upgrade --assumeyes',
'yum install --assumeyes java-11-amazon-corretto-headless',
'yum install --assumeyes java-16-amazon-corretto-devel',
);
userData.addCommands(
'mkdir /mnt/minecraft',
Expand Down Expand Up @@ -87,6 +104,7 @@ class GameServerStack extends cdk.NestedStack {
'chmod +x /mnt/minecraft/config-minecraft-props.sh',
'chown ec2-user:ec2-user /mnt/minecraft/config-minecraft-props.sh',
);

userData.addCommands(
'cat << EOF > /etc/systemd/system/minecraft.service',
'[Unit]',
Expand All @@ -99,7 +117,7 @@ class GameServerStack extends cdk.NestedStack {
'User=ec2-user',
'WorkingDirectory=/mnt/minecraft',
'ExecStartPre=/usr/bin/sudo /usr/bin/chown ec2-user:ec2-user /mnt/minecraft',
'ExecStartPre=/usr/bin/wget https://papermc.io/api/v2/projects/paper/versions/1.16.5/builds/585/downloads/paper-1.16.5-585.jar -O minecraft.jar',
`ExecStartPre=/usr/bin/wget ${downloadUrl} -O minecraft.jar`,
'ExecStartPre=/mnt/minecraft/config-minecraft-props.sh',
'ExecStart=/usr/bin/java -Xmx2G -Xmx2G -jar minecraft.jar --noconsole',
'Restart=always',
Expand Down
44 changes: 44 additions & 0 deletions lib/papermc-api-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable no-console */
import got from 'got';

class PaperMCApiClient {
//---------------------------------------------------------------------------
// CLASS ATTRIBUTES
//---------------------------------------------------------------------------

private static URL_PREFIX = 'https://papermc.io/api/v2/projects/paper';

//---------------------------------------------------------------------------
// CLASS FUNCTIONALITY
//---------------------------------------------------------------------------

public static async gatherLatestBuildNumber(version: string): Promise<number> {
let build: number;
const url = `${PaperMCApiClient.URL_PREFIX}/versions/${version}`;
try {
const response = await got.get(url);
const { builds } = JSON.parse(response.body) as Record<string, number[]>;
build = builds[builds.length - 1];
return build;
} catch (error) {
console.log(`failed to gather builds from ${url}`);
throw error;
}
}

// https://papermc.io/api/v2/projects/paper/versions/1.17.1/builds/170/downloads/paper-1.17.1-170.jar
public static async createDownloadUrl(version: string, build: number): Promise<string> {
const buildUrl = `${PaperMCApiClient.URL_PREFIX}/versions/${version}/builds/${build}`;
try {
const response = await got.get(buildUrl);
const { downloads } = JSON.parse(response.body) as Record<string, { application: unknown }>;
const application = downloads.application as { name: string };
return `${buildUrl}/downloads/${application.name}`;
} catch (error) {
console.log(`failed to create download URL from build info: ${buildUrl}`);
throw error;
}
}
}

export default PaperMCApiClient;
29 changes: 22 additions & 7 deletions lib/server-farm-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ export default class ServerFarmStack extends cdk.Stack {
//
// gather the Game Server definitions
//
const defaultValues: GameServerDefinition = {
name: '',
initSnapshot: '',
papermc: {
version: '1.17.1',
},
};
const definitions: GameServerDefinition[] = [];
const stat = fs.lstatSync(sourcePath);
if (stat.isFile()) {
const jsonString = fs.readFileSync(sourcePath).toString();
const serverDefFile = JSON.parse(jsonString) as GameServerDefinitionFile;
const serverDefFile = {
...defaultValues,
...JSON.parse(jsonString),
} as GameServerDefinitionFile;
definitions.push(...serverDefFile.definitions);
} else if (stat.isDirectory()) {
const files = fs.readdirSync(sourcePath);
Expand All @@ -68,13 +78,18 @@ export default class ServerFarmStack extends cdk.Stack {
//
this.gameServerStacks = [];
definitions.forEach((definition) => {
const stack = new GameServerStack(this, definition, network);
this.gameServerStacks.push(stack);
// const stack = new GameServerStack(this, definition, network);
GameServerStack.create(this, definition, network).then(
(stack) => {
this.gameServerStacks.push(stack);

// proper order of backup tasks is defined by resource dependencies
// (see GameBackups class for more information)
stack.node.addDependency(backups.onUpdates); // backup before stack 'Update'
backups.onDeletes.node.addDependency(stack); // backup before stack 'Delete'
// proper order of backup tasks is defined by resource dependencies
// (see GameBackups class for more information)
stack.node.addDependency(backups.onUpdates); // backup before stack 'Update'
backups.onDeletes.node.addDependency(stack); // backup before stack 'Delete'
},
() => { },
);
});
}
}
Loading

0 comments on commit 50a5ebd

Please sign in to comment.