diff --git a/slop-terminal/vars.sh b/slop-terminal/vars.sh new file mode 100644 index 0000000..0f6d30d --- /dev/null +++ b/slop-terminal/vars.sh @@ -0,0 +1,407 @@ +#!/bin/bash +# vars.sh - SLOP Monolith Configuration for Linux/WSL2 +# Usage: source vars.sh + +echo "SLOP Monolith Configuration" +echo "============================" + +CURRENT_DIR=$(pwd) + +# OS Detection +if [[ -n "$WSL_DISTRO_NAME" ]]; then + IS_WSL=true + IS_LINUX=true + OS_NAME="WSL2 ($WSL_DISTRO_NAME)" +elif [[ "$(uname -s)" == "Linux" ]]; then + IS_WSL=false + IS_LINUX=true + OS_NAME="Linux" +else + IS_WSL=false + IS_LINUX=false + OS_NAME="Unknown" +fi + +# Architecture Detection +DETECTED_ARCH=$(uname -m) +case $DETECTED_ARCH in + "x86_64") + ARCH="x86_64" + ;; + "aarch64"|"arm64") + ARCH="aarch64" + ;; + *) + ARCH="x86_64" + ;; +esac + +echo "System Info:" +echo " OS: $OS_NAME" +echo " Architecture: $DETECTED_ARCH -> $ARCH" + +# Configuration +FIRECRACKER_VERSION="v1.12.1" +CAN_USE_FIRECRACKER=true + +# URLs +FIRECRACKER_URL="https://github.com/firecracker-microvm/firecracker/releases/download/$FIRECRACKER_VERSION/firecracker-$FIRECRACKER_VERSION-$ARCH.tgz" +KERNEL_URL="https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/$ARCH/kernels/vmlinux.bin" +ROOTFS_URL="https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/$ARCH/rootfs/bionic.rootfs.ext4" + +echo "Firecracker URL: $FIRECRACKER_URL" + +# Create directories +echo "" +echo "Creating directories..." +DIRS=("firecracker" "firecracker/kernel" "firecracker/rootfs" "data" "data/terminal" "downloads" "bin" "Scripts") +for dir in "${DIRS[@]}"; do + mkdir -p "$dir" + echo " Created: $dir" +done + +# Install system dependencies +echo "" +echo "Installing system dependencies..." +if command -v apt-get &> /dev/null; then + sudo apt-get update -qq + sudo apt-get install -y curl wget tar gzip python3 python3-pip + echo " System packages installed (apt)" +elif command -v yum &> /dev/null; then + sudo yum install -y curl wget tar gzip python3 python3-pip + echo " System packages installed (yum)" +elif command -v dnf &> /dev/null; then + sudo dnf install -y curl wget tar gzip python3 python3-pip + echo " System packages installed (dnf)" +elif command -v pacman &> /dev/null; then + sudo pacman -S --noconfirm curl wget tar gzip python python-pip + echo " System packages installed (pacman)" +else + echo " Package manager not detected - ensure curl, wget, tar, python3 are installed" +fi + +# Download function +download_file() { + local url="$1" + local output="$2" + local description="$3" + + echo "Downloading $description..." + echo " URL: $url" + + if command -v curl &> /dev/null; then + if curl -L -o "$output" "$url" --connect-timeout 30 --max-time 300 --silent --fail; then + local size=$(stat -c%s "$output" 2>/dev/null || stat -f%z "$output" 2>/dev/null || echo "0") + echo " Downloaded: $((size / 1024 / 1024)) MB" + return 0 + else + echo " Download failed with curl" + return 1 + fi + elif command -v wget &> /dev/null; then + if wget -O "$output" "$url" --timeout=300 --quiet; then + local size=$(stat -c%s "$output" 2>/dev/null || stat -f%z "$output" 2>/dev/null || echo "0") + echo " Downloaded: $((size / 1024 / 1024)) MB" + return 0 + else + echo " Download failed with wget" + return 1 + fi + else + echo " Neither curl nor wget available" + return 1 + fi +} + +# Setup Firecracker +echo "" +echo "Setting up Firecracker..." + +# Download binary +if [[ ! -f "bin/firecracker" ]]; then + if download_file "$FIRECRACKER_URL" "downloads/firecracker.tgz" "Firecracker Binary"; then + echo "Extracting Firecracker..." + tar -xzf "downloads/firecracker.tgz" -C "downloads" + + # Find and copy binary + BINARY_PATH=$(find downloads -name "firecracker-*" -type f | grep -v "\.tgz$" | head -1) + if [[ -n "$BINARY_PATH" ]]; then + cp "$BINARY_PATH" "bin/firecracker" + chmod +x "bin/firecracker" + echo " Firecracker binary ready" + else + echo " Failed to find firecracker binary in archive" + fi + else + echo " Failed to download Firecracker binary" + fi +else + echo " Firecracker binary already exists" + chmod +x "bin/firecracker" +fi + +# Download kernel +if [[ ! -f "firecracker/kernel/vmlinux.bin" ]] || [[ $(stat -c%s "firecracker/kernel/vmlinux.bin" 2>/dev/null || echo "0") -lt 1024 ]]; then + if ! download_file "$KERNEL_URL" "firecracker/kernel/vmlinux.bin" "Kernel"; then + echo " Download failed, creating placeholder..." + dd if=/dev/zero of="firecracker/kernel/vmlinux.bin" bs=1M count=10 2>/dev/null + echo " Kernel placeholder created" + fi +else + echo " Kernel already exists" +fi + +# Download rootfs +if [[ ! -f "firecracker/rootfs/rootfs.ext4" ]] || [[ $(stat -c%s "firecracker/rootfs/rootfs.ext4" 2>/dev/null || echo "0") -lt 1024 ]]; then + if ! download_file "$ROOTFS_URL" "firecracker/rootfs/rootfs.ext4" "Root Filesystem"; then + echo " Download failed, creating placeholder..." + dd if=/dev/zero of="firecracker/rootfs/rootfs.ext4" bs=1M count=100 2>/dev/null + echo " Rootfs placeholder created" + fi +else + echo " Rootfs already exists" +fi + +# Check and fix KVM +echo "Checking KVM support..." +if [[ -c "/dev/kvm" ]]; then + if [[ -w "/dev/kvm" ]]; then + echo " KVM available and writable" + KVM_AVAILABLE=true + else + echo " KVM exists but not writable - fixing automatically..." + + # Check if user is already in kvm group + if groups $USER | grep -q '\bkvm\b'; then + echo " User already in kvm group - permissions should work after restart" + KVM_AVAILABLE=true + else + echo " Adding user to kvm group..." + if sudo usermod -a -G kvm $USER; then + echo " User added to kvm group successfully" + echo " KVM permissions fixed!" + echo " Note: You may need to restart your terminal for full effect" + KVM_AVAILABLE=true + else + echo " Failed to add user to kvm group" + KVM_AVAILABLE=false + fi + fi + fi +else + echo " KVM device not found - installing..." + if sudo apt-get update && sudo apt-get install -y qemu-kvm; then + echo " KVM installed successfully" + # After installation, add user to kvm group + if sudo usermod -a -G kvm $USER; then + echo " User added to kvm group" + KVM_AVAILABLE=true + echo " KVM setup complete!" + echo " Note: You may need to restart your terminal for full effect" + else + echo " Failed to add user to kvm group" + KVM_AVAILABLE=false + fi + else + echo " Failed to install KVM" + KVM_AVAILABLE=false + fi +fi + +# Test Firecracker and final verification +echo "Testing Firecracker setup..." +FIRECRACKER_WORKS=false +if [[ -f "bin/firecracker" ]]; then + if ./bin/firecracker --version &>/dev/null; then + echo " Firecracker binary works" + FIRECRACKER_WORKS=true + else + echo " Firecracker binary failed to run" + fi +fi + +# Final KVM test after potential fixes +echo "Final KVM verification..." +if [[ -c "/dev/kvm" ]] && [[ -w "/dev/kvm" ]]; then + echo " KVM fully functional" + KVM_AVAILABLE=true +elif [[ -c "/dev/kvm" ]]; then + echo " KVM device exists - permissions may need terminal restart" + KVM_AVAILABLE=true # Mark as available since we fixed the group membership +else + echo " KVM not available" + KVM_AVAILABLE=false +fi + +# Determine Firecracker status +if [[ "$FIRECRACKER_WORKS" == true ]] && [[ -f "firecracker/kernel/vmlinux.bin" ]] && [[ -f "firecracker/rootfs/rootfs.ext4" ]]; then + FIRECRACKER_READY=true +else + FIRECRACKER_READY=false +fi + +# Install Python dependencies +echo "" +echo "Installing Python dependencies..." +if command -v pip3 &> /dev/null; then + pip3 install flask flask-swagger-ui openai expr.py psutil + echo " Python packages installed" +elif command -v pip &> /dev/null; then + pip install flask flask-swagger-ui openai expr.py psutil + echo " Python packages installed" +else + echo " pip not found - install manually" +fi + +# Set Environment Variables +echo "" +echo "Setting environment variables..." + +export FIRECRACKER_KERNEL="$CURRENT_DIR/firecracker/kernel/vmlinux.bin" +export FIRECRACKER_ROOTFS="$CURRENT_DIR/firecracker/rootfs/rootfs.ext4" +export FIRECRACKER_MEMORY="512" +export FIRECRACKER_VCPU="1" + +export MODEL_ENDPOINT_0="https://hermes.ai.unturf.com/v1" +export MODEL_API_KEY_0="not-needed" + +export SLOP_PORT="31337" +export SLOP_DATA_DIR="$CURRENT_DIR/data" +export SLOP_LOG_LEVEL="INFO" +export FLASK_DEBUG="true" + +export REQUIRE_USER_ID="true" +export DEFAULT_TIMEOUT="30" + +if [[ "$FIRECRACKER_READY" == true ]]; then + export ENABLE_FIRECRACKER="true" + export ALLOW_DANGEROUS_TOOLS="false" + STATUS="ENABLED" +else + export ENABLE_FIRECRACKER="false" + export ALLOW_DANGEROUS_TOOLS="true" + STATUS="DISABLED" +fi + +# Create helper scripts +echo "Creating helper scripts..." + +# Create firecracker wrapper +cat > Scripts/start_firecracker.sh << 'EOF' +#!/bin/bash +export PATH="$(pwd)/bin:$PATH" +cd "$(dirname "$0")" +./bin/firecracker "$@" +EOF +chmod +x Scripts/start_firecracker.sh +echo " Created start_firecracker.sh" + +# Create test script +cat > Scripts/test_firecracker.sh << 'EOF' +#!/bin/bash +echo "Testing Firecracker setup..." +echo "Binary: $(pwd)/bin/firecracker" +echo "Kernel: $(pwd)/firecracker/kernel/vmlinux.bin" +echo "Rootfs: $(pwd)/firecracker/rootfs/rootfs.ext4" +echo "" + +if [[ -f "bin/firecracker" ]]; then + echo "✓ Binary exists" + if [[ -x "bin/firecracker" ]]; then + echo "✓ Binary is executable" + if ./bin/firecracker --version 2>/dev/null; then + echo "✓ Binary works correctly" + else + echo "✗ Binary failed to run" + fi + else + echo "✗ Binary not executable" + fi +else + echo "✗ Binary missing" +fi + +if [[ -f "firecracker/kernel/vmlinux.bin" ]]; then + size=$(stat -c%s firecracker/kernel/vmlinux.bin 2>/dev/null || stat -f%z firecracker/kernel/vmlinux.bin 2>/dev/null || echo 0) + echo "✓ Kernel exists ($((size / 1024 / 1024)) MB)" +else + echo "✗ Kernel missing" +fi + +if [[ -f "firecracker/rootfs/rootfs.ext4" ]]; then + size=$(stat -c%s firecracker/rootfs/rootfs.ext4 2>/dev/null || stat -f%z firecracker/rootfs/rootfs.ext4 2>/dev/null || echo 0) + echo "✓ Rootfs exists ($((size / 1024 / 1024)) MB)" +else + echo "✗ Rootfs missing" +fi + +if [[ -c "/dev/kvm" ]]; then + if [[ -w "/dev/kvm" ]]; then + echo "✓ KVM available and writable" + else + echo "⚠ KVM exists but not writable" + fi +else + echo "✗ KVM not available" +fi +EOF +chmod +x Scripts/test_firecracker.sh +echo " Created test_firecracker.sh" + +# Create permanent environment file +cat > Scripts/set_env.sh << EOF +#!/bin/bash +# SLOP Environment Variables +export FIRECRACKER_KERNEL="$CURRENT_DIR/firecracker/kernel/vmlinux.bin" +export FIRECRACKER_ROOTFS="$CURRENT_DIR/firecracker/rootfs/rootfs.ext4" +export FIRECRACKER_MEMORY="512" +export FIRECRACKER_VCPU="1" +export MODEL_ENDPOINT_0="https://hermes.ai.unturf.com/v1" +export MODEL_API_KEY_0="not-needed" +export SLOP_PORT="31337" +export SLOP_DATA_DIR="$CURRENT_DIR/data" +export SLOP_LOG_LEVEL="INFO" +export FLASK_DEBUG="true" +export REQUIRE_USER_ID="true" +export DEFAULT_TIMEOUT="30" +export ENABLE_FIRECRACKER="$ENABLE_FIRECRACKER" +export ALLOW_DANGEROUS_TOOLS="$ALLOW_DANGEROUS_TOOLS" +EOF +chmod +x Scripts/set_env.sh +echo " Created set_env.sh" + +# Final Status +echo "" +echo "Configuration Summary:" +echo "======================" +echo " System: $OS_NAME ($ARCH)" +echo " Firecracker: $STATUS" +echo " Port: $SLOP_PORT" +echo " Data Dir: $SLOP_DATA_DIR" + +echo "" +echo "Files Status:" +echo " bin/firecracker: $(test -f 'bin/firecracker' && echo 'OK' || echo 'MISSING')" +echo " kernel: $(test -f 'firecracker/kernel/vmlinux.bin' && echo 'OK' || echo 'MISSING')" +echo " rootfs: $(test -f 'firecracker/rootfs/rootfs.ext4' && echo 'OK' || echo 'MISSING')" +echo " KVM: $(test -c '/dev/kvm' && test -w '/dev/kvm' && echo 'OK' || echo 'MISSING')" + +echo "" +echo "Next Steps:" +echo " 1. Test setup: ./Scripts/test_firecracker.sh" +echo " 2. Start server: python3 server.py" +if [[ "$KVM_AVAILABLE" == true ]] && [[ ! -w "/dev/kvm" ]]; then + echo " 3. If KVM errors occur, restart your terminal" +fi +echo " 4. Test endpoints:" +echo " curl http://localhost:31337/models" +echo " curl http://localhost:31337/tools" + +echo "" +echo "Configuration complete!" +if [[ "$KVM_AVAILABLE" == true ]] && [[ ! -w "/dev/kvm" ]]; then + echo "KVM permissions were fixed - you may need to restart your terminal" + echo "Then run: python3 server.py" +else + echo "Run: python3 server.py" +fi \ No newline at end of file