1
0
mirror of https://gitee.com/sui-feng-cb/AzurLaneAutoScript1 synced 2026-03-09 18:39:04 +08:00
AzurLaneAutoScript/dev_tools/Docker-run.sh

55 lines
1.6 KiB
Bash
Raw Normal View History

#!/bin/env bash
2021-11-27 23:46:45 +08:00
pprint() { # Pretty print
2021-11-28 19:56:30 +08:00
echo -e "==> \e[36m${1}\e[39m"
}
2021-11-27 23:46:45 +08:00
prun() { # Pretty run
2021-11-28 19:56:30 +08:00
pprint "$ ${1}"
eval " ${1}" || exit 1
}
prun_or_continue() { # Pretty run or continue
pprint "$ ${1}"
eval " ${1}" || pprint "↳ Not needed!"
}
2021-11-28 19:56:30 +08:00
SOURCE="$(dirname $(realpath ${BASH_SOURCE}))" # Poiting to dev_tools folder
2021-11-27 23:46:45 +08:00
CONTAINER="azurlaneautoscript"
2021-11-28 19:56:30 +08:00
# Sanity checks
if [[ "$(id -u)" -eq 0 ]]; then
pprint "Don't run this as root!"
exit 1
fi
# Create a lockfile so only one instance of this script can run
if [[ -f "${XDG_RUNTIME_DIR}/${CONTAINER}.lock" ]]; then
pprint "This script is already running!"
pprint "If this is not the case, please restart your computer!"
exit 1
else
touch "${XDG_RUNTIME_DIR}/${CONTAINER}.lock"
fi
# ALAS update
2021-11-27 23:46:45 +08:00
pprint "Updating this repo"
2021-11-28 19:56:30 +08:00
prun "cd ${SOURCE}/.."
2021-11-28 00:00:21 +08:00
prun "git fetch origin master"
2021-11-27 23:46:45 +08:00
prun "git stash"
2021-11-28 00:01:39 +08:00
prun "git pull --ff origin master"
2021-11-28 19:56:30 +08:00
prun_or_continue "git stash pop"
2021-11-28 19:56:30 +08:00
# Container cleanup
2021-11-27 23:46:45 +08:00
pprint "Killing any previous container"
2021-11-28 19:56:30 +08:00
prun "docker ps | grep ${CONTAINER} | awk '{print \$1}' | xargs -r -n1 docker kill"
pprint "Deleting old containers"
prun "docker ps -a | grep ${CONTAINER} | awk '{print \$1}' | xargs -r -n1 docker rm"
2021-11-27 23:46:45 +08:00
2021-11-28 19:56:30 +08:00
pprint "Build the image"
prun "docker build -t ${CONTAINER} -f ${SOURCE}/Dockerfile ${SOURCE}/.."
2021-11-27 23:46:45 +08:00
2021-11-28 19:56:30 +08:00
pprint "Killing any adb servers in the host machine"
prun_or_continue "adb kill-server"
pprint "Running the container"
2021-11-28 19:56:30 +08:00
trap "rm ${XDG_RUNTIME_DIR}/${CONTAINER}.lock && docker kill ${CONTAINER}" EXIT
prun "docker run --net=host --volume=${SOURCE}/..:/app/AzurLaneAutoScript:rw --interactive --tty --name ${CONTAINER} ${CONTAINER}"