source: server/lib/ogfunctions.sh @ cae6338

918-git-images-111dconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacion
Last change on this file since cae6338 was cae6338, checked in by Ramón M. Gómez <ramongomez@…>, 5 years ago

#957: New function to show the script version.

  • Property mode set to 100755
File size: 3.1 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# Showing script version number.
61function version() {
62    awk '/^#@version/ {v=$2} END {if (v) print v}' "$0"
63    exit 0
64}
65
66# Functions to manage a service.
67function restart() {
68    _service restart "$1"
69}
70function start() {
71    _service start "$1"
72}
73function stop() {
74    _service stop "$1"
75}
76
77# Execute database operation.
78function dbexec () {
79    MYCNF=$(mktemp)
80    trap "rm -f $MYCNF" 0 1 2 3 6 9 15
81    touch $MYCNF
82    chmod 600 $MYCNF
83    cat << EOT > $MYCNF
84[client]
85user=$USUARIO
86password=$PASSWORD
87EOT
88    mysql --defaults-extra-file="$MYCNF" -D "$CATALOG" -s -N -e "$1" || \
89        raiseError access "Cannot access the databse"
90    rm -f "$MYCNF"
91}
92
93# Returns parent process name (program or script)
94function getcaller () {
95    basename "$(COLUMNS=200 ps hp $PPID -o args | \
96                awk '{if ($1~/bash/ && $2!="") { print $2; }
97                      else { sub(/^-/,"",$1); print $1; } }')"
98}
99
100### Meta-functions and private functions.
101
102# Metafunction to check if JSON result exists.
103JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"."
104function jq() {
105    local OUTPUT
106    OUTPUT=$($JQ "$@") || return $?
107    [[ "$OUTPUT" = "null" ]] && return 1
108    echo "$OUTPUT"
109}
110
111# Private function to acts on a service (do not use directly).
112function _service() {
113    local ACTION="$1"
114    local SERVICE="$2"
115    if which systemctl 2>/dev/null; then
116        systemctl "$ACTION" "$SERVICE"
117    elif which service 2>/dev/null; then
118        service "$SERVICE" "$ACTION"
119    elif [ -x /etc/init.d/"$SERVICE" ]; then
120        /etc/init.d/"$SERVICE" "$ACTION"
121    else
122        raiseError notfound "Service $SERVICE"
123    fi
124}
125
Note: See TracBrowser for help on using the repository browser.