Files
registry/install.sh

362 lines
10 KiB
Bash
Raw Normal View History

#!/bin/bash
# Stack.Gallery Registry Installer Script
# Downloads and installs pre-compiled Stack.Gallery Registry binary from Gitea releases
#
# Usage:
# Direct piped installation (recommended):
# curl -sSL https://code.foss.global/stack.gallery/registry/raw/branch/main/install.sh | sudo bash
#
# With version specification:
# curl -sSL https://code.foss.global/stack.gallery/registry/raw/branch/main/install.sh | sudo bash -s -- --version v1.0.0
#
# Options:
# -h, --help Show this help message
# --version VERSION Install specific version (e.g., v1.0.0)
# --install-dir DIR Installation directory (default: /opt/stack-gallery-registry)
# --setup-service Install and enable systemd service
set -e
# Default values
SHOW_HELP=0
SPECIFIED_VERSION=""
INSTALL_DIR="/opt/stack-gallery-registry"
SETUP_SERVICE=0
GITEA_BASE_URL="https://code.foss.global"
GITEA_REPO="stack.gallery/registry"
BINARY_NAME="stack-gallery-registry"
SERVICE_NAME="stack-gallery-registry"
CONFIG_DIR="/etc/stack-gallery-registry"
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
SHOW_HELP=1
shift
;;
--version)
SPECIFIED_VERSION="$2"
shift 2
;;
--install-dir)
INSTALL_DIR="$2"
shift 2
;;
--setup-service)
SETUP_SERVICE=1
shift
;;
*)
echo "Unknown option: $1"
echo "Use -h or --help for usage information"
exit 1
;;
esac
done
if [ $SHOW_HELP -eq 1 ]; then
echo "Stack.Gallery Registry Installer Script"
echo "Downloads and installs pre-compiled Stack.Gallery Registry binary"
echo ""
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " --version VERSION Install specific version (e.g., v1.0.0)"
echo " --install-dir DIR Installation directory (default: /opt/stack-gallery-registry)"
echo " --setup-service Install and enable systemd service"
echo ""
echo "Examples:"
echo " # Install latest version"
echo " curl -sSL https://code.foss.global/stack.gallery/registry/raw/branch/main/install.sh | sudo bash"
echo ""
echo " # Install specific version with systemd service"
echo " curl -sSL https://code.foss.global/stack.gallery/registry/raw/branch/main/install.sh | sudo bash -s -- --version v1.0.0 --setup-service"
exit 0
fi
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (sudo bash install.sh or pipe to sudo bash)"
exit 1
fi
# Helper function to detect OS and architecture
detect_platform() {
local os=$(uname -s)
local arch=$(uname -m)
# Map OS
case "$os" in
Linux)
os_name="linux"
;;
Darwin)
os_name="macos"
;;
MINGW*|MSYS*|CYGWIN*)
os_name="windows"
;;
*)
echo "Error: Unsupported operating system: $os"
echo "Supported: Linux, macOS"
exit 1
;;
esac
# Map architecture
case "$arch" in
x86_64|amd64)
arch_name="x64"
;;
aarch64|arm64)
arch_name="arm64"
;;
*)
echo "Error: Unsupported architecture: $arch"
echo "Supported: x86_64/amd64 (x64), aarch64/arm64 (arm64)"
exit 1
;;
esac
# Construct binary name
echo "${BINARY_NAME}-${os_name}-${arch_name}"
}
# Get latest release version from Gitea API
get_latest_version() {
echo "Fetching latest release version from Gitea..." >&2
local api_url="${GITEA_BASE_URL}/api/v1/repos/${GITEA_REPO}/releases/latest"
local response=$(curl -sSL "$api_url" 2>/dev/null)
if [ $? -ne 0 ] || [ -z "$response" ]; then
echo "Error: Failed to fetch latest release information from Gitea API" >&2
echo "URL: $api_url" >&2
exit 1
fi
# Extract tag_name from JSON response
local version=$(echo "$response" | grep -o '"tag_name":"[^"]*"' | cut -d'"' -f4)
if [ -z "$version" ]; then
echo "Error: Could not determine latest version from API response" >&2
exit 1
fi
echo "$version"
}
# Create systemd service file
create_service_file() {
cat > /etc/systemd/system/${SERVICE_NAME}.service << EOF
[Unit]
Description=Stack.Gallery Registry
Documentation=https://code.foss.global/stack.gallery/registry
After=network.target mongodb.service
[Service]
Type=simple
User=root
ExecStart=${INSTALL_DIR}/${BINARY_NAME} server
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=${SERVICE_NAME}
# Environment variables (customize these)
Environment=PORT=3000
Environment=HOST=0.0.0.0
# Environment=MONGODB_URL=mongodb://localhost:27017/stackgallery
# Environment=S3_ENDPOINT=http://localhost:9000
# Environment=S3_ACCESS_KEY=minioadmin
# Environment=S3_SECRET_KEY=minioadmin
# Environment=S3_BUCKET=registry
# Environment=JWT_SECRET=your-secret-here
WorkingDirectory=${INSTALL_DIR}
[Install]
WantedBy=multi-user.target
EOF
echo "Created systemd service file: /etc/systemd/system/${SERVICE_NAME}.service"
}
# Main installation process
echo "================================================"
echo " Stack.Gallery Registry Installation Script"
echo "================================================"
echo ""
# Detect platform
PLATFORM_BINARY=$(detect_platform)
echo "Detected platform: $PLATFORM_BINARY"
echo ""
# Determine version to install
if [ -n "$SPECIFIED_VERSION" ]; then
VERSION="$SPECIFIED_VERSION"
echo "Installing specified version: $VERSION"
else
VERSION=$(get_latest_version)
echo "Installing latest version: $VERSION"
fi
echo ""
# Construct download URL
DOWNLOAD_URL="${GITEA_BASE_URL}/${GITEA_REPO}/releases/download/${VERSION}/${PLATFORM_BINARY}"
echo "Download URL: $DOWNLOAD_URL"
echo ""
# Check if service is running and stop it
SERVICE_WAS_RUNNING=0
if systemctl is-enabled --quiet ${SERVICE_NAME} 2>/dev/null || systemctl is-active --quiet ${SERVICE_NAME} 2>/dev/null; then
SERVICE_WAS_RUNNING=1
if systemctl is-active --quiet ${SERVICE_NAME} 2>/dev/null; then
echo "Stopping ${SERVICE_NAME} service..."
systemctl stop ${SERVICE_NAME}
fi
fi
# Clean installation directory - ensure only binary exists
if [ -d "$INSTALL_DIR" ]; then
echo "Cleaning installation directory: $INSTALL_DIR"
rm -rf "$INSTALL_DIR"
fi
# Create fresh installation directory
echo "Creating installation directory: $INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
# Create config directory if it doesn't exist
if [ ! -d "$CONFIG_DIR" ]; then
echo "Creating config directory: $CONFIG_DIR"
mkdir -p "$CONFIG_DIR"
fi
# Download binary
echo "Downloading Stack.Gallery Registry binary..."
TEMP_FILE="$INSTALL_DIR/${BINARY_NAME}.download"
curl -sSL "$DOWNLOAD_URL" -o "$TEMP_FILE"
if [ $? -ne 0 ]; then
echo "Error: Failed to download binary from $DOWNLOAD_URL"
echo ""
echo "Please check:"
echo " 1. Your internet connection"
echo " 2. The specified version exists: ${GITEA_BASE_URL}/${GITEA_REPO}/releases"
echo " 3. The platform binary is available for this release"
rm -f "$TEMP_FILE"
exit 1
fi
# Check if download was successful (file exists and not empty)
if [ ! -s "$TEMP_FILE" ]; then
echo "Error: Downloaded file is empty or does not exist"
rm -f "$TEMP_FILE"
exit 1
fi
# Move to final location
BINARY_PATH="$INSTALL_DIR/${BINARY_NAME}"
mv "$TEMP_FILE" "$BINARY_PATH"
if [ $? -ne 0 ] || [ ! -f "$BINARY_PATH" ]; then
echo "Error: Failed to move binary to $BINARY_PATH"
rm -f "$TEMP_FILE" 2>/dev/null
exit 1
fi
# Make executable
chmod +x "$BINARY_PATH"
if [ $? -ne 0 ]; then
echo "Error: Failed to make binary executable"
exit 1
fi
echo "Binary installed successfully to: $BINARY_PATH"
echo ""
# Check if /usr/local/bin is in PATH
if [[ ":$PATH:" == *":/usr/local/bin:"* ]]; then
BIN_DIR="/usr/local/bin"
else
BIN_DIR="/usr/bin"
fi
# Create symlink for global access
ln -sf "$BINARY_PATH" "$BIN_DIR/${BINARY_NAME}"
echo "Symlink created: $BIN_DIR/${BINARY_NAME} -> $BINARY_PATH"
echo ""
# Setup systemd service if requested
if [ $SETUP_SERVICE -eq 1 ]; then
echo "Setting up systemd service..."
create_service_file
systemctl daemon-reload
systemctl enable ${SERVICE_NAME}
echo "Systemd service enabled: ${SERVICE_NAME}"
echo ""
fi
# Restart service if it was running before update
if [ $SERVICE_WAS_RUNNING -eq 1 ]; then
echo "Restarting ${SERVICE_NAME} service..."
systemctl restart ${SERVICE_NAME}
echo "Service restarted successfully."
echo ""
fi
echo "================================================"
echo " Stack.Gallery Registry Installation Complete!"
echo "================================================"
echo ""
echo "Installation details:"
echo " Binary location: $BINARY_PATH"
echo " Symlink location: $BIN_DIR/${BINARY_NAME}"
echo " Config directory: $CONFIG_DIR"
echo " Version: $VERSION"
echo ""
# Check if configuration exists
if [ -f "${CONFIG_DIR}/config.json" ]; then
echo "Configuration: ${CONFIG_DIR}/config.json (preserved)"
echo ""
echo "Your existing configuration has been preserved."
if [ $SERVICE_WAS_RUNNING -eq 1 ]; then
echo "The service has been restarted with your current settings."
else
echo "Start the service with: sudo systemctl start ${SERVICE_NAME}"
fi
else
echo "Get started:"
echo " ${BINARY_NAME} --help"
echo " ${BINARY_NAME} server # Start the registry server"
echo ""
echo "Configure environment variables:"
echo " - MONGODB_URL: MongoDB connection string"
echo " - S3_ENDPOINT: S3-compatible storage endpoint"
echo " - S3_ACCESS_KEY: S3 access key"
echo " - S3_SECRET_KEY: S3 secret key"
echo " - S3_BUCKET: S3 bucket name"
echo " - JWT_SECRET: Secret for JWT signing"
echo " - PORT: Server port (default: 3000)"
echo ""
if [ $SETUP_SERVICE -eq 1 ]; then
echo "Edit the service file to configure environment:"
echo " sudo nano /etc/systemd/system/${SERVICE_NAME}.service"
echo " sudo systemctl daemon-reload"
echo " sudo systemctl start ${SERVICE_NAME}"
else
echo "To setup as a systemd service, re-run with --setup-service:"
echo " curl -sSL ${GITEA_BASE_URL}/${GITEA_REPO}/raw/branch/main/install.sh | sudo bash -s -- --setup-service"
fi
fi
echo ""