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

.rename lcf_xx to lc_xx, bump ver

parent b648651a
No related branches found
No related tags found
No related merge requests found
...@@ -45,7 +45,7 @@ user_pref("browser.tabs.hoverPreview.enabled", false);' ...@@ -45,7 +45,7 @@ user_pref("browser.tabs.hoverPreview.enabled", false);'
[[ -d "$dir" ]] && echo "$firefox_config" > "$dir/user.js" [[ -d "$dir" ]] && echo "$firefox_config" > "$dir/user.js"
done done
lcf_bgrun /tmp/ddns.log auto_restart bash -c "curl https://recolic.net/tmp/example-ddns-api.log ; sleep 300" lc_bgrun /tmp/ddns.log auto_restart bash -c "curl https://recolic.net/tmp/example-ddns-api.log ; sleep 300"
[[ -f /usr/bin/az ]] && az config set core.login_experience_v2=off || true [[ -f /usr/bin/az ]] && az config set core.login_experience_v2=off || true
} }
...@@ -54,6 +54,6 @@ lc_login () { ...@@ -54,6 +54,6 @@ lc_login () {
# echo _:1 | bash /usr/mybin/unlock_keyrings # echo _:1 | bash /usr/mybin/unlock_keyrings
echo "$(date) test-only: lc_login called" >> /tmp/note echo "$(date) test-only: lc_login called" >> /tmp/note
chmod 777 /tmp/note chmod 777 /tmp/note
lcf_echo2 "detected login as $(whoami). it's running as expected!" lc_echo2 "detected login as $(whoami). it's running as expected!"
} }
...@@ -36,9 +36,9 @@ lc_startup () { ...@@ -36,9 +36,9 @@ lc_startup () {
# You want desktop env? Use lc_login() # You want desktop env? Use lc_login()
# want some daemon in background? # want some daemon in background?
lcf_bgrun /tmp/server.log my_server --arg1 123 --arg2 "hello world !" lc_bgrun /tmp/server.log my_server --arg1 123 --arg2 "hello world !"
# Guaranteed! linuxconf dir is your current workdir. # Guaranteed! linuxconf dir is your current workdir.
lcf_bgrun /dev/null auto_restart important_service --config files/test.conf lc_bgrun /dev/null auto_restart important_service --config files/test.conf
} }
lc_login () { lc_login () {
......
#!/bin/bash #!/bin/bash
# Useful built-in functions for both linuxconf and user scripts. # Useful built-in functions for both linuxconf and user scripts.
function lcf_echo2 () { function lc_echo2 () {
echo "$@" 1>&2 echo "$@" 1>&2
} }
function lcf_err () { function lc_err () {
lcf_echo2 Error: "$@" lc_echo2 Error: "$@"
return 1 return 1
} }
function lcf_die () { function lc_die () {
lcf_err "$@" lc_err "$@"
exit 1 exit 1
} }
function lcf_bgrun () { function lc_bgrun () {
# Usage: lcf_bgrun /var/log/your.log sslocal -s xxx -p 'hello world my password' -l xxx --xxx # Usage: lc_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 # Usage: lc_bgrun /var/log/your.log auto_restart frpc -c my_server.ini
# v202504-1 # v202504-1
local logF="$1" local logF="$1"
shift shift
...@@ -44,13 +44,13 @@ function lc_include () { ...@@ -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. # print the return code of the eval-ed function to stderr, but this function should always return success.
for script in "$@"; do for script in "$@"; do
lcf_echo2 "$script -- $LCI_SUBSHELL_OP started as $(whoami), at $(pwd)" lc_echo2 "$script -- $LCI_SUBSHELL_OP started as $(whoami), at $(pwd)"
( (
source "$script" source "$script"
declare -F "$LCI_SUBSHELL_OP" >/dev/null 2>&1 || exit 0 declare -F "$LCI_SUBSHELL_OP" >/dev/null 2>&1 || exit 0
eval "$LCI_SUBSHELL_OP" eval "$LCI_SUBSHELL_OP"
) )
lcf_echo2 "$script -- $LCI_SUBSHELL_OP completed with status $?" lc_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. # TODO: should I abort on failure? It will stop running other include files. Don't do it now.
done done
...@@ -123,7 +123,7 @@ function lci_overwrite_conf () { ...@@ -123,7 +123,7 @@ function lci_overwrite_conf () {
local confpath="$2" # user masterconf script path local confpath="$2" # user masterconf script path
local newpath="$(realpath "$confpath")" local newpath="$(realpath "$confpath")"
[ "$newpath" != "" ] || lcf_err "lci_overwrite_conf: cannot unfold path $confpath. Permission error?" || return $? [ "$newpath" != "" ] || lc_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?? # 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 if [[ -f "$fname" ]]; then
...@@ -131,26 +131,26 @@ function lci_overwrite_conf () { ...@@ -131,26 +131,26 @@ function lci_overwrite_conf () {
[[ "$oldpath" = "$newpath" ]] && return 0 [[ "$oldpath" = "$newpath" ]] && return 0
fi fi
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 $? echo -e "#autogenerated config, could be overwritten without warning.\nmasterconf=$newpath" | tee "$fname" > /dev/null || lc_err "lci_overwrite_conf: unable to create $fname" || return $?
chmod ugo+rw "$fname" chmod ugo+rw "$fname"
} }
function lci_register () { function lci_register () {
local confpath="$1" local confpath="$1"
[[ -f "$confpath" ]] || lcf_die "lci_register: $confpath not exist" [[ -f "$confpath" ]] || lc_die "lci_register: $confpath not exist"
# For new register (or register a different dir), clear all existing conf. This will trigger init again. # For new register (or register a different dir), clear all existing conf. This will trigger init again.
lci_overwrite_conf /etc/linuxconf.conf "$confpath" || lcf_die "lci_register cannot write new conf" lci_overwrite_conf /etc/linuxconf.conf "$confpath" || lc_die "lci_register cannot write new conf"
} }
function lci_call () { function lci_call () {
# calls an lc function in masterconf (and included subconf) # calls an lc function in masterconf (and included subconf)
[ "$1" = "" ] && lcf_die "logic error: lci_call without arg" [ "$1" = "" ] && lc_die "logic error: lci_call without arg"
export LCI_SUBSHELL_OP="$1" export LCI_SUBSHELL_OP="$1"
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 masterconf="$(lci_conf_get_masterconf_path /etc/linuxconf.conf)" || lc_die "unable to call lc_init. Cannot read masterconf path from /etc/linuxconf.conf"
local workdir="$(dirname "$masterconf")" local workdir="$(dirname "$masterconf")"
cd "$workdir" || lcf_die "unable to enter config directory: $workdir" cd "$workdir" || lc_die "unable to enter config directory: $workdir"
lc_include "$masterconf" lc_include "$masterconf"
export LCI_SUBSHELL_OP=__lc_operation_undefined export LCI_SUBSHELL_OP=__lc_operation_undefined
} }
...@@ -161,7 +161,7 @@ function lci_init_if_needed () { ...@@ -161,7 +161,7 @@ function lci_init_if_needed () {
lci_call lc_init lci_call lc_init
lci_state_file_append /etc/linuxconf.conf init_done "$uname" || lcf_die "lc_init functions succeeded, but unable to update /etc/linuxconf.conf" lci_state_file_append /etc/linuxconf.conf init_done "$uname" || lc_die "lc_init functions succeeded, but unable to update /etc/linuxconf.conf"
} }
function lci_startup_if_needed () { function lci_startup_if_needed () {
...@@ -169,20 +169,20 @@ function lci_startup_if_needed () { ...@@ -169,20 +169,20 @@ function lci_startup_if_needed () {
local state_file="/tmp/.linuxconf-state-$uname" local state_file="/tmp/.linuxconf-state-$uname"
if [[ ! -f $state_file ]]; then if [[ ! -f $state_file ]]; then
touch $state_file && chmod ugo+rw $state_file || lcf_die "failed to create tmp file $state_file" touch $state_file && chmod ugo+rw $state_file || lc_die "failed to create tmp file $state_file"
fi fi
lci_state_file_contains $state_file startup_done "$uname" && return 0 lci_state_file_contains $state_file startup_done "$uname" && return 0
lci_call lc_startup lci_call lc_startup
lci_state_file_append $state_file startup_done "$uname" || lcf_die "lc_startup functions succeeded, but unable to update $state_file" lci_state_file_append $state_file startup_done "$uname" || lc_die "lc_startup functions succeeded, but unable to update $state_file"
} }
function lci_usage () { function lci_usage () {
lcf_echo2 "linuxconf v$lci_version" lc_echo2 "linuxconf v$lci_version"
lcf_echo2 "This tool helps you manage all linux customization in one centralized directory, making backup/sync much easier." lc_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." lc_echo2 "Usage: Run '$0 register <path/to/masterconf.sh>', then it will work out-of-box."
exit 1 exit 1
} }
...@@ -207,14 +207,14 @@ function lci_install_startup_hook () { ...@@ -207,14 +207,14 @@ function lci_install_startup_hook () {
elif [ -f /etc/rc.local ]; then elif [ -f /etc/rc.local ]; then
echo '/usr/bin/linuxconf _startup_all' >> /usr/bin/linuxconf _startup_all echo '/usr/bin/linuxconf _startup_all' >> /usr/bin/linuxconf _startup_all
else else
lcf_err "neither systemd nor /etc/rc.local available." lc_err "neither systemd nor /etc/rc.local available."
fi || return $? fi || return $?
#TODO: check if any user installed non-root startup hook #TODO: check if any user installed non-root startup hook
else else
[ -f /etc/systemd/system/lc-hook.service ] && return 0 [ -f /etc/systemd/system/lc-hook.service ] && return 0
grep '/usr/bin/linuxconf _startup_all' /etc/rc.local >/dev/null 2>&1 && return 0 grep '/usr/bin/linuxconf _startup_all' /etc/rc.local >/dev/null 2>&1 && return 0
# TODO: install non-root startup hook # TODO: install non-root startup hook
lcf_die "non-root startup hook not supported yet" lc_die "non-root startup hook not supported yet"
fi fi
} }
function lci_install_login_hook () { function lci_install_login_hook () {
...@@ -245,17 +245,17 @@ function lci_install_login_hook () { ...@@ -245,17 +245,17 @@ function lci_install_login_hook () {
fi fi
} }
lci_version=0.2.1 lci_version=0.2.2
subcommand="$1" subcommand="$1"
if [[ "$subcommand" != register ]] && [[ "$subcommand" != "" ]]; then if [[ "$subcommand" != register ]] && [[ "$subcommand" != "" ]]; then
[[ ! -f /etc/linuxconf.conf ]] && lcf_die "Please run '$0 register <path/to/masterconf.sh>' at least once" [[ ! -f /etc/linuxconf.conf ]] && lc_die "Please run '$0 register <path/to/masterconf.sh>' at least once"
fi fi
if [[ "$subcommand" = register ]]; then if [[ "$subcommand" = register ]]; then
lcf_echo2 "Confirm linuxconf init as $(whoami) at $(dirname "$(realpath "$2")") ? Make sure all (desired) user have read access!" lc_echo2 "Confirm linuxconf init as $(whoami) at $(dirname "$(realpath "$2")") ? Make sure all (desired) user have read access!"
read -p "Confirm ? " || exit 0 read -p "Confirm ? " || exit 0
lci_register "$2" lci_register "$2"
lci_install_startup_hook || lcf_die "failed to install on_startup hook" lci_install_startup_hook || lc_die "failed to install on_startup hook"
lci_install_login_hook || lcf_die "failed to install on_login hook" lci_install_login_hook || lc_die "failed to install on_login hook"
lci_init_if_needed lci_init_if_needed
lci_startup_if_needed lci_startup_if_needed
elif [[ "$subcommand" = _cron ]]; then elif [[ "$subcommand" = _cron ]]; then
...@@ -265,10 +265,10 @@ elif [[ "$subcommand" = _startup ]]; then ...@@ -265,10 +265,10 @@ elif [[ "$subcommand" = _startup ]]; then
lci_startup_if_needed lci_startup_if_needed
elif [[ "$subcommand" = _startup_all ]]; then elif [[ "$subcommand" = _startup_all ]]; then
# systemd should call this service as root, and it will spawn subprocess for all users with sudo # systemd should call this service as root, and it will spawn subprocess for all users with sudo
[[ "$(whoami)" != root ]] && lcf_die "$0 _startup_all started as non-root. Exit because sudo might fail." [[ "$(whoami)" != root ]] && lc_die "$0 _startup_all started as non-root. Exit because sudo might fail."
ar_uname=($(lci_state_file_list /etc/linuxconf.conf init_done)) || lcf_die "List all initialized users: lci_state_file_list failed" ar_uname=($(lci_state_file_list /etc/linuxconf.conf init_done)) || lc_die "List all initialized users: lci_state_file_list failed"
for uname in "${ar_uname[@]}"; do for uname in "${ar_uname[@]}"; do
lcf_echo2 "Spawn subprocess '$0 _startup' as user $uname..." lc_echo2 "Spawn subprocess '$0 _startup' as user $uname..."
sudo -u "$uname" "$0" _startup sudo -u "$uname" "$0" _startup
done done
elif [[ "$subcommand" = _xdg_login ]]; then 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