improve and relax make-hyperbola.sh
This commit is contained in:
parent
3da67d3659
commit
f42589d1ed
@ -1,122 +1,97 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Default tag
|
# Variables
|
||||||
default_tag="v0.4.3"
|
DEFAULT_TAG="v0.4.4"
|
||||||
default_release="2023.09.25"
|
DEFAULT_RELEASE="2024.05.06"
|
||||||
latest_tag="latest"
|
LATEST_TAG="latest"
|
||||||
|
|
||||||
# Function to display help
|
# Function to display help
|
||||||
show_help() {
|
show_help() {
|
||||||
echo "Usage: $0 [-t TAG] [-h]"
|
echo "Usage: $0 [-t TAG] [-r VERSION] [-h]"
|
||||||
echo " -t TAG Specify the tag (default: $default_tag)"
|
echo " -t TAG Specify the tag (default: $DEFAULT_TAG)"
|
||||||
echo " -r VERSION Specify the release version (default: $default_release)"
|
echo " -r VERSION Specify the release version (default: $DEFAULT_RELEASE)"
|
||||||
echo " -h Show this help"
|
echo " -h Show this help"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Process command line options
|
# Function to handle errors
|
||||||
while getopts ":t:h" opt; do
|
handle_error() {
|
||||||
case $opt in
|
echo "Error: $1" >&2
|
||||||
r)
|
exit 1
|
||||||
release="$OPTARG"
|
}
|
||||||
;;
|
|
||||||
t)
|
|
||||||
tag="$OPTARG"
|
|
||||||
;;
|
|
||||||
h)
|
|
||||||
show_help && exit 0
|
|
||||||
;;
|
|
||||||
\?)
|
|
||||||
echo "Invalid option: -$OPTARG" >&2
|
|
||||||
show_help && exit 1
|
|
||||||
;;
|
|
||||||
:)
|
|
||||||
echo "Option -$OPTARG requires an argument." >&2
|
|
||||||
show_help && exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
# Use default tag if not specified
|
# Function to download files
|
||||||
tag="${tag:-$default_tag}"
|
download_files() {
|
||||||
|
local release="$1"
|
||||||
|
local script_dir="$2"
|
||||||
|
|
||||||
# Use default release version if not specified
|
|
||||||
release="${release:-$default_release}"
|
|
||||||
|
|
||||||
# Working directory
|
|
||||||
script_dir="$(cd "$(dirname "$0")" && pwd)"
|
|
||||||
|
|
||||||
# Check if files exist in the current directory
|
|
||||||
if [[ ! -e "$script_dir/hyperbola-bootstrap-i686.tar.gz" || ! -e "$script_dir/hyperbola-bootstrap-x86_64.tar.gz" ]]; then
|
|
||||||
echo "Downloading files for version $release..."
|
echo "Downloading files for version $release..."
|
||||||
|
wget -P "$script_dir" "https://archive.fridu.us/hyperbola/iso/$release/hyperbola-bootstrap-i686.tar.gz" || handle_error "Failed to download files."
|
||||||
# Download files from the provided link
|
wget -P "$script_dir" "https://archive.fridu.us/hyperbola/iso/$release/hyperbola-bootstrap-x86_64.tar.gz" || handle_error "Failed to download files."
|
||||||
wget -P "$script_dir" "https://archive.fridu.us/hyperbola/iso/$release/hyperbola-bootstrap-i686.tar.gz"
|
|
||||||
wget -P "$script_dir" "https://archive.fridu.us/hyperbola/iso/$release/hyperbola-bootstrap-x86_64.tar.gz"
|
|
||||||
|
|
||||||
echo "Files downloaded successfully for version $release."
|
echo "Files downloaded successfully for version $release."
|
||||||
else
|
}
|
||||||
echo "Files already exist in the current directory. No download is required."
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Logout docker
|
# Function to check if files exist
|
||||||
docker logout > /dev/null 2>&1
|
check_files_exist() {
|
||||||
|
local script_dir="$1"
|
||||||
|
|
||||||
# Prompt user for credentials
|
[[ -e "$script_dir/hyperbola-bootstrap-i686.tar.gz" && -e "$script_dir/hyperbola-bootstrap-x86_64.tar.gz" ]]
|
||||||
if [[ -z "$DOCKER_USERNAME" ]]; then
|
}
|
||||||
read -p "Enter your Docker Hub username: " DOCKER_USERNAME
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -z "$DOCKER_PASSWORD" ]]; then
|
# Function to remove tag from Docker Hub
|
||||||
read -sp "Enter your Docker Hub password: " DOCKER_PASSWORD
|
remove_tag() {
|
||||||
echo
|
local username="$1"
|
||||||
fi
|
local token="$2"
|
||||||
|
local tag="$3"
|
||||||
|
|
||||||
# Log in to Docker Hub
|
curl_with_retry "https://hub.docker.com/v2/repositories/$username/hyperbola/tags/$tag" "$token" DELETE \
|
||||||
echo "$DOCKER_PASSWORD" | docker login --username "$DOCKER_USERNAME" --password-stdin
|
&& (echo -n "Temporal tag $tag removed successfully." || echo -n "Failed to remove temporal tag $tag.")
|
||||||
|
}
|
||||||
|
|
||||||
# Build the images
|
# Function to remove manifest locally
|
||||||
docker build --no-cache --platform=linux/amd64 --tag "$DOCKER_USERNAME/hyperbola:$tag-amd64" -f Dockerfile.amd64 .
|
remove_manifest() {
|
||||||
docker build --no-cache --platform=linux/386 --tag "$DOCKER_USERNAME/hyperbola:$tag-386" -f Dockerfile.386 .
|
local tag="$1"
|
||||||
|
|
||||||
# Push the images to Docker Hub
|
docker manifest rm "$tag" > /dev/null 2>&1
|
||||||
docker push "$DOCKER_USERNAME/hyperbola:$tag-amd64"
|
}
|
||||||
docker push "$DOCKER_USERNAME/hyperbola:$tag-386"
|
|
||||||
|
|
||||||
# Create and push the manifest to specified tag
|
# Function to push manifest to Docker Hub
|
||||||
docker manifest create "$DOCKER_USERNAME/hyperbola:$tag" \
|
push_manifest() {
|
||||||
--amend "$DOCKER_USERNAME/hyperbola:$tag-386" \
|
local username="$1"
|
||||||
--amend "$DOCKER_USERNAME/hyperbola:$tag-amd64"
|
local tag="$2"
|
||||||
|
local latest_tag="$3"
|
||||||
|
|
||||||
# Set amd64 leader to specified tag
|
docker manifest create "$username/hyperbola:$tag" \
|
||||||
docker manifest annotate "$DOCKER_USERNAME/hyperbola:$tag" \
|
--amend "$username/hyperbola:$tag-386" \
|
||||||
"$DOCKER_USERNAME/hyperbola:$tag-amd64" --os linux --arch amd64
|
--amend "$username/hyperbola:$tag-amd64"
|
||||||
|
|
||||||
# Push specified tag
|
docker manifest annotate "$username/hyperbola:$tag" \
|
||||||
docker manifest push "$DOCKER_USERNAME/hyperbola:$tag"
|
"$username/hyperbola:$tag-amd64" --os linux --arch amd64
|
||||||
|
|
||||||
# Create and push the manifest for "latest"
|
docker manifest push "$username/hyperbola:$tag"
|
||||||
docker manifest create "$DOCKER_USERNAME/hyperbola:$latest_tag" \
|
|
||||||
--amend "$DOCKER_USERNAME/hyperbola:$tag-386" \
|
|
||||||
--amend "$DOCKER_USERNAME/hyperbola:$tag-amd64"
|
|
||||||
|
|
||||||
# Set amd64 leader to latest
|
# Push to latest if not already latest tag
|
||||||
docker manifest annotate "$DOCKER_USERNAME/hyperbola:$latest_tag" \
|
if [ "$tag" != "$latest_tag" ]; then
|
||||||
"$DOCKER_USERNAME/hyperbola:$tag-amd64" --os linux --arch amd64
|
docker manifest create "$username/hyperbola:$latest_tag" \
|
||||||
|
--amend "$username/hyperbola:$tag-386" \
|
||||||
|
--amend "$username/hyperbola:$tag-amd64"
|
||||||
|
|
||||||
# Push to latest
|
docker manifest annotate "$username/hyperbola:$latest_tag" \
|
||||||
docker manifest push "$DOCKER_USERNAME/hyperbola:$latest_tag"
|
"$username/hyperbola:$tag-amd64" --os linux --arch amd64
|
||||||
|
|
||||||
# Get the token
|
docker manifest push "$username/hyperbola:$latest_tag"
|
||||||
token=$(curl -s -H 'Content-Type: application/json' -X POST -H 'Content-Type: application/json' -d "{\"username\":\"$DOCKER_USERNAME\", \"password\":\"$DOCKER_PASSWORD\"}" "https://hub.docker.com/v2/users/login/" | jq -r .token)
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Function to perform a curl with retries
|
# Function to perform a curl with retries
|
||||||
function curl_with_retry() {
|
curl_with_retry() {
|
||||||
local url="$1"
|
local url="$1"
|
||||||
|
local token="$2"
|
||||||
|
local method="$3"
|
||||||
local retries=3 # Number of retries
|
local retries=3 # Number of retries
|
||||||
local delay=5 # Delay between retries in seconds
|
local delay=5 # Delay between retries in seconds
|
||||||
|
|
||||||
for ((i = 0; i < retries; i++)); do
|
for ((i = 0; i < retries; i++)); do
|
||||||
response=$(curl -s -X DELETE -H "Authorization: JWT $token" "$url")
|
response=$(curl -s -X "$method" -H "Authorization: JWT $token" "$url")
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
echo "$response"
|
echo "$response"
|
||||||
return 0
|
return 0
|
||||||
@ -130,15 +105,86 @@ function curl_with_retry() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Delete tags on Docker Hub
|
# Function to get the authentication token from Docker Hub
|
||||||
if curl_with_retry "https://hub.docker.com/v2/repositories/$DOCKER_USERNAME/hyperbola/tags/$tag-amd64/"; then
|
get_docker_auth_token() {
|
||||||
echo "Tag $tag-amd64 deleted successfully."
|
local docker_username="$1"
|
||||||
else
|
local docker_password="$2"
|
||||||
echo "Failed to delete tag $tag-amd64."
|
local token
|
||||||
fi
|
|
||||||
|
|
||||||
if curl_with_retry "https://hub.docker.com/v2/repositories/$DOCKER_USERNAME/hyperbola/tags/$tag-386/"; then
|
token=$(curl -s -H 'Content-Type: application/json' -X POST -d "{\"username\":\"$docker_username\", \"password\":\"$docker_password\"}" "https://hub.docker.com/v2/users/login/" | jq -r .token)
|
||||||
echo "Tag $tag-386 deleted successfully."
|
|
||||||
else
|
if [ -z "$token" ]; then
|
||||||
echo "Failed to delete tag $tag-386."
|
handle_error "Failed to obtain Docker authentication token."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "$token"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Main
|
||||||
|
main() {
|
||||||
|
local tag="$DEFAULT_TAG"
|
||||||
|
local release="$DEFAULT_RELEASE"
|
||||||
|
local script_dir="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
local docker_username="$DOCKER_USERNAME"
|
||||||
|
local docker_password
|
||||||
|
|
||||||
|
# Process command line options
|
||||||
|
while getopts ":t:r:h" opt; do
|
||||||
|
case $opt in
|
||||||
|
r) release="$OPTARG" ;;
|
||||||
|
t) tag="$OPTARG" ;;
|
||||||
|
h) show_help && exit 0 ;;
|
||||||
|
\?) handle_error "Invalid option: -$OPTARG" ;;
|
||||||
|
:) handle_error "Option -$OPTARG requires an argument." ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# Download files if not exist
|
||||||
|
if ! check_files_exist "$script_dir"; then
|
||||||
|
download_files "$release" "$script_dir"
|
||||||
|
else
|
||||||
|
echo "Files already exist in the current directory. No download is required."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Retrieve Docker credentials from ~/.docker/config.json if exists
|
||||||
|
if [ -f "$HOME/.docker/config.json" ]; then
|
||||||
|
docker_username="$(jq -r '.auths."https://index.docker.io/v1/".auth' < "$HOME/.docker/config.json" | base64 -d | cut -d ':' -f1)"
|
||||||
|
docker_password="$(jq -r '.auths."https://index.docker.io/v1/".auth' < "$HOME/.docker/config.json" | base64 -d | cut -d ':' -f2)"
|
||||||
|
echo "$docker_password" | docker login --username "$docker_username" --password-stdin || handle_error "Docker login failed."
|
||||||
|
else
|
||||||
|
# Prompt for Docker credentials if ~/.docker/config.json doesn't exist
|
||||||
|
read -p "Enter your Docker Hub username: " docker_username
|
||||||
|
read -sp "Enter your Docker Hub password: " docker_password
|
||||||
|
echo
|
||||||
|
|
||||||
|
# Login to Docker Hub
|
||||||
|
echo "$docker_password" | docker login --username "$docker_username" --password-stdin || handle_error "Docker login failed."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get Docker authentication token
|
||||||
|
local docker_token=$(get_docker_auth_token "$docker_username" "$docker_password")
|
||||||
|
|
||||||
|
# Build and push images
|
||||||
|
docker build --no-cache --platform=linux/amd64 --tag "$docker_username/hyperbola:$tag-amd64" -f Dockerfile.amd64 . || handle_error "Failed to build amd64 image."
|
||||||
|
docker build --no-cache --platform=linux/386 --tag "$docker_username/hyperbola:$tag-386" -f Dockerfile.386 . || handle_error "Failed to build 386 image."
|
||||||
|
|
||||||
|
docker push "$docker_username/hyperbola:$tag-amd64" || handle_error "Failed to push amd64 image."
|
||||||
|
docker push "$docker_username/hyperbola:$tag-386" || handle_error "Failed to push 386 image."
|
||||||
|
|
||||||
|
# Remove old manifests
|
||||||
|
remove_manifest "$docker_username/hyperbola:$tag"
|
||||||
|
remove_manifest "$docker_username/hyperbola:$LATEST_TAG"
|
||||||
|
|
||||||
|
# Push manifests
|
||||||
|
push_manifest "$docker_username" "$tag" "$LATEST_TAG"
|
||||||
|
|
||||||
|
echo -n "Cleaning up temporary images..."
|
||||||
|
# Remove old tags
|
||||||
|
remove_tag "$docker_username" "$docker_token" "$tag-amd64"
|
||||||
|
remove_tag "$docker_username" "$docker_token" "$tag-386"
|
||||||
|
echo ""
|
||||||
|
echo "Hyperbola images for version $release with tag $tag have been successfully pushed to Docker Hub."
|
||||||
|
}
|
||||||
|
|
||||||
|
# Execute main function
|
||||||
|
main "$@"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user