#!/bin/bash # # Tyto System Monitor - Installation Script # # Usage: # curl -fsSL https://somegit.dev/vikingowl/tyto/raw/branch/main/scripts/install.sh | bash # or # wget -qO- https://somegit.dev/vikingowl/tyto/raw/branch/main/scripts/install.sh | bash # # Environment Variables: # TYTO_MODE - Installation mode: standalone (default), server, agent # TYTO_VERSION - Version to install (default: latest) # TYTO_INSTALL_DIR - Installation directory (default: /opt/tyto) # TYTO_DATA_DIR - Data directory (default: /var/lib/tyto) # TYTO_USER - System user for tyto (default: tyto) # set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Default values TYTO_MODE="${TYTO_MODE:-standalone}" TYTO_VERSION="${TYTO_VERSION:-latest}" TYTO_INSTALL_DIR="${TYTO_INSTALL_DIR:-/opt/tyto}" TYTO_DATA_DIR="${TYTO_DATA_DIR:-/var/lib/tyto}" TYTO_USER="${TYTO_USER:-tyto}" TYTO_REPO="somegit.dev/vikingowl/tyto" # Logging functions log_info() { echo -e "${BLUE}[INFO]${NC} $*"; } log_success() { echo -e "${GREEN}[OK]${NC} $*"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } log_error() { echo -e "${RED}[ERROR]${NC} $*" >&2; } # Check if running as root check_root() { if [[ $EUID -ne 0 ]]; then log_error "This script must be run as root (use sudo)" exit 1 fi } # Detect OS and architecture detect_platform() { OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) case "$ARCH" in x86_64|amd64) ARCH="amd64" ;; aarch64|arm64) ARCH="arm64" ;; armv7l|armhf) ARCH="arm" ;; *) log_error "Unsupported architecture: $ARCH"; exit 1 ;; esac case "$OS" in linux) OS="linux" ;; darwin) OS="darwin" ;; *) log_error "Unsupported operating system: $OS"; exit 1 ;; esac log_info "Detected platform: ${OS}/${ARCH}" } # Check dependencies check_dependencies() { local deps=("curl" "tar") local missing=() for dep in "${deps[@]}"; do if ! command -v "$dep" &>/dev/null; then missing+=("$dep") fi done if [[ ${#missing[@]} -gt 0 ]]; then log_error "Missing dependencies: ${missing[*]}" log_info "Please install them and try again" exit 1 fi } # Create system user create_user() { if id "$TYTO_USER" &>/dev/null; then log_info "User $TYTO_USER already exists" else log_info "Creating user $TYTO_USER..." useradd --system --no-create-home --shell /sbin/nologin "$TYTO_USER" log_success "User created" fi } # Create directories create_directories() { log_info "Creating directories..." mkdir -p "$TYTO_INSTALL_DIR"/{bin,etc,logs} mkdir -p "$TYTO_DATA_DIR"/{db,certs} chown -R "$TYTO_USER:$TYTO_USER" "$TYTO_DATA_DIR" chmod 700 "$TYTO_DATA_DIR/certs" log_success "Directories created" } # Download and install binary install_binary() { local binary_name="tyto" local download_url if [[ "$TYTO_VERSION" == "latest" ]]; then download_url="https://${TYTO_REPO}/releases/latest/download/tyto-${OS}-${ARCH}.tar.gz" else download_url="https://${TYTO_REPO}/releases/download/${TYTO_VERSION}/tyto-${OS}-${ARCH}.tar.gz" fi log_info "Downloading Tyto from $download_url..." local tmp_dir tmp_dir=$(mktemp -d) trap "rm -rf $tmp_dir" EXIT if ! curl -fsSL "$download_url" -o "$tmp_dir/tyto.tar.gz"; then log_error "Failed to download Tyto" exit 1 fi tar -xzf "$tmp_dir/tyto.tar.gz" -C "$tmp_dir" mv "$tmp_dir/tyto" "$TYTO_INSTALL_DIR/bin/" chmod +x "$TYTO_INSTALL_DIR/bin/tyto" # Create symlink ln -sf "$TYTO_INSTALL_DIR/bin/tyto" /usr/local/bin/tyto log_success "Binary installed to $TYTO_INSTALL_DIR/bin/tyto" } # Create configuration file create_config() { local config_file="$TYTO_INSTALL_DIR/etc/config.yaml" if [[ -f "$config_file" ]]; then log_warn "Config file already exists, backing up..." cp "$config_file" "${config_file}.bak" fi log_info "Creating configuration for mode: $TYTO_MODE" cat > "$config_file" << EOF # Tyto System Monitor Configuration # Generated by install script mode: $TYTO_MODE http: port: 8080 host: "0.0.0.0" refresh_rate: 5 # Database (for server mode) database: type: sqlite path: $TYTO_DATA_DIR/db/tyto.db # Server settings (for multi-device) server: grpc_port: 9849 tls: enabled: false # ca_cert: $TYTO_DATA_DIR/certs/ca.crt # server_cert: $TYTO_DATA_DIR/certs/server.crt # server_key: $TYTO_DATA_DIR/certs/server.key # Log settings logging: level: info format: text # Alerts alerts: enabled: true thresholds: cpu: 90 memory: 85 disk: 90 EOF chown "$TYTO_USER:$TYTO_USER" "$config_file" chmod 640 "$config_file" log_success "Configuration created" } # Create systemd service create_systemd_service() { local service_file="/etc/systemd/system/tyto.service" log_info "Creating systemd service..." cat > "$service_file" << EOF [Unit] Description=Tyto System Monitor Documentation=https://somegit.dev/vikingowl/tyto After=network.target [Service] Type=simple User=$TYTO_USER Group=$TYTO_USER ExecStart=$TYTO_INSTALL_DIR/bin/tyto --config $TYTO_INSTALL_DIR/etc/config.yaml Restart=always RestartSec=5 StandardOutput=append:$TYTO_INSTALL_DIR/logs/tyto.log StandardError=append:$TYTO_INSTALL_DIR/logs/tyto.log # Security hardening NoNewPrivileges=true ProtectSystem=strict ProtectHome=true PrivateTmp=true ReadWritePaths=$TYTO_DATA_DIR $TYTO_INSTALL_DIR/logs ReadOnlyPaths=/proc /sys # Resource limits MemoryMax=256M CPUQuota=50% [Install] WantedBy=multi-user.target EOF systemctl daemon-reload log_success "Systemd service created" } # Enable and start service start_service() { log_info "Enabling and starting Tyto..." systemctl enable tyto systemctl start tyto sleep 2 if systemctl is-active --quiet tyto; then log_success "Tyto is running!" else log_error "Tyto failed to start. Check logs: journalctl -u tyto" exit 1 fi } # Print installation summary print_summary() { local ip ip=$(hostname -I | awk '{print $1}') echo "" echo "==========================================" echo -e "${GREEN}Tyto installation complete!${NC}" echo "==========================================" echo "" echo "Installation Details:" echo " Mode: $TYTO_MODE" echo " Binary: $TYTO_INSTALL_DIR/bin/tyto" echo " Config: $TYTO_INSTALL_DIR/etc/config.yaml" echo " Data: $TYTO_DATA_DIR" echo " Logs: $TYTO_INSTALL_DIR/logs/" echo "" echo "Service Commands:" echo " Status: systemctl status tyto" echo " Logs: journalctl -u tyto -f" echo " Restart: systemctl restart tyto" echo " Stop: systemctl stop tyto" echo "" echo "Access Dashboard:" echo " http://${ip}:8080" echo "" } # Main installation flow main() { echo "" echo " _____ _ " echo " |_ _| | | " echo " | |_ _| |_ ___ " echo " | | | | | __/ _ \\" echo " | | |_| | || (_) |" echo " |_|\__, |\__\___/" echo " __/ | " echo " |___/ " echo "" echo "Tyto System Monitor - Installation Script" echo "==========================================" echo "" check_root detect_platform check_dependencies create_user create_directories install_binary create_config create_systemd_service start_service print_summary } main "$@"