home-server/update-containers.sh
2020-12-14 20:46:35 +01:00

98 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
exec_update() {
docker-compose build --pull
docker-compose pull
docker-compose up -d
}
exec_stop() {
docker-compose stop
}
exec_start() {
docker-compose up -d
}
is_service() {
[[ "$(ls $1/docker-compose.yml)" ]]
}
get_all_services() {
echo $(find . -maxdepth 2 -name docker-compose.yml | while read line; do echo $(dirname $line | sed 's:^./::'); done)
}
#set default function
declare -A list
func=exec_update
declare -i count=0
if [[ -z "$1" ]]; then
list[$func]+=$(get_all_services)
fi
while [[ "$1" ]]; do
case $1 in
start)
func=exec_start
if [[ -z "$2" && $count == 0 ]]; then
list[$func]+=$(get_all_services)
fi
;;
stop)
func=exec_stop
if [[ -z "$2" && $count == 0 ]]; then
list[$func]+=$(get_all_services)
fi
;;
update)
func=exec_update
if [[ -z "$2" && $count == 0 ]]; then
list[$func]+=$(get_all_services)
fi
;;
*)
if is_service $1; then
list[$func]+="$1 "
else
echo Error: $1 is not recognized as a service or command
fi
;;
esac
shift
count+=1
done
declare -i count=0
if [[ ${list[exec_update]} ]]; then
echo To update: ${list[exec_update]}
count+=$(echo ${list[exec_update]} | wc -w)
fi
if [[ ${list[exec_start]} ]]; then
echo To start: ${list[exec_start]}
count+=$(echo ${list[exec_start]} | wc -w)
fi
if [[ ${list[exec_stop]} ]]; then
echo To stop: ${list[exec_stop]}
count+=$(echo ${list[exec_stop]} | wc -w)
fi
if [[ $count == 0 ]]; then
echo Nothing to do
else
echo $count operations to run
fi
for func in ${!list[@]}; do
for service in ${list[$func]}; do
pushd $service >/dev/null
echo
echo "#######" $(basename $PWD) do $func
$func
popd >/dev/null
done
done
if [[ "${list[exec_update]}" ]]; then
docker image prune -f
fi