source: server/lib/ogfunctions.sh @ 5d05b06

Last change on this file since 5d05b06 was 4972d15, checked in by OpenGnSys Support Team <soporte-og@…>, 5 years ago

#988 Read json config file in scripts

This patch implements the function source_json_config() that is used to
read the new ogserver.json config file in the scripts. This replaces the
old 'source .../ogserver.cfg'.

Co-authored-by: Javier Sánchez Parra <jsanchez@…>

  • Property mode set to 100755
File size: 3.6 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
113function source_json_config() {
114    FILE=$1
115    export ServidorAdm=$(jq -r ".rest.ip" $FILE)
116    export PUERTO=$(jq -r ".rest.port" $FILE)
117    export APITOKEN=$(jq -r ".rest.api_token" $FILE)
118    export USUARIO=$(jq -r ".database.user" $FILE)
119    export PASSWORD=$(jq -r ".database.pass" $FILE)
120    export datasource=$(jq -r ".database.ip" $FILE)
121    export CATALOG=$(jq -r ".database.name" $FILE)
122    export INTERFACE=$(jq -r ".wol.interface" $FILE)
123}
124
125# Private function to acts on a service (do not use directly).
126function _service() {
127    local ACTION="$1"
128    local SERVICE="$2"
129    if which systemctl 2>/dev/null; then
130        systemctl "$ACTION" "$SERVICE"
131    elif which service 2>/dev/null; then
132        service "$SERVICE" "$ACTION"
133    elif [ -x /etc/init.d/"$SERVICE" ]; then
134        /etc/init.d/"$SERVICE" "$ACTION"
135    else
136        raiseError notfound "Service $SERVICE"
137    fi
138}
139
Note: See TracBrowser for help on using the repository browser.