* use update.sh to update safely

* add unknown command "version". available: update, force-update, reset, cd and replace tabs with spaces

* bug fix

* bug fix

* uninstall will rm update.sh

* new force-update

* bug fix

* remove thefuck

just use `pls`

* init post-log.py

* update uuid filepath

* update.sh will post log

* dfs log

* bug fix

* bug fix

* use logging

* logging format

* bug fix

* standarlize the config path

* bug fix

* add ssh key: ltp2
This commit is contained in:
Dict Xiong 2022-08-14 20:34:18 +08:00 committed by GitHub
parent bbeffe3313
commit c834980b29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 164 additions and 38 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.zwc
update.sh

View File

@ -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

29
.update.sh Normal file
View File

@ -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

19
.zshrc2
View File

@ -49,7 +49,6 @@ antigen_plugins=( \
"fzf" \
"git" \
"ripgrep" \
"thefuck" \
"tmux" \
"ufw" \
"z" \
@ -80,18 +79,26 @@ fi
dfs()
{
case $1 in
update ) (cd "$DOTFILES" && git pull) ;;
force-update ) (cd "$DOTFILES" && git fetch --all && git reset --hard origin/main && git pull) ;;
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
"$DOTFILES/install.sh" -r
dfs update
$DOTFILES/install.sh -i
"$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" ;;
log ) "$DOTFILES/tools/post-log.py" "$2" ;;
* ) echo "unknown command \"$1\". available: update, force-update, version, reset, cd, log" ;;
esac
}

View File

@ -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"

72
tools/post-log.py Executable file
View File

@ -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)