155 lines
4.8 KiB
Bash
Executable File
155 lines
4.8 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
# set -x
|
||
set -e
|
||
|
||
TS_GIT_MIRROR=${ENV_TS_GIT_MIRROR:-}
|
||
TS_GIT_COMMIT=${ENV_TS_GIT_COMMIT:-}
|
||
TS_DOCKER_REPOSITORY=${ENV_TS_DOCKER_REPOSITORY:-}
|
||
TS_LOG=${ENV_TS_LOG:-}
|
||
|
||
TS_GIT_REPOSITORY="https://github.com/Thinstation/thinstation.git"
|
||
TS_GIT_BRANCH="6.2-Stable"
|
||
TS_GIT_PATH="/thinstation"
|
||
TS_DOCKER_IMAGE_NAME="thinstation"
|
||
TS_LOG_BUILD_FILE="/setup-chroot.log"
|
||
|
||
TS_INFO=$(cat <<-EOF
|
||
VERSION={TS_VERSION}
|
||
COMMIT={GIT_HASH}
|
||
HASH={GIT_HASH_SHORT}
|
||
DOCKER={DOCKER_IMAGE}
|
||
EOF
|
||
)
|
||
|
||
ts_git_clone() {
|
||
local git_branch=${1}
|
||
local git_repository=${2}
|
||
|
||
if ! git clone --single-branch --branch ${git_branch} --depth 1 ${git_repository} ${TS_GIT_PATH} 2>/dev/null ; then
|
||
echo "Произошла ошибка при клонировании репозитория"
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
ts_git_find_hash() {
|
||
local git_hash=${1}
|
||
local current_hash=$(git -C ${TS_GIT_PATH} rev-parse HEAD)
|
||
|
||
if [ ${git_hash} != ${current_hash} ]; then
|
||
echo "Репозиторий будет загружен полностью"
|
||
git -C ${TS_GIT_PATH} fetch --unshallow
|
||
if git -C ${TS_GIT_PATH} log ${git_hash} --oneline -n 1 2>/dev/null ; then
|
||
echo "Переключение на указанный коммит: ${git_hash}"
|
||
git -C ${TS_GIT_PATH} checkout ${git_hash}
|
||
else
|
||
echo "Указанный хеш отсутствует в репозитории: ${git_hash}"
|
||
exit 1
|
||
fi
|
||
fi
|
||
}
|
||
|
||
ts_setup_chroot() {
|
||
local ts_version=${1}
|
||
local ts_git_hash=${2}
|
||
local ts_git_hash_short=${3}
|
||
local ts_docker_image=${4}
|
||
|
||
cd ${TS_GIT_PATH}
|
||
rm -rf .git
|
||
./setup-chroot -i > ${TS_LOG_BUILD_FILE} 2>&1
|
||
cp -r build/conf/tiny/* build/
|
||
|
||
echo "${TS_INFO}" | \
|
||
sed -e "s/{TS_VERSION}/${ts_version}/" \
|
||
-e "s/{GIT_HASH}/${ts_git_hash}/" \
|
||
-e "s/{GIT_HASH_SHORT}/${ts_git_hash_short}/" \
|
||
-e "s/{DOCKER_IMAGE}/${ts_docker_image}/" > ts_version
|
||
}
|
||
|
||
ts_docker_check() {
|
||
local repository=${1}
|
||
local image_version=${2}
|
||
local image_name="${TS_DOCKER_IMAGE_NAME}:${image_version}"
|
||
local image="${repository}/${image_name}"
|
||
|
||
{ docker images --filter "reference=${image}" | tail -n 1 | awk '{ print $1":"$2 }' | grep -q "${image}"; } || \
|
||
{ curl -I -s "${repository}/v2/${TS_DOCKER_IMAGE_NAME}/manifest/${image_version}" | head -n 1 | cut -d$' ' -f3 | grep -q "OK"; }
|
||
}
|
||
|
||
ts_docker_build() {
|
||
local docker_image=${1}
|
||
|
||
export DOCKER_IMAGE="${TS_DOCKER_REPOSITORY}/${docker_image}"
|
||
|
||
cd /
|
||
docker compose --project-name thinstation build --no-cache --push
|
||
rm -rf /thinstation
|
||
}
|
||
|
||
main() {
|
||
while [ $# -gt 0 ]; do
|
||
key="${1}"
|
||
value="${2}"
|
||
case "${key}" in
|
||
mirror)
|
||
TS_GIT_MIRROR=${value}
|
||
shift
|
||
;;
|
||
branch)
|
||
TS_GIT_BRANCH=${value}
|
||
shift
|
||
;;
|
||
commit)
|
||
TS_GIT_COMMIT=${value}
|
||
shift
|
||
;;
|
||
docker)
|
||
TS_DOCKER_REPOSITORY=${value}
|
||
shift
|
||
;;
|
||
log)
|
||
TS_LOG=${value}
|
||
shift
|
||
;;
|
||
esac
|
||
shift
|
||
done
|
||
|
||
# [ -z ${TS_GIT_BRANCH} ] && echo "Не указана ветка репозитория" && exit 1
|
||
[ -z ${TS_GIT_COMMIT} ] && echo "Не указан коммит" && exit 1
|
||
[ -z ${TS_DOCKER_REPOSITORY} ] && echo "Не указан docker-репозиторий" && exit 1
|
||
|
||
if [ ! -z ${TS_GIT_MIRROR} ]; then
|
||
echo "Был установлен удаленный git-репозиторий в качестве источника: ${TS_GIT_MIRROR}"
|
||
TS_GIT_REPOSITORY=${TS_GIT_MIRROR}
|
||
fi
|
||
|
||
local TS_VERSION=${TS_GIT_BRANCH%%-*}
|
||
local TS_HASH_SHORT=${TS_GIT_COMMIT::7}
|
||
|
||
local TS_DOCKER_IMAGE_VERSION="${TS_VERSION}-${TS_HASH_SHORT}"
|
||
local TS_DOCKER_IMAGE="${TS_DOCKER_IMAGE_NAME}:${TS_DOCKER_IMAGE_VERSION}"
|
||
|
||
if ts_docker_check ${TS_DOCKER_REPOSITORY} ${TS_DOCKER_IMAGE_VERSION} ; then
|
||
echo "Образ указанной версии Thinstation уже существует: ${TS_DOCKER_IMAGE}"
|
||
exit 0
|
||
fi
|
||
|
||
# Клонирование репозитория
|
||
ts_git_clone ${TS_GIT_BRANCH} ${TS_GIT_REPOSITORY}
|
||
# Переключение на необходимый коммит
|
||
ts_git_find_hash ${TS_GIT_COMMIT}
|
||
# Распаковка и настройка Thinstation
|
||
ts_setup_chroot ${TS_VERSION} ${TS_GIT_COMMIT} ${TS_HASH_SHORT} ${TS_DOCKER_IMAGE}
|
||
# Сборка docker-образа
|
||
ts_docker_build ${TS_DOCKER_IMAGE}
|
||
|
||
if [ ! -z ${TS_LOG} ] && [ -e ${TS_LOG} ]; then
|
||
echo "Журнал сборки будет скопирован в ${TS_LOG}"
|
||
cp -v ${TS_LOG_BUILD_FILE} ${TS_LOG}
|
||
fi
|
||
}
|
||
|
||
main ${@}
|