Ultima attività 20 hours ago

Revisione 1771aec7ed80b2d005bda2ecb091fdedb9de3c5c

metube.sh Raw
1#!/usr/bin/env bash
2
3# MeTube installation script with offline Deno installation
4# Place this script and deno-x86_64-unknown-linux-gnu.zip in the same directory
5
6set -e
7
8APP="MeTube"
9APP_DIR="/opt/metube"
10DENO_INSTALL_DIR="/usr/local"
11DENO_ZIP="deno-x86_64-unknown-linux-gnu.zip"
12GITHUB_REPO="alexta69/metube"
13SERVICE_USER="root"
14
15# Colors for output
16RED='\033[0;31m'
17GREEN='\033[0;32m'
18YELLOW='\033[1;33m'
19BLUE='\033[0;34m'
20NC='\033[0m'
21
22info() { echo -e "${GREEN}[INFO]${NC} $1"; }
23warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
24error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
25step() { echo -e "${BLUE}[STEP]${NC} $1"; }
26
27# Check if running as root
28if [[ $EUID -ne 0 ]]; then
29 error "This script must be run as root"
30fi
31
32# Check for local Deno archive
33SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
34DENO_ARCHIVE_PATH="${SCRIPT_DIR}/${DENO_ZIP}"
35
36if [[ ! -f "$DENO_ARCHIVE_PATH" ]]; then
37 error "Deno archive not found at $DENO_ARCHIVE_PATH"
38fi
39
40info "Found Deno archive: $DENO_ARCHIVE_PATH"
41
42# Install dependencies
43step "Installing system dependencies"
44apt-get update
45apt-get install -y curl wget unzip git build-essential python3 python3-pip python3-venv nodejs npm
46
47# Install Deno from local archive
48step "Installing Deno from local archive"
49if [[ -d "${DENO_INSTALL_DIR}/deno" ]]; then
50 rm -rf "${DENO_INSTALL_DIR}/deno"
51fi
52
53unzip -o "$DENO_ARCHIVE_PATH" -d /tmp/deno_extract
54chmod +x /tmp/deno_extract/deno
55mv /tmp/deno_extract/deno "${DENO_INSTALL_DIR}/bin/deno"
56rm -rf /tmp/deno_extract
57
58if command -v deno >/dev/null 2>&1; then
59 info "Deno installed successfully: $(deno --version)"
60else
61 error "Deno installation failed"
62fi
63
64# Install pnpm
65step "Installing pnpm"
66npm install -g pnpm
67
68# Download and setup MeTube
69step "Downloading MeTube from GitHub"
70LATEST_URL=$(curl -s "https://api.github.com/repos/${GITHUB_REPO}/releases/latest" | grep "tarball_url" | cut -d '"' -f 4)
71
72if [[ -z "$LATEST_URL" ]]; then
73 error "Failed to get latest release URL"
74fi
75
76if [[ -d "$APP_DIR" ]]; then
77 warn "Existing installation found, backing up..."
78 mv "$APP_DIR" "${APP_DIR}_bak_$(date +%Y%m%d_%H%M%S)"
79fi
80
81mkdir -p "$APP_DIR"
82cd "$APP_DIR"
83
84curl -L "$LATEST_URL" -o metube.tar.gz
85tar -xzf metube.tar.gz --strip-components=1
86rm metube.tar.gz
87
88# Build frontend
89step "Building frontend"
90if [[ -d "$APP_DIR/ui" ]]; then
91 cd "$APP_DIR/ui"
92 pnpm install --frozen-lockfile
93 pnpm run build
94else
95 error "UI directory not found"
96fi
97
98# Setup Python backend
99step "Setting up Python backend"
100cd "$APP_DIR"
101python3 -m venv .venv
102source .venv/bin/activate
103pip install --upgrade pip
104pip install yt-dlp aiohttp python-dotenv
105
106# Check for requirements.txt
107if [[ -f "$APP_DIR/requirements.txt" ]]; then
108 pip install -r requirements.txt
109fi
110
111# Create .env file
112step "Creating configuration"
113if [[ ! -f "$APP_DIR/.env" ]]; then
114 cat > "$APP_DIR/.env" <<EOF
115# MeTube Configuration
116DOWNLOAD_DIR=/downloads
117STATE_DIR=/opt/metube/state
118URL_PREFIX=
119HTTP_USERNAME=
120HTTP_PASSWORD=
121YTDL_OPTIONS=
122EOF
123fi
124
125# Create download and state directories
126mkdir -p /downloads
127mkdir -p "$APP_DIR/state"
128
129# Create systemd service
130step "Creating systemd service"
131cat > /etc/systemd/system/metube.service <<EOF
132[Unit]
133Description=MeTube - YouTube Downloader
134After=network.target
135
136[Service]
137Type=simple
138WorkingDirectory=${APP_DIR}
139EnvironmentFile=${APP_DIR}/.env
140ExecStart=${APP_DIR}/.venv/bin/python3 ${APP_DIR}/app/main.py
141Restart=always
142User=root
143StandardOutput=journal
144StandardError=journal
145
146[Install]
147WantedBy=multi-user.target
148EOF
149
150# Reload systemd and start service
151step "Starting MeTube service"
152systemctl daemon-reload
153systemctl enable metube
154systemctl start metube
155
156# Check if service is running
157sleep 3
158if systemctl is-active --quiet metube; then
159 info "MeTube service started successfully"
160else
161 error "MeTube service failed to start. Check logs with: journalctl -u metube"
162fi
163
164# Get IP address
165IP_ADDRESS=$(hostname -I | awk '{print $1}')
166
167echo ""
168echo -e "${GREEN}========================================${NC}"
169echo -e "${GREEN}MeTube installation completed!${NC}"
170echo -e "${GREEN}========================================${NC}"
171echo -e "Access MeTube at: ${YELLOW}http://${IP_ADDRESS}:8081${NC}"
172echo ""
173echo -e "Commands:"
174echo -e " Start: ${BLUE}systemctl start metube${NC}"
175echo -e " Stop: ${BLUE}systemctl stop metube${NC}"
176echo -e " Status: ${BLUE}systemctl status metube${NC}"
177echo -e " Logs: ${BLUE}journalctl -u metube -f${NC}"
178echo ""
179echo -e "Download directory: ${YELLOW}/downloads${NC}"
180echo -e "Config file: ${YELLOW}${APP_DIR}/.env${NC}"