source: server/lib/ogfunctions.sh @ 6495c7c

918-git-images-111dconfigfileconfigure-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 6495c7c was 8495409, checked in by Ramón M. Gómez <ramongomez@…>, 6 years ago

#925: settoken: new server script to generate service access tokens.

  • Property mode set to 100755
File size: 2.4 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
72### Meta-functions and private functions.
73
74# Metafunction to check if JSON result exists.
75JQ=$(which jq 2>/dev/null) || raiseError notfound "Need to install \"jq\"."
76function jq() {
77    local OUTPUT
78    OUTPUT=$($JQ "$@") || return $?
79    [[ "$OUTPUT" = "null" ]] && return 1
80    echo "$OUTPUT"
81}
82
83# Private function to acts on a service (do not use directly).
84function _service() {
85    local ACTION="$1"
86    local SERVICE="$2"
87    if which systemctl 2>/dev/null; then
88        systemctl "$ACTION" "$SERVICE"
89    elif which service 2>/dev/null; then
90        service "$SERVICE" "$ACTION"
91    elif [ -x /etc/init.d/"$SERVICE" ]; then
92        /etc/init.d/"$SERVICE" "$ACTION"
93    else
94        raiseError notfound "Service $SERVICE"
95    fi
96}
Note: See TracBrowser for help on using the repository browser.