refactor log.py to logger.sh (#31)

* logger.sh: refactor log.py to bash script

* update.sh use beacon; ci test beacon

* install.sh: install dep first

* install.sh: DFS_UPDATED_RET=85

* more beacon; use stderr

* remove py3 dep

* install.sh: remove py3

* install.sh: -s|--secure
This commit is contained in:
Dict Xiong 2022-11-28 13:31:01 +08:00 committed by GitHub
parent 8c289c8c3c
commit e6441e018c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 127 additions and 91 deletions

View File

@ -12,7 +12,7 @@ cd $DOTFILES
git fetch --all --prune git fetch --all --prune
if [[ -n "$(git status -s)" ]]; then if [[ -n "$(git status -s)" ]]; then
fmt_error "directory not clean" fmt_error "directory not clean"
post_log "ERROR" "$THIS_FILE" "directory not clean" post_beacon "dfs.dirty" 1>/dev/null &
exit exit
fi fi
@ -25,6 +25,7 @@ case $DFS_UPDATE_CHANNEL in
esac esac
if [[ ${#DFS_COMMIT} != 40 ]]; then if [[ ${#DFS_COMMIT} != 40 ]]; then
fmt_error "invalid commit id" fmt_error "invalid commit id"
post_beacon "dfs.invalid-commit" 1>/dev/null &
post_log "ERROR" "$THIS_FILE" "invalid commit id: ${DFS_COMMIT}" post_log "ERROR" "$THIS_FILE" "invalid commit id: ${DFS_COMMIT}"
exit exit
fi fi
@ -32,11 +33,10 @@ fi
# update # update
if [[ "$(git rev-parse HEAD)" == "$DFS_COMMIT" ]]; then if [[ "$(git rev-parse HEAD)" == "$DFS_COMMIT" ]]; then
fmt_info "nothing to do" fmt_info "nothing to do"
post_log "INFO" "$THIS_FILE" "nothing to do"
else else
fmt_info "checking out to commit $DFS_COMMIT ..." fmt_info "checking out to commit $DFS_COMMIT ..."
if [[ -z "$DFS_DEV" ]]; then if [[ -z "$DFS_DEV" ]]; then
post_log "INFO" "$THIS_FILE" "will check out to commit $DFS_COMMIT" post_beacon "dfs.updated" 1>/dev/null &
git -c advice.detachedHead=false checkout $DFS_COMMIT git -c advice.detachedHead=false checkout $DFS_COMMIT
cp ./.update.sh ./update.sh && chmod +x ./update.sh && exit $DFS_UPDATED_RET cp ./.update.sh ./update.sh && chmod +x ./update.sh && exit $DFS_UPDATED_RET
else else

View File

@ -169,6 +169,7 @@ dfs()
echo 'Done. Please open a new shell to see the changes.' echo 'Done. Please open a new shell to see the changes.'
;; ;;
log ) "$DOTFILES/tools/common.sh" "post_log" "INFO" "dfs" "$2" ;; log ) "$DOTFILES/tools/common.sh" "post_log" "INFO" "dfs" "$2" ;;
beacon ) "$DOTFILES/tools/common.sh" "post_beacon" "$2" ;;
* ) echo "unknown command \"$1\". available: update, force-update, version, reset, cd, log" ;; * ) echo "unknown command \"$1\". available: update, force-update, version, reset, cd, log" ;;
esac esac
} }

View File

