diff options
-rw-r--r-- | planetwars-matchrunner/src/docker_runner.rs | 34 | ||||
-rw-r--r-- | web/pw-server/src/lib/components/SubmitPane.svelte | 17 | ||||
-rw-r--r-- | web/pw-server/src/routes/index.svelte | 3 | ||||
-rw-r--r-- | web/pw-server/src/routes/style.css | 1 |
4 files changed, 52 insertions, 3 deletions
diff --git a/planetwars-matchrunner/src/docker_runner.rs b/planetwars-matchrunner/src/docker_runner.rs index d563d60..f82e64e 100644 --- a/planetwars-matchrunner/src/docker_runner.rs +++ b/planetwars-matchrunner/src/docker_runner.rs @@ -45,10 +45,19 @@ async fn spawn_docker_process( let bot_code_dir = std::fs::canonicalize(¶ms.code_path).unwrap(); let code_dir_str = bot_code_dir.as_os_str().to_str().unwrap(); + let memory_limit = 512 * 1024 * 1024; // 512MB let config = container::Config { image: Some(params.image.clone()), host_config: Some(bollard::models::HostConfig { binds: Some(vec![format!("{}:{}", code_dir_str, "/workdir")]), + network_mode: Some("none".to_string()), + memory: Some(memory_limit), + memory_swap: Some(memory_limit), + // TODO: this applies a limit to how much cpu one bot can use. + // when running multiple bots concurrently though, the server + // could still become resource-starved. + cpu_period: Some(100_000), + cpu_quota: Some(10_000), ..Default::default() }), working_dir: Some("/workdir".to_string()), @@ -57,6 +66,7 @@ async fn spawn_docker_process( attach_stdout: Some(true), attach_stderr: Some(true), open_stdin: Some(true), + network_disabled: Some(true), ..Default::default() }; @@ -85,16 +95,35 @@ async fn spawn_docker_process( .await?; Ok(ContainerProcess { + docker, + container_id, stdin: input, output, }) } struct ContainerProcess { + docker: Docker, + container_id: String, stdin: Pin<Box<dyn AsyncWrite + Send>>, output: Pin<Box<dyn Stream<Item = Result<LogOutput, bollard::errors::Error>> + Send>>, } +impl ContainerProcess { + // &mut is required here to make terminate().await Sync + async fn terminate(&mut self) -> Result<(), bollard::errors::Error> { + self.docker + .remove_container( + &self.container_id, + Some(bollard::container::RemoveContainerOptions { + force: true, + ..Default::default() + }), + ) + .await + } +} + fn create_docker_bot( process: ContainerProcess, player_id: u32, @@ -151,6 +180,11 @@ impl DockerBotRunner { .unwrap() .resolve_request(request_id, request_response); } + + self.process + .terminate() + .await + .expect("could not terminate process"); } pub async fn communicate(&mut self, input: &[u8]) -> io::Result<Bytes> { diff --git a/web/pw-server/src/lib/components/SubmitPane.svelte b/web/pw-server/src/lib/components/SubmitPane.svelte index 740f2cf..82f752e 100644 --- a/web/pw-server/src/lib/components/SubmitPane.svelte +++ b/web/pw-server/src/lib/components/SubmitPane.svelte @@ -11,6 +11,8 @@ let availableBots: object[] = []; let selectedOpponent = undefined; let botName: string | undefined = undefined; + // whether to show the "save succesful" message + let saveSuccesful = false; let saveErrors: string[] = []; @@ -56,6 +58,9 @@ } async function saveBot() { + saveSuccesful = false; + saveErrors = []; + let response = await fetch("/api/save_bot", { method: "POST", headers: { @@ -77,8 +82,7 @@ if (!availableBots.find((bot) => bot["id"] == responseData["id"])) { availableBots = [...availableBots, responseData]; } - // clear errors - saveErrors = []; + saveSuccesful = true; } else { const error = responseData["error"]; if (error["type"] === "validation_failed") { @@ -113,7 +117,9 @@ {#if $currentUser} <div>Add your bot to the opponents list</div> <input type="text" class="bot-name-input" placeholder="bot name" bind:value={botName} /> - {#if saveErrors.length > 0} + {#if saveSuccesful} + <div class="success-text">Bot saved succesfully</div> + {:else if saveErrors.length > 0} <ul> {#each saveErrors as errorText} <li class="error-text">{errorText}</li> @@ -151,6 +157,11 @@ color: red; } + .success-text { + color: green; + margin: 0 8px; + } + .submit-button { padding: 8px 16px; border-radius: 8px; diff --git a/web/pw-server/src/routes/index.svelte b/web/pw-server/src/routes/index.svelte index 243f4da..08dc60a 100644 --- a/web/pw-server/src/routes/index.svelte +++ b/web/pw-server/src/routes/index.svelte @@ -209,6 +209,8 @@ .sidebar-left { width: 240px; background-color: $bg-color; + display: flex; + flex-direction: column; } .sidebar-right { width: 400px; @@ -246,6 +248,7 @@ list-style: none; color: #eee; padding-top: 15px; + overflow-y: scroll; } .match-card { diff --git a/web/pw-server/src/routes/style.css b/web/pw-server/src/routes/style.css index 293d3b1..f7d5388 100644 --- a/web/pw-server/src/routes/style.css +++ b/web/pw-server/src/routes/style.css @@ -1,3 +1,4 @@ body { margin: 0; + font-family: Roboto, Helvetica, sans-serif; } |