diff --git a/.gitignore b/.gitignore index 416cfaa..2fb45f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.zwc +update.sh diff --git a/.ssh/authorized_keys2 b/.ssh/authorized_keys2 index cb1ba05..e092806 100644 --- a/.ssh/authorized_keys2 +++ b/.ssh/authorized_keys2 @@ -19,6 +19,8 @@ ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFO4k0tJ+Bfu95Uavg/5P3EXMKNcq+bMqaTqzkvRZ7ji ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN8g34WHLEix6Qt0J/ClYbZeb9wh+p1IOcZkz/vNbZrv ltp1/windows +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBN24pvN1RMN+iSZyHPdyExA1Rvt8pdr3e6ih6iX+KrQ ltp2 + ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGZFTyR+R86fUpZBDkDR9yYzJpmeNsIv2CMHTh6EuEeq pc0/windows ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID8WHIpwdZl7+HIZfQpwIAhpxT1huI9sd6Ydeokilg5l pc1/windows diff --git a/.update.sh b/.update.sh new file mode 100644 index 0000000..06df086 --- /dev/null +++ b/.update.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +export DOTFILES=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) +# get the specified commit id +dfs_commit=$(curl -fsSL https://api.beardic.cn/get-var/dfs-commit-id) +if [[ ${#dfs_commit} != 40 ]]; then + echo "Error: invalid commit id." + python3 "${DOTFILES}/tools/post-log.py" "[ERROR] update.sh: invalid commit id: ${dfs_commit}" + exit +fi +# fetch origin +cd $DOTFILES +git fetch +if [[ -n "$(git status -s)" ]]; then + echo "Error: directory not clean." + python3 "${DOTFILES}/tools/post-log.py" "[ERROR] update.sh: directory not clean" + exit +fi +# update +if [[ "$(git rev-parse HEAD)" == "$dfs_commit" ]]; then + echo "Nothing to do." + python3 "${DOTFILES}/tools/post-log.py" "[INFO] update.sh: Nothing to do" +else + echo "Checking out to commit $dfs_commit ..." + git -c advice.detachedHead=false checkout $dfs_commit + cp ./.update.sh ./update.sh + chmod +x ./update.sh + python3 "${DOTFILES}/tools/post-log.py" "[INFO] update.sh: Checked out to commit $dfs_commit" +fi diff --git a/.zshrc2 b/.zshrc2 index 7932159..22511ce 100644 --- a/.zshrc2 +++ b/.zshrc2 @@ -20,23 +20,23 @@ ANTIGEN_URL="https://gitee.com/dictxiong/antigen/raw/develop/bin/antigen.zsh" ANTIGEN="$HOME/antigen.zsh" # Install antigen.zsh if not exist if [ ! -f "$ANTIGEN" ]; then - echo "Installing antigen ..." - TMPFILE="/tmp/antigen.zsh" - if [ -x "$(which curl)" ]; then - curl -L "$ANTIGEN_URL" -o "$TMPFILE" - elif [ -x "$(which wget)" ]; then - wget "$ANTIGEN_URL" -O "$TMPFILE" - else - echo "ERROR: please install curl or wget before installation !!" - exit - fi - if [ ! $? -eq 0 ]; then - echo "" - echo "ERROR: downloading antigen.zsh ($ANTIGEN_URL) failed !!" - exit - fi; - echo "move $TMPFILE to $ANTIGEN" - mv "$TMPFILE" "$ANTIGEN" + echo "Installing antigen ..." + TMPFILE="/tmp/antigen.zsh" + if [ -x "$(which curl)" ]; then + curl -L "$ANTIGEN_URL" -o "$TMPFILE" + elif [ -x "$(which wget)" ]; then + wget "$ANTIGEN_URL" -O "$TMPFILE" + else + echo "ERROR: please install curl or wget before installation !!" + exit + fi + if [ ! $? -eq 0 ]; then + echo "" + echo "ERROR: downloading antigen.zsh ($ANTIGEN_URL) failed !!" + exit + fi; + echo "move $TMPFILE to $ANTIGEN" + mv "$TMPFILE" "$ANTIGEN" fi # config and enable antigen source "$ANTIGEN" @@ -49,7 +49,6 @@ antigen_plugins=( \ "fzf" \ "git" \ "ripgrep" \ - "thefuck" \ "tmux" \ "ufw" \ "z" \ @@ -79,29 +78,37 @@ fi dfs() { - case $1 in - update ) (cd "$DOTFILES" && git pull) ;; - force-update ) (cd "$DOTFILES" && git fetch --all && git reset --hard origin/main && git pull) ;; - reset ) - antigen reset 1> /dev/null - rm -rf $HOME/.antigen - $DOTFILES/install.sh -r - dfs update - $DOTFILES/install.sh -i - echo 'Done. Please open a new shell to see the changes.' - ;; - cd ) cd "$DOTFILES" ;; - * ) echo "unknown command \"$1\". available: update, force-update, reset, cd" ;; - esac + case $1 in + update ) "$DOTFILES/update.sh" ;; + force-update ) ( + cd "$DOTFILES" + git fetch --all + ref=$(git symbolic-ref --short HEAD 2> /dev/null) || ref=$(git rev-parse --short HEAD 2> /dev/null) || return 0 + for b in $(git for-each-ref refs/heads --format='%(refname)') ; do git checkout ${b#refs/heads/} ; git pull --ff-only ; done + git checkout -c advice.detachedHead=false $ref) + ;; + version ) (cd "$DOTFILES" && git rev-parse HEAD) ;; + reset ) + antigen reset 1> /dev/null + rm -rf $HOME/.antigen + "$DOTFILES/install.sh" -r + dfs update + "$DOTFILES/install.sh" -i + echo 'Done. Please open a new shell to see the changes.' + ;; + cd ) cd "$DOTFILES" ;; + log ) "$DOTFILES/tools/post-log.py" "$2" ;; + * ) echo "unknown command \"$1\". available: update, force-update, version, reset, cd, log" ;; + esac } dogo() { - if [[ -z "$1" || "$1" =~ "-h|--help" ]]; then - echo "usage: dogo " - else - $SUDO docker exec -it $1 zsh - fi + if [[ -z "$1" || "$1" =~ "-h|--help" ]]; then + echo "usage: dogo " + else + $SUDO docker exec -it $1 zsh + fi } # alias diff --git a/install.sh b/install.sh index dccd4fe..fdde7cb 100755 --- a/install.sh +++ b/install.sh @@ -96,7 +96,7 @@ if [[ ! $dotfile_path == ${home_slashes}* ]]; then fi dotfile_home_path=${dotfile_path/${home_slashes}/\~} dotfile_relative_path=${dotfile_path#${home_slashes}\/} -crontab_job="0 * * * * cd ${dotfile_path} && env git pull" +crontab_job="0 * * * * ${dotfile_path}/update.sh" ask_for_yN() { @@ -209,7 +209,21 @@ install_vim_vundle(){ fi } +install_update(){ + fmt_note "installing update.sh ..." + cp "${dotfile_path}/.update.sh" "${dotfile_path}/update.sh" + chmod +x "${dotfile_path}/update.sh" + fmt_note "running update.sh ..." + ${dotfile_path}/update.sh +} + +uninstall_update(){ + fmt_note "removing update.sh ..." + rm "${dotfile_path}/update.sh" +} + install(){ + install_update install_crontab insert_if_not_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2" insert_if_not_exist "${HOME}/.tmux.conf" "source-file ${dotfile_home_path}/.tmux.conf2" @@ -225,6 +239,7 @@ install(){ uninstall(){ ask_for_yN "do you really want to uninstall?" if [[ $? == 1 ]]; then + uninstall_update uninstall_crontab delete_if_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2" delete_if_exist "${HOME}/.tmux.conf" "source-file ${dotfile_home_path}/.tmux.conf2" diff --git a/tools/post-log.py b/tools/post-log.py new file mode 100755 index 0000000..9cc7c60 --- /dev/null +++ b/tools/post-log.py @@ -0,0 +1,72 @@ +#!/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.fatal("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 get_hostname() -> str: + ans = socket.gethostname() + if '-' not in ans: + ans += "-ibd-ink" + return ans + + +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 = get_hostname() + uuid = get_uuid() + content=content.strip() + if not content: + logging.error("empty log content") + exit(1) + resp = post_log(url, hostname, uuid, content) + if resp.status_code == 200: + logging.info("200 ok") + exit(0) + elif resp.status_code == 403: + logging.error("403 forbidden") + logging.info("you may need to register your hostname and uuid") + else: + logging.error("unknown error ") + logging.error(f"{resp.status_code}: {resp.text}") + logging.info(f"hostname: {hostname}, uuid: {uuid}") + exit(1)