363 lines
11 KiB
Bash
Raw Normal View History

2025-10-03 02:09:53 +02:00
#!/bin/bash
# ============================================================================
# WebDAV Server Installation and Setup Script
# ============================================================================
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Functions
print_info() {
echo -e "${BLUE} ${1}${NC}"
}
print_success() {
echo -e "${GREEN}${1}${NC}"
}
print_warning() {
echo -e "${YELLOW}${1}${NC}"
}
print_error() {
echo -e "${RED}${1}${NC}"
}
print_header() {
echo ""
echo -e "${BLUE}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ WebDAV Server Installation Script ║${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════════════════════════╝${NC}"
echo ""
}
# Check if running as root
check_root() {
if [ "$EUID" -eq 0 ]; then
print_warning "Running as root. It's recommended to run as a regular user."
read -p "Continue? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
}
# Check system requirements
check_requirements() {
print_info "Checking system requirements..."
# Check Python version
if command -v python3 &> /dev/null; then
PYTHON_VERSION=$(python3 --version | awk '{print $2}')
print_success "Python $PYTHON_VERSION found"
else
print_error "Python 3 not found. Please install Python 3.8 or higher."
exit 1
fi
# Check pip
if command -v pip3 &> /dev/null; then
print_success "pip3 found"
else
print_warning "pip3 not found. Installing..."
python3 -m ensurepip --upgrade
fi
# Check git (optional)
if command -v git &> /dev/null; then
print_success "git found"
else
print_warning "git not found. Some features may be limited."
fi
}
# Install system dependencies
install_dependencies() {
print_info "Installing system dependencies..."
if [ -f /etc/debian_version ]; then
# Debian/Ubuntu
sudo apt-get update
sudo apt-get install -y python3-dev python3-venv build-essential libxml2-dev libxslt-dev
print_success "Dependencies installed (Debian/Ubuntu)"
elif [ -f /etc/redhat-release ]; then
# RHEL/CentOS/Fedora
sudo yum install -y python3-devel gcc gcc-c++ libxml2-devel libxslt-devel
print_success "Dependencies installed (RHEL/CentOS/Fedora)"
else
print_warning "Unknown distribution. Please install python3-dev, build-essential manually."
fi
}
# Setup installation directory
setup_directory() {
print_info "Setting up installation directory..."
read -p "Installation directory [./webdav-server]: " INSTALL_DIR
INSTALL_DIR=${INSTALL_DIR:-./webdav-server}
if [ -d "$INSTALL_DIR" ]; then
print_warning "Directory $INSTALL_DIR already exists."
read -p "Continue and potentially overwrite files? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
else
mkdir -p "$INSTALL_DIR"
fi
cd "$INSTALL_DIR"
print_success "Using directory: $(pwd)"
}
# Create virtual environment
setup_venv() {
print_info "Creating Python virtual environment..."
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip setuptools wheel
print_success "Virtual environment created"
}
# Install Python packages
install_packages() {
print_info "Installing Python packages..."
source venv/bin/activate
if [ -f requirements.txt ]; then
pip install -r requirements.txt
print_success "Packages installed from requirements.txt"
else
print_warning "requirements.txt not found. Installing core packages..."
pip install aiohttp aiofiles aiohttp-session cryptography python-dotenv lxml gunicorn
print_success "Core packages installed"
fi
}
# Setup configuration
setup_config() {
print_info "Setting up configuration..."
if [ ! -f .env ]; then
if [ -f .env.example ]; then
cp .env.example .env
print_success "Created .env from .env.example"
else
print_warning ".env.example not found. Creating basic .env..."
cat > .env << EOF
HOST=0.0.0.0
PORT=8080
DB_PATH=./webdav.db
WEBDAV_ROOT=./webdav
AUTH_METHODS=basic,digest
JWT_SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))")
SESSION_TIMEOUT=3600
MAX_FILE_SIZE=104857600
LOG_LEVEL=INFO
EOF
print_success "Created basic .env file"
fi
else
print_warning ".env already exists. Skipping..."
fi
# Generate secret key if needed
if grep -q "your-secret-key-here" .env 2>/dev/null; then
SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))")
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
sed -i '' "s/your-secret-key-here-change-this-in-production/$SECRET_KEY/" .env
else
# Linux
sed -i "s/your-secret-key-here-change-this-in-production/$SECRET_KEY/" .env
fi
print_success "Generated new JWT secret key"
fi
}
# Create directory structure
create_directories() {
print_info "Creating directory structure..."
mkdir -p webdav/users
mkdir -p webdav/shared
mkdir -p logs
mkdir -p backups
print_success "Directory structure created"
}
# Initialize database
init_database() {
print_info "Initializing database..."
source venv/bin/activate
python3 << EOF
import asyncio
from main import Database, create_default_user
async def init():
db = Database('./webdav.db')
await create_default_user(db)
asyncio.run(init())
EOF
print_success "Database initialized"
}
# Setup systemd service (optional)
setup_systemd() {
print_info "Would you like to set up systemd service? (requires sudo)"
read -p "Setup systemd service? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Create webdav user if doesn't exist
if ! id -u webdav &>/dev/null; then
sudo useradd -r -s /bin/false webdav
print_success "Created webdav user"
fi
# Copy service file
if [ -f webdav.service ]; then
sudo cp webdav.service /etc/systemd/system/
sudo systemctl daemon-reload
print_success "Systemd service installed"
print_info "To enable and start the service:"
echo " sudo systemctl enable webdav"
echo " sudo systemctl start webdav"
echo " sudo systemctl status webdav"
else
print_warning "webdav.service not found. Skipping..."
fi
fi
}
# Setup nginx (optional)
setup_nginx() {
print_info "Would you like to set up Nginx reverse proxy?"
read -p "Setup Nginx? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if command -v nginx &> /dev/null; then
read -p "Enter your domain name: " DOMAIN_NAME
if [ -f nginx.conf ]; then
mkdir -p nginx/conf.d nginx/ssl
sed "s/webdav.example.com/$DOMAIN_NAME/g" nginx.conf > nginx/conf.d/webdav.conf
print_success "Nginx configuration created"
print_info "Configuration saved to: nginx/conf.d/webdav.conf"
print_warning "Don't forget to configure SSL certificates!"
else
print_warning "nginx.conf template not found"
fi
else
print_warning "Nginx not installed. Skipping..."
fi
fi
}
# Setup Docker (optional)
setup_docker() {
print_info "Would you like to set up Docker deployment?"
read -p "Setup Docker? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if command -v docker &> /dev/null; then
if [ -f Dockerfile ] && [ -f docker-compose.yml ]; then
print_info "Building Docker image..."
docker-compose build
print_success "Docker image built"
print_info "To start the service:"
echo " docker-compose up -d"
echo " docker-compose logs -f"
else
print_warning "Dockerfile or docker-compose.yml not found"
fi
else
print_warning "Docker not installed. Skipping..."
fi
fi
}
# Final instructions
print_instructions() {
echo ""
echo -e "${GREEN}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ Installation Complete! ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════════════╝${NC}"
echo ""
print_info "Getting Started:"
echo ""
echo "1. Activate virtual environment:"
echo " source venv/bin/activate"
echo ""
echo "2. Start the server:"
echo " python main.py"
echo " OR with Gunicorn (production):"
echo " gunicorn main:init_app --config gunicorn_config.py"
echo ""
echo "3. Access WebDAV server:"
echo " http://localhost:8080/"
echo ""
echo "4. Default credentials:"
echo " Username: admin"
echo " Password: admin123"
echo " ⚠️ CHANGE THIS PASSWORD IMMEDIATELY!"
echo ""
echo "5. Manage users with CLI:"
echo " python webdav_cli.py user create newuser"
echo " python webdav_cli.py user list"
echo " python webdav_cli.py stats"
echo ""
print_warning "Remember to:"
echo " • Change the default admin password"
echo " • Configure SSL/TLS for production"
echo " • Set up firewall rules"
echo " • Configure backups"
echo ""
}
# Main installation process
main() {
print_header
check_root
check_requirements
install_dependencies
setup_directory
setup_venv
install_packages
setup_config
create_directories
init_database
setup_systemd
setup_nginx
setup_docker
print_instructions
print_success "Installation complete!"
}
# Run main function
main