@ -34,50 +34,42 @@ install_dependencies()
case $(get_linux_dist) in case $(get_linux_dist) in
"ubuntu"|"debian" ) "ubuntu"|"debian" )
$SUDO apt-get update $SUDO apt-get update
$SUDO apt-get install -y git zsh bash tmux vim python3 python3-pip curl inetutils-ping cmake less bsdmainutils $SUDO apt-get install -y git zsh bash tmux vim curl inetutils-ping less bsdmainutils
;; ;;
"alpine" ) "alpine" )
$SUDO apk update $SUDO apk update
$SUDO apk add zsh bash git tmux vim curl python3 py3-pip fzf iputils coreutils util-linux $SUDO apk add zsh bash git tmux vim curl fzf iputils coreutils util-linux
;; ;;
* ) fmt_error "dfs auto-install is not implemented on linux distribution: $(get_linux_dist)" * ) fmt_error "dfs auto-install is not implemented on linux distribution: $(get_linux_dist)"
esac esac
;; ;;
"macos" ) "macos" )
$SUDO brew update $SUDO brew update
$SUDO brew install git python3 zsh curl tmux vim util-linux $SUDO brew install git zsh curl tmux vim util-linux
;; ;;
"msys" ) "msys" )
pacman -Syu pacman -Syu
pacman -S tmux git zsh bash curl vim python3 python3-pip pacman -S tmux git zsh bash curl vim
SUDO="" SUDO=""
;; ;;
* ) fmt_error "dfs auto-install is not implemented on OS: $(get_os_type)" * ) fmt_error "dfs auto-install is not implemented on OS: $(get_os_type)"
esac esac
if [[ -x $(command -v pip3) ]]; then
$SUDO pip3 install requests
elif [[ -x $(command -v pip) ]]; then
$SUDO pip install requests
else
fmt_error "pip3 and pip not found. is pip correctly installed?"
fi
} }
preinstall_check() preinstall_check()
{ {
fmt_note "checking requirements ..." fmt_note "checking requirements ..."
local mandatory_commands=( "git" "zsh" "curl" ) local mandatory_commands=( "git" "zsh" "curl" "grep" "cat" "cp" "bash" "mkdir" )
local optional_commands=( "python3" "vim" "tmux" "ping" ) local optional_commands=( "vim" "tmux" "ping" )
for i in "${mandatory_commands[@]}"; do for i in "${mandatory_commands[@]}"; do
if [[ ! -x "$(command -v $i)" ]]; then if ! command -v $i 1>/dev/null; then
fmt_info "all this utils are required: ${mandatory_commands[@]}" fmt_info "all this utils are required: ${mandatory_commands[@]}"
fmt_info "install them manually or check scripts in tools/" fmt_info "install them manually or check scripts in tools/"
fmt_fatal "\"$i\" not found. aborting ..." fmt_fatal "\"$i\" not found. aborting ..."
fi fi
done done
for i in "${optional_commands[@]}"; do for i in "${optional_commands[@]}"; do
if [[ ! -x "$(command -v $i)" ]]; then if ! command -v $i 1>/dev/null; then
fmt_warning "\"$i\" not found" fmt_warning "\"$i\" not found"
ask_for_Yn "continue anyway?" ask_for_Yn "continue anyway?"
if [[ "$?" == "0" ]]; then if [[ "$?" == "0" ]]; then
@ -219,8 +211,8 @@ install_update(){
cp "${DOTFILES}/.update.sh" "${DOTFILES}/update.sh" cp "${DOTFILES}/.update.sh" "${DOTFILES}/update.sh"
chmod +x "${DOTFILES}/update.sh" chmod +x "${DOTFILES}/update.sh"
fmt_note "running update.sh ..." fmt_note "running update.sh ..."
DFS_UPDATED_RET=1 ${DOTFILES}/update.sh DFS_UPDATED_RET=85 ${DOTFILES}/update.sh
if [[ $? == 1 ]]; then if [[ $? == 85 ]]; then
fmt_note "dfs updated. re-running install.sh ..." fmt_note "dfs updated. re-running install.sh ..."
"${DOTFILES}/install.sh" && exit "${DOTFILES}/install.sh" && exit
fi fi
@ -232,12 +224,13 @@ uninstall_update(){
} }
install(){ install(){
install_update
if [[ "$INSTALL_DEP" == "1" ]]; then install_dependencies; fi if [[ "$INSTALL_DEP" == "1" ]]; then install_dependencies; fi
install_update
preinstall_check preinstall_check
install_crontab install_crontab
install_file_content install_file_content
install_symlink install_symlink
post_beacon "dfs.installed" 1>/dev/null &
# those that won't be uninstalled in the future # those that won't be uninstalled in the future
install_tmux_tpm install_tmux_tpm
install_vim_vundle install_vim_vundle
@ -254,6 +247,7 @@ uninstall(){
uninstall_crontab uninstall_crontab
uninstall_file_content uninstall_file_content
uninstall_symlink uninstall_symlink
post_beacon "dfs.uninstalled" 1>/dev/null &
fmt_note "done uninstalling!" fmt_note "done uninstalling!"
} }
@ -267,7 +261,8 @@ for i in ${PARSE_ARG_RET[@]}; do
-d|--dev ) export DFS_DEV=1 ;; -d|--dev ) export DFS_DEV=1 ;;
-l|--lite ) export DFS_LITE=1 ;; -l|--lite ) export DFS_LITE=1 ;;
-a|--auto ) INSTALL_DEP=1 ;; -a|--auto ) INSTALL_DEP=1 ;;
* ) fmt_fatal "unknown option \"$i\". available: -i, -r, -q, -d, -l, -a" ;; -s|--secure ) export DFS_DEV=0 ;;
* ) fmt_fatal "unknown option \"$i\". available: -i, -r, -q, -d, -l, -a, -s" ;;
esac esac
done done
$FUNC $FUNC

