Permalink
Browse files

feat(exec-script): bash script for executing commands in container wi…

…th timeout
  • Loading branch information...
jkuri committed Sep 5, 2017
1 parent 6abdf76 commit 60fac21d904f346bf4fdcec1b8c0ee2bc2cf92e9
Showing with 95 additions and 22 deletions.
  1. +19 −22 src/api/process.ts
  2. +2 −0 src/files/Dockerfile
  3. +74 −0 src/files/scripts/abstruse-exec.sh
@@ -46,14 +46,24 @@ export function startBuildProcess(

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'
].join(' ');
const vnc = [
'x11vnc -xkb -noxrecord -noxfixes -noxdamage',
'-display :99 -forever -bg -rfbauth /etc/x11vnc.pass',
'-rfbport 5900'
].join(' ');

debug = Observable.concat(...[
executeInContainer(name, 'sudo /etc/init.d/ssh start'),
executeInContainer(name, ssh),
getContainerExposedPort(name, 22),
executeInContainer(name, 'export DISPLAY=:99 && sudo /etc/init.d/xvfb start && ' +
'sleep 3 && sudo /etc/init.d/openbox start'),
executeInContainer(name, 'export DISPLAY=:99 && ' +
'x11vnc -xkb -noxrecord -noxfixes -noxdamage -display :99 ' +
'-forever -bg -rfbauth /etc/x11vnc.pass -rfbport 5900'),
executeInContainer(name, xvfb),
executeInContainer(name, vnc),
getContainerExposedPort(name, 5900)
]);
}
@@ -69,27 +79,14 @@ export function startBuildProcess(
stopContainer(name).subscribe((event: ProcessOutput) => {
observer.next(event);
observer.next({ type: 'data', data: err });
observer.complete();
});
}, () => {
sub.unsubscribe();
observer.complete();
stopContainer(name).subscribe((event: ProcessOutput) => {
observer.next(event);
}, err => {
sub.unsubscribe();
observer.error(err);
stopContainer(name).subscribe((event: ProcessOutput) => {
observer.next(event);
observer.next({ type: 'data', data: err });
});
}, () => {
sub.unsubscribe();
observer.complete();
stopContainer(name).subscribe((event: ProcessOutput) => {
observer.next(event);
});
});
});
});
});
}

@@ -119,7 +116,7 @@ function executeInContainer(name: string, command: string): Observable<ProcessOu
attach = nodePty.spawn('docker', ['attach', '--detach-keys=D', name]);
detachKey = 'D';
} else {
attach = nodePty.spawn('docker', ['exec', '-it', '--privileged', name, 'bash', '-l']);
attach = nodePty.spawn('docker', ['exec', '-it', name, 'bash', '-l']);
}

attach.on('data', data => {
@@ -1,2 +1,4 @@
FROM jkuri/abstruse

COPY scripts/abstruse-exec.sh /usr/bin/abstruse

@@ -0,0 +1,74 @@
#!/usr/bin/env bash

set -e

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"
COLOR_RED="\e[0;31m"
COLOR_WHITE="\e[1;37m"

print_usage() {
cat <<EOF
Abstruse CI command execution script
Synopsis
$SCRIPT [-t timeout] command
Execute a command with a time-out.
Upon time-out expiration SIGKILL (0) is sent to the process.
-t timeout
Number of seconds to wait for command completion.
Default value: $DEFAULT_TIMEOUT seconds.
Value must be integer.
EOF
}

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

error_handler() {
code=$?
if [ $code -gt 0 ]; then
echo
echo -e "$COLOR_RED[error]: command exited with error code ${code}\e[0m"
else
echo
echo -e "$COLOR_GREEN[success]: command exited with error code ${code}\e[0m"
fi

exit $code
}

trap "error_handler" INT TERM EXIT QUIT HUP

if (($# == 0)); then
print_usage
exit 1
fi

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

if wait $pid 2>/dev/null; then
kill -9 $watcher
exit 0
else
exit 1
fi

0 comments on commit 60fac21

Please sign in to comment.