Skip to content

Commit

Permalink
feature: create dedicated dev environment (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhenkel92 authored May 5, 2024
1 parent bda5f99 commit 01947c0
Show file tree
Hide file tree
Showing 19 changed files with 62 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
env: NODE_ENV
terraform_path: TERRAFORM_PATH
mysql:
database: MYSQL_DATABASE
Expand All @@ -20,3 +21,4 @@ cloudGame:
ansibleBranch: CLOUD_GAME_ANSIBLE_BRANCH
apiUrl: CLOUD_GAME_API_URL
apiToken: CLOUD_GAME_API_TOKEN
backupS3Bucket: CLOUD_GAME_BACKUP_S3_BUCKET
4 changes: 3 additions & 1 deletion async-server-provisioner/config/default.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
env: dev
terraform_path: ./terraform/02-game-server
mysql:
database: strapi
Expand All @@ -19,6 +20,7 @@ datadog:
api_key: placeholder

cloudGame:
ansibleBranch: main
ansibleBranch: create-dedicated-dev-env
apiUrl: http://host.docker.internal:1337
apiToken: placeholder
backupS3Bucket: cloud-game-dev
10 changes: 8 additions & 2 deletions async-server-provisioner/src/entities/GameDeployment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { CloudInstance, cloudInstanceFactory } from './CloudInstance';
import { GameInstance, gameInstanceFactory } from './GameInstance';
import * as config from 'config';

const env = config.get<string>('env');

export enum GameDeploymentStatus {
STARTING = 'STARTING',
Expand All @@ -15,8 +18,11 @@ export interface GameDeployment {
}

export function generateTFWorkspaceName(deploy: GameDeployment): string {
const rawString = `${deploy.id}-${deploy.gameInstance.name}`;
return rawString.replace(/([\s-_])/g, '-').toLowerCase();
const rawString = `${env}-${deploy.gameInstance.id}-${deploy.gameInstance.name}`;
return rawString
.replace(/[^a-zA-Z0-9-]/g, '')
.toLowerCase()
.slice(0, 50);
}

export function gameDeploymentFactory(consumerUid: string, row: any): GameDeployment {
Expand Down
2 changes: 2 additions & 0 deletions async-server-provisioner/src/entities/MinecraftTFConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface MinecraftTFConfig {
port: string;
description: string;
}[];
backup_s3_bucket: string;
backup_paths: {
path: string;
}[];
Expand Down Expand Up @@ -52,6 +53,7 @@ export function createMinecraftTFConfigFromGameConfig(mfConfig: GameDeployment):
port: `${port.port}`,
description: port.name,
})),
backup_s3_bucket: config.get('cloudGame.backupS3Bucket'),
backup_paths: mfConfig.gameInstance.backupPaths.map((p) => ({
path: p.path,
})),
Expand Down
2 changes: 1 addition & 1 deletion backend/.strapi-updater.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"latest": "4.24.1",
"lastUpdateCheck": 1714740073758,
"lastUpdateCheck": 1714910701412,
"lastNotification": 1714740179673
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- include_tasks: ../../../generic/bucket.yaml
- name: List all backups
amazon.aws.aws_s3:
bucket: cloud-game
bucket: "{{ server.backup_s3_bucket }}"
mode: list
prefix: "{{ backup_folder_path.stdout }}"
register: all_backups
Expand All @@ -13,7 +13,7 @@
msg: "{{ backup_path }}"
- name: Download backup
amazon.aws.s3_object:
bucket: cloud-game
bucket: "{{ server.backup_s3_bucket }}"
object: "{{ backup_path }}"
dest: /root/backup.tar.gz
mode: get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ game_instance:
id: placeholder
server:
docker_image: placeholder
backup_s3_bucket: placeholder
backup_paths:
- path: placeholder
datadog:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
shell: tar -czf /root/backup.tar.gz -C /mnt/backup .
- name: Upload Backup File to S3
amazon.aws.s3_object:
bucket: cloud-game
bucket: "{{ server.backup_s3_bucket }}"
object: "{{ backup_folder_path.stdout }}{{ backup_name.stdout }}"
src: /root/backup.tar.gz
mode: put
2 changes: 2 additions & 0 deletions infrastructure/ansible/roles/shutdown-server/vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
---
server:
backup_s3_bucket: placeholder
game_instance:
id: placeholder
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
"s3:ListBucket",
"s3:GetObjectTagging"
],
"Resource": ["arn:aws:s3:::cloud-game/*", "arn:aws:s3:::cloud-game"]
"Resource": [
"arn:aws:s3:::cloud-game/*",
"arn:aws:s3:::cloud-game",
"arn:aws:s3:::cloud-game-dev",
"arn:aws:s3:::cloud-game-dev/*"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module "cloud_game_server" {
firewall_ids = [module.firewall.id]
volume = var.cloud_game_server.volume
network = {
attach = true
subnet_id = var.subnet_id
attach = true
network_id = var.network_id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ variable "name" {
type = string
}

variable "subnet_id" {
variable "network_id" {
type = string
}

Expand Down
2 changes: 1 addition & 1 deletion infrastructure/terraform/01-game-cloud-platform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module "cloud_game_server" {
source = "./cloud-game-server"

name = var.name
subnet_id = module.network.subnet_ids[0]
network_id = module.network.net_id
location = var.location
ssh_keys = module.ssh_keys.ids
cloud_game_server = var.cloud_game_server
Expand Down
1 change: 1 addition & 0 deletions infrastructure/terraform/02-game-server/files/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ game_instance:
# game server start varibales
server:
docker_image: ${game_server_image}
backup_s3_bucket: ${backup_s3_bucket}
backup_paths:
%{ for p in backup_paths }
- path: ${p.path}
Expand Down
44 changes: 16 additions & 28 deletions infrastructure/terraform/02-game-server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ data "terraform_remote_state" "aws_platform" {
}
}

data "terraform_remote_state" "game_cloud" {
backend = "s3"
config = {
bucket = "cloud-game-tf-states"
key = "terraform/01-game-cloud-platform"
region = "eu-central-1"
}
data "hcloud_ssh_keys" "all_keys" {
}

data "hcloud_network" "cloud_game" {
name = "cloud-game"
}

data "hcloud_image" "latest_game_server_image" {
with_selector = "application=basic-gameserver"
most_recent = true
}

module "firewall" {
Expand All @@ -32,22 +35,6 @@ module "firewall" {
description = rule.description
source_ips = ["0.0.0.0/0"]
}]
# rules = [
# {
# proto = "tcp"
# port = "22"
# },
# {
# proto = "tcp"
# port = "25565"
# source_ips = ["0.0.0.0/0"]
# },
# {
# proto = "tcp"
# port = "8080"
# source_ips = ["0.0.0.0/0"]
# }
# ]
}

module "game_server" {
Expand All @@ -57,15 +44,15 @@ module "game_server" {
location = var.metadata.location

server_type = var.server.type
image = var.server.image
image = data.hcloud_image.latest_game_server_image.id
firewall_ids = [module.firewall.id]

ssh_keys = data.terraform_remote_state.game_cloud.outputs.ssh_key_ids
ssh_keys = data.hcloud_ssh_keys.all_keys.*.id
tags = local.tags

network = {
attach = true
subnet_id = data.terraform_remote_state.game_cloud.outputs.network.subnet_ids.game_server
attach = true
network_id = data.hcloud_network.cloud_game.id
}

user_data = {
Expand All @@ -79,7 +66,8 @@ module "game_server" {
ansible_branch = var.ansible_branch
game_instance_id = var.metadata.game_instance.id

backup_paths = [for path in var.server.backup_paths : { path = path.path }]
backup_s3_bucket = var.server.backup_s3_bucket
backup_paths = [for path in var.server.backup_paths : { path = path.path }]
}
}
}
1 change: 1 addition & 0 deletions infrastructure/terraform/02-game-server/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ variable "server" {
description = string
}))

backup_s3_bucket = string
backup_paths = list(object({
path = string
}))
Expand Down
11 changes: 6 additions & 5 deletions infrastructure/terraform/02-game-server/vars/test.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ metadata = {
}

server = {
type = "cx31"
image = "68074861"
docker_image = "cloudgame/minecraft:vanilla-1.18.2"
ports = []
backup_paths = [{ path = "isso" }]
type = "cx31"
image = "placeholder"
docker_image = "cloudgame/minecraft:vanilla-1.18.2"
ports = []
backup_s3_bucket = "cloud-game-dev"
backup_paths = [{ path = "isso" }]
}

datadog = {
Expand Down
4 changes: 2 additions & 2 deletions infrastructure/terraform/modules/server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ resource "hcloud_volume_attachment" "attach" {
resource "hcloud_server_network" "network_attach" {
count = var.network.attach ? 1 : 0

server_id = hcloud_server.node.id
subnet_id = var.network.subnet_id
server_id = hcloud_server.node.id
network_id = var.network.network_id
}
8 changes: 4 additions & 4 deletions infrastructure/terraform/modules/server/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ variable "volume" {

variable "network" {
type = object({
attach = bool
subnet_id = string
attach = bool
network_id = string
})
default = {
attach = false
subnet_id = ""
attach = false
network_id = ""
}
}

Expand Down

0 comments on commit 01947c0

Please sign in to comment.