Permalink
Browse files

feat(process): handling process with the new script

  • Loading branch information...
jkuri committed Sep 5, 2017
1 parent 60fac21 commit 5872b35828cec0c2c7b5f822f0c31803191fdf96
Showing with 32 additions and 44 deletions.
  1. +25 −32 src/api/process.ts
  2. +7 −12 src/files/scripts/abstruse-exec.sh
@@ -35,23 +35,17 @@ export function startBuildProcess(
): Observable<ProcessOutput> {
return new Observable(observer => {
const name = 'abstruse_' + proc.build_id + '_' + proc.job_id;
const vars = proc.commands.filter(cmd => cmd.command.startsWith('export'))
.map(cmd => cmd.command.replace('export', '-e'))
.reduce((acc, curr) => {
return acc.concat(curr.split(' '));
}, [])
.concat(proc.env.reduce((acc, curr) => acc.concat(['-e', curr]), []))
const envs = proc.env.reduce((acc, curr) => acc.concat(['-e', curr]), [])
.concat(variables.reduce((acc, curr) => acc.concat(['-e', curr]), []));
proc.commands = proc.commands.filter(cmd => !cmd.command.startsWith('export'));

let debug: Observable<any> = Observable.empty();
if (proc.sshAndVnc) {
const ssh = `sudo /etc/init.d/ssh start`;
const xvfb = [
'export DISPLAY=:99 &&',
'sudo /etc/init.d/xvfb start &&',
'sleep 3',
'sudo /etc/init.d/fluxbox start'
'sleep 3 &&',
'sudo /etc/init.d/openbox start'
].join(' ');
const vnc = [
'x11vnc -xkb -noxrecord -noxfixes -noxdamage',
@@ -68,7 +62,7 @@ export function startBuildProcess(
]);
}

const sub = startContainer(name, image, vars)
const sub = startContainer(name, image, envs)
.concat(debug)
.concat(...proc.commands.map(cmd => executeInContainer(name, cmd.command)))
.subscribe((event: ProcessOutput) => {
@@ -106,39 +100,37 @@ function executeInContainer(name: string, command: string): Observable<ProcessOu
observer.error(msg);
}


let exitCode = 255;
let executed = false;
let attach = null;
let detachKey = null;
let detachKey = 'D';

if (command.includes('init.d') && command.includes('start')) {
attach = nodePty.spawn('docker', ['attach', '--detach-keys=D', name]);
detachKey = 'D';
} else {
attach = nodePty.spawn('docker', ['exec', '-it', name, 'bash', '-l']);
}
attach = nodePty.spawn('docker', ['attach', `--detach-keys=${detachKey}`, name]);

attach.on('data', data => {
if (!executed) {
attach.write(command + ' && echo EXECOK || echo EXECNOK\r');
const cmd = `/usr/bin/abstruse '${command}'\r`;
attach.write(cmd);
observer.next({ type: 'data', data: yellow('==> ' + command) + '\r' });
executed = true;
} else if (data.includes('EXECOK')) {
exitCode = 0;
attach.write(detachKey ? detachKey : 'exit $?\r');
} else if (data.includes('EXECNOK')) {
observer.error(red(`Last executed command returned error.`));
attach.write(detachKey ? detachKey : 'exit $?\r');
} else if (!data.includes(command) && !data.includes('exit $?') &&
!data.includes('logout') && !data.includes('read escape sequence')) {
observer.next({ type: 'data', data: data.replace('> ', '') });
} else {
if (data.includes('[success]')) {
exitCode = 0;
attach.kill();
} else if (data.includes('[error]')) {
attach.kill();
observer.error(red(data.replace('[error]', '')));
observer.complete();
} else if (!data.includes('/usr/bin/abstruse') &&
!data.includes('exit $?') && !data.includes('logout') &&
!data.includes('read escape sequence')) {
observer.next({ type: 'data', data: data.replace('> ', '') });
}
}
});

attach.on('exit', code => {
code = (detachKey === 'D') ? exitCode : code;
if (exitCode !== 0) {
if (code !== 0) {
const msg = [
yellow('['),
blue(name),
@@ -160,10 +152,11 @@ function startContainer(name: string, image: string, vars = []): Observable<Proc
return new Observable(observer => {
docker.killContainer(name)
.then(() => {
const args = ['run', '--privileged', '-dit', '-P']
.concat('-m=2048M', '--cpuset-cpus=0-1')
const args = ['run', '-dit', '-P']
.concat('-m=2048M', '--cpus=2')
.concat(vars)
.concat('--name', name, image);

const process = nodePty.spawn('docker', args);

process.on('exit', exitCode => {
@@ -2,15 +2,10 @@

set -e

SCRIPT="${0##*/}"
ABSTRUSE_RUN_SCRIPT="${0##*/}"

declare -i DEFAULT_TIMEOUT=3600
declare -i DEFAULT_INTERVAL=1
declare -i DEFAULT_DELAY=1

declare -i timeout=DEFAULT_TIMEOUT
declare -i interval=DEFAULT_INTERVAL
declare -i delay=DEFAULT_DELAY

COLOR_NC="\e[0m"
COLOR_GREEN="\e[0;32m"
@@ -23,7 +18,7 @@ print_usage() {
Abstruse CI command execution script
Synopsis
$SCRIPT [-t timeout] command
$ABSTRUSE_RUN_SCRIPT [-t timeout] command
Execute a command with a time-out.
Upon time-out expiration SIGKILL (0) is sent to the process.
@@ -36,10 +31,10 @@ EOF
}

while getopts ":t:" option; do
case "$option" in
t) timeout=$OPTARG ;;
*) print_usage && exit 1 ;;
esac
case "$option" in
t) timeout=$OPTARG ;;
*) print_usage && exit 1 ;;
esac
done
shift $((OPTIND - 1))

@@ -63,7 +58,7 @@ if (($# == 0)); then
exit 1
fi

( $@ ) & pid=$!
( eval $@ ) & pid=$!
( sleep $timeout && kill -s KILL $pid ) 2>/dev/null & watcher=$!

if wait $pid 2>/dev/null; then

0 comments on commit 5872b35

Please sign in to comment.