script.js (1227B)
1 // src/worker/script.js - turning a task's commands into a shell script 2 // 3 // The script is copied into the container and run by path rather than 4 // piped to the shell's stdin, so that a command which itself reads stdin 5 // cannot swallow the rest of the script. 6 // 7 // It lives outside the tree, so that a repository cannot shadow it and 8 // nothing the task does to its own working directory can lose it. 9 10 // Single quoting is the only form that is safe for arbitrary text in POSIX 11 // sh: everything inside is literal, and an embedded quote is closed, 12 // escaped and reopened. 13 export function shellQuote(value) { 14 return `'${String(value).replace(/'/g, "'\\''")}'`; 15 } 16 17 // Each command is echoed before it runs, so a reader can tell which one 18 // produced what. set -e stops at the first failure, and the exit code of 19 // that command becomes the exit code of the script. 20 export function buildScript(commands) { 21 const lines = ['#!/bin/sh', 'set -e', '']; 22 for (const command of commands) { 23 lines.push(`printf '%s\\n' ${shellQuote(`$ ${command}`)}`); 24 lines.push(command); 25 lines.push(''); 26 } 27 return lines.join('\n'); 28 } 29 30 export const SCRIPT_NAME = 'entrypoint.sh'; 31 export const SCRIPT_PATH = `/tmp/${SCRIPT_NAME}`;