Permalink
Browse files

feat(exec-script): execute script

  • Loading branch information...
jkuri committed Sep 17, 2017
1 parent b0a9a4c commit 75a33831ba3b56d45a24b6d84b60c663b04e0a9f
@@ -0,0 +1,7 @@
{
"files.associations": {
"fstream": "c",
"locale": "c",
"*.tcc": "c"
}
}
@@ -95,7 +95,7 @@ export function attachExec(id: string, cmd: any): Observable<any> {
};

stream.pipe(ws);
stream.write('/usr/bin/abstruse \'(' + cmd.command + ')\'\r');
stream.write('/usr/bin/abstruse \'' + cmd.command + '\'\r');
});
});
}
@@ -1,69 +1,21 @@
#!/usr/bin/env bash

set -e

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

declare -i DEFAULT_TIMEOUT=3600
declare -i timeout=DEFAULT_TIMEOUT

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
$ABSTRUSE_RUN_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
}
setsid /usr/bin/abstruse-pty "$*" & pgid=$!

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

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

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

if wait $pid 2>/dev/null; then
kill -9 $watcher
exit 0
if [ $code -gt 0 ]; then
echo
echo -e "$COLOR_RED[error]: command exited with error code ${code}\e[0m"
else
exit 1
echo
echo -e "$COLOR_GREEN[success]: command exited with error code ${code}\e[0m"
fi

exit $code
@@ -0,0 +1,59 @@
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s cmd [args...]\n", argv[0]);
_exit(1);
}

int fd[2];
pipe(fd);
pid_t pid = fork();
if (pid == -1) {
perror("fork");
} else if (pid == 0) { // child
char *args[] = { NULL };
close(fd[1]);
close(STDIN_FILENO);
dup(fd[0]);
execvp("/bin/bash", args);
} else {
close(fd[0]);

const char * initrc = "source /home/abstruse/init.sh\n";
write(fd[1], initrc, strlen(initrc));

int i;
int strsize = 0;
for (i = 1; i < argc; i++) {
strsize += strlen(argv[1]);
if (argc > i + 1) {
strsize++;
}
}

char * cmdstring;
cmdstring = malloc(strsize);
cmdstring[0] = '\0';

for (i = 1; i < argc; i++) {
strcat(cmdstring, argv[i]);
if (argc > i + 1) {
strcat(cmdstring, " ");
}
}

write(fd[1], cmdstring, strlen(cmdstring));
close(fd[1]);

int status;
waitpid(pid, &status, 0);

return status;
}
}
@@ -33,10 +33,10 @@
},
"body": "",
"created_at": "2017-09-17T05:34:48Z",
"updated_at": "2017-09-17T07:27:48Z",
"updated_at": "2017-09-17T11:34:13Z",
"closed_at": null,
"merged_at": null,
"merge_commit_sha": "2a36c3bd31610885d691b92cd90793ae28e41289",
"merge_commit_sha": "5b9eb095b5ec08459c8b3ddf15adecbe06ab345b",
"assignee": null,
"assignees": [

@@ -49,11 +49,11 @@
"review_comments_url": "https://api.github.com/repos/bleenco/abstruse/pulls/169/comments",
"review_comment_url": "https://api.github.com/repos/bleenco/abstruse/pulls/comments{/number}",
"comments_url": "https://api.github.com/repos/bleenco/abstruse/issues/169/comments",
"statuses_url": "https://api.github.com/repos/bleenco/abstruse/statuses/32b1c858657f7463ee97857be10a1719373c8450",
"statuses_url": "https://api.github.com/repos/bleenco/abstruse/statuses/1d025ed0cf9bc39a9cc56c40b8494d083fc8b795",
"head": {
"label": "jkuri:fix-e2e-tests",
"ref": "fix-e2e-tests",
"sha": "32b1c858657f7463ee97857be10a1719373c8450",
"sha": "1d025ed0cf9bc39a9cc56c40b8494d083fc8b795",
"user": {
"login": "jkuri",
"id": 1796022,
@@ -139,7 +139,7 @@
"deployments_url": "https://api.github.com/repos/jkuri/abstruse/deployments",
"created_at": "2017-05-05T18:47:09Z",
"updated_at": "2017-05-15T00:14:08Z",
"pushed_at": "2017-09-17T07:27:47Z",
"pushed_at": "2017-09-17T11:34:13Z",
"git_url": "git://github.com/jkuri/abstruse.git",
"ssh_url": "git@github.com:jkuri/abstruse.git",
"clone_url": "https://github.com/jkuri/abstruse.git",
@@ -252,7 +252,7 @@
"deployments_url": "https://api.github.com/repos/bleenco/abstruse/deployments",
"created_at": "2017-03-13T22:31:58Z",
"updated_at": "2017-09-15T17:40:13Z",
"pushed_at": "2017-09-17T06:14:43Z",
"pushed_at": "2017-09-17T07:29:38Z",
"git_url": "git://github.com/bleenco/abstruse.git",
"ssh_url": "git@github.com:bleenco/abstruse.git",
"clone_url": "https://github.com/bleenco/abstruse.git",
@@ -299,7 +299,7 @@
"href": "https://api.github.com/repos/bleenco/abstruse/pulls/169/commits"
},
"statuses": {
"href": "https://api.github.com/repos/bleenco/abstruse/statuses/32b1c858657f7463ee97857be10a1719373c8450"
"href": "https://api.github.com/repos/bleenco/abstruse/statuses/1d025ed0cf9bc39a9cc56c40b8494d083fc8b795"
}
},
"author_association": "OWNER",
@@ -312,12 +312,12 @@
"review_comments": 0,
"maintainer_can_modify": true,
"commits": 2,
"additions": 65,
"deletions": 628,
"changed_files": 11
"additions": 436,
"deletions": 789,
"changed_files": 12
},
"before": "c861e8111ea91fb0653ea5a1833d46d03a106742",
"after": "32b1c858657f7463ee97857be10a1719373c8450",
"before": "c5bdefee5e6dd78eb710f29fb9aaeb5b8202b539",
"after": "1d025ed0cf9bc39a9cc56c40b8494d083fc8b795",
"repository": {
"id": 84880847,
"name": "abstruse",
@@ -384,7 +384,7 @@
"deployments_url": "https://api.github.com/repos/bleenco/abstruse/deployments",
"created_at": "2017-03-13T22:31:58Z",
"updated_at": "2017-09-15T17:40:13Z",
"pushed_at": "2017-09-17T06:14:43Z",
"pushed_at": "2017-09-17T07:29:38Z",
"git_url": "git://github.com/bleenco/abstruse.git",
"ssh_url": "git@github.com:bleenco/abstruse.git",
"clone_url": "https://github.com/bleenco/abstruse.git",

0 comments on commit 75a3383

Please sign in to comment.