Script output added if running in a terminal. Some pre-script task added.

This commit is contained in:
Christoph 2018-04-30 02:12:57 +02:00
parent 5b843393e6
commit 0231affd2c
2 changed files with 434 additions and 30 deletions

View File

@ -1,79 +1,448 @@
#!/usr/bin/env bash
# ---
# - Change API Key for user schleuder to enable managing lists
# -
# - Note: You can also run this script to initialise user 'schleuder'
# ---
script_name="$(basename $(realpath $0))"
working_dir="$(dirname $(realpath $0))"
conf_file="${working_dir}/conf/${script_name%%.*}.conf"
LOCK_DIR="/tmp/${script_name%%.*}.$$.LOCK"
log_file="${LOCK_DIR}/${script_name%%.*}.log"
backup_date="$(date +%Y-%m-%d-%H%M)"
schleuder_config="/etc/schleuder/schleuder.yml"
user_schleuder_config="/var/lib/schleuder/.schleuder-cli/schleuder-cli.yml"
# - Create API Key for user schleuder
# -
api_key="$(schleuder new_api_key)"
# ----------
# Base Function(s)
# ----------
# - Add the generated API Key to the list of valid api keys at
# - configuration file $schleuder_config
usage() {
[[ -n "$1" ]] && error "$1"
[[ $terminal ]] && echo -e "
\033[1mUsage:\033[m
$(basename $0)
\033[1mDescription\033[m
Script changes/adds API Key for user schleuder to enable managing lists.
\033[1mNote\033[m
You can also run this script to initialise user 'schleuder'.
\033[1mOptions\033[m
No Options available
\033[1mExample:\033[m
The only usage is:
$(basename $0)
"
clean_up 1
}
clean_up() {
# Perform program exit housekeeping
rm -rf "$LOCK_DIR"
blank_line
exit $1
}
echononl(){
if $terminal ; then
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n " $*\\c" 1>&2
else
echo -e -n " $*" 1>&2
fi
rm /tmp/shprompt$$
fi
}
fatal(){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mFatal\033[m ] $*"
else
echo -e " [ Fatal ] $*"
fi
echo ""
if $terminal ; then
echo -e " \033[1mScript terminated\033[m.."
else
echo -e " Script terminated.."
fi
echo ""
rm -rf $LOCK_DIR
exit 1
}
error (){
echo ""
if $terminal ; then
echo -e " [ \033[31m\033[1mError\033[m ] $*"
else
echo " [ Error ] $*"
fi
echo ""
}
warn (){
if $LOGGING || $terminal ; then
echo ""
if $terminal ; then
echo -e " [ \033[33m\033[1mWarn\033[m ] $*"
else
echo " [ Warn ] $*"
fi
echo ""
fi
}
info (){
if $LOGGING || $terminal ; then
echo ""
if $terminal ; then
echo -e " [ \033[32m\033[1mInfo\033[m ] $*"
else
echo " [ Info ] $*"
fi
echo ""
fi
}
ok (){
if $LOGGING || $terminal ; then
echo ""
if $terminal ; then
echo -e " [ \033[32m\033[1mOk\033[m ] $*"
else
echo " [ Ok ] $*"
fi
echo ""
fi
}
echo_done() {
if $terminal ; then
echo -e "\033[75G[ \033[32mdone\033[m ]"
fi
}
echo_ok() {
if $terminal ; then
echo -e "\033[75G[ \033[32mok\033[m ]"
fi
}
echo_failed(){
if $terminal ; then
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
fi
}
echo_skipped() {
if $terminal ; then
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
fi
}
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
blank_line() {
if $terminal ; then
echo ""
fi
}
# ----------
# - Jobhandling
# ----------
# - Run 'clean_up' for signals SIGHUP SIGINT SIGTERM
# -
if ! grep -q "$api_key" 2> /dev/null $schleuder_config ; then
perl -i.$backup_date -n -p \
-e "s/(^(\s*)valid_api_keys:.*)/\1\n\2 - ${api_key}/" \
$schleuder_config
trap clean_up SIGHUP SIGINT SIGTERM
# - Create lock directory '$LOCK_DIR"
#
mkdir "$LOCK_DIR"
# ----------
# - Headline
# ----------
if $terminal ; then
echo ""
echo -e "\033[1m----------\033[m"
echo -e "\033[32m\033[1mRunning script \033[m\033[1m$script_name\033[32m .. \033[m"
echo -e "\033[1m----------\033[m"
fi
# - Add generated API Key to schleuder's configuration file
# - '${user_schleuder_config}'
# ----------
# - Some checks ..
# ----------
# - Running in a terminal?
# -
if [[ -t 1 ]] ; then
terminal=true
else
terminal=false
fi
# - Print help?
# -
if [[ "$(trim $*)" = "-h" ]] || [[ "$(trim $*)" = "--help" ]] ; then
usage
fi
if [[ -z "$(which basename)" ]]; then
fatal 'It seems "basename" is not installed, but needed!'
fi
if [[ -z "$(which realpath)" ]]; then
fatal 'It seems "realpath" is not installed, but needed!'
fi
# ==========
# - Begin Main Script
# ==========
# ----------
# Read Configurations from $conf_file
# ----------
# - Give your default values here
# -
LOGGING=false
SCHLEUDER_USER="schleuder"
SCHLEUDER_MAIN_CONFIG="/etc/schleuder/schleuder.yml"
if [[ -f "$conf_file" ]]; then
source "$conf_file"
else
warn "No configuration file '$conf_file' present.\n
Loading default values.."
fi
# ----------
# - Some pre-script tasks ..
# ----------
if $terminal ; then
echo ""
echo ""
echo -e " \033[1mDoing some pre-script tasks ..\033[m"
echo ""
fi
if [[ ! -f "$SCHLEUDER_MAIN_CONFIG" ]]; then
fatal "Schleuder main configuration file not found!"
fi
if grep -q -E "^${SCHLEUDER_USER}" /etc/passwd 2> /dev/null ; then
# - This variable connot be set userdefined. Its almost the 'official' home directory.
# -
schleuder_home="$(cat /etc/passwd | grep -E "^${SCHLEUDER_USER}" | cut -d ':' -f6)"
else
fatal "User '$SCHLEUDER_USER' not present!"
fi
if [[ ! -d "$schleuder_home" ]]; then
fatal "Home Directory '$schleuder_home' for user '$SCHLEUDER_USER' not found!"
fi
# - This variables cannot be set userdefined.
# -
schleuder_user_dir="${schleuder_home}/.schleuder-cli"
schleuder_user_config="${schleuder_user_dir}/schleuder-cli.yml"
echononl "All is fine"
echo_ok
# ----------
# - Main part of script
# ----------
if $terminal ; then
echo ""
echo ""
echo -e " \033[1mMain part of script ..\033[m"
echo ""
fi
# - Create API Key for user schleuder
# -
> "$log_file"
echononl "Generate new API Key .."
api_key="$(schleuder new_api_key 2> "$log_file")"
if [[ -s "$log_file" ]] ; then
echo_failed
error "$(cat $log_file)"
> $log_file
else
echo_ok
info "generated api key: $api_key"
fi
# - Add the generated API Key to the list of valid api keys at
# - configuration file $SCHLEUDER_MAIN_CONFIG
# -
echononl "Add the generated API Key to '$SCHLEUDER_MAIN_CONFIG'.."
if ! grep -q "$api_key" 2> /dev/null $SCHLEUDER_MAIN_CONFIG ; then
perl -i.$backup_date -n -p \
-e "s/(^(\s*)valid_api_keys:.*)/\1\n\2 - ${api_key}/" \
$SCHLEUDER_MAIN_CONFIG > "$log_file" 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
fi
# - Make LOCK_DIR writable for user SCHLEUDER_USER
# -
chown -R ${SCHLEUDER_USER}:$SCHLEUDER_USER "$LOCK_DIR"
# - Add generated API Key to schleuder's user configuration file
# -
# - If no configuration file present, create a new default one.
# -
have_dot_schleuder_cli_yml=true
if [[ ! -d "~schleuder/.schleuder-cli" ]] ; then
if [[ ! -d "$schleuder_user_dir" ]] ; then
warn "Directory '$schleuder_user_dir' does not exist."
have_dot_schleuder_cli_yml=false
elif [[ ! -f "~schleuder/.schleuder-cli/schleuder-cli.yml" ]] ; then
elif [[ ! -f "$schleuder_user_config" ]] ; then
warn "Directory '$schleuder_user_dir' exists, but no file '$(basename "$schleuder_user_config")' inside"
# - If the directory is present, no default configuration file (see below)
# - will be written
# -
mv "~schleuder/.schleuder-cli" "~schleuder/.schleuder-cli.${backup_date}"
echononl "Move (backup) directory '$schleuder_user_dir' .."
mv "$schleuder_user_dir" "${schleuder_user_dir}.${backup_date}" > "$log_file" 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
have_dot_schleuder_cli_yml=false
fi
if ! $have_dot_schleuder_cli_yml ; then
# Creates a default configuration file '${user_schleuder_config}'
# Creates a default configuration file '${schleuder_user_config}'
#
su - schleuder -s /bin/bash -c "/usr//bin/schleuder-cli lists list > /dev/null 2>&1"
echononl "Create default user configuration for user '$SCHLEUDER_USER' .."
su - $SCHLEUDER_USER -s /bin/bash -c "/usr//bin/schleuder-cli lists list > \"$log_file\" 2>&1"
if [[ -f "$schleuder_user_config" ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
fi
# - Now, add the API Key..
# -
echononl "Add API Key to user configuration '$(basename "$schleuder_user_config")' .."
perl -i.$backup_date -n -p \
-e "s/^(\s*api_key:).*/\1 ${api_key}/" \
${user_schleuder_config}
${schleuder_user_config} > "$log_file" 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
# - Get tls fingerprint of configured certificate
# -
cert_fingerprint="$(schleuder cert fingerprint | awk '{print$4}')"
echononl "Get tls fingerprint of configured certificate .."
> "$log_file"
cert_fingerprint="$(schleuder cert fingerprint 2> "$log_file" | awk '{print$4}')"
if [[ -s "$log_file" ]] ; then
echo_failed
error "$(cat $log_file)"
> $log_file
else
echo_ok
info "TLS fingerprint of configured certificate: $cert_fingerprint"
fi
# - Add the fingerprint to schleuder users private configuration file
# -
if ! grep -q "$cert_fingerprint" 2> /dev/null ${user_schleuder_config} ; then
echononl "Add the fingerprint to user configuration '$(basename "$schleuder_user_config")' .."
if ! grep -q "$cert_fingerprint" 2> /dev/null ${schleuder_user_config} ; then
perl -i.$backup_date -n -p \
-e "s/^(\s*tls_fingerprint:).*/\1 ${cert_fingerprint}/" \
${user_schleuder_config}
${schleuder_user_config} > "$log_file" 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
else
echo_skipped
fi
# ----------
# - Some post-script tasks ..
# ----------
if $terminal ; then
echo ""
echo ""
echo -e " \033[1mDoing some post-script tasks ..\033[m"
echo ""
fi
# - Restart 'schleuder-api-daemon'
# -
systemctl restart schleuder-api-daemon
echononl "Restart service 'schleuder-api-daemon' .."
systemctl restart schleuder-api-daemon > "$log_file" 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_file")"
fi
exit 0
clean_up 0

View File

@ -0,0 +1,35 @@
# ==========
# - Configuration file for script change_api_key.sh
# ==========
# - LOGGING
# -
# - Enables/Disables script output. Setting this value to 'true' is
# - only useful if NOT running in a terminal (i.e. as cronjob).
# -
# - If script is running in a terminal, script output is enabled and
# - cannot be disabled.
# -
# - Running this script in a
# -
# - Default value: false
# -
#LOGGING=false
# - SCHLEUDER_USER
# -
# - User under witch schleuder is running.
# -
# - Default value: schleuder
# -
#SCHLEUDER_USER="schleuder"
# - SCHLEUDER_MAIN_CONFIG
# -
# - The global schleuder configuration file
# -
# - Default value: /etc/schleuder/schleuder.yml
# -
#SCHLEUDER_MAIN_CONFIG="/etc/schleuder/schleuder.yml"