Files
at-container-registry/pkg/appview/public/static/install.sh
T

187 lines
5.4 KiB
Bash
Executable File

#!/bin/bash
# ATCR Credential Helper Installation Script
# Usage: curl -fsSL https://atcr.io/install.sh | bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
BINARY_NAME="docker-credential-atcr"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
API_URL="${ATCR_API_URL:-https://atcr.io/api/credential-helper/version}"
# Fallback configuration (used if API is unavailable)
FALLBACK_VERSION="v0.0.1"
FALLBACK_TANGLED_REPO="https://tangled.org/evan.jarrett.net/at-container-registry"
# Detect OS and architecture
detect_platform() {
local os=$(uname -s | tr '[:upper:]' '[:lower:]')
local arch=$(uname -m)
case "$os" in
linux*)
OS="Linux"
OS_KEY="linux"
;;
darwin*)
OS="Darwin"
OS_KEY="darwin"
;;
*)
echo -e "${RED}Unsupported OS: $os${NC}"
exit 1
;;
esac
case "$arch" in
x86_64|amd64)
ARCH="x86_64"
ARCH_KEY="amd64"
;;
aarch64|arm64)
ARCH="arm64"
ARCH_KEY="arm64"
;;
*)
echo -e "${RED}Unsupported architecture: $arch${NC}"
exit 1
;;
esac
PLATFORM_KEY="${OS_KEY}_${ARCH_KEY}"
}
# Fetch version info from API
fetch_version_info() {
echo -e "${YELLOW}Fetching latest version info...${NC}"
# Try to fetch from API
local api_response
if api_response=$(curl -fsSL --max-time 10 "$API_URL" 2>/dev/null); then
# Parse JSON response (requires jq or basic parsing)
if command -v jq &> /dev/null; then
VERSION=$(echo "$api_response" | jq -r '.latest')
DOWNLOAD_URL=$(echo "$api_response" | jq -r ".download_urls.${PLATFORM_KEY}")
if [ "$VERSION" != "null" ] && [ "$DOWNLOAD_URL" != "null" ] && [ -n "$VERSION" ] && [ -n "$DOWNLOAD_URL" ]; then
echo -e "${GREEN}Found latest version: ${VERSION}${NC}"
return 0
fi
else
# Fallback: basic grep parsing if jq not available
VERSION=$(echo "$api_response" | grep -o '"latest":"[^"]*"' | cut -d'"' -f4)
# Try to extract the specific platform URL
DOWNLOAD_URL=$(echo "$api_response" | grep -o "\"${PLATFORM_KEY}\":\"[^\"]*\"" | cut -d'"' -f4)
if [ -n "$VERSION" ] && [ -n "$DOWNLOAD_URL" ]; then
echo -e "${GREEN}Found latest version: ${VERSION}${NC}"
return 0
fi
fi
fi
echo -e "${YELLOW}API unavailable, using fallback version${NC}"
return 1
}
# Set fallback download URL
use_fallback() {
VERSION="$FALLBACK_VERSION"
local version_without_v="${VERSION#v}"
DOWNLOAD_URL="${FALLBACK_TANGLED_REPO}/tags/${VERSION}/download/docker-credential-atcr_${version_without_v}_${OS}_${ARCH}.tar.gz"
}
# Download and install binary
install_binary() {
echo -e "${YELLOW}Downloading from: ${DOWNLOAD_URL}${NC}"
local tmp_dir=$(mktemp -d)
trap "rm -rf $tmp_dir" EXIT
if ! curl -fsSL "$DOWNLOAD_URL" -o "$tmp_dir/docker-credential-atcr.tar.gz"; then
echo -e "${RED}Failed to download release${NC}"
exit 1
fi
echo -e "${YELLOW}Extracting...${NC}"
tar -xzf "$tmp_dir/docker-credential-atcr.tar.gz" -C "$tmp_dir"
# Check if we need sudo
if [ -w "$INSTALL_DIR" ]; then
SUDO=""
else
SUDO="sudo"
echo -e "${YELLOW}Installing to ${INSTALL_DIR} (requires sudo)${NC}"
fi
$SUDO mkdir -p "$INSTALL_DIR"
$SUDO install -m 755 "$tmp_dir/$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME"
echo -e "${GREEN}Installed ${BINARY_NAME} to ${INSTALL_DIR}${NC}"
}
# Verify installation
verify_installation() {
if ! command -v "$BINARY_NAME" &> /dev/null; then
echo -e "${RED}${BINARY_NAME} not found in PATH${NC}"
echo -e "${YELLOW}You may need to add ${INSTALL_DIR} to your PATH${NC}"
echo -e "${YELLOW}Add this to your ~/.bashrc or ~/.zshrc:${NC}"
echo -e " export PATH=\"${INSTALL_DIR}:\$PATH\""
return 1
fi
echo -e "${GREEN}Verification successful!${NC}"
"$BINARY_NAME" version
}
# Configure Docker
configure_docker() {
echo ""
echo -e "${GREEN}Installation complete!${NC}"
echo ""
echo -e "${YELLOW}To use ATCR with Docker, configure Docker to use this credential helper:${NC}"
echo -e ' echo '\''{"credHelpers": {"atcr.io": "atcr"}}'\'' > ~/.docker/config.json'
echo ""
echo -e "${YELLOW}Or add to existing config.json:${NC}"
echo -e ' {
"credHelpers": {
"atcr.io": "atcr"
}
}'
}
# Main
main() {
echo -e "${GREEN}ATCR Credential Helper Installer${NC}"
echo ""
detect_platform
echo -e "Detected: ${GREEN}${OS} ${ARCH}${NC}"
# Check if version is manually specified
if [ -n "$ATCR_VERSION" ]; then
echo -e "Using specified version: ${GREEN}${ATCR_VERSION}${NC}"
VERSION="$ATCR_VERSION"
local version_without_v="${VERSION#v}"
DOWNLOAD_URL="${FALLBACK_TANGLED_REPO}/tags/${VERSION}/download/docker-credential-atcr_${version_without_v}_${OS}_${ARCH}.tar.gz"
else
# Try to fetch from API, fall back if unavailable
if ! fetch_version_info; then
use_fallback
fi
echo -e "Installing version: ${GREEN}${VERSION}${NC}"
fi
install_binary
verify_installation
configure_docker
}
main "$@"