source: server/lib/ogfunctions.sh @ bd4a17f

918-git-images-111dID-1020_logrotateBugID-1037_Mostrar.TipoDisco.WebID-1038_Muestra.el.numero.de.ordenadoresID-1039_Asignar_Disco.Particion.Imagen.AccesoRemotoID-824_Iniciar.Sistema.Restauradodevel-ogadmserver-for-masterdisk-imagemainmaster-1037opengnsys-1.1.1dpreqndtest
Last change on this file since bd4a17f was ca75d29, checked in by Ramón M. Gómez <ramongomez@…>, 4 years ago

#957: Script supportsave uses global functions to show help and manage errors.

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