Skip to content
Snippets Groups Projects
Commit 5853aafb authored by Recolic's avatar Recolic :house_with_garden:
Browse files

.builtin: prefix

parent 6435fe9d
No related branches found
No related tags found
No related merge requests found
#!/bin/bash
# Useful functions as built-in
function echo2 () {
# Useful built-in functions for both linuxconf and user scripts.
function lcf_echo2 () {
echo "$@" 1>&2
}
function err () {
echo2 Error: "$@"
function lcf_err () {
lcf_echo2 Error: "$@"
return 1
}
function die () {
err "$@"
function lcf_die () {
lcf_err "$@"
exit 1
}
function bgrun () {
# Usage: bgrun /var/log/your.log sslocal -s xxx -p 'hello world my password' -l xxx --xxx
# Usage: bgrun /var/log/your.log auto_restart frpc -c my_server.ini
function lcf_bgrun () {
# Usage: lcf_bgrun /var/log/your.log sslocal -s xxx -p 'hello world my password' -l xxx --xxx
# Usage: lcf_bgrun /var/log/your.log auto_restart frpc -c my_server.ini
# v202504-1
local logF="$1"
shift
......@@ -44,13 +44,13 @@ function lc_include () {
# print the return code of the eval-ed function to stderr, but this function should always return success.
for script in "$@"; do
echo2 "$script -- $LCI_SUBSHELL_OP started as $(whoami), at $(pwd)"
lcf_echo2 "$script -- $LCI_SUBSHELL_OP started as $(whoami), at $(pwd)"
(
source "$script"
declare -F "$LCI_SUBSHELL_OP" >/dev/null 2>&1 || exit 0
eval "$LCI_SUBSHELL_OP"
)
echo2 "$script -- $LCI_SUBSHELL_OP completed with status $?"
lcf_echo2 "$script -- $LCI_SUBSHELL_OP completed with status $?"
# TODO: should I abort on failure? It will stop running other include files. Don't do it now.
done
......@@ -81,7 +81,7 @@ function lci_fsmap () {
function lc_fsmap () {
lci_fsmap "$@"
local result=$?
[ $result = 0 ] || err "lc_fsmap returned error $result"
[ $result = 0 ] || lcf_err "lc_fsmap returned error $result"
return $result
}
......@@ -120,7 +120,7 @@ function lci_overwrite_conf () {
local confpath="$2" # user masterconf script path
local newpath="$(realpath "$confpath")"
[ "$newpath" != "" ] || err "lci_overwrite_conf: cannot unfold path $confpath. Permission error?" || return $?
[ "$newpath" != "" ] || lcf_err "lci_overwrite_conf: cannot unfold path $confpath. Permission error?" || return $?
# Don't re-create if old config already looks good. In this case, init_done should be preserved. TODO: rename lc register to lc init, then should I remove this behavior??
if [[ -f "$fname" ]]; then
......@@ -128,26 +128,26 @@ function lci_overwrite_conf () {
[[ "$oldpath" = "$newpath" ]] && return 0
fi
echo -e "#autogenerated config, could be overwritten without warning.\nmasterconf=$newpath" | tee "$fname" > /dev/null || err "lci_overwrite_conf: unable to create $fname" || return $?
echo -e "#autogenerated config, could be overwritten without warning.\nmasterconf=$newpath" | tee "$fname" > /dev/null || lcf_err "lci_overwrite_conf: unable to create $fname" || return $?
chmod ugo+rw "$fname"
}
function lci_register () {
local confpath="$1"
[[ -f "$confpath" ]] || die "lci_register: $confpath not exist"
[[ -f "$confpath" ]] || lcf_die "lci_register: $confpath not exist"
# For new register (or register a different dir), clear all existing conf. This will trigger init again.
lci_overwrite_conf /etc/linuxconf.conf "$confpath" || die "lci_register cannot write new conf"
lci_overwrite_conf /etc/linuxconf.conf "$confpath" || lcf_die "lci_register cannot write new conf"
}
function lci_call () {
# calls an lc function in masterconf (and included subconf)
[ "$1" = "" ] && die "logic error: lci_call without arg"
[ "$1" = "" ] && lcf_die "logic error: lci_call without arg"
export LCI_SUBSHELL_OP="$1"
local masterconf="$(lci_conf_get_masterconf_path /etc/linuxconf.conf)" || die "unable to call lc_init. Cannot read masterconf path from /etc/linuxconf.conf"
local masterconf="$(lci_conf_get_masterconf_path /etc/linuxconf.conf)" || lcf_die "unable to call lc_init. Cannot read masterconf path from /etc/linuxconf.conf"
local workdir="$(dirname "$masterconf")"
cd "$workdir" || die "unable to enter config directory: $workdir"
cd "$workdir" || lcf_die "unable to enter config directory: $workdir"
lc_include "$masterconf"
export LCI_SUBSHELL_OP=__lc_operation_undefined
}
......@@ -158,7 +158,7 @@ function lci_init_if_needed () {
lci_call lc_init
lci_state_file_append /etc/linuxconf.conf init_done "$uname" || die "lc_init functions succeeded, but unable to update /etc/linuxconf.conf"
lci_state_file_append /etc/linuxconf.conf init_done "$uname" || lcf_die "lc_init functions succeeded, but unable to update /etc/linuxconf.conf"
}
function lci_startup_if_needed () {
......@@ -166,20 +166,20 @@ function lci_startup_if_needed () {
local state_file="/tmp/.linuxconf-state-$uname"
if [[ ! -f $state_file ]]; then
touch $state_file && chmod ugo+rw $state_file || die "failed to create tmp file $state_file"
touch $state_file && chmod ugo+rw $state_file || lcf_die "failed to create tmp file $state_file"
fi
lci_state_file_contains $state_file startup_done "$uname" && return 0
lci_call lc_startup
lci_state_file_append $state_file startup_done "$uname" || die "lc_startup functions succeeded, but unable to update $state_file"
lci_state_file_append $state_file startup_done "$uname" || lcf_die "lc_startup functions succeeded, but unable to update $state_file"
}
function lci_usage () {
echo2 "linuxconf v$lci_version"
echo2 "This tool helps you manage all linux customization in one centralized directory, making backup/sync much easier."
echo2 "Usage: Run '$0 register <path/to/masterconf.sh>', then it will work out-of-box."
lcf_echo2 "linuxconf v$lci_version"
lcf_echo2 "This tool helps you manage all linux customization in one centralized directory, making backup/sync much easier."
lcf_echo2 "Usage: Run '$0 register <path/to/masterconf.sh>', then it will work out-of-box."
exit 1
}
......@@ -204,14 +204,14 @@ function lci_install_startup_hook () {
elif [ -f /etc/rc.local ]; then
echo '/usr/bin/linuxconf _startup_all' >> /usr/bin/linuxconf _startup_all
else
err "neither systemd nor /etc/rc.local available."
lcf_err "neither systemd nor /etc/rc.local available."
fi || return $?
#TODO: check if any user installed non-root startup hook
else
[ -f /etc/systemd/system/lc-hook.service ] && return 0
grep '/usr/bin/linuxconf _startup_all' /etc/rc.local >/dev/null 2>&1 && return 0
# TODO: install non-root startup hook
die "non-root startup hook not supported yet"
lcf_die "non-root startup hook not supported yet"
fi
}
function lci_install_login_hook () {
......@@ -245,12 +245,12 @@ function lci_install_login_hook () {
lci_version=0.2.0
subcommand="$1"
if [[ "$subcommand" != register ]] && [[ "$subcommand" != "" ]]; then
[[ ! -f /etc/linuxconf.conf ]] && die "Please run '$0 register <path/to/masterconf.sh>' at least once"
[[ ! -f /etc/linuxconf.conf ]] && lcf_die "Please run '$0 register <path/to/masterconf.sh>' at least once"
fi
if [[ "$subcommand" = register ]]; then
lci_register "$2"
lci_install_startup_hook || die "failed to install on_startup hook"
lci_install_login_hook || die "failed to install on_login hook"
lci_install_startup_hook || lcf_die "failed to install on_startup hook"
lci_install_login_hook || lcf_die "failed to install on_login hook"
lci_init_if_needed
lci_startup_if_needed
elif [[ "$subcommand" = _cron ]]; then
......@@ -260,10 +260,10 @@ elif [[ "$subcommand" = _startup ]]; then
lci_startup_if_needed
elif [[ "$subcommand" = _startup_all ]]; then
# systemd should call this service as root, and it will spawn subprocess for all users with sudo
[[ "$(whoami)" != root ]] && die "$0 _startup_all started as non-root. Exit because sudo might fail."
local ar_uname=($(lci_state_file_list /etc/linuxconf.conf init_done)) || die "List all initialized users: lci_state_file_list failed"
[[ "$(whoami)" != root ]] && lcf_die "$0 _startup_all started as non-root. Exit because sudo might fail."
local ar_uname=($(lci_state_file_list /etc/linuxconf.conf init_done)) || lcf_die "List all initialized users: lci_state_file_list failed"
for uname in "${ar_uname[@]}"; do
echo2 "Spawn subprocess '$0 _startup' as user $uname..."
lcf_echo2 "Spawn subprocess '$0 _startup' as user $uname..."
sudo -u "$uname" "$0" _startup
done
elif [[ "$subcommand" = _xdg_login ]]; then
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment