#!/bin/bash set -euo pipefail # tinsnip installer - Downloads and sets up tinsnip infrastructure platform REPO_URL="https://tangled.sh/dynamicalsystem.com/tinsnip" BRANCH="main" INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/opt/dynamicalsystem.tinsnip}" INSTALLER_VERSION="12f7992" # Updated automatically on commit # Service repository options SERVICE_REPO_URL="${SERVICE_REPO_URL:-}" SERVICE_INSTALL_DIR="${SERVICE_INSTALL_DIR:-$HOME/.local/opt/dynamicalsystem.service}" log() { echo "[Installer] $*" } error() { log "ERROR: $*" >&2 exit 1 } download_file() { local file_path="$1" local dest_path="$2" local url="${REPO_URL}/raw/${BRANCH}/${file_path}" local filename=$(basename "$file_path") log " $filename" if command -v curl &> /dev/null; then if curl -fsSL "$url" -o "$dest_path" 2>/dev/null; then chmod +x "$dest_path" 2>/dev/null || true return 0 fi fi error "Failed to download $file_path" } clone_with_git() { log "Cloning repository with git..." if ! command -v git &> /dev/null; then log "Git not found, installing..." sudo apt-get update -qq && sudo apt-get install -y git fi if ! git clone "git@tangled.sh:dynamicalsystem.com/tinsnip" "$INSTALL_DIR"; then error "Failed to clone repository. Make sure you have SSH access to git@tangled.sh" fi } # Infer service repo URL from tinsnip repo URL infer_service_repo() { local tinsnip_url="$1" # Replace "tinsnip" with "service" in the URL echo "${tinsnip_url/tinsnip/service}" } # Clone or download service repository install_service_catalog() { local service_repo="${SERVICE_REPO_URL:-$(infer_service_repo "$REPO_URL")}" log "Installing service catalog..." log " Repository: $service_repo" if [[ -d "$SERVICE_INSTALL_DIR" ]]; then log " Removing: $SERVICE_INSTALL_DIR" rm -rf "$SERVICE_INSTALL_DIR" fi mkdir -p "$(dirname "$SERVICE_INSTALL_DIR")" # Try git clone first if command -v git &> /dev/null; then # Convert HTTPS URL to git URL for cloning local git_url="${service_repo/https:\/\/tangled.sh\//git@tangled.sh:}" if git clone "$git_url" "$SERVICE_INSTALL_DIR" 2>/dev/null; then log " Service catalog installed: $SERVICE_INSTALL_DIR" return 0 fi fi # Fallback to downloading files if git fails log "Git clone failed, downloading service files..." # For now, skip download fallback as service repo structure may vary log "WARNING: Could not clone service repository. Services will not be available." log "You can manually clone the service repository later:" log " git clone $service_repo $SERVICE_INSTALL_DIR" } main() { log "tinsnip Infrastructure Installer (version: $INSTALLER_VERSION)" log "==============================================================" cd ~ || error "Failed to change to home directory" if [[ ! -f /etc/os-release ]] || ! grep -q "Ubuntu" /etc/os-release; then error "This installer requires Ubuntu" fi if [[ -d "$INSTALL_DIR" ]]; then log "Tinsnip directory already exists. Removing for fresh installation..." log " Removing: $INSTALL_DIR" rm -rf "$INSTALL_DIR" fi log " Creating: $INSTALL_DIR" mkdir -p "$INSTALL_DIR"/{scripts,machine/scripts,config} log "Downloading setup files..." # Download main files download_file "setup.sh" "$INSTALL_DIR/setup.sh" download_file "README.md" "$INSTALL_DIR/README.md" download_file "fresh-install.sh" "$INSTALL_DIR/fresh-install.sh" # Download legacy scripts download_file "scripts/create_tinsnip_user.sh" "$INSTALL_DIR/scripts/create_tinsnip_user.sh" download_file "scripts/setup_rootless_docker.sh" "$INSTALL_DIR/scripts/setup_rootless_docker.sh" download_file "scripts/deploy_service.sh" "$INSTALL_DIR/scripts/deploy_service.sh" # Download machine infrastructure (the important stuff!) download_file "machine/setup.sh" "$INSTALL_DIR/machine/setup.sh" download_file "machine/validate.sh" "$INSTALL_DIR/machine/validate.sh" download_file "machine/scripts/lib.sh" "$INSTALL_DIR/machine/scripts/lib.sh" download_file "machine/scripts/setup_service.sh" "$INSTALL_DIR/machine/scripts/setup_service.sh" download_file "machine/scripts/setup_station.sh" "$INSTALL_DIR/machine/scripts/setup_station.sh" download_file "machine/scripts/mount_nas.sh" "$INSTALL_DIR/machine/scripts/mount_nas.sh" download_file "machine/scripts/install_docker.sh" "$INSTALL_DIR/machine/scripts/install_docker.sh" download_file "machine/scripts/generate_nfs_exports.sh" "$INSTALL_DIR/machine/scripts/generate_nfs_exports.sh" # Always install service catalog install_service_catalog log "Installation complete!" log "" log "Next steps:" log "1. cd $INSTALL_DIR" log "2. Set up machine infrastructure for a service:" log " ./machine/setup.sh " log "" log "Examples:" if [[ -d "$SERVICE_INSTALL_DIR" ]]; then # Get list of available services from the catalog for service in $(ls -1 "$SERVICE_INSTALL_DIR" 2>/dev/null | grep -v README | head -3); do log " ./machine/setup.sh $service prod DS412plus.local" done log " ./machine/setup.sh gateway test 192.168.1.100" log "" log "Parameters:" log " - Service name (e.g., gazette, lldap, gateway)" log " - Environment (prod, test, dev)" log " - NFS server hostname or IP" log "" else log " ./machine/setup.sh gateway prod DS412plus.local" log " ./machine/setup.sh lldap test 192.168.1.100" log "" log "Parameters:" log " - Service name (e.g., gazette, lldap, gateway)" log " - Environment (prod, test, dev)" log " - NFS server hostname or IP" fi } main "$@"