Skip to content

Commit

Permalink
test is a bit broken
Browse files Browse the repository at this point in the history
  • Loading branch information
circlesabound committed Mar 25, 2024
1 parent e0c0012 commit ac65cdb
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ DISCORD_INTEGRATION=false
########

AGENT_WS_PORT=5463
LOG_LEVEL=debug
LOG_LEVEL=info

########
# Traefik reverse proxy
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.8'
services:
agent:
image: ghcr.io/circlesabound/fctrl/agent:latest
Expand Down
1 change: 0 additions & 1 deletion docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.8'
services:
agent:
image: fctrl/agent:latest
Expand Down
1 change: 0 additions & 1 deletion docker-compose.reverse-proxy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.8'
services:
reverse-proxy:
image: traefik:latest
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.8'
services:
agent:
image: ghcr.io/circlesabound/fctrl/agent:latest
Expand Down
81 changes: 78 additions & 3 deletions openapi/mgmt-server-rest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ServerConfigServerSettings'
'204':
description: This response indicates that the server-settings JSON file is not initialised on the server. To remediate, either start the server once to automatically generate it, or manually push one to the server.
put:
summary: Pushes a server-settings file to the Factorio server for use.
requestBody:
Expand Down Expand Up @@ -578,7 +576,84 @@ components:
nullable: true
type: string
ServerConfigServerSettings:
type: object
required:
- name
- description
- tags
- visibility
- autosave_interval
- autosave_only_on_server
- non_blocking_saving
- game_password
- require_user_verification
- max_players
- ignore_player_limit_for_returning_players
- allow_commands
- only_admins_can_pause_the_game
- max_upload_in_kilobytes_per_second
- max_upload_slots
- minimum_latency_in_ticks
- max_heartbeats_per_second
- minimum_segment_size
- minimum_segment_size_peer_count
- maximum_segment_size
- maximum_segment_size_peer_count
properties:
name:
type: string
description:
type: string
tags:
type: array
items:
type: string
visibility:
required:
- public
- lan
properties:
public:
type: boolean
lan:
type: boolean
autosave_interval:
type: integer
autosave_only_on_server:
type: boolean
non_blocking_saving:
type: boolean
game_password:
type: string
require_user_verification:
type: boolean
max_players:
type: integer
ignore_player_limit_for_returning_players:
type: boolean
allow_commands:
type: string
enum:
- "true"
- "false"
- "admins-only"
only_admins_can_pause_the_game:
type: boolean
max_upload_in_kilobytes_per_second:
type: integer
max_upload_slots:
type: integer
minimum_latency_in_ticks:
type: integer
max_heartbeats_per_second:
type: integer
minimum_segment_size:
type: integer
minimum_segment_size_peer_count:
type: integer
maximum_segment_size:
type: integer
maximum_segment_size_peer_count:
type: integer
ServerModList:
type: array
items:
Expand Down
6 changes: 3 additions & 3 deletions src/agent/factorio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ mod tests {
use super::*;

#[tokio::test]
async fn can_install_version_1_1_92() -> std::result::Result<(), Box<dyn std::error::Error>> {
async fn can_install_version_1_1_104() -> std::result::Result<(), Box<dyn std::error::Error>> {
fctrl::util::testing::logger_init();

let tmp_dir = std::env::temp_dir().join(Uuid::new_v4().to_string());
fs::create_dir(&tmp_dir).await?;
let mut vm = VersionManager::new(&tmp_dir).await?;
vm.install("1.1.92".to_owned()).await?;
vm.install("1.1.104".to_owned()).await?;

assert!(vm.versions.contains_key("1.1.92"));
assert!(vm.versions.contains_key("1.1.104"));

let _ = fs::remove_dir_all(tmp_dir).await;

Expand Down
12 changes: 6 additions & 6 deletions src/agent/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ impl AgentController {
self.config_server_settings_get(operation_id).await;
}

AgentRequest::ConfigServerSettingsSet { json } => {
self.config_server_settings_set(json, operation_id).await;
AgentRequest::ConfigServerSettingsSet { config } => {
self.config_server_settings_set(config, operation_id).await;
}

AgentRequest::ConfigWhiteListGet => {
Expand Down Expand Up @@ -1328,7 +1328,7 @@ impl AgentController {

async fn config_server_settings_get(&self, operation_id: OperationId) {
if let Ok(Some(ss)) = ServerSettings::read().await {
self.reply_success(AgentOutMessage::ConfigServerSettings(ss.json), operation_id)
self.reply_success(AgentOutMessage::ConfigServerSettings(ss.config), operation_id)
.await;
return;
}
Expand All @@ -1338,7 +1338,7 @@ impl AgentController {
match ServerSettings::read_or_apply_default(version).await {
Ok(ss) => {
self.reply_success(
AgentOutMessage::ConfigServerSettings(ss.json),
AgentOutMessage::ConfigServerSettings(ss.config),
operation_id,
)
.await;
Expand All @@ -1359,8 +1359,8 @@ impl AgentController {
}
}

async fn config_server_settings_set(&self, json: String, operation_id: OperationId) {
match ServerSettings::set(json).await {
async fn config_server_settings_set(&self, config: ServerSettingsConfig, operation_id: OperationId) {
match ServerSettings::set(config).await {
Ok(_) => {
self.reply_success(AgentOutMessage::Ok, operation_id).await;
}
Expand Down
35 changes: 23 additions & 12 deletions src/agent/server/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
path::PathBuf,
};

use fctrl::schema::ServerSettingsConfig;
use lazy_static::lazy_static;
use log::{error, info, warn};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -388,7 +389,7 @@ impl WhiteList {
}
}
pub struct ServerSettings {
pub json: String,
pub config: ServerSettingsConfig,
pub path: PathBuf,
}

Expand All @@ -399,10 +400,18 @@ impl ServerSettings {
Ok(None)
} else {
match fs::read_to_string(path).await {
Ok(s) => Ok(Some(ServerSettings {
json: s,
path: path.clone(),
})),
Ok(s) => {
match serde_json::from_str(&s) {
Ok(config) => Ok(Some(ServerSettings {
config,
path: path.clone(),
})),
Err(e) => {
error!("Error deserialising server settings: {:?}", e);
Err(e.into())
}
}
},
Err(e) => {
error!("Error reading server settings: {:?}", e);
Err(e.into())
Expand All @@ -416,9 +425,9 @@ impl ServerSettings {
Some(ls) => Ok(ls),
None => {
info!("Generating server settings using defaults");
let defaults = ServerSettings::read_default_server_settings(installation).await?;
let config = ServerSettings::read_default_server_settings(installation).await?;
let s = ServerSettings {
json: defaults,
config,
path: SERVER_SETTINGS_PATH.clone(),
};
if let Err(e) = s.write().await {
Expand All @@ -431,9 +440,9 @@ impl ServerSettings {
}
}

pub async fn set(json: String) -> Result<()> {
pub async fn set(config: ServerSettingsConfig) -> Result<()> {
let ss = ServerSettings {
json,
config,
path: SERVER_SETTINGS_PATH.clone(),
};
ss.write().await
Expand All @@ -455,7 +464,9 @@ impl ServerSettings {
return Err(e.into());
}

if let Err(e) = fs::write(&self.path, &self.json).await {
let pretty_out = serde_json::to_string_pretty(&self.config)?;

if let Err(e) = fs::write(&self.path, pretty_out).await {
error!(
"Error writing server settings to {}: {:?}",
self.path.display(),
Expand All @@ -467,14 +478,14 @@ impl ServerSettings {
}
}

async fn read_default_server_settings(installation: &Factorio) -> Result<String> {
async fn read_default_server_settings(installation: &Factorio) -> Result<ServerSettingsConfig> {
let path = installation
.path
.join("factorio")
.join("data")
.join("server-settings.example.json");
match fs::read_to_string(path).await {
Ok(s) => Ok(s),
Ok(s) => Ok(serde_json::from_str(&s)?),
Err(e) => {
error!("Error reading default server settings: {:?}", e);
Err(e.into())
Expand Down
13 changes: 5 additions & 8 deletions src/mgmt-server/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ use std::{

use chrono::Utc;
use fctrl::schema::{
AgentOutMessage, AgentRequest, AgentRequestWithId, AgentResponseWithId, AgentStreamingMessage,
AgentStreamingMessageInner, FactorioVersion, ModObject, ModSettingsBytes, OperationId,
OperationStatus, RconConfig, Save, SecretsObject, ServerStartSaveFile, ServerStatus,
WhitelistObject,
AgentOutMessage, AgentRequest, AgentRequestWithId, AgentResponseWithId, AgentStreamingMessage, AgentStreamingMessageInner, FactorioVersion, ModObject, ModSettingsBytes, OperationId, OperationStatus, RconConfig, Save, SecretsObject, ServerSettingsConfig, ServerStartSaveFile, ServerStatus, WhitelistObject
};
use futures::{future, pin_mut, Future, SinkExt, Stream, StreamExt};
use lazy_static::lazy_static;
Expand Down Expand Up @@ -296,19 +293,19 @@ impl AgentApiClient {
.await
}

pub async fn config_server_settings_get(&self) -> Result<String> {
pub async fn config_server_settings_get(&self) -> Result<ServerSettingsConfig> {
let request = AgentRequest::ConfigServerSettingsGet;
let (_id, sub) = self.send_request_and_subscribe(request).await?;

response_or_timeout(sub, Duration::from_millis(500), |r| match r.content {
AgentOutMessage::ConfigServerSettings(json) => Ok(json),
AgentOutMessage::ConfigServerSettings(config) => Ok(config),
m => Err(default_message_handler(m)),
})
.await
}

pub async fn config_server_settings_set(&self, json: String) -> Result<()> {
let request = AgentRequest::ConfigServerSettingsSet { json };
pub async fn config_server_settings_set(&self, config: ServerSettingsConfig) -> Result<()> {
let request = AgentRequest::ConfigServerSettingsSet { config };
let (_id, sub) = self.send_request_and_subscribe(request).await?;

response_or_timeout(sub, Duration::from_millis(500), |r| match r.content {
Expand Down
8 changes: 4 additions & 4 deletions src/mgmt-server/routes/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use factorio_mod_settings_parser::ModSettings;
use fctrl::schema::{
mgmt_server_rest::*, FactorioVersion, ModSettingsBytes, RconConfig, SecretsObject,
ServerStartSaveFile, ServerStatus,
ServerStartSaveFile, ServerStatus, ServerSettingsConfig
};
use rocket::serde::json::Json;
use rocket::{get, post, put};
Expand Down Expand Up @@ -252,7 +252,7 @@ pub async fn put_secrets(
pub async fn get_server_settings(
_a: AuthorizedUser,
agent_client: &State<Arc<AgentApiClient>>,
) -> Result<Json<String>> {
) -> Result<Json<ServerSettingsConfig>> {
let json_str = agent_client.config_server_settings_get().await?;
Ok(Json(json_str))
}
Expand All @@ -261,9 +261,9 @@ pub async fn get_server_settings(
pub async fn put_server_settings(
_a: AuthorizedUser,
agent_client: &State<Arc<AgentApiClient>>,
body: String,
body: Json<ServerSettingsConfig>,
) -> Result<()> {
agent_client.config_server_settings_set(body).await
agent_client.config_server_settings_set(body.into_inner()).await
}

#[get("/server/mods/list")]
Expand Down
Loading

0 comments on commit ac65cdb

Please sign in to comment.