Last active
July 28, 2019 10:36
-
-
Save apwan/40eb6d2a8ba92e8f4670762c78746d7f to your computer and use it in GitHub Desktop.
daemon for git/pypi http server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# check if git/pypi http servers fail # crontab -e | |
*/20 * * * * root /usr/bin/bash -c ". ~/.bashrc; svgits.sh --check >/dev/null 2>&1" | |
*/30 * * * * root /usr/bin/bash -c ". ~/.bashrc; svpypis.sh --check >/dev/null 2>&1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89c89,90 | |
< var repo = u.pathname.split('/')[1]; | |
--- | |
> var repo = /\/(.*)\/(HEAD|info\/refs|git-[^/]+|objects\/(info\/[^/]+|[0-9a-f]{2}\/[0-9a-f]{38}|pack\/pack-[0-9a-f]{40}\.(pack|idx)))$/.exec(u.pathname); | |
> if(!repo){ repo='';}else{repo=repo[1]}; //u.pathname.split('/')[1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
# get PID of TCP process listening at a port | |
lsof -i:$1 -sTCP:LISTEN|awk 'NR > 1 {print $2}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
if [ "$#" -lt 1 ] ; then | |
# may set $GIT_REPO_ROOT | |
echo "Usage: [CMD] [--option]" >&2 | |
exit 1; | |
fi | |
PIDCMD=pidofport | |
pidofport_(){ | |
lsof -i:$1 -sTCP:LISTEN|awk 'NR > 1 {print $2}' | |
} | |
# built-in pidofport | |
[ -x "$(which $PIDCMD 2>/dev/null)" ] || PIDCMD=pidofport_ | |
REPO_DIR=${GIT_REPO_ROOT:-/data/repos} | |
LOG_DIR=${REPO_DIR}/.log | |
PORT_FILE=${REPO_DIR}/.ports | |
[ -d "$REPO_DIR" ] || ( mkdir -p "$REPO_DIR" && echo \ | |
"create $REPO_DIR/ ! " >&2 ) | |
[ -d "$LOG_DIR" ] || ( mkdir -p "$LOG_DIR" && echo \ | |
"create $LOG_DIR/ ! " >&2 ) | |
[ -f "$PORT_FILE" ] || ( echo "8700 8701" > "$PORT_FILE" && echo \ | |
"create $PORT_FILE ! " >&2 ) | |
# stop server | |
if [ "$1" = "--stop" ] ; then | |
for pp in $(<${PORT_FILE});do | |
kill -9 "$(${PIDCMD} ${pp})" 2>/dev/null; | |
done | |
echo "$(date) : stopped! " >> ${LOG_DIR}/check.log; | |
exit 0; | |
fi | |
# npm install -g git-http-server # and apply patch | |
# patch -b $(readlink -f $(which git-http-server)) -i ./git-http-server.patch | |
SERVER_CMD=git-http-server | |
CMD_DIR=$(dirname $(which "$0")) | |
which $SERVER_CMD &>/dev/null || SERVER_CMD=${CMD_DIR}/${SERVER_CMD} # | |
[ -x "$(which $SERVER_CMD 2>/dev/null )" ] || ( echo "need $SERVER_CMD ! " >&2 && exit 1 ) | |
# launch server | |
for pp in $(<${PORT_FILE});do | |
if [[ -z "$(${PIDCMD} ${pp})" ]]; then | |
# launch again | |
[ "$1" = "--check" ] && echo "$(date) : restart at port ${pp} " >> ${LOG_DIR}/check.log; | |
${SERVER_CMD} -p ${pp} ${REPO_DIR} &>>${LOG_DIR}/access_${pp}.log & | |
fi | |
done | |
[ "$1" = "--check" ] && echo "$(date) : checked! " >> ${LOG_DIR}/check.log | |
[ "$1" = "--start" ] && echo "$(date) : launched! " >> ${LOG_DIR}/check.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
if [ "$#" -lt 1 ] ; then | |
# may set $PYPI_PKG_ROOT | |
echo "Usage: [CMD] [--option]" >&2 | |
exit 1; | |
fi | |
PIDCMD=pidofport | |
pidofport_(){ | |
lsof -i:$1 -sTCP:LISTEN|awk 'NR > 1 {print $2}' | |
} | |
# built-in pidofport | |
[ -x "$(which $PIDCMD 2>/dev/null)" ] || PIDCMD=pidofport_ | |
PKG_DIR=${PYPI_PKG_ROOT:-/data/pkgs} | |
LOG_DIR=${PKG_DIR}/.log | |
PORT_FILE=${PKG_DIR}/.ports | |
HTP_FILE=${PKG_DIR}/.htpasswd | |
[ -d "$PKG_DIR" ] || ( mkdir -p "$PKG_DIR" && echo \ | |
"create $PKG_DIR/ ! " >&2 ) | |
[ -d "$LOG_DIR" ] || ( mkdir -p "$LOG_DIR" && echo \ | |
"create $LOG_DIR/ ! " >&2 ) | |
[ -f "$PORT_FILE" ] || ( echo "8800 8801" > "$PORT_FILE" && echo \ | |
"create $PORT_FILE ! " >&2 ) | |
[ -f "$HTP_FILE" ] || ( touch "$HTP_FILE" && echo \ | |
"create $HTP_FILE ! " >&2 ) | |
if [ "$1" = "--stop" ] ; then | |
for pp in $(<${PORT_FILE});do | |
kill -9 "$(${PIDCMD} ${pp})" 2>/dev/null; | |
done | |
echo "$(date) : stopped" >> ${LOG_DIR}/check.log | |
exit 0; | |
fi | |
# pip install pypiserver #https://pypi.org/project/pypiserver/ | |
SERVER_CMD=pypi-server | |
CMD_DIR=$(dirname $(which "$0")) | |
which $SERVER_CMD &>/dev/null || SERVER_CMD=${CMD_DIR}/${SERVER_CMD} # | |
[ -x "$(which $SERVER_CMD 2>/dev/null )" ] || ( echo "need $SERVER_CMD ! " >&2 && exit 1 ) | |
for pp in $(<${PORT_FILE}); do | |
if [[ -z "$(${PIDCMD} ${pp})" ]]; then | |
# launch again | |
[ "$1" = "--check" ] && echo "$(date) : restart at port ${pp}" >> ${LOG_DIR}/check.log; | |
${SERVER_CMD} -o -v -p ${pp} -P ${HTP_FILE} --log-file ${LOG_DIR}/access_${pp}.log ${PKG_DIR} 2>>${LOG_DIR}/error_${pp}.log & | |
fi | |
done; | |
[ "$1" = "--start" ] && echo "$(date) : started! " >> ${LOG_DIR}/check.log | |
[ "$1" = "--check" ] && echo "$(date) : checked! " >> ${LOG_DIR}/check.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment