Skip to content

Commit

Permalink
Move job output to details, instead of summary
Browse files Browse the repository at this point in the history
  • Loading branch information
pschord committed Jan 4, 2020
1 parent 9ea026e commit c3714ff
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 deletions.
5 changes: 2 additions & 3 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}

Expand Down
68 changes: 39 additions & 29 deletions check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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] + "`<br/>\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,
Expand All @@ -139,32 +140,39 @@ 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,
check_run_id : job_ctx.check_run_id,
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);
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
10 changes: 6 additions & 4 deletions instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion util.js
Original file line number Diff line number Diff line change
@@ -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];
Expand Down

0 comments on commit c3714ff

Please sign in to comment.