summaryrefslogtreecommitdiffstats
path: root/server/lib/ogfunctions.sh
blob: f1b9e5557f1f91fbefabec86564cc04c3f2f4f79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
#/**
#@file    ogfunctions.sh
#@brief   Generic functions for OpenGnsys Server and OpenGnsys Repository.
#@version 1.1.1 - Initial version
#@author  Ramón M. Gómez, ETSII Universidad de Sevilla
#@date    2017-10-08
#*/


# Showing an error message.
function raiseError() {
    case "$1" in
        usage)
            echo "$PROG: Usage error: Type \"$PROG help\"" >&2
            exit 1 ;;
        notfound)
            echo "$PROG: Resource not found: $2" >&2
            exit 2 ;;
        access)
            echo "$PROG: Access error: $2" >&2
            exit 3 ;;
        download)
            echo "$PROG: Download error: $2" >&2
            exit 4 ;;
        cancel)
            echo "$PROG: Operation cancelled: $2" >&2
            exit 5 ;;
        *)
            echo "$PROG: Unknown error" >&2
            exit 1 ;;
    esac
}

# Showing help message.
function help() {
    [ -n "$1" ] && DESCRIPTION="$1" || DESCRIPTION=$(grep "^#@brief" "$0" | cut -f2- -d" ")
    shift
    if [ -n "$1" ]; then
         USAGE="$1"
         shift
    else
         USAGE=$(grep "^#@usage" "$0" | cut -f2- -d" ")
         [ -n "$USAGE" ] && PARAMS=$(awk '$1=="#@param" {sub($1,""); print "\t",$0}' "$0")
    fi
    [ "$PROG" ] || PROG="$(basename "$0")"
    # Showing help.
    echo "$PROG: ${DESCRIPTION:-"no description"}"
    echo "Usage: ${USAGE:-"no usage info"}"
    [ -n "$PARAMS" ] && echo -e "$PARAMS"
    if [ -n "$*" ]; then
        echo "Examples:"
        while (( "$#" )); do
            echo -e "\t$1"
            shift
        done
    fi
    exit 0
}

# Showing script version number.
function version() {
    awk '/^#@version/ {v=$2} END {if (v) print v}' "$0"
    exit 0
}

# Functions to manage a service.
function restart() {
    _service restart "$1"
}
function start() {
    _service start "$1"
}
function stop() {
    _service stop "$1"
}

# Execute database operation.
function dbexec () {
    MYCNF=$(mktemp)
    trap "rm -f $MYCNF" 0 1 2 3 6 9 15
    touch $MYCNF
    chmod 600 $MYCNF
    cat << EOT > $MYCNF
[client]
user=$USUARIO
password=$PASSWORD
EOT
    mysql --defaults-extra-file="$MYCNF" -D "$CATALOG" -s -N -e "$1" || \
        raiseError access "Cannot access the databse"
    rm -f "$MYCNF"
}

# Returns parent process name (program or script)
function getcaller () {
    basename "$(COLUMNS=200 ps hp $PPID -o args | \
                awk '{if ($1~/bash/ && $2!="") { print $2; }
                      else { sub(/^-/,"",$1); print $1; } }')"
}

### Meta-functions and private functions.

# Metafunction to check if JSON result exists.
JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"."
function jq() {
    local OUTPUT
    OUTPUT=$($JQ "$@") || return $?
    [[ "$OUTPUT" = "null" ]] && return 1
    echo "$OUTPUT"
}

# Private function to acts on a service (do not use directly).
function _service() {
    local ACTION="$1"
    local SERVICE="$2"
    if which systemctl 2>/dev/null; then
        systemctl "$ACTION" "$SERVICE"
    elif which service 2>/dev/null; then
        service "$SERVICE" "$ACTION"
    elif [ -x /etc/init.d/"$SERVICE" ]; then
        /etc/init.d/"$SERVICE" "$ACTION"
    else
        raiseError notfound "Service $SERVICE"
    fi
}