|
- #!/usr/bin/env bash
- #
- # Update the version number in Dockerfiles
- #
- # Author: donkey <anjingyu_ws@foxmail.com>
-
- # Assume the terminal is a MODERN terminal
- # support colorful output
- normal="\e[0m"
- black="\e[0;30m"
- red="\e[0;31m"
- green="\e[0;32m"
- yellow="\e[0;33m"
- blue="\e[0;34m"
- magenta="\e[0;35m"
- cyan="\e[0;36m"
- white="\e[0;37m"
-
- readonly CUR_DIR=$(cd `dirname $0`; pwd)
- readonly PROJ_DIR=`dirname $CUR_DIR`
- readonly DOCKER_DIR="$PROJ_DIR/docker"
- readonly PYPI_HOST="http://172.23.140.35:8088/repository/pypi-hosted/packages"
-
- function help()
- {
- echo "Usage: `basename $0` [-h,--help] [-t,--tag TAG]"
- echo
- echo "Options:"
- echo -e " ${green}-h${normal},${green}--help${normal} Show help and exit."
- echo -e " ${green}-t${normal},${green}--tag${yellow} TAG${normal} Specify the tag to update, default: version in __init__.py"
- echo -e " ${green}-i${normal},${green}--image${normal} Update the image files"
- echo -e " ${green}-l${normal},${green}--local${normal} Use local whl package"
- echo
- echo "Examples:"
- echo " ./`basename $0` -t 0.5.16"
- echo
- }
-
- function main()
- {
- local _TAG=""
- local _UPDATE_IMG="false"
- local _LOCAL_PKG="false"
- local _FORCE="false"
-
- while [ $# -ne 0 ]; do
- case "$1" in
- -t|--tag)
- shift
- _TAG="$1"
- ;;
- -i|--image)
- _UPDATE_IMG="true"
- ;;
- -l|--local)
- _LOCAL_PKG="true"
- ;;
- -f|--force)
- _FORCE="true"
- ;;
- -h|--help)
- help
- exit 0
- ;;
- *)
- ;;
- esac
- shift
- done
-
- if [ -z "${_TAG}" ]; then
- while read line; do
- # Skip the empty lines and commented lines
- if [ -n "$line" ] && ! [[ "$line" = \#* ]]; then
- _TAG=$(sed "s/.*__version__\s*=\s*\"\(.*\)\".*/\1/" <<< "$line")
- if [ -n "${_TAG}" ]; then
- break
- fi
- fi
- done < "${PROJ_DIR}/adtools/__init__.py"
- fi
-
- # Check this tags in all Dockerfiles
- for dockerfile in `ls $DOCKER_DIR/Dockerfile*`; do
- # Get the tag in current Dockerfile
- _DFTAG=$(sed -n "s/.*ADTOOLS_VERSION\s*=\s*\"\(.*\)\".*/\1/p" $dockerfile)
- if [ "${_DFTAG}" != "${_TAG}" ]; then
- echo -e "[I] ${cyan}Update the version number from ${yellow}${_DFTAG}${cyan} to ${green}$_TAG${cyan} for `basename ${dockerfile}`${normal}"
- sed -i "s/.*ADTOOLS_VERSION\s*=\s*\".*\".*/ARG ADTOOLS_VERSION=\"${_TAG}\"/" $dockerfile 1>/dev/null 2>&1
- fi
- done
-
- if [ "${_UPDATE_IMG}" = "true" ]; then
- pushd "$DOCKER_DIR" 1>/dev/null 2>&1
- for dockerfile in `ls Dockerfile*`; do
- local _IMG_TAG=${dockerfile#Dockerfile.*.}
- local _ARCH=${dockerfile#Dockerfile.}
- _ARCH=${_ARCH%%.*}
-
- echo -e "${cyan}-> Checking ${yellow}admake-cross-linux-${_ARCH}:${_IMG_TAG}${cyan} ...${normal}"
- local _IMG="admake-cross-linux-${_ARCH}:${_IMG_TAG}"
-
- # The docker image is existent, attempt update the adtools
- if docker image inspect "$_IMG" 1>/dev/null 2>&1; then
- local _VER=$(docker run -it --rm "${_IMG}" /bin/bash -ic 'admake --version' | tr -d '\r')
- # Remove color control sequence
- _VER=$(echo "$_VER" | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g")
-
- echo -e "[I] ${cyan}Version: ${_VER} -> ${_TAG}${normal}"
-
- if [ "${_VER}" != "${_TAG}" ] || [ "$_FORCE" = "true" ]; then
- # Update the docker image
- local _SUFFIX=$(head -c 4 /dev/urandom | hexdump -v -e '/1 "%02X"')
-
- local _FORCE_PARAM=""
- if [ "$_FORCE" = "true" ]; then
- _FORCE_PARAM="--force-reinstall"
- fi
-
- if [ "${_LOCAL_PKG}" = "true" ]; then
- local ADTOOLS_PKG="${PROJ_DIR}/dist/adtools-${_TAG}-py3-none-any.whl"
- if [ ! -f "$ADTOOLS_PKG" ]; then
- echo -e "[W] ${yellow}Required package ${cyan}$ADTOOLS_PKG ${yellow}is missing, attempt to build it.${normal}"
- pushd $PROJ_DIR 1>/dev/null 2>&1
- if command -v "pyproject-build" 1>/dev/null 2>&1; then
- pyproject-build --wheel
- else
- echo -e "[E] ${yellow}Required package ${cyan}build${yellow}is missing, please install it.${normal}"
- exit 1
- fi
- popd 1>/dev/null 2>&1
- fi
-
- if [ ! -f "$ADTOOLS_PKG" ]; then
- echo -e "[E] ${red}Required package ${cyan}$ADTOOLS_PKG ${red}is missing.${normal}"
- exit 1
- fi
-
- docker run -it -v "${ADTOOLS_PKG}:/tmp/adtools-${_TAG}-py3-none-any.whl" --name "build_${_SUFFIX}" "${_IMG}" /bin/bash -ic "python3 -m pip install ${_FORCE_PARAM} /tmp/adtools-${_TAG}-py3-none-any.whl"
- else
- local ADTOOLS_URL="${PYPI_HOST}/adtools/${_TAG}/adtools-${_TAG}-py3-none-any.whl"
- docker run -it --name "build_${_SUFFIX}" "${_IMG}" /bin/bash -ic "curl -O \"${ADTOOLS_URL}\" && python3 -m pip install ${_FORCE_PARAM} ./adtools-${_TAG}-py3-none-any.whl && rm ./adtools-${_TAG}-py3-none-any.whl"
- fi
-
- echo -e "[I] Commit new version adtools: ${green}${_TAG}${normal}"
- docker commit "build_${_SUFFIX}" ${_IMG}
- # Cleanup the exited container
- docker container rm -f "build_${_SUFFIX}"
- fi
- fi
- done
- popd 1>/dev/null 2>&1
- fi
- }
-
- main "$@"
|