Skip to content

Instantly share code, notes, and snippets.

@pavax
Last active February 8, 2025 13:13
Show Gist options
  • Save pavax/c71bd46fa1015bb5cae810ea846d831b to your computer and use it in GitHub Desktop.
Save pavax/c71bd46fa1015bb5cae810ea846d831b to your computer and use it in GitHub Desktop.
script to check for optical drive changes and update docker-compose files
#!/bin/sh
# Path to the Docker Compose file
DOCKER_COMPOSE_FILE="/volume2/mkv/makemkv-docker/docker-compose.yml"
find_device_paths_from_compose() {
echo "Searching for device paths in the Docker Compose file..."
device_path_sr=$(grep -o '/dev/sr[^:"]*' "$DOCKER_COMPOSE_FILE" | head -n1)
device_path_sg=$(grep -o '/dev/sg[^:"]*' "$DOCKER_COMPOSE_FILE" | head -n1)
# Ensure empty results are handled
device_path_sr=${device_path_sr:-""}
device_path_sg=${device_path_sg:-""}
}
restart_container() {
echo "Restarting container..."
docker-compose -f "$DOCKER_COMPOSE_FILE" down
docker-compose -f "$DOCKER_COMPOSE_FILE" up -d
}
find_device_paths_from_compose
echo "docker device paths: sr=$device_path_sr sg=$device_path_sg"
# Determine the actual paths of /dev/sr* and /dev/sg*
new_path_sr=""
new_path_sg=""
for device in /dev/sr*; do
if [ "$device" != "/dev/sr*" ]; then
new_path_sr="$device"
chmod 666 $device
break
fi
done
for device in /dev/sg*; do
if [ "$device" != "/dev/sg*" ]; then
new_path_sg="$device"
chmod 666 $device
break
fi
done
echo "host devices path: sr=$new_path_sr sg=$new_path_sg"
# Check if the paths have changed
if ([ -n "$new_path_sr" ] && [ "$new_path_sr" != "$device_path_sr" ]) || \
([ -n "$new_path_sg" ] && [ "$new_path_sg" != "$device_path_sg" ]); then
echo "New drive paths found. Updating Docker Compose file..."
# Run `sed` only if the variables are not empty
if [ -n "$new_path_sr" ] && [ -n "$device_path_sr" ]; then
sed -i "s|/dev/sr[0-9]:/dev/sr[0-9]|$new_path_sr:$new_path_sr|" "$DOCKER_COMPOSE_FILE"
fi
if [ -n "$new_path_sg" ] && [ -n "$device_path_sg" ]; then
sed -i "s|/dev/sg[0-9]:/dev/sg[0-9]|$new_path_sg:$new_path_sg|" "$DOCKER_COMPOSE_FILE"
fi
restart_container
else
echo "drive paths are matching those defined in the docker compose file"
docker-compose -f "$DOCKER_COMPOSE_FILE" up -d
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment