Torrent-Manager/install.sh

220 lines
6.5 KiB
Bash
Executable File

#!/bin/bash
# Transmission RSS Manager One-Click Installer
# This script downloads and installs Transmission RSS Manager with all dependencies
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Print section header
print_section() {
echo -e "\n${GREEN}===== $1 =====${NC}"
}
# Error handling
set -e
trap 'echo -e "${RED}An error occurred. Installation failed.${NC}"; exit 1' ERR
print_section "Transmission RSS Manager Installer"
echo -e "This script will install Transmission RSS Manager and all required components."
# Check if running as root (sudo)
if [ "$EUID" -ne 0 ]; then
echo -e "${YELLOW}Please run this script with sudo:${NC}"
echo -e "${YELLOW}sudo bash install.sh${NC}"
exit 1
fi
# Detect Linux distribution
if [ -f /etc/os-release ]; then
. /etc/os-release
DISTRO=$ID
else
echo -e "${RED}Cannot detect Linux distribution. This script supports Debian, Ubuntu, and their derivatives.${NC}"
exit 1
fi
print_section "Installing Dependencies"
# Update package lists
echo "Updating package lists..."
apt-get update
# Install basic dependencies
echo "Installing required packages..."
apt-get install -y wget curl unzip git
# Install .NET SDK
print_section "Installing .NET SDK"
if ! command -v dotnet &> /dev/null; then
echo "Installing .NET SDK 7.0..."
# Add Microsoft package repository
wget -O packages-microsoft-prod.deb https://packages.microsoft.com/config/$DISTRO/$VERSION_ID/packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
# Install .NET SDK
apt-get update
apt-get install -y apt-transport-https
apt-get update
apt-get install -y dotnet-sdk-7.0
else
echo ".NET SDK is already installed."
fi
# Verify .NET installation
dotnet --version
if [ $? -ne 0 ]; then
echo -e "${RED}.NET SDK installation failed.${NC}"
exit 1
fi
# Install PostgreSQL
print_section "Installing PostgreSQL"
if ! command -v psql &> /dev/null; then
echo "Installing PostgreSQL..."
apt-get install -y postgresql postgresql-contrib
else
echo "PostgreSQL is already installed."
fi
# Start PostgreSQL service
systemctl start postgresql
systemctl enable postgresql
# Install Entity Framework Core tools
print_section "Installing EF Core tools"
if ! su - postgres -c "dotnet tool list -g" | grep "dotnet-ef" > /dev/null; then
echo "Installing Entity Framework Core tools..."
su - postgres -c "dotnet tool install --global dotnet-ef --version 7.0.15"
else
echo "Entity Framework Core tools are already installed."
fi
# Create installation directory
print_section "Setting up application"
INSTALL_DIR="/opt/transmission-rss-manager"
mkdir -p $INSTALL_DIR
# Download or clone the application
if [ ! -d "$INSTALL_DIR/.git" ]; then
echo "Downloading application files..."
# Clone the repository
git clone https://git.powerdata.dk/masterdraco/Torrent-Manager.git $INSTALL_DIR
else
echo "Updating existing installation..."
cd $INSTALL_DIR
git pull
fi
# Setup database
print_section "Setting up database"
DB_NAME="torrentmanager"
DB_USER="torrentmanager"
DB_PASSWORD=$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 16)
# Check if database exists
DB_EXISTS=$(su - postgres -c "psql -tAc \"SELECT 1 FROM pg_database WHERE datname='$DB_NAME'\"")
if [ "$DB_EXISTS" != "1" ]; then
echo "Creating database and user..."
# Create database and user
su - postgres -c "psql -c \"CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';\""
su - postgres -c "psql -c \"CREATE DATABASE $DB_NAME OWNER $DB_USER;\""
su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;\""
else
echo "Database already exists."
fi
# Save connection string
CONFIG_DIR="/etc/transmission-rss-manager"
mkdir -p $CONFIG_DIR
echo '{
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database='$DB_NAME';Username='$DB_USER';Password='$DB_PASSWORD'"
}
}' > "$CONFIG_DIR/appsettings.json"
# Set proper permissions
chown -R postgres:postgres "$CONFIG_DIR"
chmod 750 "$CONFIG_DIR"
chmod 640 "$CONFIG_DIR/appsettings.json"
# Build and deploy the application
print_section "Building application"
# Check if TransmissionRssManager directory exists directly or as a subdirectory
if [ -d "$INSTALL_DIR/TransmissionRssManager" ]; then
PROJECT_DIR="$INSTALL_DIR/TransmissionRssManager"
elif [ -f "$INSTALL_DIR/TransmissionRssManager.csproj" ]; then
PROJECT_DIR="$INSTALL_DIR"
else
# Look for the .csproj file
PROJECT_DIR=$(find $INSTALL_DIR -name "*.csproj" -exec dirname {} \; | head -n 1)
fi
echo "Building project from: $PROJECT_DIR"
cd "$PROJECT_DIR"
dotnet restore
dotnet build -c Release
dotnet publish -c Release -o $INSTALL_DIR/publish
# Copy configuration
cp "$CONFIG_DIR/appsettings.json" "$INSTALL_DIR/publish/appsettings.json"
# Run migrations
print_section "Running database migrations"
cd "$PROJECT_DIR"
dotnet ef database update
# Create systemd service
print_section "Creating systemd service"
# Find the main application DLL
APP_DLL=$(find $INSTALL_DIR/publish -name "*.dll" | head -n 1)
APP_NAME=$(basename "$APP_DLL" .dll)
echo "[Unit]
Description=Transmission RSS Manager
After=network.target postgresql.service
[Service]
WorkingDirectory=$INSTALL_DIR/publish
ExecStart=/usr/bin/dotnet $APP_DLL
Restart=always
RestartSec=10
SyslogIdentifier=transmission-rss-manager
User=postgres
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/transmission-rss-manager.service
# Reload systemd, enable and start service
systemctl daemon-reload
systemctl enable transmission-rss-manager
systemctl start transmission-rss-manager
# Create shortcut
print_section "Creating application shortcut"
echo "[Desktop Entry]
Name=Transmission RSS Manager
Comment=RSS Feed Manager for Transmission BitTorrent Client
Exec=xdg-open http://localhost:5000
Icon=transmission
Terminal=false
Type=Application
Categories=Network;P2P;" > /usr/share/applications/transmission-rss-manager.desktop
# Installation complete
print_section "Installation Complete!"
echo -e "${GREEN}Transmission RSS Manager has been successfully installed!${NC}"
echo -e "Web interface: ${YELLOW}http://localhost:5000${NC}"
echo -e "Database username: ${YELLOW}$DB_USER${NC}"
echo -e "Database password: ${YELLOW}$DB_PASSWORD${NC}"
echo -e "Configuration file: ${YELLOW}$CONFIG_DIR/appsettings.json${NC}"
echo -e "Application files: ${YELLOW}$INSTALL_DIR${NC}"
echo -e "\nTo check service status: ${YELLOW}systemctl status transmission-rss-manager${NC}"
echo -e "View logs: ${YELLOW}journalctl -u transmission-rss-manager${NC}"