queued/sources/add_queued
2013-12-24 00:38:40 +04:00

93 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# queued is a simple daemon for starting jobs to queue of calculations
# Copyright (C) 2013 Evgeniy Alekseev
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see http://www.gnu.org/licenses
# or write to the Free Software Foundation,Inc., 51 Franklin Street,
# Fifth Floor, Boston, MA 02110-1301 USA
# functions
error_mes() {
case "$1" in
"config" ) echo "[EE] Configuration file is not set";;
"notfound" ) echo "[EE] File '$2' not found";;
"number" ) echo "[EE] '$2' is not a number";;
"priority" ) echo "[EE] Priority is not set";;
"unknown" ) echo "[EE] Unknown error";;
"user" ) echo "[EE] User is not set";;
esac
exit 1
}
func_help() {
echo -e "Script to add job for queued"
echo -e "\nUsage: add_queued [ -c /etc/queued.conf ] [ -p NUM ] [ -u USER ] [ -h | --help ] /path/to/script"
echo -e "\nParameters:"
echo -e " -c PATH - path to configuration file. Default is '/etc/queued.conf'"
echo -e " -p NUM - job priority"
echo -e " -u USER - username"
echo -e " -h --help - show this help and exit"
exit 0
}
isnum() {
(t=$(( 0$1+0 ))) 2>/dev/null
}
CONF_FILE="/etc/queued.conf"
# parametrs parsing
until [ -z $1 ]; do
case "$1" in
"-h" | "--help" ) func_help;;
"-c" ) [ -z "$2" ] && error_mes "config" || CONF_FILE="$2" && shift;;
"-p" ) [ -z "$2" ] && error_mes "priority" || JOB_PRIORITY="$2" && shift;;
"-u" ) [ -z "$2" ] && error_mes "user" || JOB_USER="$2" && shift;;
* ) JOB_SCRIPT="$1";;
esac
shift
done
# default values
WORKDIR="/var/lib/queued/work"
echo "[II] Reading configuration from '$CONF_FILE'"
[ -e "$CONF_FILE" ] || error_mes "notfound" "$CONF_FILE"
for VAR in "$(cat "$CONF_FILE")"; do eval "$VAR"; done
# error check
if [ ! -d "$WORKDIR" ]; then
[ -e "$WORKDIR" ] && error_mes "file" "$WORKDIR"
echo "[II] Creating directory '$WORKDIR'"
mkdir -m777 -p "$WORKDIR" || error_mes "unknown"
fi
if [ ! -z "$JOB_PRIORITY" ]; then
isnum "$JOB_PRIORITY" || error_mes "number" "$JOB_PRIORITY"
fi
[ -z "$JOB_SCRIPT" ] && error_mes "notfound" "$JOB_SCRIPT"
[ -e "$JOB_SCRIPT" ] || error_mes "notfound" "$JOB_SCRIPT"
# work block
if [ -e "$WORKDIR/$(basename "$JOB_SCRIPT")" ]; then
echo "[WW] File '$WORKDIR/$(basename "$JOB_SCRIPT")' already exists. Creating backup"
mv "$WORKDIR/$(basename "$JOB_SCRIPT")" "$WORKDIR/$(basename "$JOB_SCRIPT")-$(date +%Y%m%d-%N)" || error_mes "unknown"
fi
echo "[II] Copying '$JOB_SCRIPT' to '$WORKDIR'"
cp --archive "$JOB_SCRIPT" "$WORKDIR" || error_mes "unknown"
if [ ! -z "$JOB_PRIORITY" ]; then
echo "$JOB_PRIORITY" > "$WORKDIR/$(basename "$JOB_SCRIPT").pr" || error_mes "unknown"
fi
if [ ! -z "$JOB_USER" ]; then
echo "$JOB_USER" > "$WORKDIR/$(basename "$JOB_SCRIPT").user" || error_mes "unknown"
fi
exit 0