diff --git a/build.js b/build.js index dffbc0a..ea81137 100644 --- a/build.js +++ b/build.js @@ -12,10 +12,9 @@ export async function run_repo_command(repo_path, command, args) { try { const {stdout, stderr} = await execFile(command, args, { cwd : repo_path }); - return stdout; + return { code : 0, stdout : stdout, stderr : stderr }; } catch(e) { - console.log(e); - return null; + return { code : e.code, stdout : e.stdout, stderr : e.stderr }; } } diff --git a/check.js b/check.js index 974cd82..f8db259 100644 --- a/check.js +++ b/check.js @@ -90,6 +90,7 @@ export async function handle_check_run(octokit, action, github_ctx_base, github_ return; } + // TODO: dont stop instances that are actually infact newer than the rerequested check run const old_instances = await db.get_instances_by_gref(github_ctx_head.url + ":" + github_ctx_head.branch); let job_output = [], job_result; @@ -111,25 +112,25 @@ export async function handle_check_run(octokit, action, github_ctx_base, github_ job_output = ["Unknown job failure: " + e.message]; } - let summary = "" + if (job_result) { + const log_url = `https://play.psforever.net/psfci/${job_result.instance_id}`; + let summary = "", details = ""; - try { - if (job_result) { - const log_url = `https://play.psforever.net/psfci/${job_result.instance_id}`; - summary += "Set your client.ini to `play.psforever.net:" + ports[0] + "`
\n"; - summary += `**[View Server Logs](${log_url})**\n`; - summary += "## Job Output\n" - summary += "```\n" + job_output.join("\n") + "\n```\n"; - - if (old_instances.length) { - log.info("Stopping %d previous instances", old_instances.length) - - for (let i = 0; i < old_instances.length; i++) - await instance.stop(old_instances[i]) - } + summary += "Set your client.ini to `play.psforever.net:" + ports[0] + "`"; + summary += ` (**[View Server Logs](${log_url})**)\n`; + details += "## Job Output\n" + details += "```\n" + job_output.join("\n") + "\n```\n"; - log.info("Instance build completed") + if (old_instances.length) { + log.info("Stopping %d previous instances", old_instances.length) + + for (let i = 0; i < old_instances.length; i++) + await instance.stop(old_instances[i]) + } + log.info("Instance build completed") + + try { const result = await octokit.checks.update({ owner : github_ctx_base.owner, repo : github_ctx_base.repo, @@ -139,18 +140,24 @@ export async function handle_check_run(octokit, action, github_ctx_base, github_ output : { title : "Server Instance Running", summary : summary, + text : details, }, actions : [ { label : "Stop Server", description : "Stop the running server instance", identifier : "stop"} ] }); - } else { - summary += "## Job Output\n" - summary += "```\n" + job_output.join("\n") + "\n```\n"; + } catch(e) { + log.error("Failed to update check_run: ", e); + } + } else { + let summary = "", details = ""; + details += "## Job Output\n" + details += "```\n" + job_output.join("\n") + "\n```\n"; - log.error("Instance build failed") + log.error("Instance build failed") + try { const result = await octokit.checks.update({ owner : github_ctx_base.owner, repo : github_ctx_base.repo, @@ -158,13 +165,14 @@ export async function handle_check_run(octokit, action, github_ctx_base, github_ status: "completed", conclusion : "failure", output : { - title : "Job Failure", + title : "Instance Failure", summary : summary, + text : details, } }); + } catch(e) { + log.error("Failed to update check_run: ", e); } - } catch(e) { - log.error("Failed to update check_run: ", e); } } @@ -252,11 +260,12 @@ async function build_instance(log, octokit, github_ctx, job_ctx, ports) { log.info("PRERUN: " + cmdline) let output = await build.run_repo_command(dir, bin, args); - if (output === null) { - job_output.push("Prestart command failed") + if (output.code != 0) { + log.error("Prerun command failure: %d", output.code) + job_output.push(output.stdout || output.stderr); return { job_output: job_output, job_result : undefined } } else { - job_output.push(output) + job_output.push(output.stdout) } } @@ -296,12 +305,13 @@ async function build_instance(log, octokit, github_ctx, job_ctx, ports) { log.info("RUN: " + cmdline) let output = await build.run_repo_command(dir, bin, args); - if (output === null) { + if (output.code != 0) { instance.stop_docker(container_name); - job_output.push("Command failed") + log.error("Command failure: %d", output.code) + job_output.push(output.stdout || output.stderr); return { job_output: job_output, job_result : undefined } } else { - job_output.push(output) + job_output.push(output.stdout) } } diff --git a/instance.js b/instance.js index 42c0f7d..d1d093f 100644 --- a/instance.js +++ b/instance.js @@ -56,11 +56,13 @@ export async function stop(instance) { } export async function stop_docker(container_name) { - try { - const output = await build.run_repo_command(".", "docker", ["stop", "--time", "0", container_name]); + const output = await build.run_repo_command(".", "docker", ["stop", "--time", "0", container_name]); + + if (output.code != 0) { + logger.warn("Failed to stop container %s: %s", + container_name, output.stdout || output.stderr); + } else { logger.info("Stopped docker container " + container_name); - } catch (e) { - logger.warn("Failed to stop container: " + e.message); } return; diff --git a/util.js b/util.js index 69b8cef..03d4083 100644 --- a/util.js +++ b/util.js @@ -1,11 +1,18 @@ import * as build from "./build.js" +import logger from "./log.js" export async function get_free_udp_ports(start, end, amount) { const portlist = await build.run_repo_command(".", "ss", ["--udp", "--listening", "--numeric", "--no-header"]) + if (portlist.code != 0) { + logger.error("Failed to get free UDP ports: %s", + portlist.stdout || portlist.stderr); + return []; + } + const taken_ports = {}; - portlist.split("\n").forEach((line) => { + portlist.stdout.split("\n").forEach((line) => { if (!line) return; const tokens = line.split(/\s+/); const ip_port = tokens[3];