View File

@ -109,7 +109,7 @@ parse_arg()
while [[ $# > 0 || -n "$ARG" ]]; do while [[ $# > 0 || -n "$ARG" ]]; do
if [[ -z "$ARG" ]]; then ARG=$1; shift; fi if [[ -z "$ARG" ]]; then ARG=$1; shift; fi
case $ARG in case $ARG in
-q*|--quite ) DFS_QUIET=1 ;; -q*|--quite ) export DFS_QUIET=1 ;;
--* ) PARSE_ARG_RET+=("$ARG") ;; --* ) PARSE_ARG_RET+=("$ARG") ;;
-* ) PARSE_ARG_RET+=("${ARG:0:2}") ;; -* ) PARSE_ARG_RET+=("${ARG:0:2}") ;;
* ) PARSE_ARG_RET+=("$ARG") ;; * ) PARSE_ARG_RET+=("$ARG") ;;
@ -148,7 +148,12 @@ ask_for_Yn()
post_log() post_log()
{ {
python3 "${DOTFILES}/tools/log.py" "[$1] $2: $3" "${DOTFILES}/tools/logger.sh" "log" "[$1][$2] $3"
}
post_beacon()
{
"${DOTFILES}/tools/logger.sh" "beacon" "$1"
} }
get_os_type() { get_os_type() {

View File

@ -1,66 +0,0 @@
#!/usr/bin/env python3
import os, uuid, socket, argparse, logging
namespace = uuid.UUID("cc23b903-1993-44eb-9c90-48bd841eeac3")
logging.basicConfig(level=logging.INFO, format="[%(filename)s:%(lineno)d][%(levelname)s] %(message)s")
try:
import requests
except ImportError:
logging.critical("Please install requests module")
exit(1)
def get_uuid_raw() -> str:
possible_uuid_files = [
"/var/lib/dbus/machine-id",
"/etc/machine-id",
os.path.join(os.path.expanduser('~'), ".config/dotfiles/uuid"),
]
for i in possible_uuid_files:
if os.path.exists(i):
with open(i, "r") as f:
return f.read().strip()
if not os.path.exists(os.path.dirname(possible_uuid_files[-1])):
os.makedirs(os.path.dirname(possible_uuid_files[-1]))
with open(possible_uuid_files[-1], 'w') as f:
ans = str(uuid.uuid4())
f.write(ans)
return ans
def get_uuid() -> str:
return str(uuid.uuid5(namespace, get_uuid_raw()))
def post_log(url:str, hostname:str, uuid:str, content:str):
ans = requests.post(url, params={"hostname": hostname, "uuid": uuid}, data=content)
return ans
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="post log to server")
parser.add_argument("-u", "--url", help="url to post to", default="https://api.beardic.cn/post-log")
parser.add_argument("content")
args = parser.parse_args()
url = args.url
content = args.content
hostname = socket.gethostname()
uuid = get_uuid()
content=content.strip()
if not content:
logging.error("empty log content")
exit(0)
resp = post_log(url, hostname, uuid, content)
if resp.status_code == 200:
logging.info("200 ok")
exit(0)
elif resp.status_code == 403:
logging.warning("403 forbidden")
logging.info("you may need to register your hostname and uuid")
logging.info(f"hostname: {hostname}, uuid: {uuid}")
exit(0)
else:
logging.critical("unknown error")
logging.error(f"{resp.status_code}: {resp.text}")
exit(1)

100
tools/logger.sh Executable file
View File

@ -0,0 +1,100 @@
#!/bin/bash
set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/common.sh"
if [[ -x $(command -v hostname) ]]; then
hostname=$(hostname)
elif [[ -x $(command -v uname) ]]; then
hostname=$(uname -n)
elif [[ -x $(command -v hostnamectl) ]]; then
hostname=$(hostnamectl --static)
elif [[ -n "$HOSTNAME" ]]; then
hostname=$HOSTNAME
elif [[ -f /proc/sys/kernel/hostname ]]; then
hostname=$(cat /proc/sys/kernel/hostname)
elif [[ -f /etc/hostname ]]; then
hostname=$(cat /etc/hostname)
else
fmt_fatal "unable to get hostname"
fi
init_uuid()
{
local raw_uuid
if [[ -f /var/lib/dbus/machine-id ]]; then
raw_uuid=$(cat /var/lib/dbus/machine-id)
elif [[ -f /etc/machine-id ]]; then
raw_uuid=$(cat /etc/machine-id)
elif [[ -f ~/.config/dotfiles/uuid ]]; then
raw_uuid=$(cat ~/.config/dotfiles/uuid)
else
mkdir -p ~/.config/dotfiles
raw_uuid=$(uuidgen)
echo "$raw_uuid" > ~/.config/dotfiles/uuid
fi
uuid=$(uuidgen -n "cc23b903-1993-44eb-9c90-48bd841eeac3" -s -N "$raw_uuid")
}
post_beacon()
{
local beacon_type=$1
if [[ -z "$beacon_type" ]]; then
fmt_fatal "beacon type is required"
fi
resp=$(curl -sSL -X POST "https://api.beardic.cn/post-beacon?hostname=$hostname&beacon=$beacon_type")
if grep -q "200" <<< "$resp"; then
echo $resp
else
echo $resp >&2
fmt_fatal "error posting beacon"
fi
}
post_log()
{
local log_content=$1
if [[ -z "$log_content" ]]; then
fmt_fatal "log content is required"
fi
init_uuid
resp=$(curl -sSL -X POST -H "Content-Type: text/plain" -d "$1" "https://api.beardic.cn/post-log?hostname=$hostname&uuid=$uuid")
if grep -q "200" <<< "$resp"; then
echo $resp
elif grep -q "403" <<< "$resp"; then
echo $resp >&2
fmt_error "error posting log: authentification failed"
fmt_info "try to register you hostname and uuid"
fmt_info "hostname: $hostname"
fmt_info "uuid: $uuid"
else
echo $resp >&2
fmt_fatal "error posting log"
fi
}
print_help()
{
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
}
if [[ $# != 2 ]]; then
print_help >&2
exit 1
fi
case "$1" in
-h|--help)
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
;;
beacon)
post_beacon "$2"
;;
log)
post_log "$2"
;;
*)
fmt_fatal "invalid argument"
;;
esac

View File

@ -17,6 +17,7 @@ grep -q ".zshrc2" ~/.zshrc
# check scripts and functions # check scripts and functions
dfs version dfs version
dfs log 1 dfs log 1
dfs beacon github-ci
z ~ z ~
test ~ -ef "$(pwd)" test ~ -ef "$(pwd)"
dogo dogo