source: server/lib/ogfunctions.sh @ 7da63e81

Last change on this file since 7da63e81 was e93dfe5, checked in by Ramón M. Gómez <ramongomez@…>, 6 years ago

#839: Use global function to execute commands in the database.

  • Property mode set to 100755
File size: 2.7 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    # Showing help.
47    echo "$PROG: ${DESCRIPTION:-"no description"}"
48    echo "Usage: ${USAGE:-"no usage info"}"
49    [ -n "$PARAMS" ] && echo -e "$PARAMS"
50    if [ -n "$*" ]; then
51        echo "Examples:"
52        while (( "$#" )); do
53            echo -e "\t$1"
54            shift
55        done
56    fi
57    exit 0
58}
59
60# Functions to manage a service.
61function restart() {
62    _service restart "$1"
63}
64function start() {
65    _service start "$1"
66}
67function stop() {
68    _service stop "$1"
69}
70
71# Execute database operation.
72function dbexec () {
73    MYCNF=$(mktemp)
74    trap "rm -f $MYCNF" 0 1 2 3 6 9 15
75    touch $MYCNF
76    chmod 600 $MYCNF
77    cat << EOT > $MYCNF
78[client]
79user=$USUARIO
80password=$PASSWORD
81EOT
82    mysql --defaults-extra-file="$MYCNF" -D "$CATALOG" -s -N -e "$1" || \
83        raiseError access "Cannot access the databse"
84    rm -f "$MYCNF"
85}
86
87
88### Meta-functions and private functions.
89
90# Metafunction to check if JSON result exists.
91JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"."
92function jq() {
93    local OUTPUT
94    OUTPUT=$($JQ "$@") || return $?
95    [[ "$OUTPUT" = "null" ]] && return 1
96    echo "$OUTPUT"
97}
98
99# Private function to acts on a service (do not use directly).
100function _service() {
101    local ACTION="$1"
102    local SERVICE="$2"
103    if which systemctl 2>/dev/null; then
104        systemctl "$ACTION" "$SERVICE"
105    elif which service 2>/dev/null; then
106        service "$SERVICE" "$ACTION"
107    elif [ -x /etc/init.d/"$SERVICE" ]; then
108        /etc/init.d/"$SERVICE" "$ACTION"
109    else
110        raiseError notfound "Service $SERVICE"
111    fi
112}
113
Note: See TracBrowser for help on using the repository browser.