source: server/lib/ogfunctions.sh

qndtest
Last change on this file was da6986dc, checked in by Ramón M. Gómez <ramongomez@…>, 4 years ago

#957: Script listclientlive uses common server functions.

  • Property mode set to 100755
File size: 3.2 KB
Line 
1#!/bin/bash
2#/**
3#@file    ogfunctions.sh
4#@brief   Generic functions for OpenGnsys Server and OpenGnsys Repository.
5#@version 1.1.1 - Initial version
6#@author  Ramón M. Gómez, ETSII Universidad de Sevilla
7#@date    2017-10-08
8#*/
9
10
11# Showing an error message.
12function raiseError() {
13    [ "$PROG" ] || PROG="$(basename "$0")"
14    case "$1" in
15        usage)
16            echo "$PROG: Usage error: Type \"$PROG help\"" >&2
17            exit 1 ;;
18        notfound)
19            echo "$PROG: Resource not found: $2" >&2
20            exit 2 ;;
21        access)
22            echo "$PROG: Access error: $2" >&2
23            exit 3 ;;
24        download)
25            echo "$PROG: Download error: $2" >&2
26            exit 4 ;;
27        cancel)
28            echo "$PROG: Operation cancelled: $2" >&2
29            exit 5 ;;
30        *)
31            echo "$PROG: Unknown error" >&2
32            exit 1 ;;
33    esac
34}
35
36# Showing help message.
37function help() {
38    [ -n "$1" ] && DESCRIPTION="$1" || DESCRIPTION=$(grep "^#@brief" "$0" | cut -f2- -d" ")
39    shift
40    if [ -n "$1" ]; then
41         USAGE="$1"
42         shift
43    else
44         USAGE=$(grep "^#@usage" "$0" | cut -f2- -d" ")
45         [ -n "$USAGE" ] && PARAMS=$(awk '$1=="#@param" {sub($1,""); print "\t",$0}' "$0")
46    fi
47    [ "$PROG" ] || PROG="$(basename "$0")"
48    # Showing help.
49    echo "$PROG: ${DESCRIPTION:-"no description"}"
50    echo "Usage: ${USAGE:-"no usage info"}"
51    [ -n "$PARAMS" ] && echo -e "$PARAMS"
52    if [ -n "$*" ]; then
53        echo "Examples:"
54        while (( "$#" )); do
55            echo -e "\t$1"
56            shift
57        done
58    fi
59    exit 0
60}
61
62# Showing script version number.
63function version() {
64    awk '/^#@version/ {v=$2} END {if (v) print v}' "$0"
65    exit 0
66}
67
68# Functions to manage a service.
69function restart() {
70    _service restart "$1"
71}
72function start() {
73    _service start "$1"
74}
75function stop() {
76    _service stop "$1"
77}
78
79# Execute database operation.
80function dbexec () {
81    MYCNF=$(mktemp)
82    trap "rm -f $MYCNF" 0 1 2 3 6 9 15
83    touch $MYCNF
84    chmod 600 $MYCNF
85    cat << EOT > $MYCNF
86[client]
87user=$USUARIO
88password=$PASSWORD
89EOT
90    mysql --defaults-extra-file="$MYCNF" -D "$CATALOG" -s -N -e "$1" || \
91        raiseError access "Cannot access the databse"
92    rm -f "$MYCNF"
93}
94
95# Returns parent process name (program or script)
96function getcaller () {
97    basename "$(COLUMNS=200 ps hp $PPID -o args | \
98                awk '{if ($1~/bash/ && $2!="") { print $2; }
99                      else { sub(/^-/,"",$1); print $1; } }')"
100}
101
102### Meta-functions and private functions.
103
104# Metafunction to check if JSON result exists.
105JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"."
106function jq() {
107    local OUTPUT
108    OUTPUT=$($JQ "$@") || return $?
109    [[ "$OUTPUT" = "null" ]] && return 1
110    echo "$OUTPUT"
111}
112
113# Private function to acts on a service (do not use directly).
114function _service() {
115    local ACTION="$1"
116    local SERVICE="$2"
117    if which systemctl 2>/dev/null; then
118        systemctl "$ACTION" "$SERVICE"
119    elif which service 2>/dev/null; then
120        service "$SERVICE" "$ACTION"
121    elif [ -x /etc/init.d/"$SERVICE" ]; then
122        /etc/init.d/"$SERVICE" "$ACTION"
123    else
124        raiseError notfound "Service $SERVICE"
125    fi
126}
127
Note: See TracBrowser for help on using the repository browser.