cloudron-support: better recreate-docker

This commit is contained in:
Girish Ramakrishnan
2024-06-14 10:57:40 +02:00
parent 424bc588f6
commit 7bad90009e

View File

@@ -467,6 +467,26 @@ function check_disk_space() {
fi
}
function do_recreate_containers() {
echo -e "Re-creating addon and app containers\n"
info "Follow re-create logs in a second terminal with:"
info "$ tail -f ${logfile}"
echo ""
echo -n "This takes a while ."
line_count=$(cat "${logfile}" | wc -l)
sed -e 's/"version": ".*",/"version":"48.0.0",/' -i /home/yellowtent/platformdata/INFRA_VERSION
systemctl enable -q box
systemctl restart -q box # will re-create docker network
while ! tail -n "+${line_count}" "${logfile}" | grep -q "platform is ready"; do
echo -n "."
sleep 2
done
echo -e "\n\nDone! Addon containers successfully re-created. The apps in the dashboard will say 'Configuring (Queued)'. They will come up in a short while.\n"
}
function recreate_containers() {
readonly logfile="/home/yellowtent/platformdata/logs/box.log"
@@ -474,82 +494,82 @@ function recreate_containers() {
read -p "Do you want to proceed? (y/N) " -n 1 -r choice
echo
if [[ ! $choice =~ ^[Yy]$ ]]; then
exit 1
fi
[[ ! $choice =~ ^[Yy]$ ]] && exit 1
echo ""
info "Follow re-create logs in a second terminal with:"
info "$ tail -f ${logfile}"
echo ""
do_recreate_containers
}
echo -n "Re-creating addon containers (this takes a while) ."
line_count=$(cat "${logfile}" | wc -l)
sed -e 's/"version": ".*",/"version":"48.0.0",/' -i /home/yellowtent/platformdata/INFRA_VERSION
systemctl restart box
function download_docker_images() {
info "Downloading addon images"
images=$(node -e "const i = require('/home/yellowtent/box/src/infra_version.js'); console.log(Object.keys(i.images).map(x => i.images[x]).join(' '));")
while ! tail -n "+${line_count}" "${logfile}" | grep -q "platform is ready"; do
echo -n "."
sleep 2
for image in ${images}; do
info "Pulling ${image}"
docker pull -q "${image}" &>/dev/null # this pulls the image using the sha256
docker pull -q "${image%@sha256:*}" &>/dev/null # this will tag the image for readability
done
}
echo ""
echo "Done! Addon containers successfully re-created. The apps in the dashboard will say 'Configuring (Queued)'. They will come up in a short while."
function ask_reboot() {
read -p "Do you want to reboot ? (y/N) " -n 1 -r choice
echo -e "\n"
[[ ! $choice =~ ^[Yy]$ ]] && exit 1
reboot
}
function recreate_docker() {
readonly logfile="/home/yellowtent/platformdata/logs/box.log"
readonly stagefile="/home/yellowtent/platformdata/recreate-docker-stage"
docker_root=/var/lib/docker
if ! docker_root=$(docker info -f '{{ .DockerRootDir }}'); then
warning "Unable to detect docker root. Assuming /var/lib/docker"
fi
echo -e "Use this command when docker storage (at $docker_root) is corrupt. It will delete the docker storage, re-download docker images and re-create containers. Dashboard and apps will be unreachable for a while. No data will be lost.\n"
read -p "Do you want to proceed? (y/N) " -n 1 -r choice
echo -e "\n"
if [[ ! -e "${stagefile}" ]]; then
echo -e "Use this command when docker storage (at $docker_root) is corrupt. It will delete the docker storage, re-download docker images and re-create containers. Dashboard and apps will be unreachable for a while. No data will be lost.\n"
echo -e "The server will have to be rebooted twice for this process.\n"
read -p "Do you want to proceed? (y/N) " -n 1 -r choice
echo -e "\n"
if [[ ! $choice =~ ^[Yy]$ ]]; then
exit 1
[[ ! $choice =~ ^[Yy]$ ]] && exit 1
info "Stopping box"
systemctl disable -q box || true
systemctl stop -q box || true
info "Stopping docker"
systemctl disable -q docker || true # for the reboot situation, we don't want it start again
systemctl stop -q docker || true
echo -e "clearing_storage" > "${stagefile}" # init
fi
info "Stopping box"
systemctl disable box || true
systemctl stop -q box || true
info "Stopping docker"
systemctl disable -q docker || true # for the reboot situation, we don't want it start again
systemctl stop -q docker || true
info "Clearing docker storage at ${docker_root}"
if ! rm -rf "${docker_root}/"*; then
fail "Could not delete storage directory. This can happen because of stray containers that docker has lost track of. To fix this, reboot the server and run this command again"
exit 1
if grep -q "clearing_storage" "${stagefile}"; then
info "Clearing docker storage at ${docker_root}"
if ! rm -rf "${docker_root}/"*; then
echo -e "\nThe server has to be rebooted to clear the docker storage. After reboot, run this command again.\n"
ask_reboot
fi
echo -e "cleared_storage" > "${stagefile}"
fi
info "Cleared docker storage"
info "Starting docker afresh"
systemctl enable -q docker
systemctl start -q docker
if grep -q "cleared_storage" "${stagefile}"; then
info "Starting docker afresh"
systemctl enable -q docker
systemctl start -q docker
sleep 5 # give docker some time to initialize the storage directory
download_docker_images
echo -e "downloaded_images" > "${stagefile}"
echo -e "\nThe server has to be rebooted again for docker to initialize properly. After reboot, run this command again.\n" # else docker network is not completely functional
ask_reboot
fi
echo ""
info "Follow re-create logs in a second terminal with:"
info "$ tail -f ${logfile}"
echo ""
if grep -q "downloaded_images" "${stagefile}"; then
do_recreate_containers
fi
echo -n "Re-downloading images and re-creating addon containers (this takes a while) ."
line_count=$(cat "${logfile}" | wc -l)
sed -e 's/"version": ".*",/"version":"48.0.0",/' -i /home/yellowtent/platformdata/INFRA_VERSION
systemctl enable box
systemctl restart box # this will create docker network as well
while ! tail -n "+${line_count}" "${logfile}" | grep -q "platform is ready"; do
echo -n "."
sleep 2
done
echo ""
echo "Done! Addon containers successfully re-created. The apps in the dashboard will say 'Configuring (Queued)'. They will come up in a short while."
rm "${stagefile}"
}
check_disk_space