container-compose/container-compose

213 lines
6.1 KiB
Bash
Executable File

#!/bin/bash
check_file() {
file=""
engine=""
pull="false"
push="false"
build="false"
local opt
local OPTARG
local OPTIND=1
# Manually parse --engine= argument
for arg in "$@"; do
case $arg in
--engine=*)
engine="${arg#*=}"
shift # Remove --engine= from positional parameters
;;
build)
build=true
shift # Remove build from positional parameters
;;
push)
push=true
shift # Remove build from positional parameters
;;
pull)
pull=true
shift # Remove build from positional parameters
;;
esac
done
# Check if -f flag is provided
while getopts ":f:" opt; do
case $opt in
f)
file="$OPTARG"
;;
\?)
echo "Usage: $0 [-f file] [--engine=engine_name]"
return 1
;;
:)
echo "Option -$OPTARG requires an argument."
return 1
;;
esac
done
# Check if file is provided, if not look for default files
if [ -z "$file" ]; then
if [ -f "compose.yaml" ]; then
file="compose.yaml"
elif [ -f "compose.yml" ]; then
file="compose.yml"
elif [ -f "docker-compose.yaml" ]; then
file="docker-compose.yaml"
elif [ -f "docker-compose.yml" ]; then
file="docker-compose.yml"
elif [ -f "podman-compose.yaml" ]; then
file="podman-compose.yaml"
elif [ -f "podman-compose.yml" ]; then
file="podman-compose.yml"
elif [ -f "container-compose.yaml" ]; then
file="container-compose.yaml"
elif [ -f "container-compose.yml" ]; then
file="container-compose.yml"
else
echo "no compose.yaml, compose.yml, docker-compose.yaml, docker-compose.yml, podman-compose.yml, podman-compose.yaml, container-compose.yaml or container-compose.yml file found, pass files with -f"
exit 1
fi
elif [ ! -f "$file" ]; then
echo "File $file not found."
exit 1
fi
# Set engine based on file if engine is not set
if [ -z "$engine" ]; then
case $file in
docker-compose.yaml|docker-compose.yml)
engine="docker"
;;
podman-compose.yaml|podman-compose.yml)
engine="podman"
;;
*)
echo "Engine not set and cannot be determined from file name. Specify with --engine=engine_name"
return 1
;;
esac
fi
if ! command -v "$engine" &> /dev/null
then
echo "$engine Not found. Please install $engine or use a different engine. Specify with --engine=engine_name"
exit 1
fi
}
check_file "$@"
# Output the file and engine being used
echo "Using $file with engine $engine"
yq() {
$engine run --rm -i -v "${PWD}":/workdir mikefarah/yq "$@"
}
# Read and print service name, platform, and image
services=$(yq e '.services | keys | .[]' "$file")
for service in $services; do
platform=$(yq e ".services.$service.platform" "$file")
image=$(yq e ".services.$service.image" "$file")
context=$(yq e ".services.$service.build.context" "$file")
dockerfile=$(yq e ".services.$service.build.dockerfile" "$file")
echo "Service: $service"
echo "Platform: $platform"
echo "Image: $image"
echo "Context: $context"
echo "Dockerfile: $dockerfile"
echo "---------------------"
done
build(){
services=$(yq e '.services | keys | .[]' "$file")
for service in $services; do
platform=$(yq e ".services.$service.platform" "$file")
image=$(yq e ".services.$service.image" "$file")
build=$(yq e ".services.$service.build" "$file")
context=$(yq e ".services.$service.build.context" "$file")
dockerfile=$(yq e ".services.$service.build.dockerfile" "$file")
# Replace '/' and ':' with '-' in the platform string to make it suitable for a tag
platform_tag=$(echo "$platform" | tr '/:' '-')
# Append platform information to the image tag
new_image_tag="${image}-${platform_tag}"
# If dockerfile is null or empty, set it to "Dockerfile"
if [ -z "$dockerfile" ]; then
dockerfile="Dockerfile"
fi
if [ -z "$context" ]; then
context="."
fi
$engine buildx build --platform $platform -t $new_image_tag -f $context/$dockerfile $context --load
done
}
push(){
services=$(yq e '.services | keys | .[]' "$file")
for service in $services; do
platform=$(yq e ".services.$service.platform" "$file")
image=$(yq e ".services.$service.image" "$file")
build=$(yq e ".services.$service.build" "$file")
context=$(yq e ".services.$service.build.context" "$file")
dockerfile=$(yq e ".services.$service.build.dockerfile" "$file")
# Replace '/' and ':' with '-' in the platform string to make it suitable for a tag
platform_tag=$(echo "$platform" | tr '/:' '-')
# Append platform information to the image tag
new_image_tag="${image}-${platform_tag}"
# If dockerfile is null or empty, set it to "Dockerfile"
if [ -z "$dockerfile" ]; then
dockerfile="Dockerfile"
fi
if [ -z "$context" ]; then
context="."
fi
echo push both tags cmd: $engine push $new_image_tag
$engine push $new_image_tag
done
################################################################ BOTH IMAGES PUSHED
manifest_create_cmd="$engine manifest create $image"
echo $manifest_create_cmd
services=$(yq e '.services | keys | .[]' "$file")
for service in $services; do
platform=$(yq e ".services.$service.platform" "$file")
image=$(yq e ".services.$service.image" "$file")
# Replace '/' and ':' with '-' in the platform string to make it suitable for a tag
platform_tag=$(echo "$platform" | tr '/:' '-')
# Append platform information to the image tag
new_image_tag="${image}-${platform_tag}"
manifest_create_cmd+=" $new_image_tag"
done
echo Manifest Create cmd: $manifest_create_cmd
$manifest_create_cmd
#########################################
echo manifest push cmd: $engine manifest push $image
$engine manifest push $image
}
pull(){
services=$(yq e '.services | keys | .[]' "$file")
for service in $services; do
$engine pull $(yq e ".services.$service.image" "$file")
done
}
# If pull argument is provided, then pull
if [ "$pull" = true ]; then
pull
elif [ "$build" = true ]; then
build
elif [ "$push" = true ]; then
# echo push
push
fi